$(recipeGallery_init);

function recipeGallery_init (){
	var thumb_width = $("#recipe-gallery .gallery li:first-child a").width() + 10 + 4;
	var thumb_count = $("#recipe-gallery .gallery li").length;
			
	$("#recipe-gallery .gallery ul").width(thumb_width * thumb_count);

	$("#recipe-gallery .prev-link").click(recipeGallery_prev);
	$("#recipe-gallery .next-link").click(recipeGallery_next);
}


function recipeGallery_prev (){
	var gallery_ul = $("#recipe-gallery .gallery ul"),
		gallery_pos = $(gallery_ul).css("margin-left"),
		gallery_width = $("#recipe-gallery .gallery").width();
		
	if (gallery_pos != "0" && gallery_pos != "0px" && gallery_pos != undefined){
		$(gallery_ul).animate({marginLeft: "+="+gallery_width+"px"}, 500);
	}
	return false;
}



function recipeGallery_next (){
	var gallery_ul = $("#recipe-gallery .gallery ul"),
		gallery_pos = $(gallery_ul).css("margin-left"),
		gallery_ul_width = $(gallery_ul).width(),
		gallery_width = $("#recipe-gallery .gallery").width(),
		max_page = Math.ceil(gallery_ul_width / gallery_width),
		current_page;
		
		
	// Calculate the page based on the left margin	
	if (gallery_pos === "0" || gallery_pos === "0px" || gallery_pos === undefined){
		gallery_pos = 0;
		current_page = 1;
	}else{
		gallery_pos = parseFloat(gallery_pos.substr(0, gallery_pos.indexOf("px")));
		current_page = Math.ceil(Math.abs(gallery_pos) / gallery_width) + 1;
	}
	
	console.log(current_page + ' of ' + max_page);
	
	if (current_page < max_page){
		$(gallery_ul).animate({marginLeft: "-="+gallery_width+"px"}, 500);
	}
	

	return false;
}