
ASPxClientPopupMenu = _aspxCreateClass(ASPxClientMenuBase, {
	constructor: function(name){
		this.constructor.prototype.constructor.call(this, name);
        
        this.popupAction = "RightMouseClick";
        this.popupElementID = "";
        this.popupElement = null;
        this.popupHorizontalOffset = 0;
        this.popupVerticalOffset = 0;
        this.popupHorizontalAlign = __aspxNotSetAlignIndicator;
        this.popupVerticalAlign = __aspxNotSetAlignIndicator;
        this.left = 0;
        this.top = 0;
    },    
    Initialize: function(){
        this.GetPopupElement();
        ASPxClientMenuBase.prototype.Initialize.call(this);
    },
    
    GetPopupElement: function(){
        if(!_aspxIsExistsElement(this.popupElement)){
            this.popupElement = _aspxFindPopupElementById(this.popupElementID);
            if(_aspxIsExistsElement(this.popupElement)){
                this.popupElement.DXMenu = this;
                if(this.popupAction == "LeftMouseClick")
                    _aspxAttachEventToElement(this.popupElement, "mouseup", aspxMEvent);
                else if(this.popupAction == "RightMouseClick")
                    _aspxAttachEventToElement(this.popupElement, "contextmenu", aspxMEvent);
                else if(this.popupAction == "MouseOver"){
                    _aspxAttachEventToElement(this.popupElement, "mouseover", aspxMEvent);
                    _aspxAttachEventToElement(this.popupElement, "mouseout", aspxMEvent);
                }
                
                if (this.popupAction == "LeftMouseClick"){
                    _aspxAttachEventToElement(this.popupElement, "mousedown", aspxMEvent);
                    this.popupElement.isPopuped = false;
                }
            }
        }
        return this.popupElement;
    },
    IsMenuVisible: function(){
        var element = this.GetMainElement();
        return (element != null) ? _aspxGetElementDisplay(element) : false;
    },
    IsParentElementPositionStatic: function(indexPath){
        return false;
    },
    GetClientSubMenuPos: function(element, indexPath, pos, isXPos){
        if (indexPath == "") {
            var popupPosition = null;
            if (isXPos){
                popupPosition = _aspxGetPopupAbsoluteX(this.GetMenuMainCell(this.GetMainElement()),
                    this.GetPopupElement(), this.popupHorizontalAlign, this.popupHorizontalOffset, pos, this.left);
            }
            else{
                popupPosition = _aspxGetPopupAbsoluteY(this.GetMenuMainCell(this.GetMainElement()), 
                    this.GetPopupElement(), this.popupVerticalAlign, this.popupVerticalOffset, pos, this.top);
            }
            popupPosition.position -= _aspxGetPositionElementOffset(element, isXPos);
            return popupPosition;
        }
        return ASPxClientMenuBase.prototype.GetClientSubMenuPos.call(this, element, indexPath, pos, isXPos);
    },
    GetMenuElement: function(indexPath){
        if (indexPath == "")
            return this.mainElement;
        return ASPxClientMenuBase.prototype.GetMenuElement.call(this, indexPath);
    },

    OnItemOverTimer: function(indexPath){
        if(indexPath == ""){
            aspxGetMenuCollection().DoHidePopupMenus(null, -1, this.name, false, "");
            aspxGetMenuCollection().DoShowAtCurrentPos(this.name, indexPath);
        }
        else
            ASPxClientMenuBase.prototype.OnItemOverTimer.call(this, indexPath);
    },
    DoShow: function(x, y){
        var element = this.GetMainElement();
        if(element != null && !_aspxGetElementDisplay(element)){
            aspxGetMenuCollection().DoHidePopupMenus(null, -1, this.name, false, "");

            this.DoShowPopupMenu(element, x, y, "");
        }
    },
    DoShowByAPI: function(x, y){
        var enableAnimationBackup = this.enableAnimation;
        this.enableAnimation = false;
        this.DoShow(x, y);
        this.enableAnimation = enableAnimationBackup;
    },
    DoHidePopupMenu: function(evt, element){
        if ((__aspxNS || __aspxSafari) && _aspxIsExists(evt) && this.GetPopupElement() != null 
            && _aspxIsExists(this.GetPopupElement().isPopuped)){
            if (_aspxFindParentPopupElementByEvent(evt, aspxTestPopupMenuElement) == this.GetPopupElement()) {
                this.GetPopupElement().isPopuped = true;
            }
        }
        ASPxClientMenuBase.prototype.DoHidePopupMenu.call(this, evt, element);
    },
    DoShowPopupMenuBorderCorrector: function(element, x, y, indexPath, toTheLeft, toTheTop){
        if (indexPath != "")
            ASPxClientMenuBase.prototype.DoShowPopupMenuBorderCorrector.call(this, element, x, y, indexPath, toTheLeft, toTheTop);
    },
    ShowInternal: function(evt){
        var x = _aspxGetEventX(evt);
        var y = _aspxGetEventY(evt);
        if (evt.type == "mouseover")
            aspxGetMenuCollection().SetAppearTimer(this.name, "", this.appearAfter);
        else
            this.DoShow(x, y);
    },
    GetAnimationHorizontalDirection: function(indexPath, popupPosition){
        if (this.GetMenuLevel(indexPath) == 0)
            return _aspxGetAnimationHorizontalDirection(popupPosition, this.popupHorizontalAlign, this.popupVerticalAlign);
        else 
            return popupPosition.isInverted ? 1 : -1;
    },
    GetAnimationVerticalDirection: function(indexPath, popupPosition){
        if (this.GetMenuLevel(indexPath) == 0)
            return _aspxGetAnimationVerticalDirection(popupPosition, this.popupHorizontalAlign, this.popupVerticalAlign);
        else 
            return 0;
    },
    // API
    ReplacePopupElementID: function(newPopupElementID){
        var popupElementIDBackup = this.popupElementID;
        this.popupElementID = newPopupElementID;
        this.popupElement = null;
        this.GetPopupElement();
        return popupElementIDBackup;
    },
    Hide: function(){
        var element = this.GetMainElement();
        if(element != null) this.DoHidePopupMenu(null, element);
    },
    Show: function(){
        this.DoShowByAPI(__aspxInvalidPosition, __aspxInvalidPosition);
    },
    ShowAtElement: function(htmlElement){
        var popupElementBackup = this.popupElement;
        this.popupElement = htmlElement;
        this.DoShowByAPI(__aspxInvalidPosition, __aspxInvalidPosition);
        this.popupElement = popupElementBackup;
    },
    ShowAtElementByID: function(id){
        var htmlElement = document.getElementById(id);
        this.ShowAtElement(htmlElement);
    },
    ShowAtPos: function(x, y){
        var popupElementIDBackup = this.ReplacePopupElementID("");
        this.DoShowByAPI(x, y);
        this.ReplacePopupElementID(popupElementIDBackup);
    },
    
    GetVisible: function(){
        return this.IsMenuVisible();
    },
    SetVisible: function(visible){
        if(visible && !this.IsMenuVisible())
            this.Show();
        else if(!visible && this.IsMenuVisible())
            this.Hide();
    }
});

function aspxMEvent(evt){
    var element = _aspxFindParentPopupElementByEvent(evt, aspxTestPopupMenuElement);
	if(element != null) {
        if (evt.type == "mouseout")
            return aspxPMOnMouseOut(evt, element);
        else if (evt.type == "mouseover")
            return aspxPMOnMouseOver(evt, element);
        else if (evt.type == "mousedown")
            return aspxPMOnMouseDown(evt, element);
        else
            return aspxPMOnMouseUp(evt, element);
    }
}
function aspxPMOnMouseDown(evt, element){
    if (!__aspxNS && !__aspxSafari && _aspxIsExists(element.isPopuped))
        element.isPopuped = element.DXMenu.IsMenuVisible();
}
function aspxPMOnMouseUp(evt, element){
    if (element.DXMenu.popupAction=="RightMouseClick" || _aspxGetIsLeftButtonPressed(evt)) {
        if (element.DXMenu.popupAction=="RightMouseClick")
            _aspxPreventContextMenu(evt);
        if (element.isPopuped != true)
            element.DXMenu.ShowInternal(evt);
        else if ((__aspxNS || __aspxSafari) && _aspxIsExists(element.isPopuped))
            element.isPopuped = false;
        return _aspxCancelBubble(evt);
    }
}
function aspxPMOnMouseOver(evt, element){
    if(!element.DXMenu.IsMenuVisible())
        element.DXMenu.ShowInternal(evt);
}
function aspxPMOnMouseOut(evt, element){
    if(!element.DXMenu.IsMenuVisible())
        aspxGetMenuCollection().ClearAppearTimer();
}

function aspxTestPopupMenuElement(element){
    return _aspxIsExists(element.DXMenu);
}
_aspxAttachEventToDocument("mousemove", aspxPopupControlDocumentMouseMove);
function aspxPopupControlDocumentMouseMove(evt){
    if (aspxGetMenuCollection().IsAppearTimerActive()){
        var element = _aspxFindParentPopupElementByEvent(evt, aspxTestPopupMenuElement);
	    if(element != null && !element.DXMenu.IsMenuVisible())
	        aspxGetMenuCollection().SaveCurrentMouseOverPos(evt);
	}
}