var __aspxMenuAnimationAccelerator = 1/10;
var __aspxPCAnimationAccelerator = 1/3;
var __aspxAnimationDiscardPixels = 3;

var __aspxNotSetAlignIndicator = "NotSet";
var __aspxInnerAlignIndicator = "Sides";

function _aspxIsAlignNotSet(align){
    return align == __aspxNotSetAlignIndicator;
}
function _aspxIsInnerAlign(align){
    return align.indexOf(__aspxInnerAlignIndicator) != -1;
}
function _aspxIsOuterAlign(align){
    return (!this.IsInnerAlign(align)) && (!_aspxIsAlignNotSet(align));
}

function _aspxPopupPosition(position, isInverted){
    this.position = position;
    this.isInverted = isInverted;
}
function _aspxSegment(pos, len){
    this.pos = pos;
    this.len = len;
}
function _aspxRect(left, top, width, height){
    this.left = left;
    this.top = top;
    this.width = width;
    this.height = height;
}

function _aspxFindPopupElementById(id){
    if(id == "") return null; // FIX Opera return element with id=""
    
    var popupElement = _aspxGetElementById(id);
    if(!_aspxIsExistsElement(popupElement)){
        var idParts = id.split("_");
        var uniqueId = idParts.join("$");
        popupElement = _aspxGetElementById(uniqueId);
    }
    return popupElement;
}
function _aspxFindParentPopupElementByEvent(evt, testFunc){
    return _aspxFindParentPopupElement(_aspxGetEventSource(evt), testFunc);
}
function _aspxFindParentPopupElement(element, testFunc){
    if (!_aspxIsExists(testFunc)) return null;
    
    while(element != null && element.tagName != "BODY"){
		if(testFunc(element))
		    return element;
        element = element.parentNode;  		    
	}
    return null;
}
function _aspxPreventContextMenu(evt){
    if (__aspxSafari)
		evt.stopPropagation();
	else if (__aspxNS)
	    evt.preventDefault();
}
function _aspxSetStylePosition(element, x, y){
    element.style.left = x + "px";
    element.style.top = y + "px";
}
function _aspxSetStyleSize(element, width, height){
    element.style.width = width + "px";
    element.style.height = height + "px";
}
function _aspxIsExistsAbsolutePosParent(element){
    return _aspxIsExistsParentWithSpecPosition(element, ["absolute"])
}
function _aspxIsExistsAbsoluteOrRelativePosParent(element){
    return _aspxIsExistsParentWithSpecPosition(element, ["absolute", "relative"])
}
function _aspxIsExistsParentWithSpecPosition(element, positions){
    var curEl = element.offsetParent;
    while(curEl != null) {
        for(var i = 0; i < positions.length; i ++){
		    if (_aspxGetCurrentStyle(curEl).position == positions[i])
		        return true;
		}
		curEl = curEl.offsetParent;
    }
    return false;
}
function _aspxGetPopupAbsoluteX(element, popupElement, hAlign, hOffset, x, left){
    var width = element.offsetWidth;
    var bodyWidth = _aspxGetDocumentClientWidth();
    var elementX = _aspxGetAbsoluteX(popupElement);
    var scrollX = _aspxGetDocumentScrollLeft();

    if (hAlign == "WindowCenter")
        return new _aspxPopupPosition(Math.ceil(bodyWidth / 2 - width / 2) + scrollX + hOffset, false);

    if (_aspxIsExists(popupElement)){
        var leftX = elementX - width;
        var rightX = elementX + popupElement.offsetWidth;
        var innerLeftX = elementX;
        var innerRightX = elementX + popupElement.offsetWidth - width;
        var isMoreFreeSpaceLeft = bodyWidth - (rightX + width) < leftX - 2 * scrollX;
    }
    else
        hAlign = "";
    
    var isInverted = false;    
    if (hAlign == "OutsideLeft"){
        isInverted = !(leftX - scrollX > 0 || isMoreFreeSpaceLeft);
        if(isInverted)
            x = rightX - hOffset;
        else
            x = leftX + hOffset;
    }
    else if (hAlign == "LeftSides"){
        x =  innerLeftX + hOffset;
    }
    else if (hAlign == "Center"){
        x =  elementX + Math.round((popupElement.offsetWidth  - width) / 2) + hOffset;
    }
    else if (hAlign == "RightSides"){
        x = innerRightX + hOffset;
    }
    else if (hAlign == "OutsideRight"){
        isInverted = !(rightX + width < bodyWidth + scrollX || !isMoreFreeSpaceLeft);
        if(isInverted)
            x = leftX - hOffset;
        else
            x = rightX + hOffset;
    }
    else{
        if (!_aspxGetIsValidPosition(x))
            x = (_aspxIsExists(popupElement)) ? _aspxGetAbsoluteX(popupElement) : left;
        isInverted = x - scrollX + width > bodyWidth && x - scrollX > bodyWidth / 2;
        if(isInverted)
            x = x - width - hOffset;
        else
            x = x + hOffset;
    }
    if(__aspxIE55)
        x -= _aspxGetIEDocumentClientOffset(true);
    return new _aspxPopupPosition(x, isInverted);
}
function _aspxGetPopupAbsoluteY (element, popupElement, vAlign, vOffset, y, top){
    var height = element.offsetHeight;
    var bodyHeight = _aspxGetDocumentClientHeight(); 
    var elementY = _aspxGetAbsoluteY(popupElement);
    var scrollY = _aspxGetDocumentScrollTop();
    
    if (vAlign == "WindowCenter")
        return new _aspxPopupPosition(Math.ceil(bodyHeight / 2 - height / 2) + scrollY + vOffset, false);
    
    if (_aspxIsExists(popupElement)){
        var bottomY = elementY + popupElement.offsetHeight;
        var topY = elementY - height;
        var innerBottomY = elementY + popupElement.offsetHeight - height;
        var innerTopY = elementY;
        var isMoreFreeSpaceAbove = bodyHeight - (bottomY + height) < topY - 2 * scrollY;
    }
    else
        vAlign = "";
    
    var isInverted = false;
    if (vAlign == "Above"){
        isInverted = !(topY - scrollY > 0 || isMoreFreeSpaceAbove);
        if(isInverted)
            y = bottomY - vOffset;
        else
            y = topY + vOffset;
    }
    else if (vAlign == "TopSides"){
        y = innerTopY + vOffset;
    }
    else if (vAlign == "Middle"){
        y =  elementY + Math.round((popupElement.offsetHeight  - height) / 2) + vOffset;
    }
    else if (vAlign == "BottomSides"){
        y = innerBottomY + vOffset;
    }
    else if (vAlign == "Below"){
        isInverted = !(bottomY + height < bodyHeight + scrollY || !isMoreFreeSpaceAbove);
        if(isInverted)
            y = topY - vOffset;
        else
            y = bottomY + vOffset;
    }
    else{
        if (!_aspxGetIsValidPosition(y))
            y = (_aspxIsExists(popupElement)) ? _aspxGetAbsoluteY(popupElement) : top;
        isInverted = y - _aspxGetDocumentScrollTop() + height > bodyHeight && y - _aspxGetDocumentScrollTop() > bodyHeight / 2;
        if(isInverted)
            y = y - height - vOffset;
        else
            y = y + vOffset;
    }
    if(__aspxIE55)
        y -= _aspxGetIEDocumentClientOffset(false);
    return new _aspxPopupPosition(y, isInverted);
}
function _aspxGetIntersectionRect(left1, top1, left2, top2, width, height){
    var segment1 = _aspxGetIntersectionSegment(left1, left2, width);
    var segment2 = _aspxGetIntersectionSegment(top1, top2, height);
    if (segment1 != null && segment2 != null){
        var left = segment1.pos;
        var top = segment2.pos;
        var width = segment1.len;
        var height = segment2.len;
        return new _aspxRect(left, top, width, height);
    }
    return null;
}
function _aspxGetIntersectionSegment(pos1, pos2, len){
    var posDifferense = Math.abs(pos1 - pos2);
    if (posDifferense <= len){
        var pos = pos1 > pos2 ? pos1 : pos2;
        var len = len - posDifferense;
        return new _aspxSegment(pos, len);
    }
    return null;
}

function _aspxInitAnimationDiv(element, x, y, onTimerString, onAnimStopCallString){
    element.animationStart = new Date();
    element.absoluteLeft = x;
    element.absoluteTop = y;
    element.popuping = true;
    element.onTimerString = onTimerString;
    element.onAnimStopCallString = onAnimStopCallString;
    element.style.overflow = "hidden";
    element.style.position = "absolute";
    
    _aspxSetStylePosition(element, x,y);
    _aspxSetStyleSize(element, element.offsetWidth, element.offsetHeight);
}
function _aspxOnAnimationTimer(animationDivElement, element, mainCell, iframeElement, animationDelay, animationMaxDelay, animationAccelerator){
    animationDivElement.timerID = _aspxClearTimer(element.timerID);

    var timeExpired = new Date() - animationDivElement.animationStart > animationMaxDelay;
    if(timeExpired)
        _aspxAnimationFinished(animationDivElement);
        
    var left = animationDivElement.popuping ? _aspxGetNextAnimationPosInternal(element.offsetLeft, animationAccelerator) : 0;
    var top = animationDivElement.popuping ? _aspxGetNextAnimationPosInternal(element.offsetTop, animationAccelerator) : 0;
    
    if (left == 0 && top == 0)
        _aspxAnimationFinished(animationDivElement);
    
    element.style.top = top + "px";
    element.style.left = left + "px";
    
    var rect = _aspxGetIntersectionRect(animationDivElement.offsetLeft, animationDivElement.offsetTop, 
        element.offsetLeft + animationDivElement.offsetLeft, element.offsetTop + animationDivElement.offsetTop,
        mainCell.offsetWidth, mainCell.offsetHeight);
    if (_aspxIsExists(rect) && _aspxIsExists(iframeElement)){
        _aspxSetStylePosition(iframeElement, rect.left, rect.top);
        _aspxSetStyleSize(iframeElement, rect.width, rect.height);
    }
    
    if (!timeExpired && animationDivElement.popuping)
        animationDivElement.timerID = window.setTimeout(animationDivElement.onTimerString, animationDelay);
}
function _aspxGetNextAnimationPosInternal(pos, animationAccelerator){
    pos = Math.round(pos * animationAccelerator);
    if (Math.abs(pos) < __aspxAnimationDiscardPixels)
        pos = 0;
    return pos;
}
function _aspxAnimationFinished(element){
    if(_aspxStopAnimation(element) && _aspxIsExists(element.onAnimStopCallString) && 
        element.onAnimStopCallString !== ""){
        eval(element.onAnimStopCallString);
    }
}
function _aspxStopAnimation(element){
    if(element.popuping){
        element.popuping = false;
        element.onTimerString = "";
	    element.style.overflow = "visible";
	    return true;
	}
	return false;
}
function _aspxGetAnimationHorizontalDirection(popupPosition, horizontalAlign, verticalAlign){
    if (_aspxIsInnerAlign(horizontalAlign) 
        && !_aspxIsInnerAlign(verticalAlign) 
        && !_aspxIsAlignNotSet(verticalAlign))
        return 0;
    var toTheLeft = (horizontalAlign == "OutsideLeft" || horizontalAlign == "RightSides") ^ popupPosition.isInverted;
    return toTheLeft ? 1 : -1;
}
function _aspxGetAnimationVerticalDirection(popupPosition, horizontalAlign, verticalAlign){
    if (_aspxIsInnerAlign(verticalAlign) 
        && !_aspxIsInnerAlign(horizontalAlign) 
        && !_aspxIsAlignNotSet(horizontalAlign))
        return 0;
    var toTheTop = (verticalAlign == "Above" || verticalAlign == "BottomSides") ^ popupPosition.isInverted;
    return toTheTop ? 1 : -1;
}
function _aspxHideBodyScroll(){
    if(__aspxIE){
        _aspxChangeAttribute(document.body, "scroll", "no");
        _aspxChangeStyleAttribute(document.documentElement, "overflow", "hidden");
    } else if(__aspxFirefox && !__aspxFirefox3) { //Q109177
        var scrollTop = document.documentElement.scrollTop;
        _aspxChangeStyleAttribute(document.body, "overflow", "hidden");
        document.documentElement.scrollTop = scrollTop;
    } else
        _aspxChangeStyleAttribute(document.body, "overflow", "hidden");
   _aspxChangeStyleAttribute(document.body, "marginRight", "16px");
}
function _aspxRestoreBodyScroll(){
    if(__aspxIE){
        _aspxRestoreAttribute(document.body, "scroll");
        _aspxRestoreStyleAttribute(document.documentElement, "overflow");
    }
    else
        _aspxRestoreStyleAttribute(document.body, "overflow");
    _aspxRestoreStyleAttribute(document.body, "marginRight");
    
    if(__aspxSafari){ // B96454 // TODO Chrome has same bug, so add __aspxChrome
        var scrollTop = document.body.scrollTop;
        document.body.scrollTop ++;
        document.body.scrollTop --;
        document.body.scrollTop = scrollTop;
    }
}