function Menu_SetExpandedStyle(item)
{
    if(item!=null)
    {
        var node=item.parentNode.parentNode.parentNode.parentNode.parentNode;;        
        if (!node.id) {            
            node = node.parentNode;
        } 
              
        var isStaticNode=(''+node.onmouseover).indexOf('Menu_HoverStatic')>-1?true:false;        
        var nodeTable = WebForm_GetElementByTagName(node, "table");
        var extendedMenuProperties=Menu_FindExtendedProperties(item);        
        if(extendedMenuProperties) {
            var data = Menu_GetData(item);
            var extendedStyleClass=isStaticNode?extendedMenuProperties.get_StaticExpandedStyleClass():extendedMenuProperties.get_DynamicExpandedStyleClass();
            if(extendedStyleClass && extendedStyleClass != "") { 
                if(!Sys.UI.DomElement.containsCssClass(nodeTable,extendedStyleClass))
                {
                    WebForm_AppendToClassName(nodeTable,extendedStyleClass);
                }
            }                        
        }
    }
    return;
}
function Menu_RemoveExpandedStyle(item)
{    
    if(item!=null)
    {
        var node=item.parentNode.parentNode.parentNode.parentNode.parentNode;;        
        if (!node.id) {            
            node = node.parentNode;
        } 
              
        var isStaticNode=((''+node.onmouseover).indexOf('Menu_HoverStatic')>-1)?true:false;        
        var nodeTable = WebForm_GetElementByTagName(node, "table");
        var extendedMenuProperties=Menu_FindExtendedProperties(item);
        if(extendedMenuProperties) {
            var data = Menu_GetData(item);
            var extendedStyleClass=((isStaticNode) ? extendedMenuProperties.get_StaticExpandedStyleClass() : extendedMenuProperties.get_DynamicExpandedStyleClass());
            if(extendedStyleClass && extendedStyleClass != "") {
                    WebForm_RemoveClassName(nodeTable,extendedStyleClass);
            }
        }
    }
    return;    
}
var _currentExtendedPropety;
function Menu_FindExtendedProperties(item)
{
    var ret;
    var ptrMenu=Menu_FindMenu(item);
    if(_currentExtendedPropety && _currentExtendedPropety.get_element()==ptrMenu)
    {
             ret=_currentExtendedPropety;
    }
    else
    {
        var myBehaviors=Sys.UI.Behavior.getBehaviors(ptrMenu);
        var myBehavior=null;    
            for (var i = 0, l = myBehaviors.length; i < l; i++) {
                if (Object.getTypeName(myBehaviors[i])=='XonAjaxControlExtender.MenuBehavior') {
                    myBehavior = myBehaviors[i];
                    break;
                }
            }
        ret=_currentExtendedPropety=myBehavior;    
    }
    return ret;
}
 
 
 
function Menu_Expand(item, horizontalOffset, verticalOffset, hideScrollers) {
    Menu_ClearInterval();
    var tr = item.parentNode.parentNode.parentNode.parentNode.parentNode;
    var horizontal = true;
    if (!tr.id) {
        horizontal = false;
        tr = tr.parentNode;
    }    

    var child = Menu_FindSubMenu(item);
    if (child) {
        var data = Menu_GetData(item);
        if (!data) {
            return null;
        }
        child.rel = tr.id;
        child.x = horizontalOffset;
        child.y = verticalOffset;
        if (horizontal) child.pos = "bottom";
        Menu_SetExpandedStyle(item);
        PopOut_Show(child.id, hideScrollers, data);
        
    }
    Menu_SetRoot(item);
    if (child) {
        if (!document.body.__oldOnClick && document.body.onclick) {
            document.body.__oldOnClick = document.body.onclick;
        }
        if (__rootMenuItem) {
        document.body.onclick = Menu_HideItems;        
        }
    }
    Menu_ResetSiblings(tr);
    return child;
}

function Menu_HideItems(items) {
    
    if (document.body.__oldOnClick) {
        document.body.onclick = document.body.__oldOnClick;
        document.body.__oldOnClick = null;
    }
    
    Menu_ClearInterval();
    if (!items || ((typeof(items.tagName) == "undefined") && (items instanceof Event))) {
        items = __rootMenuItem;
    }
    var table = items;
    if ((typeof(table) == "undefined") || (table == null) || !table.tagName || (table.tagName.toLowerCase() != "table")) {
        table = WebForm_GetElementByTagName(table, "TABLE");
    }
    
    if ((typeof(table) == "undefined") || (table == null) || !table.tagName || (table.tagName.toLowerCase() != "table")) {
        return;
    }    
    var rows = table.rows ? table.rows : table.firstChild.rows;
    var isVertical = false;
    for (var r = 0; r < rows.length; r++) {
        if (rows[r].id) {
            isVertical = true;
            break;
        }
    }
    
    var i, child, nextLevel;
    if (isVertical) {
        for(i = 0; i < rows.length; i++) {
            if (rows[i].id) {
                child = WebForm_GetElementById(rows[i].id + "Items");
                if (child) {
                    Menu_HideItems(child);
                }
            }
            else if (rows[i].cells[0]) {
                nextLevel = WebForm_GetElementByTagName(rows[i].cells[0], "TABLE");
                if (nextLevel) {
                    Menu_HideItems(nextLevel);
                }
            }
        }
    }
    else {
        for(i = 0; i < rows[0].cells.length; i++) {
            if (rows[0].cells[i].id) {
                child = WebForm_GetElementById(rows[0].cells[i].id + "Items");
                if (child) {
                    Menu_HideItems(child);
                }
            }
            else {
                nextLevel = WebForm_GetElementByTagName(rows[0].cells[i], "TABLE");
                if (nextLevel) {
                    Menu_HideItems(rows[0].cells[i].firstChild);
                }
            }
        }
    }
    
    if (items && items.id) {        
        var parentItem=WebForm_GetElementByTagName(Menu_FindParentItem(items), "A");
        if(items.style.visibility!='hidden')
        {
            Menu_RemoveExpandedStyle(parentItem);
        }                
        PopOut_Hide(items.id);    
    }    
}

function WebForm_RemoveClassName(element, className) {
    var current = element.className;
    if (current) {
        if (current.substring(current.length - className.length - 1, current.length) == ' ' + className) {
            element.className = current.substring(0, current.length - className.length - 1);
            return;
        }
        if (current == className) {
            element.className = "";
            return;
        }
        var index = current.indexOf(' '+className+' ');
        if (index != -1) {
            element.className = current.substring(0, index) + current.substring(index + className.length + 1, current.length);
            return;
        }
        if (current.substring(0, className.length) == className + ' ') {
            element.className = current.substring(className.length + 1, current.length);
        }
    }
}
