﻿/// <reference path="jquery-1.4.1.min.js" />
/// <reference path="jquery-ui-1.8.14.custom.min.js" />
/// <reference path="jquery.timers-1.2.js" />

function createMenuAnimation(menuItemsInfoArray, showAllButtonId) {
    var showAllButton = $('#' + showAllButtonId);
    var showAllButtonClicked = false;
    showAllButton.click(function () {
        if (showAllButtonClicked) {
            hideAllMenuItems(menuItemsInfoArray);
            animateMenuItems(menuItemsInfoArray);
            showAllButtonClicked = false;
        }
        else {
            unbindAnimation(menuItemsInfoArray);
            showAllMenuItems(menuItemsInfoArray);
            showAllButtonClicked = true;
        }
    });
    animateMenuItems(menuItemsInfoArray);
}

function animateMenuItems(menuItemsInfoArray) {
    $.each(menuItemsInfoArray, function () {
        animateMenuItem(this[0], this[1]);
    });
}

function unbindAnimation(menuItemsInfoArray) {
    $.each(menuItemsInfoArray, function () {
        var logo = $('#' + this[0]);
        logo.unbind('mouseover');
        logo.unbind('mouseout');

        var menuItemBody = $('#' + this[1]);
        menuItemBody.unbind('mouseover');
        menuItemBody.unbind('mouseout');
    });
}

function showAllMenuItems(menuItemsInfoArray) {
    $.each(menuItemsInfoArray, function (index) {
        var menuItemBody = $('#' + this[1]);
        setTimeout(function () {
            showMenuItem(menuItemBody);
        }, 100 * index);
    });
}

function hideAllMenuItems(menuItemsInfoArray) {
    $.each(menuItemsInfoArray, function (index) {
        var menuItemBody = $('#' + this[1]);
        setTimeout(function () {
            hideMenuItem(menuItemBody);
        }, 100 * index);
    });
}

function animateMenuItem(logoId, menuItemBodyId) {
    var logo = $('#' + logoId);
    var menuItemBody = $('#' + menuItemBodyId);
    var timerLabel = generateGUID();
    logo.mouseover(function () {
        menuItemBody.stopTime(timerLabel);
        showMenuItem(menuItemBody);
    }).mouseout(function () {
        menuItemBody.oneTime(300, timerLabel, function () { hideMenuItem(menuItemBody) });
    });

    menuItemBody.mouseover(function () {
        menuItemBody.stopTime(timerLabel);
    }).mouseout(function () {
        menuItemBody.oneTime(300, timerLabel, function () { hideMenuItem(menuItemBody) });
    });
}

function showMenuItem(menuItemBody) {
    menuItemBody.switchClass("HiddenPictogramma", "Pictogramma", 450);
}

function hideMenuItem(menuItemBody) {
    menuItemBody.switchClass("Pictogramma", "HiddenPictogramma", 450);
}

function generateGUID() {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
