$(document).ready(function(){
	
	//Code for left Navigation button
	$(".leftNav").hover(function() {
		$(this).fadeTo(500, 1)
	}, function() {
		$(this).fadeTo(500, 0)
	});
	$(".leftNav").click(function(){
		goDirection(-1)
	});
	
	//Code for right navigation button
	$(".rightNav").hover(function() {
		$(this).fadeTo(500, 1)
	}, function() {
		$(this).fadeTo(500, 0)
	});
	$(".rightNav").click(function(){
		goDirection(1);
	});
	
	$("#galleryImage").click(function(){
		goDirection(1);
	});
	
	//code for controls
	$("#play").click(playSlideShow);

	//thumbslider
	assembleThumbs();
	$(".thumbsHolder").hide();
	$("#thumbs").click(thumbPower);
	
	//statementslider
	$(".statementHolder").hide();
	$("#statement").click(showStatement)
	
	changeImage(1);
	
	//keyboard listener
	$(this).keydown(function(e) {
		switch(e.keyCode)
		{
			//<
			case 37:
				goDirection(-1);
			break;
			
			//>
			case 39:
				goDirection(1);
			break;
			
			//p
			case 80:
				playSlideShow();
			break;
			
			//s
			case 83:
				showStatement();
			break;
			
			//t
			case 84:
				thumbPower();
			break;
			
			//j
			case 74:
				goDirection(-1);
			break;
			
			//k
			case 75:
				goDirection(1);
			break;
			
			//+
			case 187:
			break;
		}
	})
});

function assembleThumbs(){
	$(".thumbsContent").append("<span>");
	for (i=1; i <= numImages; i++){
		$(".thumbsContent").append("<img src=\"images/Thumbs/" + $galleryString + "_" + i + "t.jpg\" alt=\"" + i + "\" class=\"thumbImages\"/>");
	}
	$(".thumbsContent").append("</span>")
}

function resizeGallery(_w, _h){
	$(".galleryBackground").css("width", _w + 20);
	$(".galleryBackground").css("height", _h+45);
	$(".gallery").css("width", _w);
	$(".gallery").css("height", _h);
	$(".controls").css("width", _w);
	$(".gallery").css("height", _h);
	$(".thumbsHolder").css("height", _h);
	$(".thumbsHolder").css("width", _w);
	$(".thumbsBackground").css("height", _h);
	$(".thumbsBackground").css("width", _w);
	$(".thumbsBackground").css("top", _h);
	$(".thumbsContent").css("height", _h);
	$(".thumbsContent").css("width", _w-40);
	$(".thumbsContent").css("top", _h);
	$(".statementHolder").css("height", _h);
	$(".statementBackground").css("height", _h);
	$(".statementHolder").css("width", _w);
	$(".statementBackground").css("top", _h);
	$(".statementBackground").css("width", _w);
	$(".statementContent").css("height", _h);
	$(".statementContent").css("width", _w-30);
	$(".statementContent").css("top", _h);
	$(".posTextStatement").css("margin-left", _w-150);
	$(".backgroundNav").css("margin-top", (_h-45)/2);
}

function goDirection(_dir) {
	if($playState)
		playSlideShow();
	changeImage(parseInt($imageNum)+_dir);
}

function changeImage($_val){
	document.getElementById('galleryImage').src = "";
	if($_val == -2)
		$imageNum++;
	else
		$imageNum = $_val;
	if($imageNum > numImages)
		$imageNum = 1;
	if($imageNum < 1)
		$imageNum = numImages;
	if (jQuery.inArray($_val, $resizeArray) == -1) {
		document.getElementById('posTextStr').innerHTML = "";
		resizeGallery($imageWidth, $imageHeight);
	}
	else {
		document.getElementById('posTextStr').innerHTML = "";
		resizeGallery(640, 800);
		$("html, body").animate({
			scrollTop: 150
		}, 400);
	}
	document.getElementById('posTextStr').innerHTML = $imageNum + "/" + numImages;
	document.getElementById('galleryImage').src = "images/" + $galleryString + "_" + $imageNum + ".jpg";
}

function playSlideShow(){
	$playState = !$playState;
	if ($playState) {
		$("#playImage").attr("src", "../images/pauseButton.png");
		$imageChangeInterval = setInterval("changeImage(-2)", 3000);
	} else {
		$("#playImage").attr("src", "../images/playButton.png");
		clearInterval($imageChangeInterval);
	}
}

function thumbPower(){
	$thumbsState = !$thumbsState;
	$(".thumbImages").click(function(){
		if($playState)
			playSlideShow();
		changeImage(parseInt($(this).attr("alt")))
		if($thumbsState)
			thumbPower();
	});
	if ($thumbsState) {
		$(".thumbsHolder").show();
		$(".thumbsBackground").animate({
			top: 0
		}, 1200);
		$(".thumbsContent").animate({
			top: 0
		}, 1200);
	} else {
		$(".thumbsContent").animate({
			top: $(".thumbsHolder").css("height")
		}, 1200);
		$(".thumbsBackground").animate({
			top: $(".thumbsHolder").css("height")
		}, 1200, function() { $(".thumbsHolder").hide(); });
	}
}

function showStatement(){
	$statementState = !$statementState;
	if ($statementState) {
		$(".statementHolder").show();
		$(".statementBackground").animate({
			top: $statementHeight
		}, 1200);
		$(".statementContent").animate({
			top: $statementHeight
		}, 1200);
	} else {
		$(".statementBackground").animate({
			top: $imageHeight
		}, 1200, function() { $(".statementHolder").hide(); });
		$(".statementContent").animate({
			top: $imageHeight
		}, 1200);
	}
}

