﻿var initTime = 5000;
var duration = 3000;
var recall = 2000;
var currentcall = null;
var currenttimeout = null;
function Next(instancecall, recuring, iduration) {
    iduration = iduration == null ? duration : iduration;
    //$("#debug").html("<b>Next</b><br />instancecall: " + instancecall + "<br />currentcall: " + currentcall + "<br />recuring: " + recuring + "<br />");
    if (instancecall == currentcall) {
        $("#controls").fadeOut(250);
        $("#slidePanel a.stepleft").animate({ height: 100, width: 113, top: 90, left: -130 }, iduration);
        $("#slidePanel a.center").animate({ height: 200, width: 226, top: 50, left: 0 }, iduration);
        $("#slidePanel a.stepright").animate({ height: 300, width: 339, top: 10, left: 240 }, iduration);
        $("#slidePanel a.nextright").animate({ height: 200, width: 226, top: 50, left: 589 }, iduration);
        $("#slidePanel a.hidden").css("width", "113");
        currenttimeout = setTimeout(function () { ResetAndRecall(instancecall, "next", recuring) }, iduration + 50);
    }
}
function Previous(instancecall, recuring, iduration) {
    iduration = iduration == null ? duration : iduration;
    //$("#debug").html("<b>Previous</b><br />instancecall: " + instancecall + "<br />currentcall: " + currentcall + "<br />recuring: " + recuring + "<br />");
    if (instancecall == currentcall) {
        $("#controls").fadeOut(250);
        $("#slidePanel a.nextleft").animate({ height: 200, width: 226, top: 50, left: 0 }, iduration);
        $("#slidePanel a.stepleft").animate({ height: 300, width: 339, top: 10, left: 240 }, iduration);
        $("#slidePanel a.center").animate({ height: 200, width: 226, top: 50, left: 589 }, iduration);
        $("#slidePanel a.stepright").animate({ height: 100, width: 113, top: 90, left: 820 }, iduration);
        $("#slidePanel a.hidden").css("width", "113");
        currenttimeout = setTimeout(function () { ResetAndRecall(instancecall, "previous", recuring) }, iduration + 50);
    }
}
function ResetAndRecall(instancecall, type, recuring) {
    //$("#debug").html("<b>ResetAndRecall</b><br />instancecall: " + instancecall + "<br />currentcall: " + currentcall + "<br />type: " + type + "<br />recuring: " + recuring + "<br />");
    if (instancecall == currentcall) {
        if (type == "next") {
            var nextleft = $("#slidePanel a.nextleft");
            var stepleft = $("#slidePanel a.stepleft");
            var center = $("#slidePanel a.center");
            var stepright = $("#slidePanel a.stepright");
            var nextright = $("#slidePanel a.nextright");
            stepleft.removeClass("stepleft").addClass("nextleft");
            nextleft.removeClass("nextleft").addClass("hidden");
            $("#slidePanel").append(nextleft);
            var nextnext = nextright.next();
            center.removeClass("center").addClass("stepleft")
            stepright.removeClass("stepright").addClass("center")
            nextright.removeClass("nextright").addClass("stepright")
            nextnext.removeClass("hidden").addClass("nextright");
        } else {
            var nextleft = $("#slidePanel a.nextleft");
            var stepleft = $("#slidePanel a.stepleft");
            var center = $("#slidePanel a.center");
            var stepright = $("#slidePanel a.stepright");
            var nextright = $("#slidePanel a.nextright");
            stepleft.removeClass("stepleft").addClass("center");
            nextleft.removeClass("nextleft").addClass("stepleft");
            var nextnext = $("#slidePanel").children().last();
            center.removeClass("center").addClass("stepright")
            stepright.removeClass("stepright").addClass("nextright")
            nextright.removeClass("nextright").addClass("hidden")
            nextnext.removeClass("hidden").addClass("nextleft");
            $("#slidePanel").prepend(nextnext);
        }
        $("#slidePanel a.hidden,#slidePanel a.nextright").css({ height: '', width: '', top: '', left: '' });
        if (recuring == true) {
            currenttimeout = setTimeout(function () { Next(NewInstanceID(), true) }, recall);
        }
        $("#controls").fadeIn(250);
    }
}
function NewInstanceID() {
    if (currenttimeout != null) {
        clearTimeout(currenttimeout);
        currenttimeout = null;
    }
    var y = Math.floor(Math.random() * 1000);
    currentcall = y;
    return y;
}
$(document).ready(function () {
    try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { };
    setTimeout(function () { if (currentcall == null) { Next(NewInstanceID(), true) } }, initTime);
    $("#imgNext").click(function () { Next(NewInstanceID(), false, 1000); $("#imgPlay").show(); $("#imgPause").hide(); })
    $("#imgPrev").click(function () { Previous(NewInstanceID(), false, 1000); $("#imgPlay").show(); $("#imgPause").hide(); })
    $("#imgPlay").click(function () { Next(NewInstanceID(), true); $("#imgPause").show(); $("#imgPlay").hide(); })
    $("#imgPause").click(function () { NewInstanceID(); $("#imgPlay").show(); $("#imgPause").hide(); })
});
