var DelayMenu = function(idmenu) {
    
    /* Private Members */
    var _hoverClass     = 'jshovered';
    var _hoverDelay     = 300;
    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 (_$('purecssmenu')) {
                var cssrules = _$('purecssmenu');
                cssrules.parentNode.removeChild(cssrules);
            };
                
            // collect all first-level list-items
            var mLinks = _$(idmenu).getElementsByTagName('a');
            for (var i=0; i<mLinks.length; i++) { 
                if (mLinks[i].rel === 'first-level' && 
                    mLinks[i].parentNode.className !== 'current') { 
                    _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.myMenu1 = new DelayMenu('menu_id1');
	myApp.myMenu1.init();

	/* myApp.myMenu2 = new DelayMenu('menu_id2');
	myApp.myMenu2.setHoverDelay(500);
	myApp.myMenu2.init(); */

};    


