
$(document).ready(function(){

// -----------------------------------------------------------------------------------------------------------------------
// photo gallery
// -----------------------------------------------------------------------------------------------------------------------
	
	browserCheck = jQuery.support.opacity;
	
	currentPhoto = 1;
	currentGallery = 1;
	totalPhotos = $("ul#galleryPhotos li").size();
	totalGalleries = Math.ceil(totalPhotos/6);	
	thumbScroll = "243";
	totalScroll = thumbScroll*currentGallery;
	
	$("div#photoGallery ul#galleryPhotos li").hide();
	$("div#photoGallery ul#galleryPhotos li:first").show();
	$("div#photoGallery div#photoThumbs ul li:first").addClass("thumbSelected");
	$("div#photoGallery div#photoThumbs ul li:even").css({marginRight: "8px"}); // add margin to first column of thumbnail list
	
	
	// photo navigation
	$("div#photoGallery div#photoThumbs ul li").hover(
		function()
		{
			if ( $(this).hasClass("thumbSelected") ) { /* do nothing */ }
			else { $(this).addClass("thumbHover"); }
		},
		function()
		{
			if ( $(this).hasClass("thumbSelected") ) { /* do nothing */ }
			else { $(this).removeClass("thumbHover"); }
		}
	);
	
	$("div#photoGallery div#photoThumbs ul li").click(function(){
		
		if ( $(this).hasClass("thumbSelected") )
		{
			// do nothing
			return false;
		}
		else
		{
			thumbPosition = (getPosition(this.parentNode,this));
			
			$("div#photoGallery div#photoThumbs ul li").removeClass("thumbSelected").removeClass("thumbHover");
			$(this).addClass("thumbSelected");
			$("ul#previousNextGallery li a").removeClass("disabled");
			
			currentPhoto = (thumbPosition+1);
			showPhoto();
			
			if ( currentPhoto == 1 ) { $("ul#previousNextGallery li#previousGallery a").addClass("disabled"); } 
			if ( currentPhoto == totalPhotos ) { $("ul#previousNextGallery li#nextGallery a").addClass("disabled"); }
			
			return false;
		}
		
	});
	
	$("ul#previousNextGallery li#nextGallery a").click(function(){
		
		if ( currentPhoto == totalPhotos )
		{
			currentPhoto = totalPhotos;
			currentGallery = totalGalleries;
			$("ul#previousNextGallery li a").removeClass("disabled");
			$(this).addClass("disabled");
			return false;
		}
		if ( currentPhoto == (totalPhotos-1) )
		{
			currentPhoto = totalPhotos;
			$("div#photoGallery div#photoThumbs ul li").removeClass("thumbSelected").removeClass("thumbHover");
			$("div#photoGallery div#photoThumbs ul li:nth-child(" + totalPhotos + ")").addClass("thumbSelected");
			showPhoto();
			$(this).addClass("disabled");
			return false;
		}
		else
		{
			
			currentPhoto++;
			
			if ( browserCheck == false )
			{
				if ( currentPhoto == 7 ) { $("div#photoGallery div#photoThumbs ul li:lt(6)").hide(); }
				if ( currentPhoto == 13 ) { $("div#photoGallery div#photoThumbs ul li:lt(12)").hide(); }
				if ( currentPhoto == 19 ) { $("div#photoGallery div#photoThumbs ul li:lt(18)").hide(); }
			}
			if ( browserCheck == true )
			{
				if ( currentPhoto == 7 ) { $("div#photoGallery div#photoThumbs ul").animate({marginTop: "-243px"}, {duration: 500, queue: false}); }
				if ( currentPhoto == 13 ) { $("div#photoGallery div#photoThumbs ul").animate({marginTop: "-486px"}, {duration: 500, queue: false}); }
				if ( currentPhoto == 19 ) { $("div#photoGallery div#photoThumbs ul").animate({marginTop: "-729px"}, {duration: 500, queue: false}); }
			}

			showPhoto();
			return false;
		}
		
	});
	$("ul#previousNextGallery li#previousGallery a").click(function(){
		
		if ( currentPhoto == 1 )
		{
			currentPhoto = 1;
			$("ul#previousNextGallery li a").removeClass("disabled");
			$(this).addClass("disabled");
			return false;
		}
		if ( currentPhoto == 2 )
		{
			currentPhoto = 1;
			$("div#photoGallery div#photoThumbs ul li").removeClass("thumbSelected").removeClass("thumbHover");
			$("div#photoGallery div#photoThumbs ul li:nth-child(1)").addClass("thumbSelected");
			showPhoto();
			$(this).addClass("disabled");
			return false;
		}
		else
		{
			currentPhoto--;
			
			if ( browserCheck == false )
			{
				if ( currentPhoto == 6 ) { $("div#photoGallery div#photoThumbs ul li").show(); }
				if ( currentPhoto == 12 ) { $("div#photoGallery div#photoThumbs ul li:gt(5)").show(); }
				if ( currentPhoto == 18 ) { $("div#photoGallery div#photoThumbs ul li:gt(11)").show(); }
			}
			if ( browserCheck == true )
			{
				if ( currentPhoto == 6 ) { $("div#photoGallery div#photoThumbs ul").animate({marginTop: "0"}, {duration: 500, queue: false}); }
				if ( currentPhoto == 12 ) { $("div#photoGallery div#photoThumbs ul").animate({marginTop: "-243px"}, {duration: 500, queue: false}); }
				if ( currentPhoto == 18 ) { $("div#photoGallery div#photoThumbs ul").animate({marginTop: "-486px"}, {duration: 500, queue: false}); }
			}
			
			showPhoto();
			return false;
		}
	});
	
}); /* $(document).ready(function */

function setClickThruTreatment()
{
	if ( browserCheck == false )
	{
		$("div#photoGallery ul#galleryPhotos li").hide();
		$("div#photoGallery ul#galleryPhotos li:nth-child(" + currentPhoto + ")").show();
	}
	if ( browserCheck == true )
	{
		$("div#photoGallery ul#galleryPhotos li").fadeOut(500);
		$("div#photoGallery ul#galleryPhotos li:nth-child(" + currentPhoto + ")").fadeIn(500);
	}
}

function showPhoto() {
	$("div#photoGallery div#photoThumbs ul li").removeClass("thumbSelected").removeClass("thumbHover");
	$("div#photoGallery div#photoThumbs ul li:nth-child(" + currentPhoto + ")").addClass("thumbSelected");
	
	setClickThruTreatment();
	
	$("ul#previousNextGallery li a").removeClass("disabled");
}