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);

		var 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='background-image:url("+ip+"headerL.gif);width:5px;height:26px'></td><td style='font-weight: bold; font-size: 10pt; background-image:url("+ip+"headerBg.gif);cursor: move; font-family: Arial CE, Arial; height: 26px'><img src='"+ip+"../ico2/16_info.gif' align='absmiddle'>&nbsp;<label id='_HeaderText'>Loading, Please wait...</label></td><td style='padding-right: 5px;background-image:url("+ip+"headerBg.gif);height:26px;'>&nbsp;</td><td align='right' style='background-image:url("+ip+"headerBg.gif);height:26px;width:95px;' valign='top'><img src='"+ip+"btn_close.gif' alt='Close' border='0' height='18' width='44' style='margin-top:1px;'/></td><td style='background-image:url("+ip+"headerR.gif);width:5px;height:26px'></td></tr></table></td></tr>";
		var innerWnd= "<iframe style='width:100%;height:100%;background-color:#FFF;' frameborder='0' scrolling='no'></iframe>";
		sWnd+= "<tr style='height:100%'><td style='border-right:#3A434B 1px solid;padding-right:5px;padding-left:5px;border-left:#3A434B 1px solid;height:100%;background-color:#c0d7eb;'>" + innerWnd + "</td></tr>";
		sWnd+= "<tr><td><table width='100%' cellpadding='0' cellspacing='0'><tr><td style='background-image:url("+ip+"bottomL.gif);width:6px;height:7px'></td><td style='background-image:url("+ip+"bottomBg.gif);height:7px'><img src='"+ip+"pixel.gif' border='0' /></td><td style='background-image:url("+ip+"bottomR.gif);width:6px;height:7px'></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._tdButtons= 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.parentNode.removeChild(this._content);
		this._content= null;
  },

  Open : function(url, w, h, loadCallback, refreshCallback, title, closeMode) {
		this._loadCallback= loadCallback;
		this._refreshCallback= refreshCallback;
		this._onCloseUrl= null;
		if (closeMode == "reloadOnClose") this._onCloseUrl= url;
		else if (closeMode == "clearOnClose") this._onCloseUrl= "about:blank";
		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 {
			if (this._loadCallback) this._loadCallback(this.contentWindow);
		}
		
		this.Show();
		if (this.contentWindow) this.contentWindow.document.body.focus();

		this._attachPopup();
  },
  
  //need test
  Refresh : function() {
		if (this._wndBody) this._wndBody.contentWindow.location.reload();
		else if (this._loadCallback) this._loadCallback(this.contentWindow);
  },

  onWindowLoaded : function() {
		$removeHandler(this._wndBody, 'load', this._windowLoadedHandler);
		this.contentWindow= this._wndBody.contentWindow;
		if (this._loadCallback) 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._tdButtons.tabIndex= 100;
 		$addHandlers(this._tdButtons, {
			'click': this.buttons_Click,
			'mouseover': this.buttons_Over,
			'mouseout': this.buttons_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._fullScreen= false; // fix it
		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); this.refreshParent= false; }

		this.buttons_Out(this._tdButtons.childNodes[2]);
		AX.UI.Window.restoreZIndex();
	},
	
	buttons_Click : function(e) {
		if (e.target.tagName != "IMG") return;
		var src= e.target.src;
		if (src.indexOf("btn_close") > 0) this.Hide();
		else if (src.indexOf("btn_max") > 0) this._ResizeWnd(null, e.target); 
		else if (src.indexOf("btn_!") > 0) this._ShowInfo();
	},

	Close : function() {
		this.Hide();
		if (this._onCloseUrl && this._wndBody) this._wndBody.src= this._onCloseUrl;
	},

	raiseBeforeCloseEvent : function() {
		if (this.onBeforeClose) return this.onBeforeClose(this.contentWindow);
		return true;
	},

	refreshParentAfterClose : function(bRefresh) { this.refreshParent= (bRefresh != false); },

	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;
		}
		
		var brwWidth= ax_getWindowWidth();
		var brwHeight= ax_getWindowHeight();
		
		if (this._fullScreen) {
			this._content.style.top= "0px";
			this._content.style.left= "0px";
			this._content.style.width= brwWidth + "px";
			this._content.style.height= (brwHeight - this.headerHeight - 7) + "px";
		} else {
			var h= this.height + this.headerHeight + 7;
			if (brwHeight < h) {
				this.height= brwHeight - this.headerHeight - 7;
				this.top= 0;
			} else {
				this.top= scrolledY + (brwHeight - (this.height + this.headerHeight)) / 2;
			}
			this.left= scrolledX + (brwWidth - this.width) / 2;

			//this._bg.style.width= brwWidth + "px";
			//this._bg.style.height= brwHeight + "px";
			
			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";
		}
	},

  buttons_Over : function(e) { 
		if (e.target.tagName != "IMG") return;
		var src= e.target.src;
		if (src.indexOf("_on.gif") == -1) e.target.src= src.replace(".gif", "") + "_on.gif";
	},
  buttons_Out : function(e, img) { 
		if (e != null) img= e.target;
		if (img == null || img.tagName != "IMG") return;
		var src= img.src;
		if (src.indexOf("_on.gif") > 0 -1) img.src= src.replace("_on.gif", "") + ".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(e, img) {
		if (!this.Visibled) return;
		this._fullScreen= !this._fullScreen;
		this.setPosition();
		
		if (img == null) img= this._tdButtons.childNodes[1];
		if (this._fullScreen) { img.src= "/ASP.NET/Resources/FM/Common/Images/Window/btn_max_min.gif"; img.title= "Restore Down";  }
		else { img.src= "/ASP.NET/Resources/FM/Common/Images/Window/btn_max.gif"; img.title= "Maximize";}
	},

	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, closeMode) {
	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; window.parent._axwindows[name]= wnd; }
	wnd.Open(url, w, h, loadCallback, refreshCallback, title, closeMode);
	return wnd;
}

Sys.Application.notifyScriptLoaded();
