var __aspxWindowResizer = null;
var __aspxClientPopupFilterWindow = null;

ASPxClientWindowResizer = _aspxCreateClass(null, {
	constructor: function(e, resizedEl){
        __aspxWindowResizer = this;
        this.resizedEl = resizedEl;
        this.minWidth = 50;
        this.minHeight = 50;
        this.lastX = e.clientX;
        this.lastY = e.clientY;    
        this.newWidth = resizedEl.offsetWidth;
        this.newHeight = resizedEl.offsetHeight;
        this.startWidth = this.newWidth;
        this.startHeight = this.newHeight;
        this.virtWidth = this.newWidth;
        this.virtHeight = this.newHeight;
        
        this.onSetSize = null;
        this.onEndResizing = new Function();
    },    
    doResizing: function(e) {
        this.virtWidth = this.virtWidth + e.clientX - this.lastX;
        this.virtHeight = this.virtHeight + e.clientY - this.lastY;
        this.lastX = e.clientX;
        this.lastY = e.clientY;        
        this.newWidth = this.virtWidth;
        this.newHeight = this.virtHeight;
        if(this.newWidth < this.minWidth)
            this.newWidth = this.minWidth;
        if(this.newHeight < this.minHeight)
            this.newHeight = this.minHeight;
        this.doSetSize();
    },
    cancel: function() {
        this.newWidth = this.startWidth;
        this.newHeight = this.startHeight;
        this.stop();
    },
    endResizing: function(e) {
        this.onEndResizing(e);
        this.stop();    
    },
    stop: function() {
        this.doSetSize();
        __aspxWindowResizer = null;
    },
    doSetSize : function() {
        if(this.onSetSize == null || this.onSetSize(this)) {
            this.setSize();  
        }
    },
    setSize: function() {
        this.resizedEl.style.width = this.newWidth + "px";
        this.resizedEl.style.height = this.newHeight + "px";
    }
});

ASPxClientPopupFilterWindow = _aspxCreateClass(null, {
	constructor: function(name){
	    this.name = name;
	    _aspxAttachEventToElement(this.GetWindowResizer(), "dragstart", _aspxPreventDragStart);
    },
    GetWindow: function() { return _aspxGetElementById(this.name + "_FPW"); },
    GetElement: function() { return _aspxGetElementById(this.name + "_FPC"); },
    GetWindowResizer: function() { return _aspxGetElementById(this.name + "_FPWR"); },
    IsShowing: function() {
        if(!_aspxIsExists(this.window)) return false;
        return _aspxGetElementDisplay(this.window);
    },
    Hide: function() {
        if(!_aspxIsExists(this.window)) return;
        __aspxClientPopupFilterWindow = null;
        _aspxSetElementDisplay(this.window, false);
        this.header = null;
    },
    Show: function(element, mainElement) {
        __aspxClientPopupFilterWindow = this;
        this.window = this.GetWindow();
        if(!_aspxIsExists(this.window)) return;
        
        _aspxSetElementDisplay(this.window, true);
        var left = _aspxGetRelevantX(element, mainElement);
        var top = _aspxGetRelevantY(element, mainElement) + element.offsetHeight;
        
        this.window.style.left = left + "px";
        this.window.style.top = top + "px";
        this.header = element;
    },
    SetDefaultWidth: function() {
        this.GetWindow().style.width = "";
        this.GetElement().style.width = "";
    },
    onMouseDown: function(e) {
        if(this.IsShowing() && !_aspxGetIsParent(this.window, _aspxGetEventSource(e))) {
            this.Hide();
        }
        return true;
    },
    onResizerMouseDown: function(e) {
        var resizer = new ASPxClientWindowResizer(e, this.GetElement());
        resizer.onSetSize = this.ResizerSetSize;
        resizer.resizedWindow = this.window;        
        return _aspxCancelBubble(e);
    },
    ResizerSetSize: function(resizer) {
        var oldElWidth = resizer.resizedEl.style.width;
        var oldWindowWidth = resizer.resizedWindow.style.width;
        var difEl = resizer.resizedEl.offsetWidth;
        var difWindow = resizer.resizedWindow.offsetWidth;
        resizer.resizedWindow.style.width = resizer.newWidth + "px";
        resizer.resizedEl.style.width = resizer.newWidth + "px";
        difEl = resizer.resizedEl.offsetWidth - difEl;
        difWindow = resizer.resizedWindow.offsetWidth - difWindow;
        if(difEl != difWindow) {
            resizer.resizedEl.style.width = oldElWidth;
            resizer.resizedWindow.style.width = oldWindowWidth;
        }
	    if(__aspxNS) {
            resizer.resizedWindow.style.width = resizer.resizedEl.style.width;
	        if(resizer.resizedWindow.offsetWidth < resizer.resizedEl.offsetWidth) {
		       resizer.resizedWindow.style.width = "";
	        }
	    }
	    resizer.resizedWindow.style.height = resizer.newHeight + "px";
        resizer.resizedEl.style.height = resizer.newHeight + "px";
        return false;
    }
});

function WindowResizer_onmouseup(e) {
    if(__aspxWindowResizer != null)
        __aspxWindowResizer.endResizing(e);
    return true;
}
function WindowResizer_onmousemove(e) {
    if(__aspxWindowResizer != null) {
        __aspxWindowResizer.doResizing(e);
    }
    return true;
}
function WindowResizer_onkeydown(e) {
    if(__aspxWindowResizer == null) return;
    if(e.keyCode == 27)
        __aspxWindowResizer.cancel();
    return true;
}
function WindowResizer_onselectstart(e) {
    if(__aspxWindowResizer == null) return;
    document.selection.empty();
    return false;
}

 
var __aspxClientPopupFilterWindowShowAgain = false;
function PopupFilterWindow_DocumentOnMouseDown(e){
    if(__aspxClientPopupFilterWindow == null) return;
    var src = _aspxGetEventSource(e),
		header = __aspxClientPopupFilterWindow.header;
    var onclickText = _aspxIsExists(src.onclick) ? src.onclick.toString() : "";
    if(onclickText.indexOf("_ShowFilterPopup") >= 0 && __aspxClientPopupFilterWindow.IsShowing() && _aspxGetIsParent(header, src)) {
		__aspxClientPopupFilterWindowShowAgain = true;
	}
    return __aspxClientPopupFilterWindow.onMouseDown(e);
}
function PopupFilterWindow_WindowOnResize(e){
    if(__aspxClientPopupFilterWindow == null) return;
	__aspxClientPopupFilterWindow.Hide();
}

function PopupFilterWindow_WindowOnLoad(e) {
    _aspxAttachEventToDocument("mousedown", PopupFilterWindow_DocumentOnMouseDown);
    _aspxAttachEventToElement(window, "resize", PopupFilterWindow_WindowOnResize);
}


_aspxAttachEventToDocument("mouseup", WindowResizer_onmouseup);
_aspxAttachEventToDocument("mousemove", WindowResizer_onmousemove);
_aspxAttachEventToDocument("keydown", WindowResizer_onkeydown);
_aspxAttachEventToDocument("selectstart", WindowResizer_onselectstart);
_aspxAttachEventToElement(window, "load", PopupFilterWindow_WindowOnLoad);