﻿var OffsetTypes = (function()
{
   function ctor( value )
   {
        this.value = value;
   }
    
    ctor.Left = new ctor(0);
    ctor.Top = new ctor(1);
    
    /*return the constructor.*/
    return ctor;
})();

var BrowserEdge = ( function()
{
    function ctor( value )
    {
        this.value = value;
    }
    
    ctor.Left = new ctor(0);
    ctor.Bottom = new ctor(1);
    /*return the constructor.*/
    return ctor;    
})();

var CssDropdownMenu = 
{
    _menuHideDelay: 250, //milliseconds delay before menu disappears onmouseout
    _menuParentIsDisabled: true, //disable parent menu item's link?
    _animateMenu: true, 
    _animateMenuTimer: undefined,  
    _dropDownMenu: null, 
    _browserIsIE: document.all, 
    _browserIsFireFox: (document.getElementById && !document.all), 
    _menuBottom:0,
    
    GetOffset:function(offsetIsLeft, offsetSource )
    {
        return (offsetIsLeft)? offsetSource.offsetLeft : offsetSource.offsetTop;
    },
    
    GetPositionOffset:function(offsetTarget, offsetType)
    {
        var offsetIsLeft = ( offsetType == OffsetTypes.Left );
        var totalOffset = this.GetOffset( offsetIsLeft, offsetTarget );
        
        var targetParent = offsetTarget.offsetParent;
        while (targetParent != null)
        {
            totalOffset += this.GetOffset( offsetIsLeft, targetParent );
            targetParent = targetParent.offsetParent;
        }
        return totalOffset;
    },

    AnimateMenu:function()
    {
        if ( this._menuBottom < parseInt(this._dropDownMenu.offsetHeight))
        {
            this._menuBottom += 10 + (this._menuBottom/10);
            this._dropDownMenu.style.clip = "rect(0 auto " + this._menuBottom + "px 0)";
        }
        else return;
        this._animateMenuTimer = setTimeout("CssDropdownMenu.AnimateMenu();", 10);
    },
    
    HandleMenuAnimation:function( targetStyle )
    {
        if (typeof this._animateMenuTimer != "undefined") 
        {
            clearTimeout(this._animateMenuTimer);
        }
        targetStyle.clip = "rect(0 auto 0 0)";
        this._menuBottom = 0;
        this.AnimateMenu();
    },

    ToggleVisiblity:function( targetStyle, e )
    {
        if (this._browserIsIE || this._browserIsFireFox) 
        {
            this._dropDownMenu.style.left = this._dropDownMenu.style.top = "-500px";
        }
        if (e.type == "click" && targetStyle.visibility == hidden || e.type == "mouseover")
        {
            
            if ( this._animateMenu )
            {
                this.HandleMenuAnimation( targetStyle );
            }
            targetStyle.visibility = "visible";
        }
        else if (e.type == "click")
        {
            targetStyle.visibility = "hidden";
        }
    },

    GetBodyElementForIE:function()
    {
        return (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
    },

    GetEdgeOffset:function(obj, browserEdge)
    {
        var edgeOffset = 0;
        var browserIsIE = (this._browserIsIE && !window.opera);
        if (browserEdge == BrowserEdge.Right)
        {
            var windowEdge = ( browserIsIE )
                ? this.GetBodyElementForIE().scrollLeft + this.GetBodyElementForIE().clientWidth - 15 
                : window.pageXOffset+window.innerWidth-15;
                
            this._dropDownMenu.contentmeasure = this._dropDownMenu.offsetWidth;
            
            //move menu to the left?
            if (windowEdge - this._dropDownMenu.x < this._dropDownMenu.contentmeasure)  
                edgeOffset = this._dropDownMenu.contentmeasure - obj.offsetWidth;
        }
        else
        {
            var topedge = ( browserIsIE )? this.GetBodyElementForIE().scrollTop : window.pageYOffset;
            var windowEdge = ( browserIsIE )
                ? this.GetBodyElementForIE().scrollTop + this.GetBodyElementForIE().clientHeight - 15 
                : window.pageYOffset + window.innerHeight - 18;
                
            this._dropDownMenu.contentmeasure = this._dropDownMenu.offsetHeight;
            
            //move up?
            if (windowEdge - this._dropDownMenu.y < this._dropDownMenu.contentmeasure)
            {
                edgeOffset = this._dropDownMenu.contentmeasure + obj.offsetHeight;
                
                //up no good either?
                if ((this._dropDownMenu.y - topedge) < this._dropDownMenu.contentmeasure ) 
                    edgeOffset = this._dropDownMenu.y + obj.offsetHeight - topedge;
            }
        }
        return edgeOffset
    },

    ExpandMenu:function(obj, e, dropMenuID )
    {
        //hide previous menu
        if (this._dropDownMenu != null) this._dropDownMenu.style.visibility="hidden"; //hide menu
        this.ClearHideMenuTimeout();
        if (this._browserIsIE || this._browserIsFireFox)
        {
            obj.onmouseout=function(){CssDropdownMenu.SetHideMenuTimeout();}
            obj.onclick=function(){return !CssDropdownMenu._menuParentIsDisabled;} //disable main menu item link onclick?
            this._dropDownMenu = document.getElementById( dropMenuID );
            this._dropDownMenu.onmouseover=function(){CssDropdownMenu.ClearHideMenuTimeout();}
            this._dropDownMenu.onmouseout=function(e){CssDropdownMenu.HideMenu(e);}
            this._dropDownMenu.onclick=function(){CssDropdownMenu.SetHideMenuTimeout();}
            this.ToggleVisiblity(this._dropDownMenu.style, e);
            this._dropDownMenu.x = this.GetPositionOffset(obj, OffsetTypes.Left );
            this._dropDownMenu.y = this.GetPositionOffset(obj, OffsetTypes.Top );
            this._dropDownMenu.style.left = this._dropDownMenu.x - this.GetEdgeOffset(obj, BrowserEdge.Right ) + "px";
            this._dropDownMenu.style.top = this._dropDownMenu.y - this.GetEdgeOffset(obj, BrowserEdge.Bottom ) + obj.offsetHeight + 1 + "px";
            this.RenderMenuInIFrame();
        }
    },
    
    RenderMenuInIFrame:function()
    { 
        if ( typeof (this.IFrameChildMenu) != "undefined" )
        {
            if (this._dropDownMenu.style.visibility == "visible")
            {
                this.IFrameChildMenu.style.width = this._dropDownMenu.offsetWidth + "px";
                this.IFrameChildMenu.style.height = this._dropDownMenu.offsetHeight + "px";
                this.IFrameChildMenu.style.left = this._dropDownMenu.style.left;
                this.IFrameChildMenu.style.top = this._dropDownMenu.style.top;
            }
            this.IFrameChildMenu.style.display = (this._dropDownMenu.style.visibility == "visible")? "block" : "none";
        }
    },

    HideIFrameMenu:function()
    {
        if (typeof this.IFrameChildMenu != "undefined")
        {
            this.IFrameChildMenu.style.display = 'none';
        }
    },

    CurrentEventTargetContainsRelatedEventTarget:function(currentEventTarget, relatedEventTarget) 
    {
        var result = relatedEventTarget == currentEventTarget;
        
        if ( !result && relatedEventTarget.parentNode )
        {
            result = this.CurrentEventTargetContainsRelatedEventTarget( currentEventTarget, relatedEventTarget.parentNode );
        }
        
        return result;
    },

    HideMenu:function(e)
    {
        if (!this.EventOccuredInDropdown(e))
        {
            this.SetHideMenuTimeout();
        }
    },

    EventOccuredInDropdown:function(e)
    {
        var eventObject = ( window.event )? window.event : e;
        var result = false;
        
        if ( this._browserIsIE )
        {
            result = this._dropDownMenu.contains(eventObject.toElement);
        }
        else if ( this._browserIsFireFox )
        {
            result = this.CurrentEventTargetContainsRelatedEventTarget(eventObject.currentTarget, eventObject.relatedTarget);
        }
        
        return result;
    },

    SetHideMenuTimeout:function()
    {
        this.delayhide = setTimeout("CssDropdownMenu._dropDownMenu.style.visibility='hidden'; CssDropdownMenu.HideIFrameMenu();",this._menuHideDelay);
    },

    ClearHideMenuTimeout:function()
    {
        if (this.delayhide != "undefined")
        {
            clearTimeout(this.delayhide);
        }
    },

    InitDropdownMenu:function()
    {    
        if (window.createPopup && !window.XmlHttpRequest)//if IE5.5 to IE6, create iframe
        { 
            document.write('<IFRAME id="IFrameChildMenu"  src="" style="display: none; left: 0; top: 0; z-index: 90; position: absolute" frameBorder="0" scrolling="no"></IFRAME>');
            this.IFrameChildMenu=document.getElementById("IFrameChildMenu"); //reference iframe object
        }
    }
    
/*
    InitializeDropdownMenu:function()
    {
        for (var ids = 0; ids < arguments.length; ids++)
        {
            var menuitems=document.getElementById(arguments[ids]).getElementsByTagName("a")
            for (var i = 0; i < menuitems.length; i++)
            {
                if (menuitems[i].getAttribute("rel"))
                {
                    var relvalue = menuitems[i].getAttribute("rel");
                    menuitems[i].onmouseover=function(e)
                    {
                        var event = (typeof e != "undefined")? e : window.event;
                        CssDropdownMenu.ExpandMenu(this,event,this.getAttribute("rel"));
                    }
                }
            }
        }
        if (window.createPopup && !window.XmlHttpRequest)//if IE5.5 to IE6, create iframe
        { 
            document.write('<IFRAME id="IFrameMenu"  src="" style="display: none; left: 0; top: 0; z-index: 90; position: absolute frameBorder="0" scrolling="no"></IFRAME>');
            this.IFrameChildMenu=document.getElementById("IFrameMenu"); //reference iframe object
        }
    }
    */
}
CssDropdownMenu.InitDropdownMenu();

function AddCssDropdownMenuListener(menuLinkId, childMenuId)
{
    var menuLink = document.getElementById( menuLinkId );
    menuLink.onmouseover = function(e)
    {
        var firedEvent = (typeof( e ) != "undefined")? e : window.event;
        CssDropdownMenu.ExpandMenu( this, firedEvent, childMenuId );
    }
}
