﻿var jqActiveMenu = null;
var navTimeout = 0;

$(document).ready(function() {
    var p = window.location.href;
    if (p.indexOf(".com/") > -1) p = p.substr(p.indexOf(".com/") + 4);
    else if (p.indexOf(".uk/") > -1) p = p.substr(p.indexOf(".uk/") + 3);
    else if (p.indexOf(".biz/") > -1) p = p.substr(p.indexOf(".biz/") + 4);
    if (p.charAt(p.length - 1) != "/") p = p + "/";
    if (/^\/([a-zA-Z\-]+\/)?$/.test(p)) {
        var jqActiveLink = $("#Navigation a[href=" + p + "]");
        if (jqActiveLink.length > 0) {
            jqActiveLink.addClass("active");
            jqActiveLink.children("img").css("background-position", "0px -74px");
        }
    }

    $("#Navigation a:not(.active)").mouseover(function() { $(this).children("img").css("background-position", "0px -37px") }).mouseout(function() { $(this).children("img").css("background-position", "") });

    $(".menu").each(function() {
        $("#MenuParent_" + this.id.substr(5)).mouseover(function() {
            clearTimeout(navTimeout);
            if (jqActiveMenu != null && jqActiveMenu.selector == "#Menu_" + this.id.substr(11)) return;
            if (jqActiveMenu != null) {
                jqActiveMenu.slideUp(0);
                hideActiveMenu();
            }
            jqActiveMenu = $("#Menu_" + this.id.substr(11));
            jqActiveMenu.css({ "left": $(this).offset().left + "px", "top": ($(this).offset().top + $(this).outerHeight() - 2) + "px" }).slideDown("fast");
        }).mouseout(function() {
            navTimeout = setTimeout(hideActiveMenu, 250);
        });
    }).mouseover(function() {
        clearTimeout(navTimeout);
    }).mouseout(function() {
        navTimeout = setTimeout(hideActiveMenu, 500);
    });
});

function hideActiveMenu() {
    jqActiveMenu.css({ "display": "none" });
    jqActiveMenu = null;
}

var Gallery = {

    imageWidth: 0,
    imageUrls: null,
    isLoaded: null,
    index: 0,
    isBusy: false,
    timeoutId: 0,

    initialise: function(imageUrls) {
        this.imageUrls = imageUrls;
        this.isLoaded = new Array(imageUrls.length);
        for (var i = 0; i < this.isLoaded.length; i++) this.isLoaded[i] = i == 0;

        this.imageWidth = $("#Gallery div.images").width();
        $("#Gallery .previous").css("opacity", "0.4");
    },

    next: function() {
        if (this.isBusy || this.index == this.imageUrls.length - 1) return;
        this.index++;
        this.go();
    },

    previous: function() {
        if (this.isBusy || this.index == 0) return;
        this.index--;
        this.go();
    },

    go: function() {
        this.isBusy = true;
        $("#Gallery .next").css("opacity", this.index == this.imageUrls.length - 1 ? "0.4" : "1.0");
        $("#Gallery .previous").css("opacity", this.index == 0 ? "0.44" : "1.0");
        if (this.isLoaded[this.index]) {
            $("#Gallery div.images div").animate({ "left": -(Gallery.index * Gallery.imageWidth) + "px" }, 400, "swing", function() { Gallery.isBusy = false; });
        } else {
            this.timeoutId = setTimeout(function() { $("#Gallery img.loading").css("visibility", "visible") }, 100);
            $("#Gallery div.images div img:eq(" + this.index + ")").load(function() {
                Gallery.isLoaded[Gallery.index] = true;
                clearTimeout(Gallery.timeoutId);
                $("#Gallery img.loading").css("visibility", "hidden");
                $("#Gallery div.images div").animate({ "left": -(Gallery.index * Gallery.imageWidth) + "px" }, 400, "swing", function() { Gallery.isBusy = false; });
            }).attr("src", this.imageUrls[this.index]);
        }
    }

}