﻿
(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        $("img").each(function () {
            var cacheImage = document.createElement('img');
            cacheImage.src = $(this).attr("src");
            cache.push(cacheImage);
        });
    }
})(jQuery)

$(document).ready(function () {
    jQuery.preLoadImages();
    registerHovers();

    $(document).bind("contextmenu", function (e) {
        return false;
    });
});
$(window).load(function () {
    initSlide();
});

function registerHovers() {
    $(".menu a img").hover(
        function () { $(this).attr("src", $(this).attr("src").replace("_off", "_on")); },
        function () { $(this).attr("src", $(this).attr("src").replace("_on", "_off")); }
    );
    $(".footer a img").hover(
        function () { $(this).attr("src", $(this).attr("src").replace("_off", "_on")); },
        function () { $(this).attr("src", $(this).attr("src").replace("_on", "_off")); }
    );
}

var titleID = "";
var tInterval = null;
var loading = false;
function initSlide() {
    if ($(".slide div").length > 1) {

        $(".slide").prepend('<a class="slide-left"><img src="/images/arrow_left.png" title="Previous" alt="Previous" /></a>')
        $(".slide").append('<a class="slide-right"><img src="/images/arrow_right.png" title="Next" alt="Next" /></a>')

        $(".slide-left").hover(function () { $(this).find("img").show(); }, function () { $(this).find("img").hide(); });
        $(".slide-right").hover(function () { $(this).find("img").show(); }, function () { $(this).find("img").hide(); });

        $(".slide-left").click(function () {
            if (!loading) {
                if (tInterval != null)
                    clearInterval(tInterval);
                slide("prev");
                tInterval = setInterval("slide('next');", 5000);
            }
        });
        $(".slide-right").click(function () {
            if (!loading) {
                if (tInterval != null)
                    clearInterval(tInterval);
                slide("next");
                tInterval = setInterval("slide('next');", 5000);
            }
        });

        $(".slide div.active").show();
        if ($('.slide div.active').attr("rel") != "") {
            if (titleID != "")
                $(titleID).removeClass("selected");
            titleID = "#" + $('.slide div.active').attr("rel");
            $(titleID).addClass("selected");
        }
        tInterval = setInterval("slide('next');", 5000);
    }
}
function slide(dir) {
    loading = true;
    var $active = $(".slide div.active");

    if ($active.length == 0) $active = $(".slide div:first");
    var $next = null;
    if (dir == "next")
        $next = $active.next("div").length ? $active.next() : $(".slide div:first");
    else
        $next = $active.prev("div").length ? $active.prev() : $(".slide div:last");

    $active.addClass("last-active");

    $active.show();
    $next.show();

    $next.css({ opacity: 0.0 })
        .addClass("active")
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.hide();
            $active.removeClass("active");
            $active.removeClass("last-active");
            loading = false;
        });

    if ($next.attr("rel") != "") {
        if (titleID != "")
            $(titleID).removeClass("selected");
        titleID = "#" + $next.attr("rel");
        $(titleID).addClass("selected");
    }
}
