Type.registerNamespace("AX");
Type.registerNamespace("AX.UI");

AX.UI.Window= function(name) {
                this.Name= name;
                var w= 400; var h= 300;
                this.width= w;
                this.height= h;
                this.headerHeight= 26;
                this._fullScreen= false;
                this.simpleHide= false;

                this._content= document.createElement("DIV");
                with (this._content.style) {
                        display= "none"; position= "absolute"; left= "0px"; top= "0px"; zIndex= 1001;
                        width= w + "px"; height= h + "px";
                }
                document.body.insertBefore(this._content, document.body.firstChild);

                this.ip= "/ASP.NET/Resources/FM/Common/Images/Window/";
                var sWnd= "<table style='width:100%;height:100%;' cellspacing='0' cellpadding='0'>";
                sWnd+= "<tr><td><table width='100%' cellpadding='0' cellspacing='0' style='-moz-user-select:none;'><tr><td style='width:6px;height:26px;background-image:url("+this.ip+"mw_headerL.gif)'></td><td style='height:26px;cursor:move;background-image:url("+this.ip+"mw_headerBg.gif);font-family:Arial CE, Arial;font-size:10pt;font-weight:bold;'><img src='"+this.ip+"ico_top.gif' align='absmiddle' >&nbsp;<label id='_HeaderText'>Loading, Please wait...</label></td><td style='text-align:right;padding-right:5px;height:26px;background-image:url("+this.ip+"mw_headerBg.gif);'>&nbsp;</td><td style='width:56px;height:26px;background-image:url("+this.ip+"mw_headerR.gif);cursor:pointer;' title='Close(Esc)'><img src='"+this.ip+"mw_headerROn.gif' style='display:none'/></td></tr></table></td></tr>";
                var innerWnd= "<iframe style='width:100%;height:100%;' frameborder='0' scrolling='auto'></iframe>";
                sWnd+= "<tr style='height:100%'><td style='height:100%;background-color:#E0ECF7;padding-left:6px;padding-right:6px;border-right:solid 1px #404851;border-left:solid 1px #404851;'>" + innerWnd + "</td></tr>";
                sWnd+= "<tr><td><table width='100%' cellpadding='0' cellspacing='0'><tr><td style='width:6px;height:7px;background-image:url("+this.ip+"mw_bottomL.gif)'></td><td style='height:7px;background-image:url("+this.ip+"mw_bottomBg.gif);'><img src='"+this.ip+"pixel.gif' border='0' /></td><td style='width:6px;height:7px;background-image:url("+this.ip+"mw_bottomR.gif)'></td></tr></table></td></tr>";
                sWnd+= "</table>";

                this._content.innerHTML= sWnd;
                this._wndBody= this._content.firstChild.rows[1].cells[0].firstChild;

                this._header= this._content.firstChild.rows[0].cells[0].firstChild.rows[0].cells[1].lastChild;
                this.contentWindow= null;
                this._footer= null;
                this._wndHeader= this._content.firstChild.rows[0].cells[0].firstChild.rows[0].cells[1];

                this._tdClose= this._content.firstChild.rows[0].cells[0].firstChild.rows[0].cells[3];

                this._closeHandler= Function.createDelegate(this, this.Hide);
                this._moveHandler= Function.createDelegate(this, this._MoveStart);
                this._resizeWndHandler= Function.createDelegate(this, this._ResizeWnd);

                this._windowLoadedHandler= Function.createDelegate(this, this.onWindowLoaded);

                //
                this.onBeforeClose= null;
                this.refreshParent= false;

                this.innerObject= null;
                this.Visibled= false;

                this.ID= AX.UI.Window.getUniqueID();
}

AX.UI.Window.prototype= {

  dispose : function() {
                if (!this._content) return;
    this._detachPopup();

                //this._resizeHandler= null;
                //this._scrollHandler= null;
                this._closeHandler= null;
                this._moveHandler= null;
                this._resizeWndHandler= null;

                this._content.psrentNode.removeChild(this._content);
                this._content= null;
  },

  Open : function(url, w, h, loadCallback, refreshCallback, title) {
                this._loadCallback= loadCallback;
                this._refreshCallback= refreshCallback;
                this.width= w; this.height= h;
                this.Header= title;
                this.innerObject= null;
                if (this._wndBody.src != url || this.contentWindow == null) {
                        this._wndBody.ContainerWindow= this;
                        this._wndBody.src= url;
                        $addHandler(this._wndBody, 'load', this._windowLoadedHandler);
                } else {
                        this._loadCallback(this.contentWindow);
                }

                this.Show();
                if (this.contentWindow) this.contentWindow.document.body.focus();

                this._attachPopup();
  },

  onWindowLoaded : function() {
                $removeHandler(this._wndBody, 'load', this._windowLoadedHandler);
                this.contentWindow= this._wndBody.contentWindow;
                this._loadCallback(this.contentWindow);
                this._header.innerHTML= (this.Header != null) ? this.Header : this.Name;
  },

  _attachPopup : function() {
                if (this._windowHandlersAttached) return;
                //$addHandler(window, 'resize', this._resizeHandler);
                //$addHandler(window, 'scroll', this._scrollHandler);
    $addHandler(this._wndHeader, 'mousedown', this._moveHandler);
    $addHandler(this._wndHeader, 'dblclick', this._resizeWndHandler);
    this._windowHandlersAttached= true;

                 this._tdClose.tabIndex= 100;
                 $addHandlers(this._tdClose, {
                        'click': this.Close,
                        'mouseover': this.close_Over,
                        'mouseout': this.close_Out
                }, this);

                $addHandlers(this._wndBody, {
                        'focus': this.focus
                }, this);
  },

        focus : function(e) {
                if (this.innerObject != null) this.innerObject.focus();
        },

  _detachPopup : function() {
    if (this._windowHandlersAttached) {
                        //$removeHandler(window, 'resize', this._resizeHandler);
                        //$removeHandler(window, 'scroll', this._scrollHandler);
                        //$removeHandler(window, 'resize', this._resizeHandler); // IE FIX
                        //$removeHandler(window, 'scroll', this._scrollHandler); // IE FIX
      $removeHandler(this._wndHeader, 'mousedown', this._moveHandler);
      $removeHandler(this._wndHeader, 'dblclick', this._resizeWndHandler);
                        $clearHandlers(this._tdClose);
                        this._tdClose.tabIndex= -1;
      this._windowHandlersAttached= false;
    }
  },

        Show : function() {
                this._content.style.width= this.width + "px";
                this._content.style.height= this.height + "px";
                this.setPosition();

                this.zIndex= AX.UI.Window.reserveZIndex();

                this.Visibled= true;
                this._content.style.zIndex= this.zIndex;
                this._content.style.display= '';

                this._wndBody.focus();

                this.raiseOnShow();
        },

        Disable : function() {
                //this._bg.style.zIndex= this.zIndex + 100;
        },
        Enable : function() {
                //this._bg.style.zIndex= this.zIndex - 1;
        },

        Hide : function() {
                if (!this._content) return;
                var allowClose= this.raiseBeforeCloseEvent();
                if (!allowClose) return;

                this.Visibled= false;
                this._content.style.display= "none";

                //if (this._refreshCallback && this.refreshParent) { this._refreshCallback(); this._refreshCallback= null; this.refreshParent= false; }
                if (this._refreshCallback) this._refreshCallback();

                this.close_Out();
                AX.UI.Window.restoreZIndex();
        },

        Close : function() {
                this.Hide();
                this.dispose;
        },

        raiseBeforeCloseEvent : function() {
                if (this.onBeforeClose) return this.onBeforeClose(this.contentWindow);
                return true;
        },

        refreshParentAfterClose : function() { this.refreshParent= true; },

        setPosition : function() {
                var scrolledX, scrolledY;
                if (self.pageYOffset) {
                        scrolledX= self.pageXOffset;
                        scrolledY= self.pageYOffset;
                } else if (document.documentElement && document.documentElement.scrollTop) {
                        scrolledX= document.documentElement.scrollLeft;
                        scrolledY= document.documentElement.scrollTop;
                } else if (document.body) {
                        scrolledX= document.body.scrollLeft;
                        scrolledY= document.body.scrollTop;
                }

                // Determine the coordinates of the center of browser's window

                var centerX= ax_getWindowWidth();
                var centerY= ax_getWindowHeight();

                var h= (this.height + this.headerHeight);
                if (centerY <= h) this.height= centerY - 10 - this.headerHeight;

                if (this._fullScreen) {
                        this._content.style.top= "0px";
                        this._content.style.left= "0px";
                        this._content.style.width= centerX + "px";
                        this._content.style.height= centerY + "px";
                } else {
                        this.left= scrolledX + (centerX - this.width) / 2;
                        this.top= scrolledY + (centerY - (this.height + this.headerHeight)) / 2;
                        //if (this.top > 100) this.top-= 50;

                        this._content.style.top= this.top + "px";
                        this._content.style.left= this.left + "px";
                        this._content.style.width= this.width + "px";
                        this._content.style.height= this.height + "px";
                }
        },

  close_Over : function() { this._tdClose.style.backgroundImage= 'url(' +this.ip+ 'mw_headerROn.gif)'; },
  close_Out : function() { this._tdClose.style.backgroundImage= 'url(' + this.ip + 'mw_headerR.gif)'; },

  SetHeader : function(title, desc) {
                this._header.innerHTML= (title != null) ? title : "";
  },

  _MoveStart : function(e) {
                if (!this.Visibled || this._fullScreen) return;
    var dobj= this._content;
    this._tx= parseInt(dobj.style.left+0);
    this._ty= parseInt(dobj.style.top+0);
    this._x= e.clientX;
    this._y= e.clientY;

    AX.UI.Window.moveMouseFrame(e.clientX, e.clientY);

    if (!this._moveCompleteHandler) this._moveCompleteHandler= Function.createDelegate(this, this._MoveComplete);
    if (!this._movingHandler) this._movingHandler= Function.createDelegate(this, this._Move);
    $addHandler(window.document, 'mouseup', this._moveCompleteHandler);
                $addHandler(window.document, 'mousemove', this._movingHandler);
    return false;
  },

  _Move : function(e) {
                AX.UI.Window.moveMouseFrame(e.clientX, e.clientY);
                var dobj= this._content;
                var left= this._tx + e.clientX - this._x;
                var top= this._ty + e.clientY - this._y;
                dobj.style.left= ((left > 0) ? left : 0) + 'px';
                dobj.style.top= ((top > 0) ? top : 0) + 'px';
                return false;
  },

  _MoveComplete : function(e) {
                $removeHandler(window.document, 'mouseup', this._moveCompleteHandler);
                $removeHandler(window.document, 'mousemove', this._movingHandler);
                AX.UI.Window.hideMouseFrame();
                this.focus();
        },

        _ResizeWnd : function() {
                if (!this.Visibled) return;
                this._fullScreen= !this._fullScreen;
                this.setPosition();
        },

        SetStatus : function(s) {
                this._header.title= s;
        },

        lastFocus : function(e) { this.close_Over(); },
        lastBlur : function(e) { this.close_Out(); this.focus(); },

        // Events
  add_OnShow: function(h) { this._onShowCallback= h; },
  remove_OnShow: function() { this._onShowCallback= null; },
  raiseOnShow: function(fl) { if (this._onShowCallback) this._onShowCallback(fl); },

        EndFunc : function() { }
}

AX.UI.Window.registerClass('AX.UI.Window', null, Sys.IDisposable);


AX.UI.Window._zIndex= 1000;
AX.UI.Window.reserveZIndex= function() { return AX.UI.Window._zIndex+= 100; return AX.UI.Window._zIndex; }
AX.UI.Window.restoreZIndex= function() { AX.UI.Window._zIndex-= 100; }
AX.UI.Window.isActive= function(wnd) { return wnd.zIndex == AX.UI.Window._zIndex; }

AX.UI.Window._uniqueID= 0;
AX.UI.Window.getUniqueID= function () { return ("wnd_" + (++AX.UI.Window._uniqueID)); }
AX.UI.Window.find= function(id) { alert('AX.UI.Window.find is not supported now'); return null; }

AX.UI.Window.getMouseFrame= function() {
if (!AX.UI.Window._mouseFrame) {
        var o= document.createElement("DIV");
  o.style.position= "absolute"; o.style.zIndex= 5000;
        o.style.width= "120px"; o.style.height= "140px";
        document.body.appendChild(o);
  AX.UI.Window._mouseFrame= o;
}
return AX.UI.Window._mouseFrame;
}
AX.UI.Window.moveMouseFrame= function(x, y) {
        var o= AX.UI.Window.getMouseFrame();
        o.style.left= (x - 60) + "px";
        o.style.top= (y - 50) + "px";
        if (o.style.display == "none") o.style.display= "";
}
AX.UI.Window.hideMouseFrame= function() { AX.UI.Window.getMouseFrame().style.display= "none"; }

// Gets real Top value with respect to client area
ax_getAbsoluteTop= function(o, dy) {
  var top= (dy != null) ? dy : 0;
  while (o && o.tagName != "BODY") { top+= o.offsetTop; o= o.offsetParent; }
  return top;
}
// Get real Left value with respect to client area
ax_getAbsoluteLeft= function(o, dx) {
        var left= (dx != null) ? dx : 0;
  while (o && o.tagName != "BODY") { left+= o.offsetLeft; o= o.offsetParent; }
  return left;
}
ax_getWindowWidth= function() {
        if (self.innerWidth) return self.innerWidth;
        else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
        else if (document.body) return document.body.clientWidth;
        return 0;
}
ax_getWindowHeight= function() {
        if (self.innerHeight) return self.innerHeight;
        else if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
        else if (document.body) return document.body.clientHeight;
        return 0;
}

AX.UI.Window.Open= function(name, url, w, h, loadCallback, refreshCallback, title) {
        var mas= window.parent._axwindows;
        if (mas == null) window.parent._axwindows= mas= new Array();
        var wnd= mas[name];
        if (wnd == null) { wnd= new AX.UI.Window(name); wnd.simpleHide= true; wnd.refreshParent= true; window.parent._axwindows[name]= wnd; }
        wnd.Open(url, w, h, loadCallback, refreshCallback, title);
        return wnd;
}

Sys.Application.notifyScriptLoaded();