﻿/*menu*/

var DelayMenu = function(idmenu) {

    /* Private Members */
    var _hoverClass = 'statoHoverJs';
    var _hoverDelay = 500;
    var _hoverLi = [];
    var _hoverIntv;

    /* Private Methods */
    var _$ = function(id) {
        return (document.getElementById)
			? document.getElementById(id)
			: document.all(id);
    };
    var _addClass = function(el, c) {
        if (el.className !== '') {
            var classes = el.className.toLowerCase().split(' ');
            for (var i = 0; i < classes.length; i++) {
                if (c.toLowerCase() === classes[i]) { return; }
            }
        }
        el.className = [el.className, c].join(' ');
    };
    var _removeClass = function(el, c) {
        var cRe = new RegExp(['(^|\\b)', c, '($|\\b)'].join(''), 'i');
        el.className = el.className.replace(cRe, '');
        if (el.className.toString() === '') {
            el.removeAttribute('class');
        }
    };
    return {
        /* Privileged Methods */
        setHoverClass: function(hc) {
            if (typeof hc === 'string' && (/^\w[\w\d]*$/).test(hc)) {
                _hoverClass = hc;
            }
        },
        setHoverDelay: function(hd) {
            if (!isNaN(hd)) {
                _hoverDelay = hd;
            }
        },
        /* Public Method (constructor) */
        init: function() {

            /* remove pure css approach */
            if (_$('stileNoJavascript')) {
                var cssrules = _$('stileNoJavascript');
                cssrules.parentNode.removeChild(cssrules);
            };

            // collect all vocePrimoLivello list-items
            var mLinks = _$(idmenu).getElementsByTagName('a');
            for (var i = 0; i < mLinks.length; i++) {
                if (mLinks[i].rel === 'vocePrimoLivello' &&
                    mLinks[i].parentNode.className !== 'corrente') {
                    _hoverLi[_hoverLi.length] = mLinks[i].parentNode;
                }
            };
            /* Set onmouseover/onmouseout events for timed delay */
            for (var i = 0; i < _hoverLi.length; i++) {
                var li = _hoverLi[i];
                li.onmouseover = (function(i) {
                    return function() {
                        var _thisLi = this;
                        clearInterval(_hoverIntv);
                        _hoverIntv = setTimeout(function() {
                            for (var j = 0; j < _hoverLi.length; j++) {
                                _removeClass(_hoverLi[j], _hoverClass)
                            };
                            _addClass(_thisLi, _hoverClass);
                        }, _hoverDelay);
                    }
                })(i);
                li.onmouseout = (function(i) {
                    return function() {
                        var _thisLi = this;
                        clearInterval(_hoverIntv);
                        _hoverIntv = setTimeout(function() {
                            _removeClass(_thisLi, _hoverClass)
                        }, _hoverDelay);
                    }
                })(i);
            }
        }  /* end init function */
}  /* end return statement */
    };
    window.onload = function() {
        /* Creo un unico namespace per un'ipotetica applicazione 
        * ed evitare la creazione di un numero eccessivo di variabili globali.
        */
        var myApp = {};
        myApp.myMenu2 = new DelayMenu('menu');
        myApp.myMenu2.setHoverDelay(500);
        myApp.myMenu2.init();
    };

    /*Focus da tastiera del menu 
    visualizzaMenuFocus () {
        document.getElementById
    }*/

    /*Home page*/



    

