$(document).ready(function() {
	
	var slidePlaying;
	var delay = 3;
	var playSlideshow;
	var baseOffset;
	var mouseOffset;
	var thumbsOffset;
	var totalArea;
	var totalScroll;
	var totalWidth;
	var shouldScroll;
	var imgWidth;
	var imgHeight;
	var slideControl = "#slidecontrol";
	var nextBtn = "#next";
	var prevBtn = "#prev";
	var imgLoader = "#imgLoader";
	var galleryHolder = "#galleryholder";
	var imgLoading = false;
	var thumbArea = "#thumbholder";
	var thumbHolder = "#thumbholder ul";
	var thumbs = thumbHolder+" a";
	var imgStage = "#showimg";
	var imgholder =  imgStage+" .imgholder";
	var theImg = $(imgholder).find("img");
	$('<div id="imgLoader"></div>').prependTo(galleryHolder);
	
	window.setTimeout(function() {
		slidePlaying = true;
		checkSlideStatus();
		buildMiniThumbs();
		setDefaults();
		$(thumbHolder+" a:first").trigger("click");
	}, 300);
	
	function setDefaults()
	{
		$(thumbs).css({"opacity" : "0.6"})

		baseOffset = $(thumbArea).offset().left;

		totalArea = $(thumbArea).width() + $(thumbArea).offset().left;

		totalScroll = $(thumbArea).width();

		totalWidth = $(thumbHolder).outerWidth();
		
		$(imgStage).height($(galleryHolder).height() - 42);
		$(imgStage).width($(galleryHolder).width());
		
		$("#imgLoader").height($(galleryHolder).height() - 42);
		
		$(thumbArea).width($(galleryHolder).width() - ($("#galleryactions").width() + 20));

		if(jQuery.support.leadingWhitespace == false) {
			$("li", $(thumbholder)).css({"display":"inline"});
			$(imgholder).css({"-ms-interpolation-mode": "bicubic"});
		}
	}
	
	function resizeImage()
	{
		if(imgHeight > imgWidth)
		{
				$(theImg).height($(imgStage).height());
				$(theImg).width("auto");
				if($(theImg).width() > $(imgStage).width())
				{
					$(theImg).width($(imgStage).width());
					$(theImg).height("auto");
					var vertCenter = ($(imgStage).height() - $(theImg).height()) / 2;
					$(imgholder).css({"padding-top" : vertCenter});
				}
				else
				{
					$(theImg).height($(imgStage).height());
					$(theImg).width("auto");
					$(imgholder).css({"padding" : "0"});
				}
				$(imgholder).css({"margin" : "0 auto"});
		}
		else if(imgWidth > imgHeight)
		{
				$(theImg).width($(imgStage).width());
				$(theImg).height("auto");
				var vertCenter = ($(imgStage).height() - $(theImg).height()) * 0.5;
				$(imgholder).css({"padding-top" : "0"});

				if($(theImg).height() > $(imgStage).height()) {
					$(theImg).height($(imgStage).height());
					$(theImg).width("auto");
					$(imgholder).css({"margin" : "0 auto"});
					$(imgholder).css({"padding-top" : "0"});
				} else {
					$(theImg).width($(imgStage).width());
					$(theImg).height("auto");
					var vertCenter = ($(imgStage).height() - $(theImg).height()) / 2;
					$(imgholder).css({"padding-top" : vertCenter});
				}

		}
		else
		{
				$(theImg).height($("#showimg").height());
				$(theImg).width($("#showimg").width());
		}
		$(imgholder).width($(theImg).width());
		$(imgholder).height($(theImg).height());
		$(imgholder).fadeTo("normal",100);
		$(theImg).fadeTo("normal",99);
		$(".imgdesc",imgholder).css({"top" : ($(imgStage).outerHeight() - $(".imgdesc").outerHeight()),"opacity" : "0.8"})
	}
	
	function preloadAnim(action)
	{
		if(action == "show")
		{
			$(imgLoader).css({"opacity" : "1.0"});
		}
		if(action == "hide")
		{
			$(imgLoader).fadeTo("slow",0);
		}
	}
	
	$(thumbArea).mouseover(function(e) {
		scrollChecker = setInterval(checkScroll, 1);
			shouldScroll = true;
			var x = e.pageX - $(thumbArea).offset().left;
			var y = e.pageY - $(galleryHolder).offset().top;
			mouseOffset = x;
	});
	
	$(thumbArea).mouseout(function() {
		scrollThumbs(false);
		clearInterval(scrollChecker);
	});
	
	function checkScroll() {
		thumbsOffset = $(thumbHolder).offset().left;
		if((mouseOffset - 65) > (totalScroll/2) && shouldScroll)
		{
			scrollThumbs("left", true);
		}
		else if((mouseOffset + 65) < (totalScroll/2) && shouldScroll)
		{
			scrollThumbs("right", true);
		}
		else
		{
			scrollThumbs(false);
			clearInterval(scrollChecker);
		}
	}

	function scrollThumbs(direction, active) {
		if(direction == "left" && active == true)
		{
			$(thumbArea).animate({"scrollLeft" : "+=40px"}, 150, "linear");
		}
		else if(direction == "right" && active == true)
		{
			$(thumbArea).animate({"scrollLeft" : "-=40px"}, 150, "linear");
		}
		else
		{
			$(thumbArea).stop(true,false);
		}
	}
	
	$(thumbs).mouseover(function(e) {
		$(this).not(".active").css({"opacity" : "0.7"});
		showBigThumb($(this).find("img"),e);
	});
	
	$(thumbs).mouseout(function(e) {
		$(this).not(".active").css({"opacity" : "0.3"});
		killBigThumb();
	});
	
	
	$(thumbs).click(function() {
		clearInterval(playSlideshow);
		$(thumbs).removeClass("active");
		$(thumbs).css({"opacity" : "0.3"});
		$(this).addClass("active");
		$(this).css({"opacity" : "1.0"});
		theImg = $("img",this).attr("src");
		var imgURL = $(this).attr("href");
		var imgDesc = $("img",this).attr("alt");

		$(imgholder).fadeTo("slow", 0, function() {
			var preloader = new Image();
			preloadAnim("show");
			
			// once image has loadded...
			preloader.onload = function() {
			clearInterval(playSlideshow);

				if(slidePlaying) {
					playSlideshow = setInterval(nextImage, delay*1000);
				}
				
				// get image dimensions
				imgWidth = preloader.width;
				imgHeight = preloader.height;

				preloadAnim("hide");
				
				if(imgDesc && imgDesc != "")
				{
					var imgCode = '<div class="imgdesc"><p>'+imgDesc+'</p></div><img src="'+imgURL+'" border="0" alt="" style="-ms-interpolation-mode: bicubic" />';
				}
				else
				{
					var imgCode = '<img src="'+imgURL+'" border="0" alt="" style="-ms-interpolation-mode: bicubic" />';
				}
				$(imgholder).html(imgCode);
				theImg = $("img",imgholder);
				resizeImage();
				preloader.onload=function(){};
			};
			preloader.src = imgURL;
		});
		return false;
	});
	
	$("#functionstrip a").click(function() {
		if($(this).attr("id") == "prev")
		{
			prevImage();
		}
		else if($(this).attr("id") == "next")
		{
			nextImage();
		}
		return false;
	});
	
	
	function prevImage()
	{
		var curThumb = $(thumbHolder+" .active");
		if($(curThumb).parent().prev().find("a").length > 0)
		{
			var newThumb = $(curThumb).parent().prev().find("a");
			$(newThumb).trigger("click");
			if($(newThumb).offset().left < baseOffset)
			{
				var theDistance = $(newThumb).offset().left;
				if(theDistance > 0)
				{
					$(thumbArea).animate({"scrollLeft" : "-="+(theDistance/2)}, 150, "linear");
				}
				else
				{
					$(thumbArea).animate({"scrollLeft" : "+="+(theDistance/2)}, 150, "linear");
				}
			}
		}
		else
		{
			$(thumbHolder+" a:last").trigger("click");
			$(thumbArea).scrollLeft($(thumbHolder+" a:last").offset().left);
		}
	}
	
	
	function nextImage()
	{
		var curThumb = $(thumbHolder+" .active");
		if($(curThumb).parent().next().find("a").length > 0)
		{
			var newThumb = $(curThumb).parent().next().find("a");
			$(newThumb).trigger("click");
			if((totalScroll + baseOffset) < ($(newThumb).offset().left + $(newThumb).width()))
			{
				$(thumbArea).animate({"scrollLeft" : "+="+($(newThumb).offset().left - baseOffset)}, 150, "linear");
			}
			else if(($(newThumb).offset().left + $(newThumb).width()) < baseOffset)
			{
				$(thumbArea).animate({"scrollLeft" : ($(newThumb).offset().left - baseOffset)}, 150, "linear");
			}

		}
		else
		{
			$(thumbHolder+" a:first").trigger("click");
			$(thumbArea).scrollLeft(0);
		}
	}
	
	function buildMiniThumbs() {
		$(thumbHolder).find("img").each(function() {
			$(this).width(30);
			$(this).height(30);
		});
	
	}
	
	function showBigThumb(target,e) {
		var offset = $(target).offset();
		var bigThumb = $(target).attr("src");
		var mouseX = e.pageX - (75/2);
		var mouseY = offset.top - 90;
		$("body").prepend('<div id="thumbPreview"><img src="'+bigThumb+'" width="75" height="75" alt="" /></div>');
		$("#thumbPreview").css({
			"position" : "absolute",
			"top" : mouseY + "px",
			"left" : mouseX + "px",
			"z-index" : "5000",
			"background" : "#fff",
			"padding" : "5px"
		});
	}
	
	function killBigThumb() {
		$("#thumbPreview").remove();
	}
	
	
	$(prevBtn).click(function()
	{
		prevImage();				
		return false;
	});
	
	$(nextBtn).click(function() {
		nextImage();
		return false;
	})
	
	$(slideControl).click(function() {
		if($(this).hasClass("paused"))
		{
			slidePlaying = true;
		}
		else
		{
			slidePlaying = false;
		}
		checkSlideStatus();
		return false;
	});
	
	function checkSlideStatus()
	{
		if(slidePlaying)
		{
			playSlideshow = setInterval(nextImage, delay*1000);
			$(slideControl).removeClass("paused");
			$(slideControl).addClass("playing");
			slidePlaying = true;
		}
		else
		{
			clearInterval(playSlideshow);
			$(slideControl).removeClass("playing");
			$(slideControl).addClass("paused");
			slidePlaying = false;
		}
	}
	
	
});
