/**
 * Common dynamic behaviour for Park Farm Cottages pages
 */

$(document).ready(function(){
	// Init DHTML menu
	$('#nav > ul > li').each(function(){
		var li = window.$(this);
		li.find('li a').width(li.width()-12);
	});	
	ddsmoothmenu.init({
		mainmenuid: "nav", //menu DIV id
		orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
		classname: 'ddsmoothmenu', //class added to menu's outer DIV
		contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
	})
	
	// Init Gallery lightboxes
	$('img.lightbox').each(function(){
		var link = document.createElement("a");
		link.className = "lightbox-link";
		link.title = this.alt;
		link.href = this.src.replace("thumbnail", "websize");
		window.$(this).wrap(link);
	});
    $('a.lightbox-link').lightBox();
	
	// Init banner cycle
	$('#banner_cycle').cycle({
		fx: 'scrollUp',
		timeout: 8000
	});
	
	// Populate slideshows
	$('div.slideshow').each(function(i,slideshow){
		
		//Create URL using filters encoded in class names
		var classList = $(this).attr('class').split(' ');
		var filters = [];
		$.each(classList, function(i,cl) {
			if( cl.match(/\w+:.*/g) ) {
				filters.push( cl.replace(":","=") );
			}
		});
		var url = "/cgi-bin/get-recordset.pl/photo_feed?OutputFormat=JSON;" + filters.join(";");
		
		//Fetch JSON		
		$.getJSON(url,null,function(data) {
			//Build a list of slides
			$.each(data.Records, function(i, record) {
				var url = "/cgi-bin/process-image.pl/photo_feed?Image=/images/photos/websize/"+record.Filename;
				if(record.Title) {
					var slide = $("<div class='slide'/>)");
					$(slide).append("<img src='"+url+"' /><div class='title'>"+record.Title+"</div>");
					if(record.Description) $(slide).append("<div class='description'>"+record.Description+"</div>");
					$(slideshow).append(slide);
				}
			});

			//Make into a slideshow
			$(slideshow).cycle({
				timeout: 12000
			});
		});
				
	});
	
	// Unhide nav when rendered
	$('#nav').css('visibility', 'visible');
		
});