{
	//----------------------------------
	// action popup menu
    var m_aDynamicMenus;
    if (top.m_aDynamicMenus != null && top.m_aDynamicMenus != undefined)
    {
        // global top.m_aDynamicMenus already exists
    }
    else
    {
        top.m_aDynamicMenus = new Array();
    }

	function dynamicMenu()
	{
		// initialize

		// method headers
		this.addEntry = addEntry;
		this.addSecuredEntry = addSecuredEntry;
		this.addDivider = addDivider;

		this.showByEvent = showByEvent;
		this.showByButton = showByButton;
		this.showByObject = showByObject;
		
		this.parentMenuIndex = null;
		
		this.menuIndex = top.m_aDynamicMenus.length;
		top.m_aDynamicMenus.push(this);
		this.aMenuItems = new Array();

		// method implmentations
		function addEntry(Name, JavascriptAction, icon, hasSubmenu, closeOnClick)
		{
		    if (JavascriptAction == null)
		        JavascriptAction = '';
		    if (icon == '')
		        icon = null;
		    
		    var menuItem = new dynamicMenuItem();
		    menuItem.text = Name;
		    menuItem.action = JavascriptAction.replace(/"/g, "'");
		    menuItem.icon = icon;
		    menuItem.hasSubmenu = hasSubmenu;
		    if (hasSubmenu)
		    {
		        menuItem.submenu = new dynamicMenu();
		        menuItem.submenu.parentMenuIndex = this.menuIndex;
		    }
		    
		    menuItem.closeOnClick = (closeOnClick == null || closeOnClick);
		    
		    if (this.aMenuItems == null)
		        this.aMenuItems = new Array();

		    this.aMenuItems.push(menuItem);

		    return menuItem; //menuItemArray[menuItemArray.length-1];
		}
		
		function addSecuredEntry(ClassID, ActionNumber, Name, JavascriptAction, icon, hasSubmenu, closeOnClick)
		{
			if (isActionAllowed(ClassID, ActionNumber))
				return this.addEntry(Name, JavascriptAction, icon, hasSubmenu, closeOnClick);
		}
		
		function addDivider()
		{
			this.addEntry('<hr/>');
		}
		
		function showByButton(positionX, positionY)
		{
		    var btnObj;
		    	    
		    if (event != null)
		        btnObj = event.srcElement;
		    if (btnObj == null && top.event != null) 
		        btnObj = top.event.srcElement;
		    
		    while (btnObj != null && btnObj.getAttribute('isButton') != '1')
		        btnObj = btnObj.parentNode;
		    
		    if (btnObj == null)
		        return;
		        
		    
		    this.showByObject(btnObj, positionX, positionY, 'DynamicToolbarMenu');		    
		    btnObj.suspendUnhover = true;
		    
		    //btnObj.onmouseout += ';alert(' + this.menuIndex + ')';

		    m_uiGenerator.attachEvent(btnObj, 'onmouseout', 'closeMenuFromTrigger()');
		}
		
		function showByEvent(showEvent, cssClass)
		{
		    if (showEvent == null)
		        showEvent = event;
		        
		    this.showByObject(showEvent.srcElement, 5, 3, cssClass);

		    m_uiGenerator.attachEvent(showEvent.srcElement, 'onmouseout', 'closeMenuFromTrigger()');

		}

        function showByObject(obj, positionX, positionY, cssClass)
        {
            //positionX: pixel offset from left edge of obj OR 'right' to align to right edge
            //positionY: pixel offset from top edge of obj OR 'bottom' to align to bottom edge
            if (obj == null)
                return;
                
            var doc = obj.document;
            if (doc == null)
                doc = document;

            if (doc.getElementById('divDynamicMenu' + this.menuIndex) != null)
		        return;
		        
		    if (cssClass == null)
		        cssClass = 'DynamicMenu';
		    
		    var textString = "";
		    if (this.aMenuItems.length > 0) 
		    {
		        //assemble the html for the menu
			    textString = '<table width="100%" cellpadding="0" cellspacing="0" id="tblDynamicMenu' + this.menuIndex + '">'
    			
			    var hasIcons = false;
			    for (var i = 0; i < this.aMenuItems.length; i++)
			        if (this.aMenuItems[i].icon != null)
			        {
			            hasIcons = true;
			            break;
			        }
    			
			    for (var i = 0; i < this.aMenuItems.length; i++) {
				    var jsHolder;
				    var jsHolderQuote;
				    var clickJS = '';
				    var mouseoutJS = 'menuItemUnHover(' + this.menuIndex + ',' + i + ',\'' + cssClass + '\');';
			        var mouseoverJS = 'menuItemHover(' + this.menuIndex + ',' + i + ',\'' + cssClass + '\');';
    				
				    if (this.aMenuItems[i] != null)
				    {
    					if (this.aMenuItems[i].hasSubmenu)
    					{
				            mouseoverJS += 'top.m_aDynamicMenus[' + this.aMenuItems[i].submenu.menuIndex + '].showByEvent(null, \'' + cssClass + '\');';
				        }
				        else
				        {
				            if (this.aMenuItems[i].action == null || this.aMenuItems[i].action != '')
				            {
                                if (this.aMenuItems[i].closeOnClick)
                                    clickJS = 'closeMenu(' + this.menuIndex + ',' + i + ');';
                                clickJS += this.aMenuItems[i].action + ';';
                            }
                            else
                                clickJS = '';
                        }
    					
    					if (this.aMenuItems[i].text.toLowerCase().slice(0, Math.min(3, this.aMenuItems[i].text.length)) == '<hr')    					
						    textString += '<tr><td style="height:1px; background=#666;" colspan="3"></td></tr>';
					    else
					    {
						    textString += '<tr onmouseover="' + mouseoverJS + '" onmouseout="' + mouseoutJS + '" onclick="' + clickJS + '">';
						    if (hasIcons)
						    {
						        var icon = this.aMenuItems[i].icon;
						        if (icon == null)
		                            icon = 'skins/common/images/blank.gif';
		                        else if (cssClass == 'DynamicToolbarMenu')
		                            icon = 'skins/common/images/icons/gif_buttons_solid/' + icon; 
		                        else
		                            icon = 'skins/common/images/icons/gif/' + icon; 
						        textString += '<td style="padding:0px 4px;width:16px;"><img height="16" width="16" src="' + icon + '"></td>'
						    }
						    textString += '<td nowrap style="padding:4px;">' + this.aMenuItems[i].text + '</td>';
						    textString += '<td width="1" style="text-align:right;">';
						    if (this.aMenuItems[i].hasSubmenu)
						        textString += '<img src="skins/common/images/menu_arrow.gif">&nbsp;';
						    textString += '&nbsp;';
						    textString += '</td>';
						    textString += '</tr>';
					    }
				    }
			    }
			    textString = textString + "</table>"

			    var menuDiv = doc.createElement('div');
			    menuDiv.id = 'divDynamicMenu' + this.menuIndex;
			    menuDiv.className = cssClass;
			    
			    menuDiv.menuIndex = this.menuIndex;
			    menuDiv.parentMenuIndex = this.parentMenuIndex;

			    menuDiv.innerHTML = textString;
			    // only IE6 and below
			    if (!window.XMLHttpRequest)
			    {  // fix for bleeding dropdowns over dhtml code        
			        var html = "<iframe style=\"position: absolute; display: block; " + "z-index: -1; width: 100%; height: 100%; top: 0; left: 0;" +
                    "filter: mask(); background-color: #ffffff; \"></iframe>";
			        if (menuDiv)
			            menuDiv.innerHTML += html;
			        // forced refresh of a div
			        var olddisplay = menuDiv.style.display;
			        menuDiv.style.display = 'none';
			        menuDiv.style.display = olddisplay;
			    }

			    m_uiGenerator.attachEvent(menuDiv, 'onmouseout', 'closeMenu()');

			    doc.body.appendChild(menuDiv);
    			
			    //position the menu
			    
			    var pos = m_uiUtil.getAbsolutePosition(obj, false);
			    var blockerX = 1;
			    			    
			    if (cssClass == 'DynamicToolbarMenu')
			    {
			        var windowRight = top.document.body.scrollLeft + top.document.body.offsetWidth - 10;
			        if (pos.x + menuDiv.offsetWidth > windowRight)
			        {
			            var newX = windowRight - menuDiv.offsetWidth - 20;
			            blockerX = pos.x - newX + 1;
			            pos.x = newX;
			        }
			    }
			    else
			    {
			        var windowBottom = top.document.body.scrollTop + top.document.body.offsetHeight - 90;
			        if (pos.y + menuDiv.offsetHeight > windowBottom)
			        {
			            if (menuDiv.offsetHeight < (windowBottom - 40))
			            {
			                pos.y = pos.y - menuDiv.offsetHeight + 10;
			                if (this.parentMenuIndex != null)
			                    pos.y += 12;
			            }
			            else
			            {
			                menuDiv.style.height = windowBottom - 40;
			                menuDiv.style.overflowY = 'scroll';
			            }
			        }
			    }
    			
			    if (this.parentMenuIndex != null)
			    {
			        menuDiv.style.top = pos.y - 1;
			        
			        var parentMenu = doc.getElementById('divDynamicMenu' + this.parentMenuIndex);
			        pos = m_uiUtil.getAbsolutePosition(parentMenu, false);
			        menuDiv.style.left = pos.x + parentMenu.offsetWidth - 5;			        
			    }
			    else
			    {
			        if (positionX == null)
			            positionX = 0;
			        if (positionY == null)
			            positionY = 0;
			            
			        if (positionY == 'bottom')
			            menuDiv.style.top = pos.y + obj.offsetHeight;
			        else
			            menuDiv.style.top = pos.y + positionY;
			            
			        if (positionX == 'right')
			            menuDiv.style.left = pos.x + obj.offsetWidth;
			        else
			            menuDiv.style.left = pos.x + positionX;
    			}
    			
    			
    			//position the partial top-border blocker for menu lists
    			if (cssClass == 'DynamicToolbarMenu')
    			{
    			    var divBlocker = doc.createElement('div');
    			    divBlocker.style.position = 'absolute';
    			    divBlocker.style.height = '2px';
    			    divBlocker.style.backgroundColor = '#aad4f9';
    			    divBlocker.style.width = obj.offsetWidth - 2;
    			    divBlocker.style.top = '-1px';
    			    divBlocker.style.left = blockerX;
    			    divBlocker.style.innerHTML = '<img src="skins/common/images/blank.gif" height="1" width="1">';
   			        divBlocker.style.overflowY = 'hidden';
    			
    			    menuDiv.appendChild(divBlocker);
    			    
    			    //obj.style.borderColor = '#aaa';
    			}
    			
    			//debugger;
    			
    			obj.triggeredMenuIndex = this.menuIndex;
    			this.triggerID = obj.uniqueID;
		    }
        }
	}

	
	function getMenuObjFromChildObj(obj)
	{
	    while (obj != null && obj.menuIndex == null)
	        obj = obj.parentNode;
	    
	    return obj;
	}

	function closeMenu()
	{		    
	    var e = event;
	    if (e == null)
	        e = top.event;
	        
	    var oDest = getMenuObjFromChildObj(e.toElement);
	    var oSrc = getMenuObjFromChildObj(e.srcElement);

	    if (oSrc == null) 
	        return;  //not called from a menu. weird case.
	    
	    if (oSrc == oDest)
	        return; //moving within menu. do nothing.
        
	    if (oDest != null) //if moving to another menu
	    {
	        //don't close if moving to child menu
	        if (oSrc.parentMenuIndex != oDest.menuIndex)	        
	            return;

	        //oSrc.removeNode(true);
	        document.body.removeChild(oSrc);
	    }
        else
        {
            oDest = getTriggerObjFromChildObj(e.toElement);
            if (oDest == null || oDest.triggeredMenuIndex != oSrc.menuIndex)
            {
                closeAllMenus();
            }
	    }
	}
	
	
	function getTriggerObjFromChildObj(obj)
	{
	    while (obj != null && obj.triggeredMenuIndex == null)
	    {
	        obj = obj.parentNode;
	    }
	    return obj;	    
	}

	function closeMenuFromTrigger()
	{
	    var e = event;
	    if (e == null)
	        e = top.event;

	    var oDest = getTriggerObjFromChildObj(e.toElement);
	    var oSrc = getTriggerObjFromChildObj(e.srcElement);


        if (oSrc == null)  //shouldnt happen
            return;
            
        
        if (oDest != null)  
            if (oSrc.triggeredMenuIndex == oDest.triggeredMenuIndex)
                return;
        
        oDest = getMenuObjFromChildObj(e.toElement);

        if (oDest == null)
            closeAllMenus();

        
	}
	
	
	function closeAllMenus()
	{
	    //close all menus
	    
        for (var i = top.m_aDynamicMenus.length-1; i >= 0; i--)
        {
            try
                {
                if (top.m_aDynamicMenus[i] != null)
                {
                    if (top.m_aDynamicMenus[i].triggerID != null)
                    {
                        var t = document.getElementById(top.m_aDynamicMenus[i].triggerID);
                        if (t == null)
                            t = top.document.getElementById(top.m_aDynamicMenus[i].triggerID);
                        if (t != null)
                            t.triggeredMenuIndex = null;
                            if (t.className == 'ToolbarButton')
                            {
                                t.style.backgroundColor = 'transparent';
                                t.style.backgroundPosition = '0px 29px'; 
                                t.style.borderColor = '';
                                t.suspendUnhover = null;
                            }
                    }
                    
                    var m = document.getElementById('divDynamicMenu' + i);
                    if (m == null)
                        m = top.document.getElementById('divDynamicMenu' + i);
                    if (m != null)
                    //m.removeNode(true);
                        document.body.removeChild(m);
                                                
                }
            }
            catch (e)
            {  }
        }

   
        top.m_aDynamicMenus = new Array(); //(top.m_aDynamicMenus.length);
	}
	
	
	function menuItemHover(menuIndex, menuItemIndex, cssClass)
	{
	    var tr = m_uiUtil.getParentNodeByTagName(event.srcElement,'tr')
	    tr.oldBG = tr.style.backgroundColor;
	    
	    if (cssClass == 'CrumbMenu')
	        tr.style.backgroundColor = '#555';
	    else
	        tr.style.backgroundColor = '#fff';
	}
	
	function menuItemUnHover(menuIndex, menuItemIndex, cssClass)
	{
	    var tr = m_uiUtil.getParentNodeByTagName(event.srcElement,'tr');
	    
	    tr.style.backgroundColor = tr.oldBG;
	    
	    if (top.m_aDynamicMenus[menuIndex] != null 
	        && top.m_aDynamicMenus[menuIndex].aMenuItems != null 
	        && top.m_aDynamicMenus[menuIndex].aMenuItems[menuItemIndex] != null
	        && top.m_aDynamicMenus[menuIndex].aMenuItems[menuItemIndex].hasSubmenu)
	    {
	        var oMenu = document.getElementById('divDynamicMenu' + menuIndex);
	        
	        var oDest = getMenuObjFromChildObj(event.toElement);	        
	        if (oDest != oMenu)
	            return;
	            
	        if (m_uiUtil.getParentNodeByTagName(event.toElement, 'tr') == m_uiUtil.getParentNodeByTagName(event.srcElement, 'tr'))
	            return;
    	        	    
	        var oSubmenu = document.getElementById('divDynamicMenu' + top.m_aDynamicMenus[menuIndex].aMenuItems[menuItemIndex].submenu.menuIndex);
	        if (oSubmenu != null)
	        //oSubmenu.removeNode(true);
	        document.body.removeChild(oSubmenu);
	    }
	    
	}
	
	
	
	function dynamicMenuItem()
	{
	    this.text = '';
	    this.action = null;
	    this.icon = '';
	    this.hasSubmenu = false;
	    this.submenu = null;
	    this.closeOnClick = null;
	}
}


