$(function() {
	
	var hidden = false;
	var active;
	var total = $("div.thumbs a").size();
	var hidden;
	var slideTimer;
	var isPaused = false;
	
	if (total<2) {
		$("div.thumbs a").hide();
	}
	else {
		$("#container .left").append("<a href='#' id='pause'><img src='img/control-pause.png' alt='pause' /></a>").find("a#pause").click(pauseSlideShow);
		$("#container .left")
			.append("<a href='#' class='navSl' id='prev'><img src='img/slidenavLeft.png' alt='' /></a><a id='next' class='navSl' href='#'><img src='img/slidenavRight.png' alt='' /></a>")
			.find(".navSl")
			.click(slideNavClick);
		initTimer();
	}
	function initTimer() {
		slideTimer = setInterval(function() {
				$("a#next").click();
			}, 10000);
		isPaused = false;
	}
	function pauseSlideShow(e) {
		if (!isPaused) {
			isPaused = true;
			clearInterval(slideTimer);
			$(this).html("<img src='img/control.png' alt='play' />");
		}
		else {
			$(this).html("<img src='img/control-pause.png' alt='pause' />");
			initTimer();	
		}
		return false;
	}
	$("div.thumbs a").click(function() {
		
		active = $("div.thumbs a").index(this);
		
		var html = $.trim($(this).next(".imageInfo").html());
		var image = $(this).find("img").attr("src");
		
		//$("#container .left img").animate({opacity:0},{duration:1000, queue:false, complete});
		
		$("#container .left img").eq(0).attr("src", image);
		$("#imageInfoContainerContent").html(html);
	
		if (html == "")
			$("#imageInfoContainer").hide();
		else
			$("#imageInfoContainer").animate({bottom:0}, {duration:"normal"});hidden = false;
		return false;
	
	}).eq(0).click();
	
	$("a#min").click(function() {
		
		var bottom = parseInt($("#imageInfoContainer").height()) -20;
		if (!hidden) {
			$("#imageInfoContainer").animate({bottom:"-"+bottom+"px"}, {duration:"normal"});
			hidden = true;
		}
		else {
			$("#imageInfoContainer").animate({bottom:0}, {duration:"normal"});
			hidden = false;	
		}
		return false;
	
	});
	function slideNavClick(e) {
		if (e.currentTarget.id == "next") {
			active++;
		}
		else {
			active--;
		}
		active = active > total-1 ? 0:(active < 0 ? total-1: active);
		$("div.thumbs a").eq(active).click();	
		return false;
	}
	
});
