var currentPage = 1;

$(document).ready(function() {
	
	getImageBatch(currentPage);

	$("#right_arrow").click(function(){
		getImageBatch(++currentPage);
		return false;
	})
	
	$("#left_arrow").click(function(){
		getImageBatch(--currentPage);
		return false;
	})
});

function attatchLight() {
	$("a.light_image").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	180, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayOpacity':  0.7 
	});
}

function getImageBatch(page) {
		$("#photo_container").hide();
		$("#main").addClass("loading");
		$("#photo_container").load('code/php/flickr/flickr_gallary.php?page=' + page, function(){	
			var $images = $("#photo_container img");
			if ($images.length) {
				var imageCount = $images.length;
				var imageLoadedCount = 0;
			
				$images.each(function() {
					$(this).load(function() {
						imageLoadedCount += 1;
					});
				});
				
				var intervalId = setInterval(function() {
					if (imageCount == imageLoadedCount) {
						clearInterval(intervalId);
						$("#main").removeClass("loading");
						$("#photo_container").fadeIn();
						attatchLight();
						$("#photo_container").fadeIn();	
				   };
				}, 100);
			};
		});
}
