$(document).ready(function(){
	var current = 1;
	var last = 1;
    	var total = $("#img-container").children().length;
	var slideshowOpen = false;
    	var intervalID;
	var intervalLength = 3000;
    	var stopped = false;
    	var dW = 40;
	var fullWidth = 715;
	var fullHeight = 490;
	var medWidth = 590;
    	var cWidth = 0;
	var animLength = 300;
	var thumbsCount = 6;

    	$("#slideshow-alpha-box").appendTo("#NCC");
    	$("#image-slideshow-control").appendTo("#NCC");

	
	$("#img-container img").each(function(){
		var cr = $(this);
                var t = new Image();
                t.src = (cr[0].getAttribute ? cr[0].getAttribute("src") : false) || cr[0].src;

                var width = t.width;

                if(medWidth > width) {
                        var left = (medWidth - width) / 2;
                        cr.css("margin-left",left);
                } else {
			cr.css("width",medWidth);
		}
	});

	$("#img-slideshow-container img").each(function(){
		var cr = $(this);
		var t = new Image();
    		t.src = (cr[0].getAttribute ? cr[0].getAttribute("src") : false) || cr[0].src;

		var width = t.width;

		if(fullWidth > width) {
			var left = (fullWidth - width) / 2;
			cr.css("left",left);
		}
	});

    	if(typeof document.body.style.maxHeight === "undefined") {
            	$("#slideshow-alpha-box").css("height", $("#NCC").height());
            	$("#slideshow-alpha-box").css("width", $("#NCC").width());
    	}

    	$(".close-slideshow").click(function(){
            	$("#image-slideshow-control").css("display", "none");
            	$("#slideshow-alpha-box").css("display", "none");
            	slideshowOpen = false;

		var elem = $(".img-container");
                elem.children().each(function(){
			$(this).css("display","none");
		});
		
		var elem2 = $(".img-title");
		elem2.each(function(){
                        $(this).removeClass("show");
                });

            	update(current);
            	$(".toggleSlideshow").html("Pysäytä");
            	stopped = false;
            	window.clearInterval(intervalID);
    	});

    	$(".openShow").click(function(){
            	var elem = $("#img-slideshow-container");
            	elem.css("height", elem.children("img:nth-child("+current+")")[0].height);
            	$("#image-slideshow-control").css("display", "block");
            	$("#slideshow-alpha-box").css("display", "block");
            	slideshowOpen = true;
            	update(current);
            	intervalID = window.setInterval(crossfade, intervalLength);
            	return false;
    	});

    	$(".thumb").click(function() {
            	var index = $(this).parent().children().index(this) +1;
            	last = current;
		current = index;
            	update(current);
		stopSlideShow();
    	});

    	$(".prev").click(function(){
		last = current;
            	if(current > 1) {
                    	current--;
            	} else {
                    	current = total;
            	}
            	update(current);
            	posUpdate(true);
		stopSlideShow();		
    	});

    	$(".next").click(function(){
		last = current;
            	if(total > current) {
                    	current++;
            	} else {
                    	current = 1;
            	}
            	update(current);
            	posUpdate(false);
		stopSlideShow();
    	});

    	$(".toggleSlideshow").click(function(){
		if(stopped) {
                        intervalID = window.setInterval(crossfade, intervalLength);
                        stopped = false;
                        $(this).html('Pysäytä');
                } else {
                        stopped = true;
                        window.clearInterval(intervalID);
                        $(this).html('Jatka');
                }
	});

	function stopSlideShow() {
		if(slideshowOpen) {
			stopped = true;
        	        window.clearInterval(intervalID);
                	$(".toggleSlideshow").html('Jatka');
		}
	}

    	function posUpdate(dir) {
            	if(total > thumbsCount) {
                	if(dir) {
                            	// previous
                            	var n = current - 1;
                            	var test = n * dW;
                            	if(current == total) {
                                    	cWidth = (total - thumbsCount) * dW;
                            	} else if(cWidth > test) {
                                    	cWidth = test;
                            	}
                    	} else {
                            	var n = current - thumbsCount;
                            	var test = n * dW;
                            	if(current == 1) {
                                    	cWidth = 0;
                            	} else if(test > cWidth) {
                                    	cWidth = test;
                            	}
                    	}
                    	$(".ul_thumbs").css("left","-"+cWidth+"px");
            	}
    	}

	function crossfade() {
		last = current;
            	var n = current;
            	if(total > current) {
                    	current = current + 1;
            	} else {
                    	current = 1;
            	}

		$(".slideshow-caption-container").children(".img-caption:nth-child("+last+")").removeClass("show");

            	var elem = $("#img-slideshow-container");
            	var lastElem = elem.children("img:nth-child("+n+")");
            	var cr = elem.children("img:nth-child("+current+")");

            	lastElem.fadeOut(animLength);
            	cr.fadeIn(animLength, function(){
			var el = $(".slideshow-caption-container").children(".img-caption:nth-child("+current+")");
                	if((el[0].className).indexOf("showable") > -1) {
                        	el.addClass("show");
                	}
			$(".title-container").children(".img-title:nth-child("+last+")").removeClass("show");
			$(".title-container").children(".img-title:nth-child("+current+")").addClass("show");
		});

            	var height = cr[0].height == 0 ? cr.height() : cr[0].height;
            	var width = cr[0].naturalWidth != 'undefined' ? cr[0].naturalWidth : cr.width();
            	if(width > fullWidth) {
                    	if(height > fullHeight) {
                            	height = ((fullWidth/width) * height);
                    	}
            	}
            	elem.animate({"height": (height+5)+"px"}, 200);

		$(".thumb:nth-child("+last+") img").css("border","2px solid #fff");
            	$(".thumb:nth-child("+current+") img").css("border","2px solid #2375BB");
            	$(".thumb_span").html(current);
            	posUpdate(false);
    	}

	function update(current) {
		$(".thumb:nth-child("+last+") img").css("border","2px solid #fff");
		var index = ".caption-container";
            	if(slideshowOpen) {
			var elem = $("#img-slideshow-container");
			var cr = elem.children("img:nth-child("+current+")");
			elem.children("img:nth-child("+last+")").css("display","none");
                    	cr.css("display","block");

                    	var height = cr[0].height == 0 ? cr.height() : cr[0].height;
                    	var width = cr[0].naturalWidth != 'undefined' ? cr[0].naturalWidth : cr.width();
                    	if(width > fullWidth) {
                            	if(height > fullHeight) {
                                    	height = ((fullWidth/width) * height);
                            	}
                    	}
                    	elem.css("height", (height+5)+"px");
			index = ".slideshow-caption-container";
			
			$(".title-container").children(".img-title:nth-child("+last+")").removeClass("show");
                        $(".title-container").children(".img-title:nth-child("+current+")").addClass("show");			
            	} else {
			var elem = $("#img-container");
			elem.children("img:nth-child("+last+")").css("display","none");
                    	elem.children("img:nth-child("+current+")").css("display","block");
            	}
            	$(".thumb:nth-child("+current+") img").css("border","2px solid #2375BB");
            	$(".thumb_span").html(current);

		$(index).children(".img-caption:nth-child("+last+")").removeClass("show");
		var el = $(index).children(".img-caption:nth-child("+current+")");
		if((el[0].className).indexOf("showable") > -1) {
			el.addClass("show");
		}
    	}
});

