﻿var rotationTimerId = null;

function Hook() {
    $("#Icons img").click(function (event) {
        event.preventDefault();

        clearInterval(rotationTimerId);

        setTimeout(
            function () {
                clearInterval(rotationTimerId);
                rotationTimerId = setInterval(onInterval, 8000);
            },
            12500
        );

        onClickedImage($(this));
    });

    $("#Icons img").stop().fadeTo(350, 0.5);

    onClickedImage($("#Icons img").first());

    $("#Icons img").hover(
        function () { if ($(this).hasClass("Current")) { return; } $(this).stop().fadeTo(250, 1); },
        function () { if ($(this).hasClass("Current")) { return; } $(this).stop().fadeTo(250, 0.5); }
    );

    $("#Shots img").click(function (event) {
        window.location = $(this).attr("href");
    });

    rotationTimerId = setInterval(
        onInterval,
        8000
    );
}

function onInterval(){
    var index = $("#Icons img.Current").parent().index();

    if ((index + 1) >= $("#Icons img").length) { index = -1; }

    onClickedImage(
        $("#Icons img:eq(" + (index + 1).toString() + ")")
    );
}

function onClickedImage(ele) {
    $("#Icons img.Current").stop().fadeTo(250, 0.5).removeClass("Current");
    ele.stop().fadeTo(250, 1).addClass("Current");

    if ($("#Shots img:eq(" + ele.parent().index() + ")").hasClass("Current")) return;

    $("#Shots img.Current").removeClass("Current").css("z-index", "0").animate({ top: '-=437', opacity: 0 }, 1400, 'easeOutBack', function () { $(this).css("top", "-437px").hide(); });

    $("#Shots img:eq(" + ele.parent().index() + ")").delay(400).show().css("z-index", "1").animate({ top: '+=437', opacity: 1 }, 750, 'easeOutBack', function () { $(this).css("opacity", ""); }).addClass("Current");
}
