


$(document).ready(function() {
	
	
	imageID = 0;
	firstLoad = 0;
	$('#full_image').append('<img id="img_full2" src="" width="843" height="562" style="display:none;" alt="" title="" />');
	$('#img_full').attr('alt', '');
	 $('#homegrid').click(function(){
		
		cssObj = {
			'padding-top' : '0',
			'position' : 'relative'
		}
		$('#content').css(cssObj);			
		
		$('#full_div').css('display', 'none');
		$('#content').fadeIn(500);
		
		return false;
	});	
	

	//stop a link if js is enabled
	//replace #gallery div with #full_image div
	$('#gallery .thumbs_link').click(function(){
		img1 =0;
		
	    $('#img_full').attr("src", '');
	        
	    //image id    
	    imageID = $(this).attr("id").replace('imageid_', '');
	    
	    imageID = parseInt(imageID);
		
		//insert page ID into top nav
		$("#imageNum").html(imageID);
		
		//find the thumb source
	    thumbsrc = $("img",this).attr("src");
	    
	    imageIDNext = imageID + 1;// next image link
		imageIDPrev = imageID - 1;// previous image link
		
		checkNumLimit();
		    
		//change the thumb to the full version    
		thumbsrc = thumbsrc.replace('_thumb.jpg', '.jpg');
		
		$('#img_full').attr("src", thumbsrc);
		
		//fade in full thumbnail
		cssObj = {
			'padding-top' : '10px',
			'position' : 'absolute'
		}
		$('#content').css(cssObj);
	  	
		$('#img_full').fadeIn(700);
		$('#content').fadeOut(100);	
		
		if(firstLoad == 0){
			//display navigation
			nextImageLinkSmall = '<a href="#" id="topnav_next" class="nav_next"> >> </a>';
			prevImageLinkSmall = '<a href="#" id="topnav_previous" class="nav_previous"> << </a>';
			
			nextImageLink = '<a href="" id="nav_next" class="nav_borders nav_next"><img src="/images/site_images/next.png" alt="next"/></a>';
			prevImageLink = '<a href="" id="nav_previous" class="nav_borders nav_previous"><img src="/images/site_images/previous.png" alt="previous"/></a>';
		
	    	$('#topnav_previousjs').html(prevImageLinkSmall);
			$('#topnav_nextjs').html(nextImageLinkSmall);
	    	
	    	$('#full_image').append(nextImageLink);
			$('#full_image').append(prevImageLink);
			
			$('#full_image').hover(
				function(){
					$('#nav_next').stop(true, true).fadeIn(1000);
					$('#nav_previous').stop(true, true).fadeIn(1000);
				},
				function(){
					$('#nav_next').stop(true, true).fadeOut(400);
					$('#nav_previous').stop(true, true).fadeOut(400);
				}
			);
			
			$('#nav_next').css('display', 'none');
			$('#nav_previous').css('display', 'none');
			
		    
			navNext();
			navPrevious();
		
		
	    	firstLoad = 1;
	    	
	    }else{
	    	$('#img_full2').attr('src', '');
	    	
	    }
		
		//hide thumbs and show the full image
		$('#gallery #full_div').css("display","block");
		
		//preloadImages
		getThumbSrc(imageIDNext, 1);
		jQuery.preLoadImages(thumbsrcCache);
		getThumbSrc(imageIDPrev, 1);
		jQuery.preLoadImages(thumbsrcCache);
		
 
		return false;
	}); 
	
		//keep numbers from going over 36 and under 0
	function checkNumLimit(){
		if(imageIDNext == 37){
	   		imageIDNext = 1;
	    }
	    
	    if(imageIDPrev == 0){
	   		imageIDPrev = 36;
	    }
	};
	
	function fadeNextPrev(){
			if(img1 == 0){
				
				$('#img_full2').attr('src',thumbsrc);
				
				$('#img_full').stop(false, true).fadeOut(500, function(){
					$(this).attr('src', '');
				});
				setTimeout("$('#img_full2').stop(true, true).fadeIn(1300);", 500);
				
				img1 = 1;
				
			}else{
			
				$('#img_full').attr('src',thumbsrc);
				
				$('#img_full2').stop(false, true).fadeOut(500, function(){
					$(this).attr('src', '');
				});
				setTimeout("$('#img_full').stop(true, true).fadeIn(1300);", 500);
				
				img1 = 0;
			}
	}
	 
//Next Full image 
	function navNext(){
		
		$('.nav_next').click(function(){
			
			getThumbSrc(imageIDNext, 0);
			$("#imageNum").html(imageIDNext);
			
			fadeNextPrev();
			
			imageIDPrev = imageIDNext - 1;// previous image link	
			imageIDNext = imageIDNext + 1;// next image link
		    
		    checkNumLimit();
		    
		    getThumbSrc(imageIDNext, 1);
			
			jQuery.preLoadImages(thumbsrcCache);
		    
			return false;
		});
	
	}
	
//Previous Full Image 
	function navPrevious(){
	
		$('.nav_previous').click(function(){
			getThumbSrc(imageIDPrev, 0);
			$("#imageNum").html(imageIDPrev);
			
			fadeNextPrev();
				
			imageIDNext = imageIDPrev + 1;// next image link
		    imageIDPrev = imageIDPrev - 1;// previous image link
		    
		    checkNumLimit();
			
			getThumbSrc(imageIDPrev, 1);
			
			jQuery.preLoadImages(thumbsrcCache);
			
			return false;
		});
	
	}

//find Source of next/previous from id	
	function getThumbSrc(id, nextPrev){
		if(nextPrev == 1){
			idURL = '#imageid_'+ id;
			thumbsrcCache = $('img', idURL).attr("src");
			
			//change the thumb to the full version    
			thumbsrcCache = thumbsrcCache.replace('_thumb', '');
		}else{
			idURL = '#imageid_'+ id;
			thumbsrc = $('img', idURL).attr("src");
			
			//change the thumb to the full version    
			thumbsrc = thumbsrc.replace('_thumb', '');
		}
		
	
	}

//preload images

(function($) {
	var cache = [];
  	// Arguments are image paths relative to the current page.
  	$.preLoadImages = function() {
    	var args_len = arguments.length;
    	for (var i = args_len; i--;) {
      	var cacheImage = document.createElement('img');
      	cacheImage.src = arguments[i];
      	cache.push(cacheImage);
    	}
  	}
})(jQuery)

});
