$().ready(
	
	function(){
		
		//create a handle to the photo gallery
		var oGallery = $('#photo_gallery');
		var oCurrent = $('#photo_gallery_current');
		var oLoader  = $('#photo_gallery_loader'); 		
		var oIndicator = $('#photo_gallery_indicator');
		var bLoading = false;

		//define a function for loading and crossfading images
		function loadGalleryImage( url ){
			
			//only load an image if one isnt already loading
			if( bLoading === false ){
			
				//set the loading flag
				bLoading = true;
				
				//fade in the indicator
				oIndicator.fadeIn('slow');
				
				//first, load the image in the background
				var oImg = new Image();
				oImg.onload = function(){
					
					var width 	= this.width;
					var height 	= this.height;
					
					//fix image dimensions
					if( width/height > height/width && width > oGallery.width() ){
						this.width  = oGallery.width();
						this.height = this.width/width * height;						
					} 
					if( height > oGallery.height() ){
						this.height = oGallery.height();
						this.width = this.height/height * width;	
					}
					
					//now, center the image if needed	
					var iHeightDifference = (oGallery.height() - this.height)/2;
					$(this).css({position:'relative',top: iHeightDifference });
														
					//fadeout the indicator
					oIndicator.fadeOut('fast',
						function(){
						
							oCurrent.stop().fadeOut( 'slow' );
							oLoader.stop().fadeIn( 'slow' );
							
							//now relink 
							var tmp = oCurrent;
							oCurrent = oLoader;
							oLoader = tmp;	
							
							//set the loading flag to false now
							bLoading = false;	
												
						}
					);
				}

				oImg.src = url;
				oLoader.hide().empty().append( oImg );
			
			}
			
			
		}		
		
		//based on the current image selected, adds or removes next and previous image links
		function addNextPreviousLinks(){
			
			//first, get the current thumbnail
			var $thumbListItems = $('#photo_gallery_thumbnails ul li');
			var $currentThumb 	= $thumbListItems.filter('.current');
			var indexCurrent 		= $thumbListItems.index( $currentThumb.get(0) );
			
			//add the previous 
			$previous = $('<a href="#" id="photo_gallery_previous" title="Previous Image">Previous Image</a>');
			oGallery.append( $previous );

			//add the next link
			$next = $('<a href="#" id="photo_gallery_next" title="Next Image">Next Image</a>');
			oGallery.append( $next );
			
			//assign the click handlers
			$previous.click(
				function(e){
					e.preventDefault();
					var $thumbListItems = $('#photo_gallery_thumbnails ul li');
					var $current = $thumbListItems.filter('.current');					
					if( $current.prev().length == 0 ){
						 $thumbListItems.filter(':last-child').addClass('current').find('a').click();
					}else{
						$current.prev().addClass('current').find('a').click();
					}
				}
			);
			
			$next.click(
				function(e){
					e.preventDefault();
					var $thumbListItems = $('#photo_gallery_thumbnails ul li');
					var $current = $thumbListItems.filter('.current');					
					if( $current.next().length == 0 ){
						 $thumbListItems.filter(':first-child').addClass('current').find('a').click();
					}else{
						$current.next().addClass('current').find('a').click();
					}
				}
			);
			
			
		}
		
		//add a hover class on the photo gallery when hovered for prev/next links
		$('#photo_gallery')
			.hover(
				function(){
					$(this).addClass('hover');
				},
				function(){
					$(this).removeClass('hover');
				}
			);
		
		//first order of business is to select the current
		//random image
		var sCurrentUrl = $('#current_image').css({display:'none'}).attr('src');
		
		loadGalleryImage(sCurrentUrl);
		
		//add next/previous links if needed
		addNextPreviousLinks();		
		
		//mark the current random image as current in the thumbnail list
		$('#photo_gallery_thumbnails ul a[href=' + sCurrentUrl + ']').parent().addClass('current');
		
		//next, setup a click event to load the image
		$('#photo_gallery_thumbnails ul a').click(
			
			function( e ){
				
				//prevent the click from loading the image
				e.preventDefault();
				
				//rip the current class from the current thumbnail
				$('#photo_gallery_thumbnails ul li.current').removeClass('current');
				
				//add current to this thumbnail
				$(this).parent().addClass('current');
				
				//load the image in
				loadGalleryImage($(this).attr('href'));
				
			}
		);
		
	}	
	
);

//let the document know that scripts are enabled
$('body').addClass('script');
