/**
 * HMenu
 * @author Patrick S. de Palma (patrick dot depalma at gmail dot com)
 * @projectDescription
 * A função addEvent (no arquivo functions.js) é necessária para que este script possa ser rodado.
 * @version 1.5
 */

hmenu = function()
{
    var w = "", me = hmenu;
    var suffixID = 'menu_item_';
    
    if (!document.getElementById('menu'))
        return;
    
    var ul = document.getElementById('menu').getElementsByTagName('UL');
    me.isIE6 = window.navigator.appVersion.indexOf('MSIE 6') != -1;
    
    /**
     * Workaround for IE6 bug. IE6 consider any line-break it founds as space, breaking out the page's layout.
     * @param {Object} o HTML Element to start the fix.
     */
    function removeLineBreaks(o)
    {
        if (o.hasChildNodes()) {
            for (var i = 0; i < o.childNodes.length; i++) {
                removeLineBreaks(o.childNodes[i]);
            }
        } else {	
			if (o.nodeValue != null && o.nodeValue.trim() == '') {
				o.nodeValue = '';
			}
        }
    };
    
    me.startTime = function()
    {
        me.endTime();
        hmenu.timeout = setTimeout(hmenu.timedOut, 800);
    }
    
    me.endTime = function()
    {
        if (me.timeout)
            clearTimeout(me.timeout);
    }
    
    hmenu.timedOut = function()
    {
        if (hmenu.isIE6 && document.body.className.indexOf('hideSelects') != -1) {
            document.body.className = '';
        }
        
        hmenu.hideAll();
    }
    
    me.hideAll = function()
    {
        if (hmenu.hideStarted) 
            return false;
        else
            hmenu.hideStarted = true;
            
        for (var i = 0, l = ul.length; i < l; i++) {
            // A verificação a seguir melhora um pouco a velocidade do processamento para o IE 6.
            if (ul[i].style.display != '')
                ul[i].style.display = '';
        }
                
        hmenu.hideStarted = false;
    }
    
    me.showSubMenu = function(ul)
    {
        if (ul && ul.nodeName == 'UL') {
            ul.style.display = 'block';
        }
            
        if (ul && ul.id != 'menu')
            me.showSubMenu(ul.parentNode);
    }
    
    /**
     * mouseover event
     */
    function mouseOver(e)
    {
        var ul = this.childNodes[2] || this;
        me.endTime();
        me.hideAll();
        
        if (me.isIE6 && document.body.className.indexOf('hideSelects') == -1) {
            document.body.className = 'hideSelects';
        }
        hmenu.showSubMenu(ul);
        e.stopPropagation();
    };
    
    /**
     * mouseout event
     */
    function mouseOut(e)
    {
        me.startTime();
        e.stopPropagation();
    };
    
    if (document.getElementById && document.getElementById("menu")) {
        var navRoot = document.getElementById("menu");
    
      removeLineBreaks(navRoot);
        
        var childs = navRoot.getElementsByTagName('*');
        var c = 1;
        for (var i = 0, len = childs.length; i < len; i++) {
            var node = childs[i];
            if (!node.nodeName) continue;
            switch (node.nodeName.toUpperCase()) {
                case 'UL':
                    node.id = suffixID + (c++);    
                break;
                    
                case 'LI':
                    node.id = suffixID + (c++);
                    if (node.parentNode.id != 'menu' && node.getElementsByTagName('UL').length > 0) {
                        node.getElementsByTagName('A')[0].className = 'submenu';
                    }
                    addEvent(node, 'mouseover', mouseOver);
                    addEvent(node, 'mouseout', mouseOut);
                break;
            }
        }
    }
};

addEvent(window, 'load', hmenu);
