$(function(){

    var totalImages = 6;
    var fadeSpeed = 2500;
    var panSpeed = 6000;
    
    var totalReached = false;
    var index = 0;
    
    var i;
    var img;
    

    
    function animateImage(){

        var con = $("#slideshow");
        var src = $("#slideshow .active").attr("src");
        var path = src.substring(0, src.lastIndexOf("/") + 1);
        index = src.replace(path, "").replace(".jpg", "");
        
        if (!totalReached) { // load the next image
            var zindex = totalImages - index;
            index++;
                       
            if (index == totalImages) {
                totalReached = true;
            }
            else {
                if (con.html().indexOf(index + ".jpg") == -1) {
                    con.append('<img src="' + path + index + '.jpg" style="z-index:' + zindex + '"/>');
                }
            }
        }
        
        i = $("#slideshow .active").css("z-index");
        
        var x = $("#slideshow").width() - $("#slideshow .active").width();
        var y = $("#slideshow").height() - $("#slideshow .active").height();
        
        img = $("#slideshow .active");
        img.css("z-index", 100);
        
        switch (index - 1) {
			case 2:
				img.css("margin-top", y).css("margin-left", x).delay(1000).animate({
                    marginTop: 0,
                    marginLeft: 0
                }, panSpeed, function(){
                    nextImage(true);
                });
				break;
            case 3:
                img.css("margin-top", 0).css("margin-left", 0).delay(1000).animate({
                    marginTop: y,
                    marginLeft: x
                }, panSpeed).delay(2000).animate({
                    marginTop: 0,
                    marginLeft: 0
                }, panSpeed, function(){
                    nextImage(true);
                });
                break;
            case 4:
                img.css("margin-top", 0).css("margin-left", 0).delay(1000).animate({
                    marginTop: y
                }, panSpeed, function(){
                    nextImage(false);
                });
                
                break;
            case 5:
                img.css("margin-top", y).css("margin-left", x).delay(1000).animate({
                    marginTop: 0,
                    marginLeft: 0
                }, panSpeed).delay(1000).animate({
                    marginTop: y + 200,
                    marginLeft: x + 100
                }, panSpeed, function(){
                    nextImage(false);
                });
                
                break;
                
            default:
                img.css("margin-top", y).css("margin-left", x).delay(1000).animate({
                    marginTop: 0,
                    marginLeft: 0
                }, panSpeed, function(){
                    nextImage(false);
                });
                break;
        }
    }
    
    function nextImage(top){
        if (!img.next().attr("complete")) {
            if (totalReached) {
                $("#slideshow img:first").show();
                img.delay(5000).fadeOut(5000);
            }
        }
        else {
            if (!top) 
                img.next().css("margin-top", $("#slideshow").height() - img.next().height()).css("margin-left", $("#slideshow").width() - img.next().width());
            img.delay(2000).fadeOut(fadeSpeed, function(){
                $(this).css("z-index", i).removeClass("active").next().addClass("active");
                animateImage();
            });
        }
        
    }
    
    
    $(window).load(function(){
		$("#slideshow").css("visibility","visible");
        $("#slideshow .active").css("z-index", totalImages);
        animateImage();
    });
    
    $(window).resize(function(){
		if (!totalReached) {
			$("#slideshow .active").clearQueue();
			$("#slideshow .active").stop();
			$("#slideshow").html("").html('<img src="slideshow/0.jpg" class="active"/>');
			totalReached = false;
			index = 0;
			animateImage();
		}
    });
    
});

