$package("js.ui");


js.ui.BaseWindow = function (){
}
js.ui.BaseWindow.prototype = new js.ui.Container();

js.ui.BaseWindow.prototype.borderWidth = 20;
js.ui.BaseWindow.prototype.state=1;
js.ui.BaseWindow.prototype.NORMAL=1;
js.ui.BaseWindow.prototype.MINIMIZE=2;
js.ui.BaseWindow.prototype.MAXIMIZE=3;
js.ui.BaseWindow.prototype.transparentBorder = true;
js.ui.BaseWindow.prototype.optimizeDisplay = true;


js.ui.BaseWindow.prototype.getNativeClass = function (){
	return "js.ui.BaseWindow";
}

js.ui.BaseWindow.prototype.extendUI = function (){
}

js.ui.BaseWindow.prototype.bindUI = function (){
	if(browser.isIE55_6){
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[0].cells[0]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[0].cells[1]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[0].cells[2]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[0].cells[3]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[0].cells[4]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[0].cells[5]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[0].cells[6]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[1].cells[0]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[1].cells[2]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[2].cells[0]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[2].cells[1]);
		js.util.Utils.prototype.correctPNGBackground(this.ui.rows[2].cells[2]);
	}
	var __this = this;
	this.titleRow = new js.ui.Component();
	this.titleRow.ui = this.ui.rows[0];
	this.bodyRow = new js.ui.Component();
	this.bodyRow.ui = this.ui.rows[1];
	this.bottomRow = new js.ui.Component();
	this.bottomRow.ui = this.ui.rows[2];
	this.titleText = this.ui.rows[0].cells[2].getElementsByTagName("span")[0];
	this.iconCell = this.ui.rows[0].cells[1];
	this.closeCell = this.ui.rows[0].cells[5];
	this.container = this.ui.getElementsByTagName("div")[3];
	this.closeButton = new js.ui.ImageButton();
	this.closeCell.appendChild(this.closeButton.getUI());
	this.closeButton.setImage(imagePath+'window/closewindow.gif');
	this.closeButton.mouseoverImage = imagePath+'window/closewindow_mouseover.gif';
	this.closeButton.mousedownImage = imagePath+'window/closewindow_mousedown.gif';
	this.closeButton.getUI().onclick = function(){
		__this.close();
	}
	if(this.minButton)
		this.titleRow.getUI().ondblclick  = function (event){
			if(!event)event =  window.event;
			var x = event.clientX ? event.clientX : event.pageX;
			if(x<__this.getOffsetLeft()+20+__this.borderWidth){
				__this.close();
			}
			else if(__this.resizeable){
				if(__this.state == __this.NORMAL)
					__this.maximize();
				else {
					__this.normal();
				}
			}

		};	
	this.setResizeable(true);
	this.setMoveable(true);
	this.extendUI();
	browser.sendMessage(null, 1, null, this);//WM_CREATE:1
}

js.ui.BaseWindow.prototype.setMoveable = function(enable_move){
	this.__superSetMoveable = js.ui.Component.prototype.setMoveable;
	this.__superSetMoveable(enable_move);
	if(enable_move){
		this.getUI().onmousedown = js.ui.BaseWindow.__onmousedown;
	}
	else{
		this.getUI().onmousedown = null;
	}
}

js.ui.BaseWindow.__onmousedown = function (event){
	var __this = this.owner;
	__this.setActive(true);
	if(!event)event =  window.event;
	this.__superonmousedown = js.ui.Component.__onmousedown;
	this.__superonmousedown(event);
	var x = event.clientX ? event.clientX : event.pageX;
	var y = event.clientY ? event.clientY : event.pageY;
	if(__this.__isDrag){
		if(y>__this.titleRow.getOffsetTop(null, true)+__this.titleRow.getUI().offsetHeight)
			__this.__isDrag = false;
	}
	browser.sendMessage(null, 201, event, window);//WM_ONMOUSEDOWN(WM_LBUTTONDOWN):201
}

/**
 * set the Icon of title
 * @param  {string} url
 */

js.ui.BaseWindow.prototype.setIcon = function (url, w, h){
	if(!this.ui) this.getUI();
	if(!this.icon){
		this.icon = new js.ui.Img();
		this.iconCell.appendChild(this.icon.getUI());
	}
	if(!w) w= 16;
	if(!h) h = 16;
	this.icon.setSrc(url, w, h);
	browser.sendMessage(null, 74, url, this);//WM_SETICON:12
}

js.ui.BaseWindow.prototype.title = "";
js.ui.BaseWindow.prototype.icon = null;
/**
 * set the title of the form
 * @param  {string} title
 */
js.ui.BaseWindow.prototype.setTitle = function (title){
	this.title = title;
    this.titleText.innerHTML = title;
	browser.sendMessage(null, 12, title, this);//WM_SETTEXT:12
}

js.ui.BaseWindow.prototype.showModal = function (){
	if(!this.__modalDiv){
		this.__modalDiv = new js.ui.Div();
		document.body.appendChild(this.__modalDiv.getUI());
		this.__modalDiv.ui.style.position = "absolute";
		this.__modalDiv.ui.style.background = "black";
		this.__modalDiv.setOpacity(10);
	}
	this.__modalDiv.setBounds(0, 0, document.body.offsetWidth, document.body.offsetHeight);
	this.__modalDiv.show();
	this.__oldzIndex = this.ui.style.zIndex;
	this.show();
	this.setActive(true);
	this.__modalDiv.ui.style.zIndex = this.ui.style.zIndex;
	this.ui.style.zIndex = this.ui.style.zIndex+1;
}

/**
 * close the form
 */
js.ui.BaseWindow.prototype.close = function (){
  if(this.onclose)
	  if(!this.onclose()) return;
  this.state = this.HIDE;
  if(this.__modalDiv)
	  this.__modalDiv.hide();
  if(this.__oldzIndex)
	this.getUI().style.zIndex = this.__oldzIndex;
  this.__oldResizeDelay = this.resizeDelay;
  this.hide();
  this.setActive(false);
  browser.sendMessage(null, 16, null, this);//WM_CLOSE:16
  if(this.destoryOnClose)
	this.destory();
}
/**	
 * destory the form
 */
js.ui.BaseWindow.prototype.destory = function (){

}

/**
 * change the form to the minimize size
 */
js.ui.BaseWindow.prototype.minimize = function (){
	this.__oldState = this.state;
	if(browser.taskProcessBar){
		this.hide();
	}
	else{
		if(this.state==this.NORMAL){
			this.oldLeft        = this.getUI().offsetLeft;
			this.oldTop         = this.getUI().offsetTop;
			this.oldWidth       = this.getUI().offsetWidth;
			this.oldHeight      = this.getUI().offsetHeight;
		}
		this.getUI().style.height = this.titleRow.getUI().offsetHeight;
		this.bodyRow.hide();
		this.bottomRow.hide();
		this.getUI().style.width = 100;
		if(this.oldLeft){
			this.getUI().style.left        = this.oldLeft;
			this.getUI().style.top         = this.oldTop;
		}
	}
	this.state = this.MINIMIZE;
	this.setActive(false);
}
/**
 * reset state of the form 
 */
js.ui.BaseWindow.prototype.reset = function (){
	if(this.__oldState==this.NORMAL){
		this.normal();
	}
	else if(this.__oldState==this.MAXIMIZE){
		this.maximize();
	}
	if(browser.taskProcessBar){
		this.show();
	}
}

/**
 * change the form to the Maximize size
 */
js.ui.BaseWindow.prototype.maximize = function (){
	if(this.state==this.NORMAL){
		this.oldLeft        = this.getUI().offsetLeft;
		this.oldTop         = this.getUI().offsetTop;
		this.oldWidth       = this.getUI().offsetWidth;
		this.oldHeight      = this.getUI().offsetHeight;
	}
	if(this.bodyRow.getUI().style.display =='none'){
		this.bodyRow.show();
		this.bottomRow.show();
	}
	if(browser.taskProcessBar){
		this.setVisible(true);
		this.getUI().style.height = document.body.clientHeight-36;
		this.getUI().style.top   = 36;//Browser.TaskProcessBar.offsetHeight;
	}
	else{
		this.getUI().style.top    = 0;
		this.getUI().style.height = "100%";
	}
	this.getUI().style.width  = "100%";
	this.getUI().style.left   = 0;
	this.state        = this.MAXIMIZE;
}

/**
 * change the form to the Normal size
 */
js.ui.BaseWindow.prototype.normal = function (){
	this.show();
	if(this.bodyRow.getUI().style.display == 'none'){
		this.bodyRow.show();
		this.bottomRow.show();
	}
	if(this.oldWidth){
		this.getUI().style.left    = this.oldLeft;
		this.getUI().style.top     = this.oldTop;
		this.getUI().style.width   = this.oldWidth;
		this.getUI().style.height  = this.oldHeight;
	}
	this.state = this.NORMAL;
}

js.ui.BaseWindow.prototype.setActive = function (active){
	if(this.active == active) return;
	this.active = active;
	if(active){
		for(var i=0; i<browser.windows.length; i++){
			if(this!=browser.windows[i])
				browser.windows[i].setActive(false);
		}
		this.getUI().style.zIndex = 100;
		this.titleRow.setOpacity(null);
		this.bottomRow.setOpacity(null);
		this.titleText.className = null;
		browser.sendMessage(null, 6, null, this);//WM_ACTIVE:6
		if(this.onactive) this.onactive();
	}
	else{
		if(this.getUI().style.zIndex>2)
			this.getUI().style.zIndex = this.getUI().style.zIndex-1;
		else
			this.getUI().style.zIndex = 1;
		var opacity = 70;
		this.titleRow.setOpacity(opacity);
		this.bottomRow.setOpacity(opacity);
		this.titleText.className = "inactive";
		browser.sendMessage(null, 801, null, this);//WM_INACTIVE:801
		if(this.oninactive) this.oninactive();
	}
}

js.ui.Window = function (parent){
}

js.ui.Window.prototype = new js.ui.BaseWindow();


js.ui.Window.prototype.extendUI = function (){
	var __this = this;
	this.minCell = this.ui.rows[0].cells[3];
	this.minButton = new js.ui.ImageButton();
	this.minCell.appendChild(this.minButton.getUI());
	this.minButton.setImage(imagePath+'window/minwindow.gif');
	this.minButton.mouseoverImage = imagePath+'window/minwindow_mouseover.gif';
	this.minButton.mousedownImage = imagePath+'window/minwindow_mousedown.gif';
	this.minButton.getUI().onclick = function(){
		__this.minimize();
		__this.setActive(true);
	}

	this.maxCell = this.ui.rows[0].cells[4];
	this.maxButton = new js.ui.ImageButton();
	this.maxCell.appendChild(this.maxButton.getUI());
	this.maxButton.setImage(imagePath+'window/maxwindow.gif');
	this.maxButton.mouseoverImage = imagePath+'window/maxwindow_mouseover.gif';
	this.maxButton.mousedownImage = imagePath+'window/maxwindow_mousedown.gif';
	this.maxButton.getUI().onclick = function(){
		__this.maximize();
		__this.setActive(true);
	}
}

/**
 * change the WebForm to the Minimize size
 */
js.ui.Window.prototype.minimize = function (){
	var __this = this;
	this.maxButton.setImage(imagePath+'window/resetwindow.gif');
	this.maxButton.mouseoverImage     = imagePath+'window/resetwindow_mouseover.gif';
	this.maxButton.mousedownImage     = imagePath+'window/resetwindow_mousedown.gif';
	this.maxButton.getUI().onclick = function(){__this.reset();__this.setActive(true);};
	this.__superMinimize = js.ui.BaseWindow.prototype.minimize;
	this.__superMinimize();
}

/**
 * change the WebForm to the Maximize size
 */
js.ui.Window.prototype.maximize = function (){
	var __this = this;
	this.maxButton.setImage(imagePath+'window/resetwindow.gif');
	this.maxButton.mouseoverImage = imagePath+'window/resetwindow_mouseover.gif';
	this.maxButton.mousedownImage = imagePath+'window/resetwindow_mousedown.gif';
	this.maxButton.getUI().onclick = function(){__this.normal();__this.setActive(true);};
	this.__superMaximize = js.ui.BaseWindow.prototype.maximize;
	this.__superMaximize();
}

/**
 * change the WebForm to the Normal size
 */
js.ui.Window.prototype.normal = function (){
	var __this = this;
	this.maxButton.setImage(imagePath+'window/maxwindow.gif');
	this.maxButton.mouseoverImage = imagePath+'window/maxwindow_mouseover.gif';
	this.maxButton.mousedownImage = imagePath+'window/maxwindow_mousedown.gif';
	this.maxButton.getUI().onclick = function(){__this.maximize();__this.setActive(true);};
	this.__superNormal = js.ui.BaseWindow.prototype.normal;
	this.__superNormal();
}
