(function($) {
	
	// Flickr config
	var userId = '29988157@N05';
	var apiKey = 'cc2df3ba8db23bbf13f1cada1ccd087f';
	var getPhotos = 'flickr.photosets.getPhotos';
	var getSizes = 'flickr.photos.getSizes';
	
	// State Vars 
	var initialSet = "Commercial";
	var currentImage = { "set" : '', "id" : 0 };
	
	// Photo Set JSON
	var photoSets = {
							"Commercial" : { "id" : "72157613576509932" , "content" : {}, "byline" : "Your Business Needs..." },
							"Landscapes" : { "id" : "72157607401355363", "content" : {}, "byline" : "See Ireland Differently..." },
							"Weddings" : { "id" : "72157607005023371", "content" : {}, "byline" : "Photo-Journalistic..." },
							"Portraiture" : { "id" : "72157607401274215", "content" : {}, "byline" : "The Human Study..."}
					};
	
	// concurrency workaround
	var setCount = 3;				
	var doneCount = 0;
	// when true, blocks clicks that would start a new showPhoto call
	var photoChanging = false;
	
	// Photo Swap
	function showPhoto( set, id ) {
		photoChanging = true;
		
		var photo = photoSets[set].content[id];
		$('#image').fadeOut('fast', function() {
			$('#img-loader').fadeIn('fast', function() {
				$.getJSON( 'http://api.flickr.com/services/rest/?&method='+ getSizes +'&api_key=' + apiKey + '&photo_id='+ photo.id +'&format=json&jsoncallback=?', function(data) {
					
					var target = data.sizes.size[data.sizes.size.length - 1].source;
					
					$('#image a').attr('href', target );
					$('#image a').attr('title', photo.title );
					
					$('#image img').attr('src', target ).load( function() {
					$('#image .title').html(photo.title);
					$('#image .description').html(photo.description._content);							
					
					$('#img-loader').fadeOut('fast', function() {
							$('#image').fadeIn('fast');
							photoChanging = false;
						});
					});
				});
			});
		});
		
		// Update current image
		currentImage.set = set;
		currentImage.id  = id;
	}
				
	$(document).ready(function() {
	
		$('#latest-dock, #gallery, #image').hide();
		
		$('#categories').append('<ul></ul>');
		
		// Grab all the photosets
		$.each( photoSets, function( setName ) {
			var set = this;
			
			$.getJSON(
				'http://api.flickr.com/services/rest/?&method='+getPhotos+'&api_key=' + apiKey + '&photoset_id='+ set.id +'&extras=description,originalsecret&format=json&jsoncallback=?', 
				function(data) {
					set.content = data.photoset.photo;
					
					$('#categories ul').append('<li><a href="#" rel="' + setName + '">' + setName + '</a> <br/> <span>' + set.byline + '</span> </li>');
					
					var carouselHtml = '<ul id="thumbs'+setName+'">';
			
					$.each( set.content, function( i ) {
				
						carouselHtml += '<li><a href="#" class="'+setName+'" rel="'+ i +'"><img src="http://farm'+this.farm+'.static.flickr.com/'+this.server+'/'+this.id+'_'+this.secret+'_s.jpg" alt="'+this.title+'" /></a></li>';
				
					});
					
					$('#carousel').append( carouselHtml + '</ul><div id="scroll'+setName+'"></div>' );
					
					// 94px per thumbnail according to style - hardcoded for lightweight carousel implementation
					var carouselCount = $('#carousel ul#thumbs'+setName+' li').length;
					var carouselWidth = 94 * carouselCount;
					
					$('#carousel ul#thumbs'+setName).css('width', carouselWidth + 'px');
					
					$('#carousel div#scroll'+setName).slider({ 
						animate: 200,
						max: carouselCount - 10,
						slide: function(event, ui) { 
							$('#carousel ul#thumbs'+setName).stop().animate({'left' : -94 * ui.value }, 200);
							//$('#carousel ul.'+setName).css('left', -94 * ui.value );
						}
					});
					
					doneCount++;
					
					// If all sets are done, lift the veil
					if ( setCount == doneCount ) {
						$('#loader').fadeOut('fast', function() {
							
							$('#carousel ul, #carousel div').hide();
							$('#carousel ul#thumbs'+initialSet+', #carousel div#scroll'+initialSet).show();
							
							$('h3.post-title span').html(' - '+initialSet);
							
							showPhoto( initialSet, 0 );
							
							$('#latest-dock, #gallery').fadeIn('fast');
						});
					}
				} 
			);
		});
		
		// Category bar clicks
		$('#categories ul li a').live('click', function(e) {					
			var set = $(this).attr('rel');
			
			if ( set != currentImage.set && !photoChanging ) {
			
				$('h3.post-title span').html(' - '+set);
				
				$('#carousel ul#thumbs'+currentImage.set+', #carousel div#scroll'+currentImage.set).fadeOut('fast', function() {
					$('#carousel ul#thumbs'+set+', #carousel div#scroll'+set).fadeIn('fast');
				});
				
				showPhoto( $(this).attr('rel'), 0 );
			
			}
	
			e.preventDefault();
		});					
		
		// Carousel image clicks
		$('#carousel ul li a').live('click', function(e) {					
			var set = $(this).attr('class');
			var id = $(this).attr('rel');
			
			// Prevent attempts to reload current image
			if ( !( set == currentImage.set && id == currentImage.id ) && !photoChanging ) {
				showPhoto( set, id );
			}
			
			e.preventDefault();
		});
		
		$('#image').hover(function(){
			$("#image .info").stop().animate({top:'328px'},{queue:false,duration:300});
		}, function() {
			$("#image .info").stop().animate({top:'438px'},{queue:false,duration:300});
		});	
		
		$('body').supersleight({ shim: 'images/x.gif' });
	});

})(jQuery);