$(document).ready(function() {
	
	
	var apiKey = 'f9631e39ee095c74f622fa7e40cfddc4';
    var userId = '88088144@N00';
	
	
	//    page initialization
	resizeContentWindow();
	getHero();
	getExhibitions();
	
    
	
	
	//    navigational events
	
	
	$('#nav a').click(function(){
		
		var trigger=$(this).attr('href');
		var targetWindow="";
		
		
		switch (trigger){
			case '#': 
				$('#thumbs div').css('display','none');
				$('#blurb div').css('display','none');
				break
			case 'exhibitions': targetWindow="#" + trigger; break;
			case 'profile': targetWindow="#" + trigger; break;
			case 'press': targetWindow="#" +trigger; break;
			case 'works': showGallery('72157594534559763'); return false;
			
			default: return;
		}
		
		
		$('#thumbs div').css('display','none');
		$('#blurb div').css('display','none');
		$(targetWindow).fadeIn('slow');
		
		
		return false;
		
	});
	
	
	
	$('a.setLink').live('click', function() {
 		 
		 var target=$(this).attr('href');
		 showGallery(target);		 
		 return false;
		 
	});
	
	
	function showGallery(targetSet){
		 
		 
		 $('#galleryView').css('display','none').html('');				//    make sure gallery elements are empty and hidden
		 $('#galleryBlurb').css('display','none').html('');				
		 $('#thumbs div').css('display','none');						//	  hide any other divs
		 $('#blurb div').css('display','none');	
		 populateGallery(targetSet);									//    populate the gallery window
		 $('#galleryBlurb').fadeIn('slow');								//	  unveil the blurb
		 $('#galleryView').fadeIn('slow');								//	  unveil the gallery
		
	}
	
	
	
	
	// the following function controls the size of the 'content' div dynamically based on the height of the window - the height of the menu bar. 
	
	$(window).resize(resizeContentWindow);
				 
	function resizeContentWindow() {
		var height=$(window).height();
		var menuHeight=$('#menu').height()+10;
		
		$('#content').height(height-menuHeight);
	};
	
	
	
	// BEGIN flickr API HANDLING  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
	 	
	// GET HERO IMAGE
	
	function getHero(){
		
		$.getJSON('http://api.flickr.com/services/rest/?format=json&method=flickr.photos.search&api_key=' + apiKey + '&user_id=' + userId + '&tags=feature&jsoncallback=?',  function(data){
		
			$.each(data.photos.photo, function(i, rPhoto){
				var heroPath = 'http://farm' + rPhoto.farm + '.static.flickr.com/' + rPhoto.server + '/' + rPhoto.id + '_' + rPhoto.secret + '.jpg';   	
				$("#hero").attr("src",heroPath);
			});
		});
	}
	
	
	
	
	
	// POPULATE EXHIBITIONS DROPDOWN MENU
	
	
	function getExhibitions(){
		
		$.getJSON('http://api.flickr.com/services/rest/?method=flickr.collections.getTree&api_key=' + apiKey + '&collection_id=2389445-72157626846409140&user_id=' + userId + '&format=json&jsoncallback=?',  function(data){
		
			$.each(data.collections.collection, function(i, rCollection){
				
				var count=0;
				
				while (count < rCollection.set.length){	
					
					var thisSet=rCollection.set[count];
					var setLinkCode="<a class='setLink' href='" + thisSet.id + "'>" + thisSet.title + "</a>";
					
					//alert(setLinkCode);
					
					$('#sets').append('<li>' + setLinkCode + '</li>');
					$('#exhibitions').append(setLinkCode + "<br/>");
			
					count++;
				}
			});
		});
		
	}
	
	
	
	
	function populateGallery(setId){
		
		
		
		//GET SET DETAILS AND INSERT INTO GALLERYBLURB DIV
		$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&api_key=' + apiKey + '&photoset_id=' + setId + ' &format=json&jsoncallback=?', function(data){
			
			var exhibitionName=data.photoset.title._content;	
			var exhibitionDescription=data.photoset.description._content.replace(/\n/g, "<br />");
			
			$("#galleryBlurb").append("<h1>" + exhibitionName + "</h1>").append(exhibitionDescription);
			
		});
		
				
		
		//GET SET PHOTOS AND INSERT INTO GALLERYTHUMBS DIV
		$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=' + setId + ' &format=json&jsoncallback=?', function(data){
				
		 
			$.each(data.photoset.photo, function(i, rPhoto){
				
					
					
				//get photo description
				$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=' + apiKey + '&photo_id=' + rPhoto.id + ' &format=json&jsoncallback=?', function(data){
					var PhotoDescription=data.photo.description._content.replace(/\n/g, "<br />");
													 
					var basePhotoURL = 'http://farm' + rPhoto.farm + '.static.flickr.com/'
					+ rPhoto.server + '/' + rPhoto.id + '_' + rPhoto.secret;            			//base string for photo path
					
					var thumbPhotoURL = basePhotoURL + '_s.jpg';									//add thumbnail denoter
					var mediumPhotoURL = basePhotoURL + '.jpg';										//add medium image denoter (this case, is default)
					
					var photoStringStart = '<a ';													//start compiling the anchor
					var photoStringEnd = 'title="' + PhotoDescription + '" class="lightbox" href="'+ 					
						mediumPhotoURL +'"><img src="' + thumbPhotoURL + '" class="thumbnail" alt="' + 
						PhotoDescription + '"/></a>;'                
					var photoString = photoStringStart + photoStringEnd ; 
					$(photoString).appendTo("#galleryView");										//add item as last child in gallery div	
					$("a.lightbox").lightBox();														//bind photos to lightbox
				});
												 
								
											
				
				
			});
			
			
		
		});
		
	}
	
 });	
