/* Author: 

*/

$(document).ready(function(){
	var list = $('#thumbs ul'),
		info = $('#info'),
		banner = $('#banner'),
		set = $('#gallery').attr('data-set'),
		apiKey = '5fdb36063d1bee39ebb8e43e7d20cbc1',
		items = [];
	
	if ($('#gallery').attr('data-set') != "") {
		$.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id='+set+'&format=json&jsoncallback=?',
			function(data){
				$.each(data.photoset.photo, function(i,photo){
					items.push($("<li>").attr('data-id', photo.id));
					if(items.length === 8 || i === data.photoset.photo.length-1) {
						ul = $("<ul>");
						for (i=0; i<items.length; i++) {
							ul.append(items[i]);
						}
						
						ul.appendTo(list).wrap("<li></li>");
						
						items = [];
					}
					
					$("<li>").attr('data-id', photo.id).appendTo(banner.find('ul'));
					
					$.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photos.getInfo&api_key=' + apiKey + '&photo_id=' + photo.id + '&format=json&jsoncallback=?', function(data) {    	
						$('li[data-id='+photo.id+']').attr({
							'data-title' : data.photo.title._content,
							'data-description' : data.photo.description._content
						});
						
						if (i === 0) {
							info.find('h2').text(data.photo.title._content);
							info.find('p').html(data.photo.description._content);
						}
					});
					
					$.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photos.getSizes&api_key=' + apiKey + '&photo_id=' + photo.id + '&format=json&jsoncallback=?', function(data) {
						thumb = $('<img/>').attr({
							'src' : data.sizes.size[0].source
						});
						
						large = $("<img/>").attr({
							'src' : data.sizes.size[data.sizes.size.length-1].source
						});
						
						banner.find('ul li[data-id='+photo.id+']').append(large);
						list.find('li[data-id='+photo.id+']').attr('data-large', data.sizes.size[data.sizes.size.length-1].source).append(thumb);
	    			});
	    			applyClasses();
				});
				
			});
			
		$(list).find('li').live('click', function(e) {
			e.preventDefault();
			info.find('h2').text($(this).attr('data-title'));
			info.find('p').html($(this).attr('data-description'));
			
			target = banner.find('li[data-id='+$(this).attr('data-id')+']').position();
			banner.find('ul').animate({
				left : '-'+target.left
			}, 500);
		});
		
		function applyClasses() {
			$(list).find('> li:first-child').addClass('current');
		}
		
		
		$("<a>").attr('href', 'prev').text("Prev").prependTo(list.parent()).click(function(e) {
			e.preventDefault();
			var current = $(list).find('li.current'),
				prev = current.prev();
			
			if (prev.length) {
				current.removeClass('current');
				prev.addClass('current');
				
				left = parseInt(list.css('margin-left')) + parseInt(prev.width());
				list.animate({
					marginLeft: left
				});
			}	
		});
		$("<a>").attr('href', '#next').text("Next").prependTo(list.parent());
		
		$('#thumbs a[href="#next"]').live('click', function(e) {
			e.preventDefault();
			var current = $(list).find('li.current'),
				next = current.next();
			
			if (next.length) {
				current.removeClass('current');
				next.addClass('current');
				
				left = parseInt(list.css('margin-left')) - parseInt(next.width());
				list.animate({
					marginLeft: left
				});
			}
		})
	}
});
