$(document).ready(function () {

    var loadComplete = false;
    var pageData = "";
    $(".nav a, .ajaxme").live("click", function (e) {
        e.preventDefault();
        $.History.go($(this).attr("href"));
    });
    $.History.bind(function (state) {
        $(".replace").fadeTo(300, 0, function () {
            loadNew();
        });
        clearTimeout(thetimer);
        $(".slideshow img").stop().fadeTo(500, 0, function () {
            $(this).remove();
        });
        $.ajax({ type: "GET", url: "slideshow/" + state, success: function (data) {
            $(data).fadeTo(0, 0).appendTo(".slideshow").fadeTo(500, 1);
            if (state == "index.php" || state == "") {
                c = 0;
                q = 0;
                li = false;
                goSlideshow();
                $(".bar").animate({ "top": "240px", "left": "50px", "width": "840px" }, 500);
                $(".columns").css("display", "block");
            } else {
                $(".bar").animate({ "top": "235px", "left": "15px", "width": "912px" }, 500);
                $(".columns").css("display", "none");
            }
        }
        });
        $.ajax({ type: "GET", url: "content/" + state, success: function (data) {
            pageData = data;
            loadComplete = true;
            if (state == 'index.php') {
                $(".bar").css("text-align", "right");
            } else {
                $(".bar").css("text-align", "center");
            }
        }
        });
    });
    function loadNew() {
        if (loadComplete == false) {
            setTimeout(loadNew, 200);
        } else {
            loadComplete = false;
            animatePage(pageData, $(".replace"));
            $(".accordion").accordion();
        }
    }

    $("#submit").live("click", function (e) {
    });

    var c = 0;
    var q = 0;
    var li = false;
    var s = [];
    // image, delay, speed, anchor, effect
    s[0] = ["3.jpg", 3000, 2000, "in", 480];
    s[1] = ["4.jpg", 3000, 2000, "in", 480];
    s[2] = ["7.jpg", 3000, 2000, "in", 0];
    s[3] = ["8.jpg", 3000, 2000, "in", -200];
    s[4] = ["12.jpg", 3000, 2000, "out", -480];
    s[5] = ["15.jpg", 3000, 2000, "in", 480];
    s[6] = ["16.jpg", 3000, 2000, "in", 480];
    s[7] = ["17.jpg", 3000, 2000, "in", 0];
    s[8] = ["18.jpg", 3000, 2000, "out", -480];
    s[9] = ["1.jpg", 3000, 2000, "out", 0];

    var thetimer = "";
    function goSlideshow() {
        $("<img src='images/slides/" + s[c][0] + "' alt='' />").bind("load", function () { li = true }).prependTo(".slideshow");
        q = c;
        if ($.History.state == "index.php" || $.History.state == "") {
            thetimer = setTimeout(goAnimation, s[c][1]);
        }
        c += 1;
        if (s.length == c) {
            c = 0
        };
    };
    function goAnimation() {
        if (li == false) {
            thetimer = setTimeout(goAnimation, 100);
        } else {
            li = false;

            if (s[q][3] == "in") {
                $(".slideshow>img:eq(0)").clone().insertAfter(".slideshow>img:eq(0)").css({ "opacity": 0, "width": "0px", "margin-left": 0 + 480 - (s[q][4]) + "px", "margin-top": "160px" }).animate({ "width": "960px", "margin-left": "0px", "margin-top": "0px", "opacity": 1 }, s[q][2] * 0.8, function () {
                    $(this).remove();
                });
                $(".slideshow>img:eq(0)").css("opacity", 0).fadeTo(s[q][2], 1);
                $(".slideshow>img").last().clone().appendTo(".slideshow").animate({ "opacity": 0, "width": "1440px", "margin-left": 0 - 240 + (s[q][4] * 0.5) + "px", "margin-top": "-80px" }, s[q][2] * 0.5, function () {
                    $(this).remove();
                });
                $(".slideshow>img:eq(2)").animate({ "opacity": 0, "width": "2880px", "margin-left": 0 - 960 + (s[q][4] * 2) + "px", "margin-top": "-320px" }, s[q][2], function () {
                    $(this).remove();
                    thetimer = goSlideshow();
                });
            } else {
                $(".slideshow>img:eq(0)").css({ "opacity": 0, "width": "2880px", "margin-left": 0 - 960 + (s[q][4] * 2) + "px", "margin-top": "-320px" }).appendTo(".slideshow").clone().insertAfter(".slideshow>img:eq(1)").css({ "width": "1440px", "margin-left": 0 - 240 + (s[q][4] * 0.5) + "px", "margin-top": "-80px" });
                $(".slideshow>img:eq(0)").clone().prependTo(".slideshow");
                $(".slideshow>img:eq(1)").animate({ "width": "0px", "margin-left": 0 + 480 - (s[q][4]) + "px", "margin-top": "160px" }, s[q][2], function () {
                    $(".slideshow>img:eq(1), .slideshow>img:eq(0)").remove();
                });
                $(".slideshow>img:eq(2)").animate({ "width": "960px", "margin-left": "0px", "margin-top": "0px", "opacity": 1 }, s[q][2]);
                $(".slideshow>img:eq(3)").animate({ "width": "960px", "margin-left": "0px", "margin-top": "0px", "opacity": 1 }, s[q][2], function () {
                    $(this).remove();
                    thetimer = goSlideshow();
                });
            }
        }
    }
    $(window).load(function () {
        if (window.location.hash == "" || window.location.hash == "index.php") {
            goSlideshow();
        }
    });
});
