/**
 * @namespace com.thesis.control.CommonDialog
 * @author 钟军锐 August.R@263.net
 */

/** @id CommonDialog */
function CommonDialog(sTitle, oBody, bLock, bClose){
	this.TitleString = sTitle;
	//this.Body = oBody.cloneNode(true);
	//oBody.parentNode.removeChild(oBody);
	this.Body = oBody;
	this.bLock = bLock;
	this.bClose = bClose;
	this.Form = C$("Div");
	this.Background = C$("Div");
	this.Frame = C$("Div");
	
	this.Symbol = C$("Div");
	this.BusySymbol = C$("Div");
	this.BusyImage = C$("img");
	this.BusyImage.src = "../img/Trade/sLoadinfo.gif";
	this.BusySymbol.appendChild(this.BusyImage);
	this.BusyMessage = C$("Div");
	this.BusySymbol.appendChild(this.BusyMessage);
	this.Symbol.appendChild(this.BusySymbol);
	
	this.ExceptionSymbol = C$("Div");
	this.ExceptionImage = C$("img");
	this.ExceptionImage.src = "../img/Trade/Error_Red.png";
	this.ExceptionSymbol.appendChild(this.ExceptionImage);
	this.ExceptionMessage = C$("Div");
	this.ExceptionSymbol.appendChild(this.ExceptionMessage);
	this.ExceptionButton = C$("Input");
	this.ExceptionButton.value = "返回";
	this.ExceptionButton.type = "button";
	this.ExceptionButton.style.marginBottom = "1em";
	this.ExceptionSymbol.appendChild(this.ExceptionButton);
	this.Symbol.appendChild(this.ExceptionSymbol);
	
	this.MessageSymbol = C$("Div");
	this.MessageImage = C$("img");
	this.MessageImage.style.marginTop = "20px";
	this.MessageImage.src = "../img/Trade/Message.png";
	this.MessageSymbol.appendChild(this.MessageImage);
	this.MessageArea = C$("Div");
	this.MessageArea.style.margin = "1em 0 2em 0";
	this.MessageSymbol.appendChild(this.MessageArea);
	this.MessageButton = C$("Input");
	this.MessageButton.value = "确定";
	this.MessageButton.type = "button";
	this.MessageButton.style.marginBottom = "1em";
	this.MessageSymbol.appendChild(this.MessageButton);
	this.Symbol.appendChild(this.MessageSymbol);
	
	this.TitleBar = C$("h3");
	this.TitleBar.appendChild(T$(sTitle));
	
	this.CloseButton = C$("Div");
	this.CloseButton.appendChild(T$("X"));
	
	this.Filter = new PauseControl();
	this.Draggable = null;
	this.Shadow = new Shadow(this.Form, 0);
	
	this.AutoClose = false;
	this.AutoLocate = true;
	
	if(typeof CommonDialog._initialized == "undefined"){
		CommonDialog._initialized = true;
		
		/** @id locate */
		CommonDialog.prototype.locate = function(){
			if(!this.AutoLocate) return ;
			var x = document.documentElement.scrollLeft || document.body.scrollLeft;
			var y = document.documentElement.scrollTop || document.body.scrollTop;
			var w = document.documentElement.clientWidth || document.body.clientWidth;
			var h = document.documentElement.clientHeight || document.body.clientHeight;
			var fw = this.Form.clientWidth;
			var fh = this.Form.clientHeight;
			this.Form.style.left = x + (w-fw>0?w-fw:0)*2/5 + "px";
			this.Form.style.top = y + (h-fh>0?h-fh:0)/3 + "px";
		};
		
		/** @id show */
		CommonDialog.prototype.show = function(){
			if(this.bLock)
				this.Filter.show();
			this.Shadow.setOpacity(0.01);
			this.locate();
			uEvent.Listener.notify(this, "show");
			this.Shadow.display(1,100);
		};
		
		/** @id hide */
		CommonDialog.prototype.hide = function(){
			if(this.bLock)
				this.Filter.hide();
			uEvent.Listener.notify(this, "hide");
			this.Shadow.display(0,300);
		};
		
		/** @id ask */
		CommonDialog.prototype.ask = function(){
			uEvent.Listener.notify(this, "asking");
			this.Body.style.display = "block";
			this.Symbol.style.display = "none";
			this.show();
		};
		
		/** @id busy */
		CommonDialog.prototype.busy = function(sMsg){
			if((typeof sMsg == "string") || sMsg.toString()){
				this.BusyMessage.innerHTML = sMsg;
			}
			else{
				this.BusyMessage.innerHTML = "";
			}
			this.Body.style.display = "none";
			this.Symbol.style.display = "block";
			this.BusySymbol.style.display = "block";
			this.ExceptionSymbol.style.display = "none";
			this.MessageSymbol.style.display = "none";
			this.show();
		};
		
		/** @id exception */
		CommonDialog.prototype.exception = function(sMsg, bClose){
			if((typeof sMsg == "string") || sMsg.toString()){
				this.ExceptionMessage.innerHTML = sMsg;
			}
			else{
				this.ExceptionMessage.innerHTML = "";
			}
			this.Body.style.display = "none";
			this.Symbol.style.display = "block";
			this.BusySymbol.style.display = "none";
			this.ExceptionSymbol.style.display = "block";
			this.MessageSymbol.style.display = "none";
			if(bClose) this.AutoClose = true;
			else this.AutoClose = false;
			this.show();
		};
		
		/** @id message */
		CommonDialog.prototype.message = function(sMsg){
			if((typeof sMsg == "string") || sMsg.toString()){
				this.MessageArea.innerHTML = sMsg;
			}
			else{
				this.MessageArea.innerHTML = "";
			}
			this.Body.style.display = "none";
			this.Symbol.style.display = "block";
			this.BusySymbol.style.display = "none";
			this.ExceptionSymbol.style.display = "none";
			this.MessageSymbol.style.display = "block";
			this.show();
		};
		
		/** @id initialize */
		CommonDialog.prototype.initialize = function(Root){
			
			this.Background.className = "fM";
			this.Frame.className = "sM";
			this.CloseButton.className = "X";
			
			this.Form.style.width = this.Body.style.width;
			this.Form.style.zIndex = 251;
			
			this.BusySymbol.style.display = "none";
			this.BusyImage.style.marginTop = "12px";
			this.BusyMessage.style.lineHeight = "32px";
			this.BusyMessage.style.verticalAlign = "middle";
			this.BusyMessage.style.marginLeft = "6px";
			
			this.ExceptionSymbol.style.display = "none";
			this.ExceptionImage.style.marginTop = "12px";
			this.ExceptionMessage.style.lineHeight = "32px";
			this.ExceptionMessage.style.verticalAlign = "middle";
			this.ExceptionMessage.style.marginLeft = "6px";
			
			this.Form.appendChild(this.Background);
			this.Background.appendChild(this.Frame);
			if(this.bClose)
				this.Frame.appendChild(this.CloseButton);
			this.Frame.appendChild(this.TitleBar);
			this.Frame.appendChild(this.Body);
			this.Frame.appendChild(this.Symbol);
			
			document.body.appendChild(this.Form);
			
			this.Draggable = new Draggable(this.Form, Draggable.DRAG_Both, this.TitleBar);
			
			uEvent.addBrowserHandler(this.CloseButton, "click", function(){
				Root.hide();
			});
			
			uEvent.addBrowserHandler(this.CloseButton, "mouseover", function(){
				Root.CloseButton.className = "X Active";
			});
			
			uEvent.addBrowserHandler(this.CloseButton, "mouseout", function(){
				Root.CloseButton.className = "X";
			});
			
			uEvent.addBrowserHandler(this.ExceptionButton, "click", function(){
				if(Root.AutoClose){
					Root.AutoClose = false;
					Root.hide();
				}
				else{
					Root.ask();
				}
			});
			uEvent.addBrowserHandler(this.MessageButton, "click", function(){
				Root.hide();
			});
		};
		
	}
	
	this.initialize(this);
	
}

