// JavaScript Document
$(function(){
		var thumbdirectory="images/thumb/";
		var largedirectory="images/large/";
		var onImg=1;
		var caption="";
		var activeImage=0;
		var useImg="";
		var offImg="";
		var linkurl="";
		
		var slidecount=$("#thumbnails img").size();
		var slideheight=$("#thumbnails img").eq(0).height() + parseInt($("#thumbnails img").eq(0).css("padding-bottom").split("px")[0]) + parseInt($("#thumbnails img").eq(0).css("margin-bottom").split("px")[0]);
		var slideInterval = 0;
		var maxheight=(slidecount * slideheight) - parseInt($("#thumbnailContainer").eq(0).css("height").split("px")[0]);
		
		
		$("#play").click(function(){
			slideInterval = setInterval (startautoscroller, 10);
		});
		
		$("#stop").click(function(){
			clearInterval(slideInterval);
		});
		
		$("#up").hover(
			function () {
				slideInterval = setInterval ( upbutton, 10 );
		  	},
			function () {
				clearInterval(slideInterval);
		  	}
		);
		$("#down").hover(
			function () {
				slideInterval = setInterval ( downbutton, 10 );
		  	},
			function () {
				clearInterval(slideInterval);
		  	}
		);
		
		/*$("#galleryCategoriesContainer li").click(function(){
			var category=($(this).text());
			$("#thumbnails").load("newcategory.php", { 'category': category } );
		});*/

		$("#thumbnails img").click(function(){
		
			activeImage=$(this).parent().children("img").index($(this));
		
			var src=largedirectory + this.src.split(thumbdirectory)[1];
			var ImgNo=onImg % 2;
			
			if(ImgNo == 1) {
				useImg = "#mainImage2";
				offImg = "#mainImage";
			}else{
				useImg = "#mainImage";
				offImg = "#mainImage2";
			}
			onImg++;
			
			$(useImg).attr('src',src);
			
			$(offImg).fadeOut("slow");
			$(useImg).fadeIn("slow");
			
			var captionsplit=this.alt.split("|");
			caption=captionsplit[0];
			linkurl=captionsplit[1];
			$(useImg).parent().attr('href',linkurl);
			//alert($(useImg).parent().attr('href'));
			//alert(linkurl);
			
			$("#caption").fadeOut("slow", function(){
				$("#caption").html(caption);
			});
			$("#caption").fadeIn("slow");
			
		});
		
		/*
		$("#up").hover(function(){
			//alert("clicked"+slidecount);
			alert("hovering");
		},function(){
			alert("not hovering");
		});*/
		/*$("#down").hover(function(){
			//alert("clicked"+slidecount); parseInt($(".slide").css("width").split("px")[0]);
			alert("hovering");
			
			//currenttop=$("#thumbnails").css("top").split("px")[0];
			//var slideInterval = setInterval ( downbutton, 5000 );
		},{
			alert("not hovering");
		});
		*/
		function downbutton(){
			var maxtop=-maxheight;
			var currenttop=$("#thumbnails").css("top").split("px")[0];
			if(currenttop>maxtop){
				var topPos=(currenttop-1)+"px";
				$("#thumbnails").css({top:topPos});
			}
		}
		
		function upbutton(){
			var maxtop=0;
			var currenttop=parseInt($("#thumbnails").css("top").split("px")[0]);
			if(currenttop<maxtop){
				var topPos=(currenttop+1)+"px";
				$("#thumbnails").css({top:topPos});
			}
		}
		
		function startautoscroller(){
			if(activeImage == slidecount-1){
				activeImage=0;
			}else{
				activeImage++;
			}
			
			topPos=parseInt(-activeImage*slideheight);
			var maxtop = -maxheight;
			if(topPos <= maxtop){
				topPos=maxtop;
			}
			topPos=topPos + "px";
			$("#thumbnails").animate({
				top:topPos
			},1000);			
			/*var maxtop=-maxheight;
			var currenttop=parseInt($("#thumbnails").css("top").split("px")[0]);
			if(currenttop>maxtop){
				var topPos = currenttop-slideheight;
				
				if(currenttop>activeImage*slideheight){
					topPos = topPos + (Math.abs(currenttop%slideheight));
				}else{
					topPos = topPos - (Math.abs(currenttop%slideheight));
				}
				topPos = topPos + "px";
				
				$("#thumbnails").animate({
					top:topPos
				},1000);
			}else{
				$("#thumbnails").animate({
					top:"0px"
				},1000);
			}*/
			
			
			// Animate Main Image Change
			var ImgNo=onImg % 2;
			//alert(ImgNo);
			
			if(ImgNo == 1) {
				useImg = "#mainImage2";
				offImg = "#mainImage";
			}else{
				useImg = "#mainImage";
				offImg = "#mainImage2";
			}
			
			
			//alert(activeImage);
			var src=largedirectory + $("#thumbnails img").eq(activeImage).attr("src").split(thumbdirectory)[1];
			//alert($("#thumbnails img").index().eq(activeImage).src);
			
			
			$(useImg).attr('src',src);
			$(offImg).fadeOut("slow");
			$(useImg).fadeIn("slow");
			//
			//caption=$("#thumbnails img").eq(activeImage).alt;
			caption=$("#thumbnails img").eq(activeImage).attr("alt");
			
			var captionsplit=caption.split("|");
			caption=captionsplit[0];
			linkurl=captionsplit[1];
			$(useImg).parent().attr('href',linkurl);
			
			$("#caption").fadeOut("slow", function(){
				$("#caption").html(caption);
			});
			$("#caption").fadeIn("slow");
			
			onImg++;
			
			// Animate Image Change
			//var oldchild = Math.abs((currenttop + Math.abs(currenttop % slideheight)) / slideheight);
			//var useImgSrc = $("#thumbnails").children().eq(activechild).src;
			//alert(activechild);
			
			// Adjust the interval (since the first time this is called, it fires immediately)
			clearInterval(slideInterval);
			slideInterval = setInterval (startautoscroller, 3000);
		}
		
	});
		
		/*
		// Featured Content Carousel
		var i=0;
		var slidecount = $(".slide").size();
		var slidewidth = parseInt($(".slide").css("width").split("px")[0]);
		var clickedft=-1;
		
		$(".slide").each(function(){
			var leftPos = ($(this).parent().children(".slide").index($(this))*slidewidth)+"px";
			$(this).css({left:leftPos});
		});
		
		var next_interval = setInterval(slidenext,8000);
		
		$("#featuredBottom ul li").click(function(){
			clickedft=parseInt($("#featuredBottom ul li").index(this))+1;
			clearInterval(next_interval);
			slidenext();
		});
		
		function slidenext(){
			var passclicked = clickedft;
			var slidecounter = 0;
			$(".slide").each(function(){
				if(passclicked>0){
					var leftPos = ((slidecounter-(passclicked-1))*slidewidth)+"px";
					slidecounter++;
				}else{
					if(i%slidecount<slidecount-1){
						var leftPos = ($(this).css("left").split("px")[0]-slidewidth)+"px";
					}else{
						var leftPos = ($(".slide").index($(this))*slidewidth)+"px";
					}
				}
				$(this).animate({left:leftPos},1500);
			});
			if(passclicked>0){
				movearrow(passclicked-1);
			}else{
				movearrow((i+1)%slidecount);
			}
			i++;
		}
		
		function movearrow(active){
			var arrowPos = ((active)*30)+19 +"px";
			$("#ftArrow").animate({
				left: arrowPos
			}, 1500 );
		}
	
		// Tabber - Right Sidebar
		$("#tabNav").children("li").click(function(){
			var clickedtab=($(this).parent().children("li").index($(this)));
			$("#tabNav").children("li").removeClass();
			$("#tabNav").children("li").eq(clickedtab).addClass("selected");
			$("#tabber").children(".tabbed").css({display:"none"});
			$("#tabber").children(".tabbed").eq(clickedtab).css({display:"block"});
		});
	});*/