﻿/// <reference path="jquery-1.5-vsdoc.js" />


jQuery.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this; 
    }


function randomizeArray(v) {
    n = v.length-1;
    //per 20 volte scorre array e scambia 
    for (i = 0; i <= 20; i++ ) {
        for (j = 0; j <= n;j++ ) {
            p = parseInt(Math.random() * n);
            tmp = v[j];
            v[j] = v[p];
            v[p] = tmp;
        }
    }
}

var menu;
var menu_min = 30;
var menu_max = 200;
var menu_delta=menu_max-menu_min;
var current_SubMenu;


jQuery(document).ready(
    function () {
        menu = jQuery('#menu');

        //Inizializzazione menu / sottomenu
        menu.css({ opacity: 1, height: menu_min }); //Chiuso

//        jQuery("#sm_Prodotti").css("top", menu_max); //nascosto
//        jQuery("#sm_Multimedia").css({ top: menu_max, left: 200 }); //nascosto

        jQuery("#sm_Prodotti").css({ top: 40, left: 200 }).hide();
        jQuery("#sm_Multimedia").css({ top: 40, left: 200 }).hide(); 


        //handler apertura/chiusura menu principale
        menu.hover(function () { openMenu() }, function () { closeMenu() });

        //handler attivazione voci menu
        jQuery(".menuvoiceContainer a").hover(
           function () { jQuery(this).animate({ color: "#fff" }, 300)},
           function () { jQuery(this).animate({ color: "#555" }, 400) })
        jQuery("#menuLeft a").hover(
           function () { openSubMenu(jQuery(this).attr("submenuCtl")); },
           function () { })


        //handler comparsa sottomenu       
        //jQuery("#mn_Prodotti").hover(function () { openSubMenu("#sm_Prodotti") }, function () { });
        //jQuery("#mn_Multimedia").hover(function () { openSubMenu("#sm_Multimedia") }, function () { });

    });

    function openSubMenu(sm_name) {
        closeSubMenu();
        //sm = jQuery(sm_name).animate({ top: 30 }, 300, 'swing');
        sm = jQuery(sm_name).fadeTo(1000,1); //.animate({ top: 30 }, 300, 'swing');
        current_SubMenu = sm;
    }
     

function closeSubMenu() {
    if (current_SubMenu == null) return;
    current_SubMenu.stop();
    
    //current_SubMenu.css({ top: menu_max });
    current_SubMenu.hide();

    current_SubMenu = null;
    //current_SubMenu.animate({ top: menu_max }, 300, 'swing', function () { current_SubMenu = null });
}    


function openMenu() {

    menu.stop();
    menu.css("z-index",1000);
    menu.animate({
        opacity: 0.8
        , height: menu_max
    }
        , 500, 'swing');
    //jQuery('#txt').html(menu.height());
}

function closeMenu() {
    //Se il mouse esce da menu ma verso sotto e il menu si sta aprendo allora non lo chiude
    if (jQuery(menu).is(':animated') && window.event.clientY > jQuery(menu)[0].offsetTop) return;
    menu.stop();
    menu.animate({
        opacity: 1
        , height: menu_min
    }
    , 800, 'swing');
    //jQuery('#txt').html(menu.height());
}


