/* Menu
----------------------------------------------- */

Menu = Class.create();
Menu.prototype = {

    items: [],

    initialize: function(element)
    {
        this.element = $(element);
        if (this.element)
        {
            $A(this.element.getElementsByTagName("li")).each(function(item)
            {
                this.Add(new MenuItem(item, this));
            }.bind(this));
        }
    },

    Add: function(item)
    {
        this.items.push(item);
    }
}

/* Menu Item
----------------------------------------------- */

MenuItem = Class.create();
MenuItem.prototype = {

    activeClassName: "active",

    initialize: function(element, menu)
    {
        this.element = $(element);
        this.menu = menu;

        if (this.element.getElementsByTagName("ul").length > 0)
        {
            this.submenu = this.element.getElementsByTagName("ul")[0];

            Event.observe(this.element, "mouseover", this.onMouseOver.bindAsEventListener(this));
            Event.observe(this.element, "mouseout", this.onMouseOut.bindAsEventListener(this));
        }
    },

    onMouseOver: function(e)
    {
        this.Open();
    },

    onMouseOut: function(e)
    {
        this.Close();
    },

    Close: function()
    {
        Element.removeClassName(this.element, this.activeClassName);
    },

    Open: function()
    {
        Element.addClassName(this.element, this.activeClassName);

        if (!this.iefix && (navigator.appVersion.indexOf('MSIE') > 0) && (navigator.userAgent.indexOf('Opera') < 0))
        {
            this.iefix = document.createElement("iframe");
            this.iefix.src = "javascript:false;";
            this.iefix.setAttribute("scrolling", "no");
            this.iefix.setAttribute("frameborder", "0");
            this.iefix.style.width = this.submenu.offsetWidth + "px";
            this.iefix.style.height = this.submenu.offsetHeight + "px";

            this.submenu.appendChild(this.iefix);
        }
    }
}

/* Only show submenu tree for active node */
function SubMenu()
{
    var submenu = $("submenu");
    if (submenu)
    {
        var rootlist = submenu.getElementsByTagName("ul")[0];
        var currentitem = $("submenuCurrentItem");
        if (currentitem)
        {
            var parent = currentitem;
            while (parent != rootlist)
            {
                Element.addClassName(parent, "active");
                for (var i = 0; i < parent.childNodes.length; i++)
                {
                    var node = parent.childNodes[i];
                    if (node.nodeName.toLowerCase() == "ul" || node.nodeName.toLowerCase() == "a")
                    {
                        Element.addClassName(node, "active");
                    }
                }
                parent = parent.parentNode;
            }
        }
    }
}

/* Make all content columns a consistent height */
function ColumnHeight()
{
    var maxheight = 400;
    var columns = $("content", "dnn_leftpane", "dnn_contentpane", "dnn_rightpane");
    if (content)
    {
        columns.each(function(column)
        {
            if (column)
            {
                if (column.offsetHeight > maxheight)
                {
                    maxheight = column.offsetHeight;
                }
            }
        });

        columns.each(function(column)
        {
            if (column)
            {
                column.style.height = maxheight + "px";
                
                if (column.offsetHeight > maxheight)
                {
                    column.style.height = (maxheight - (column.offsetHeight - maxheight)) + "px";
                }
            }
        });
    }
}

/* Right column to float nicely with content */
function RightPane()
{
    if ($("dnn_rightpane"))
    {
        Element.addClassName($("dnn_contentpane"), "threecol");
    }
}

Event.observe(window, "load", function()
{
    SubMenu();
    RightPane();
    ColumnHeight();
    new Menu("nav");
});