// IE localName property does not work, so extract from node name.
function getLocalName( element )
{
    var ua = YAHOO.env.ua;

    if ( ua.ie )
    {
        var fullName = element.nodeName;
        var nodeNameVal = fullName.substring( ( fullName.indexOf(":") + 1 ) , fullName.length );
        return nodeNameVal;
    }

    return element.localName;
}

YAHOO.util.Event.onContentReady("consumeMenu", function () {

    var recursiveParse = function( element , level , index ) {
            var retObj = element.cloneNode( false );
            var nodeType = getLocalName( element );

            if( 3 == level && ( "UL" == nodeType || "ul" == nodeType ) ) return;

            if( ( "UL" == nodeType || "ul" == nodeType ) )
            {
                var addDiv0 = document.createElement( "div" );
                addDiv0.className = "yuimenu";

                var addDiv1 = document.createElement( "div" );
                addDiv1.className = "bd";

                var ul0 = document.createElement( "ul" );

                if( 0 == level )
                {
                    addDiv0.id = "mainMenuMasterDiv";
                    addDiv0.className = "yuimenubar yuimenubarnav";
                    ul0.className = "first-of-type";
                }

                addDiv1.appendChild( ul0 );
                addDiv0.appendChild( addDiv1 );
                retObj = ul0;
            }
            else if( ( "LI" == nodeType || "li" == nodeType ) )
            {
                if( 0 == level )
                {
                    retObj.className = "yuimenubaritem";
                }
                else
                {
                    retObj.className = "yuimenuitem";
                }

                if( 0 == index )
                {
                    retObj.className += " first-of-type";
                }

                level ++;
            }
            else if( ( "A" == nodeType || "a" == nodeType ) )
            {
                if( 0 == level )
                {
                    retObj.className = "yuimenubaritemlabel";
                }
                else
                {
                    retObj.className = "yuimenuitemlabel";
                }
            }

            if( element.hasChildNodes() && 3 >= level )
            {
                for( var i = 0 ; i < element.childNodes.length ; i ++ )
                {
                    var newChild = recursiveParse( element.childNodes[ i ] , level , i );
                    if( undefined !== newChild )
                    {
                        retObj.appendChild( newChild );
                    }
                }
            }

            if( ( "UL" == nodeType || "ul" == nodeType ) )
            {
                retObj = addDiv0;
            }

            return retObj;
        }

    // Parse output from TweakCMS
    var menuSource = document.getElementById( "consumeMenu" );
    menuSource.parentNode.appendChild( recursiveParse( menuSource , 0 , 0 ) );
    menuSource.parentNode.removeChild( menuSource );

    // Now for YUI
    var ua = YAHOO.env.ua, oAnim;  // Animation instance

    function onSubmenuBeforeShow(p_sType, p_sArgs) {
        var oBody, oElement, oShadow, oUL;
        if (this.parent) {
            oElement = this.element;
            oShadow = oElement.lastChild;
            oShadow.style.height = "0px";

            if (oAnim && oAnim.isAnimated()) {
                oAnim.stop();
                oAnim = null;
            }

            oBody = this.body;

            if (this.parent && !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
                if (ua.gecko) {
                    oBody.style.width = oBody.clientWidth + "px";
                }

                if (ua.ie == 7) {
                    oElement.style.width = oElement.clientWidth + "px";
                }
            }

            oBody.style.overflow = "hidden";
            oUL = oBody.getElementsByTagName("ul")[0];
            oUL.style.marginTop = ("-" + oUL.offsetHeight + "px");
        }
    }

    function onTween(p_sType, p_aArgs, p_oShadow) {
        if (this.cfg.getProperty("iframe")) {
            this.syncIframe();
        }

        if (p_oShadow) {
            p_oShadow.style.height = this.element.offsetHeight + "px";
        }
    }

    function onAnimationComplete(p_sType, p_aArgs, p_oShadow) {
        var oBody = this.body, oUL = oBody.getElementsByTagName("ul")[0];

        if (p_oShadow) {
            p_oShadow.style.height = this.element.offsetHeight + "px";
        }

        oUL.style.marginTop = "";
        oBody.style.overflow = "";

        if (this.parent && !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
            if (ua.gecko) {
                oBody.style.width = "";
            }

            if (ua.ie == 7) {
                this.element.style.width = "";
            }
        }
    }

    function onSubmenuShow(p_sType, p_sArgs) {
        var oElement, oShadow, oUL;

        if (this.parent) {
            oElement = this.element;
            oShadow = oElement.lastChild;
            oUL = this.body.getElementsByTagName("ul")[0];

            oAnim = new YAHOO.util.Anim(oUL, { marginTop: { to: 0 } }, .5, YAHOO.util.Easing.easeOut);

            oAnim.onStart.subscribe(function () {
                oShadow.style.height = "100%";
            });

            oAnim.animate();

            if (YAHOO.env.ua.ie) {
                oShadow.style.height = oElement.offsetHeight + "px";
                oAnim.onTween.subscribe(onTween, oShadow, this);
            }

            oAnim.onComplete.subscribe(onAnimationComplete, oShadow, this);
        }
    }

    var oMenuBar = new YAHOO.widget.MenuBar("mainMenuMasterDiv", {
                                                autosubmenudisplay: true,
                                                hidedelay: 750,
                                                lazyload: true
                                                });

    oMenuBar.subscribe("beforeShow", onSubmenuBeforeShow);
    oMenuBar.subscribe("show", onSubmenuShow);
    oMenuBar.render();
});
