﻿function buildTopMenuNavigation()
{
    var _root = document.getElementById("navigation_control");
    if (_root != undefined)
    {
        var ul = _root.getElementsByTagName("DIV")[0].getElementsByTagName("UL")[0];
        buildListItems(ul);
    }

    function buildListItems(holder)
    {
        for (var i in holder.childNodes)
        {
            if (holder.childNodes[i].nodeName == "LI")
            {
                var li = holder.childNodes[i];
                li.span = getElementsByClass("sub", li)[0];
                li.keepopen = false;
               
                function showSubMenu()
                {
                    var targ;

                    if (this.nodeName == "LI")
                    {
                        targ = getElementsByClass("sub", this)[0];
                    }
                    else
                    {
                        if (this.parentNode != undefined)
                        {
                            this.parentNode.keepopen = true;
                            targ = this.parentNode.span;
                        }
                    }
                    
                    if(targ != undefined && countHTMLnodes(targ.childNodes) > 0) targ.style.display = "block";
                }

                function hideSubMenu(caller)
                {
                    var targ = getElementsByClass("sub", caller)[0];
                    if (targ != undefined && caller.keepopen == false) targ.style.display = "none";
                }

                li.startDelay = function()
                {
                    var caller = this;
                    hideSubMenu(caller)
                }

                li.onmouseover = showSubMenu;
                li.onmouseout = li.startDelay;

                if (li.childNodes[0] ) // the text element
                {
                    li.childNodes[0].onmouseover = showSubMenu;
                    li.childNodes[0].onmouseout = function()
                    {
                        if (this.parentNode != undefined)
                        {
                            this.parentNode.keepopen = false;
                            showSubMenu();
                        }
                    }
                }
                if (li.span != undefined) // submenu
                {
                    li.span.onmouseover = showSubMenu;
                    li.span.onmouseout = function()
                    {
                        if (this.parentNode != undefined)
                        {
                            this.parentNode.keepopen = false;
                        }
                    }
                }
            }
        }
    }

    function countHTMLnodes(children)
    {
        var count = 0;

        for (var i = 0; i < children.length; i++)
        {
            var type = children[i].nodeType;
            
            if(type != 3 && type != 8) count++
        }
        
        return count;
    }

    function getElementsByClass(searchClass, node, tag)
    {
        var classElements = new Array();
        if (node == null)
            node = document;
        if (tag == null)
            tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++)
        {
            if (pattern.test(els[i].className))
            {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
    
    function oAlert(item)
    {
        var msg = "";
        for (var i in item)
        {
            msg += i + ": " + item[i] + "\n";
        }

        alert(msg);
    }
}

