$(function() {	var api_key 			= '62e75fb28c9ba6231e655b139048c4b5';	var user_id  			= '44400586@N08';		/*	use:	Square,Thumbnail,Small,Medium or Original for the large image size you want to load!	*/	var large_image_size 	= 'Medium';	/*	the current Set id / the current Photo id	*/	var photo_set_id,photo_id;	var current	= -1;	var continueNavigation = false;	/*	flickr API Call to get List of Sets	*/	var sets_service 		= 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getList' + '&api_key=' + api_key;	var sets_url			= sets_service + '&user_id=' + user_id + '&format=json&jsoncallback=?';	/*	flickr API Call to get List of Photos from a Set	*/	var photos_service 		= 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos' + '&api_key=' + api_key;	/*	flickr API Call to get List of Sizes of a Photo	*/	var large_photo_service = 'http://api.flickr.com/services/rest/?&method=flickr.photos.getSizes' + '&api_key=' + api_key;	/* 	elements caching... 	*/	var $setsContainer 		= $('#sets').find('ul');	var $photosContainer 	= $('#images').find('ul');	var $photopreview		= $('#flickr_photopreview');	var $flickrOverlay		= $('#flickr_overlay');	var $loadingStatus		= $('#flickr_toggle').find('.loading_small');		var ul_width,spacefit,fit;		$(document).ready(function(){				LoadSets();			});		/*	Loads the User Photo Sets	*/	function LoadSets(){		$loadingStatus.css('visibility','visible');				$.getJSON(sets_url,function(data){			if(data.stat != 'fail') {				var sets_count = data.photosets.photoset.length;				/*				adapt ul width based on number of results				*/				ul_width = sets_count * 73 + 73;				$setsContainer.css('width',ul_width + 'px');								for(var i = 0; i < sets_count; ++i){					var photoset		= data.photosets.photoset[i];					var primary 		= photoset.primary;					var secret			= photoset.secret;					var server			= photoset.server;					var farm			= photoset.farm;					/*					source for the small thumbnail					*/					var photoUrl		= 'http://farm'+farm+'.static.flickr.com/'+server+'/'+primary+'_'+secret+'_s.jpg';					var $elem 			= $('<li />');					var $link 			= $('<a class="toLoad" href="javascript:void(0)" />');					/*					save the info of the set in the li element,					we will use it later						*/					$link.data({						'primary'	:primary,						'secret'	:secret,						'server'	:server,						'farm'		:farm,						'photoUrl'	:photoUrl,						'setName'	:photoset.title._content,						'id'		:photoset.id					});										$setsContainer.append($elem.append($link));					$link.bind('click',function(e){						var $this = $(this);						/*						save the current Set id in the photo_set_id variable						and load the photos of that Set						*/						$('#images').stop().animate({'bottom':'0px'},200);						if(photo_set_id!=$this.data('id')){							photo_set_id = $this.data('id');							$('#setName').html($this.data('setName'));							LoadPhotos();						}						e.preventDefault();					});				}				/*				now we load the images 				(the ones in the viewport)				*/				LoadSetsImages();			}		});		}		/*	loads the images of the sets that are in the viewport	*/	function LoadSetsImages(){		var toLoad 			= $('.toLoad:in-viewport').size();		if(toLoad > 0)			$loadingStatus.css('visibility','visible');		var images_loaded 	= 0;		$('.toLoad:in-viewport').each(function(i){			var $space			= $setsContainer.find('.toLoad:first');			var $img 			= $('<img style="display:none;" />').load(function(){				++images_loaded;				if(images_loaded == toLoad){					$loadingStatus.css('visibility','hidden');					$setsContainer.find('img').fadeIn();				}				}).error(function(){				//TODO				++images_loaded;				if(images_loaded == toLoad){					$loadingStatus.css('visibility','hidden');					$setsContainer.find('img').fadeIn();				}				}).attr('src',$space.data('photoUrl')).attr('alt',$space.data('id'));			var $set_name		= $('<span />',{'html':$space.data('setName')});			$space.append($set_name).append($img).removeClass('toLoad');		});	}		/*	Loads the Set's Photos	*/	function LoadPhotos(){		$photosContainer.empty();		$loadingStatus.css('visibility','visible');		var photos_url	= photos_service + '&photoset_id=' + photo_set_id + '&media=photos&format=json&jsoncallback=?';				$.getJSON(photos_url,function(data){			if(data.stat != 'fail') {				var photo_count = data.photoset.photo.length;				/*				adapt ul width based on number of results				*/				var photo_count_total = photo_count + $photosContainer.children('li').length;				ul_width = photo_count_total * 85 + 85;				$photosContainer.css('width',ul_width + 'px');								for(var i = 0; i < photo_count; ++i){						var photo			= data.photoset.photo[i];					var photoid			= photo.id;										var secret			= photo.secret;					var server			= photo.server;					var farm			= photo.farm;										var photoUrl		= 'http://farm'+farm+'.static.flickr.com/'+server+'/'+photoid+'_'+secret+'_s.jpg';					var imageUrl		= 'http://farm'+farm+'.static.flickr.com/'+server+'/'+photoid+'_'+secret+'.jpg';										var $elem 			= $('<li />');					var $link 			= $('<a class="toLoad lightbox" href="'+imageUrl+'" />');										$link.data({						'photoid'		:photoid,						'secret'		:secret,						'server'		:server,						'farm'			:farm,						'photoUrl'		:photoUrl,						'photo_title'	:photo.title					});					$photosContainer.append($elem.append($link));														}				LoadPhotosImages();			}					});	}				/*	loads the images of the set's 	photos that are in the viewport	*/	function LoadPhotosImages(){		var toLoad 			= $('.toLoad:in-viewport').size();		if(toLoad > 0)			$loadingStatus.css('visibility','visible');		var images_loaded 	= 0;				$('.toLoad:in-viewport').each(function(i){			var $space			= $photosContainer.find('.toLoad:first');			var $img 			= $('<img style="display:none;" />').load(function(){				++images_loaded;				if(images_loaded == toLoad){					$loadingStatus.css('visibility','hidden');					$photosContainer.find('img').fadeIn();					/*					if we were navigating through the large images set:					*/					if(continueNavigation){						continueNavigation 	= false;						var $thumb 			= $photosContainer.find('li:nth-child(' + parseInt(current + 1) + ')').find('img');						photo_id 			= $thumb.attr('alt');											}				}				}).error(function(){				//TODO				++images_loaded;				if(images_loaded == toLoad){					$loadingStatus.css('visibility','hidden');					$photosContainer.find('img').fadeIn();					/*					if we were navigating through the large images set:					*/					if(continueNavigation){						continueNavigation 	= false;						var $thumb 			= $photosContainer.find('li:nth-child(' + parseInt(current + 1) + ')').find('img');						photo_id 			= $thumb.attr('alt');						}				}							}).attr('src',$space.data('photoUrl'))			  .attr('alt',$space.data('photoid'));						var $photo_title	= $('<span/>',{'html':$space.data('photo_title')});			$space.append($photo_title).append($img).removeClass('toLoad');		});	}			/*	events to navigate through the sets / photos containers	*/	var scrollAllow = true;	$('#sets,#images').find('.next').bind('click',function(e) {		var target_id = $(e.target).parent().attr('id');				var $theContainer;		if(target_id == 'sets')			$theContainer = $setsContainer;		else if(target_id == 'images')			$theContainer = $photosContainer;					if(scrollAllow){			scrollAllow		= false;			spacefit 		= 144;			fit 			= Math.floor(spacefit / 85);			var left 		= parseFloat($theContainer.css('left'),10);			var moveleft 	= left - 143;			if(ul_width - Math.abs(left) < 288){ 				scrollAllow = true;				e.preventDefault();				return;			}			$theContainer.animate({'left':moveleft+'px'},1000,function(){				scrollAllow = true;				if(target_id == 'sets')					LoadSetsImages();				else if(target_id == 'images')					LoadPhotosImages();			});			e.preventDefault();		}	});	$('#sets,#images').find('.prev').bind('click',function(e) {		var target_id = $(e.target).parent().attr('id');				var $theContainer;		if(target_id == 'sets')			$theContainer = $setsContainer;		else if(target_id == 'images')			$theContainer = $photosContainer;					if(scrollAllow){			scrollAllow		= false;			spacefit 		= 144;			fit 			= Math.floor(spacefit / 85);			var left = parseFloat($theContainer.css('left'),10);			var moveleft = left +143;			if(left >= 0){ 				scrollAllow = true;				e.preventDefault();				return;			}			$theContainer.animate({'left':moveleft+'px'},1000,function(){				scrollAllow = true;			});			e.preventDefault();		}	});		/*	when cliking "Back to Sets"	minimize photos bar	*/	$('#images_toggle').bind('click',function(){		$('#images').stop().animate({'bottom':'-145px'},200);	});			});
