function showGallery(photoset_id){
	$.getJSON("http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=674bc87ae40ce9482954e03c86bcf216&photoset_id="+photoset_id+"&extras=url_sq,url_m&format=json&jsoncallback=?", function(data){	
			var title = "";
			var thumbz = [];
			
			$.each(data.photoset.photo, function(i,photo){
				var href = photo.url_m;
				var thumb = photo.url_sq;
				var title = photo.title;
				var w = parseFloat(photo.width_m);
				var h = parseFloat(photo.height_m);
				newWidth = (w < 420)? w : 420;
				newHeight = (w > 420)? h*(420/w) : h;
				
				link = '<a href="'+href+'" class="thumb" rel="'+newWidth+','+newHeight+'">\
							<img src="'+thumb+'" alt="'+title+'" />\
						</a>'
				thumbz.push(link);
			});
			
			$('#thumbs').html(thumbz.join(''));
			
			$('.thumb').click(function(){
				var size = $(this).attr('rel').split(',');
				var width = size[0];
				var height = size[1];
				showImage($(this).attr('href'), width, height);
				return false;
			});
			$('a.thumb:first').trigger('click');
		
			$("#thumbs").each(function(){
				var currentPage = 0;
				var numPerPage = 6;
				var $list = $(this);
				var totalItems = $(".thumb").length;
				var totalPages = Math.ceil( totalItems / numPerPage )-1;

				$list.bind('repaginate', function(){
					var start = currentPage * numPerPage;
					var end = currentPage * numPerPage + numPerPage;
					if (totalItems < end){
						end = totalItems
					}
					$("#pageCount").text(start+1+ " - " +end+ " of " +totalItems+ " photos");
					$list.find(".thumb").hide().slice(start, end).show();
				});

				$("#pagePrevious").click(function(){
					currentPage--;
					if(currentPage < 0){ currentPage = totalPages;}
					$list.trigger("repaginate");
				});
				$("#pageNext").click(function(){
					currentPage++;
					if(currentPage > totalPages){ currentPage = 0;}
					$list.trigger("repaginate");
				});

				$list.trigger("repaginate");
		});
	});
	
}
function showImage(link,w,h){
	$('#displayImage').empty().append('<img src="'+link+'" width="'+w+'" height="'+h+'"/>');
	
	var tag = $('a[href='+link+'] img').attr('alt');
	$('#displayCopy').empty().append('<p class="copy">'+ tag +'</p>').hide();

	$('#display #displayImage img').css({display: 'none'}).load(function(){ 
			$('#displayImage img, #displayCopy').fadeIn('200');
	});
}

$(document).ready(function(){
	var apiKey = '585b0df28f6092a16e95355c84f35853';
	var userId = '42113654@N03';
	
	$.getJSON("http://api.flickr.com/services/rest/?&method=flickr.photosets.getList&api_key="+apiKey+"&user_id="+userId+"&format=json&jsoncallback=?",
	function(data){	
		var ids = [];
		var titles = [];
		var photosetID = "";
		var title = "";
		
		$.each(data.photosets.photoset, function(i,set){
			photosetID = set.id;
			title = set.title._content;
			ids.push(photosetID);
			titles.push(title);
		});
		
		for(i=0; i<ids.length;i++){
			var option = new Option(titles[i],ids[i]),
			dropDown = document.getElementById("gallerySelect");
			dropDown.options[i] = option;
		}
		
		$("#gallerySelect").change(function(){
			var id = $(this).val();
			showGallery(id);
		});
		
		var firstGallery = $('#gallerySelect option:first').val();
		showGallery(firstGallery);
		
	});
		$("#sppSearchTerm").change(function(){
		window.location = "/spp/search-results.asp?industry_id=" + $(this).val();
	});
});
