﻿var GUITip = function(html){
	GUITip.superclass.constructor.call(this,html);
}
YAHOO.extend(GUITip,JTFrame,{
	timer:{
		interval:1500,
		timer:null,
		animation:null
	},
	persist:function(msg){
		if(this.timer.timer){
			clearTimeout(this.timer.timer);
			this.timer.timer = null;
		}
		this.panel.innerHTML = msg;
		this.setVisible(true);
	},
	print:function(msg){
		if(this.timer.timer != null){
			clearTimeout(this.timer.timer);
			this.timer.timer = null;
		}
		this.panel.innerHTML = msg;
		this.setVisible(true);
		var _this = this;
		this.timer.timer = setTimeout(function(){
			_this.hide();
		},this.timer.interval);
	},
	hide:function(){
		var _this = this;
		if(!this.timer.animation){
			this.timer.animation = new YAHOO.util.Anim(this.panel,{opacity:{from:1,to:0}},0.5,YAHOO.util.Easing.easeOut);
			this.timer.animation.onComplete.subscribe(
				function(){
					YAHOO.util.Dom.setStyle(_this.panel,"opacity",1);
					_this.setVisible(false);
				}
			);
		}
		this.timer.animation.animate();
		if(_this.timer != null){
			clearTimeout(_this.timer.timer);
			_this.timer.timer = null;
		}
	}
});
var JMessenger = function(){
	this.consoleWrapperUI = null;
	this.consoleUI = null;
	this.console = null;
	this.timer = {
		timer:null,
		start:0
	}
	this.defaultUIHeight = 0;
	this.defaultUIWidht = 0;
	this.effect = null;
	this.init();
	JMessenger.superclass.constructor.call(this,this.consoleWrapperUI);
}
YAHOO.extend(JMessenger,JTFrame,{
	init:function(){
		this.consoleWrapperUI = document.createElement("div");
		this.consoleWrapperUI.className = "miniconsole";
		document.body.appendChild(this.consoleWrapperUI);
		this.consoleUI = document.createElement("div");
		this.consoleUI.className = "miniconsolei t1";
		this.consoleWrapperUI.appendChild(this.consoleUI);
		this.console = document.createElement("div");
		this.consoleUI.appendChild(this.console);
		this.defaultUIHeight = this.consoleUI.offsetHeight;
		this.defaultUIWidth = this.consoleWrapperUI.offsetWidth;
	},
	notify:function(msg){
		this.console.innerHTML = msg;
		var D = YAHOO.util.Dom,_this=this;
		D.replaceClass(this.consoleUI,"t2","t1");
		this.setDisplay(true);
		this.timer.timer = setTimeout(function(){
			_this.setDisplay(false);
		},1000);
		this.timer.start = new Date().getTime();
	},
	warning:function(msg){
		this.console.innerHTML = msg;
		var D = YAHOO.util.Dom,_this=this;
		D.replaceClass(this.consoleUI,"t1","t2");
		this.setDisplay(true);
		this.timer.timer = setTimeout(function(){
			_this.setDisplay(false);
		},2000);
		this.timer.start = new Date().getTime();
	},
	persist:function(msg,callback){
		this.console.innerHTML = msg;
		var D = YAHOO.util.Dom,_this=this;
		D.replaceClass(this.consoleUI,"t2","t1");
		this.setDisplay(true);
		if(callback){
			this.timer.timer = setTimeout(function(){
				_this.console.innerHTML += "<strong style='color:red'>,时间似乎超过了预期，<a href='#' style='color:red;text-decoration:underline'>取消</a></strong>";
				_this.console.getElementsByTagName("a")[0].onclick = callback;
			},30000);
		}
		this.timer.start = new Date().getTime();
	},
	setDisplay:function(b){
		var _this = this;
		if(this.timer.timer){
			clearTimeout(this.timer.timer);
			this.timer.timer = null;
			this.timer.start = 0;
		}
		if(b){
			var a = new YAHOO.util.Anim(this.consoleUI,{marginTop:{from:-this.defaultUIHeight,to:0}},0.2,YAHOO.util.Easing.easeOut);
			a.onStart.subscribe(function(){
				_this.panel.style.display = "block";
			});
			a.animate();
		}else{
			var a = new YAHOO.util.Anim(this.consoleUI,{marginTop:{from:0,to:-this.defaultUIHeight}},0.2,YAHOO.util.Easing.easeOut);
			a.onComplete.subscribe(function(){
				_this.panel.style.display = "none";
			});
			a.animate();
		}
	}
});


var JOptionPanel = function(panel,dragable){
	JOptionPanel.superclass.constructor.call(this,panel);
	this._dragable = dragable && true;
	this.cancelListeners = [];
	this.okListeners = [];
	this.init();
}
JOptionPanel.ACTION_OK=0;
JOptionPanel.ACTION_CANCEL=1;
YAHOO.extend(JOptionPanel,JTFrame,{
	init:function(){
		var _this=this,D=YAHOO.util.Dom,E=YAHOO.util.Event;
		this.addComponentListener(function(mask){
			GUIAssistant.getCanvas().setVisible(mask == JTFrame.COMPONENT_SHOWN);
		});
		E.on(window,"resize",function(){
			_this.setLocation(
				(D.getViewportWidth()-_this.getSize()[0])/2,
				(D.getViewportHeight()-_this.getSize()[1]-200)/2
			);
		});
		this.setLocation(
			(D.getViewportWidth()-this.getSize()[0])/2,
			(D.getViewportHeight()-this.getSize()[1]-200)/2
		);
		if(this._dragable){
			var dd = new YAHOO.util.DD(this.panel,"dialog");
			dd.setHandleElId(this.panel.getElementsByTagName("h4")[1]);
		}
		//attach event handlers
		var cmds = this.panel.getElementsByTagName("input"),_this=this;
		for(var i=0,len=cmds.length;i<len;i++){
			if(cmds[i].type=="button"){
				if(cmds[i].className.toLowerCase().indexOf("ok") != -1){
					cmds[i].onclick = function(){
						_this.fireActionOK();
					}
				}else if(cmds[i].className.toLowerCase().indexOf("cancel") != -1){
					cmds[i].onclick = function(){
						_this.fireActionCancel();
					}
				}
			}
		}
		var cls = this.panel.getElementsByTagName("a");
		for(var i=0,len=cls.length;i<len;i++){
			if(cls[i].className.toLowerCase().indexOf("close")!=-1){
				cls[i].onclick = function(){
					_this.fireActionCancel();
				}
				break;
			}
		}
		E.on(this.panel,"keydown",function(e){
			if(!_this.isVisible())
				return;
			if(e.keyCode==13){//perform
				_this.fireActionOK();
			}
		});
		E.on(document,"keydown",function(e){
			if(!_this.isVisible())
				return;
			if(e.keyCode==27){
				_this.fireActionCancel();
			}
		});
	},
	fireActionOK:function(){
		for(var i=0,len=this.okListeners.length;i<len;i++){
			this.okListeners[i].call(this);
		}
	},
	fireActionCancel:function(){
		for(var i=0,len=this.cancelListeners.length;i<len;i++){
			this.cancelListeners[i].call(this);
		}
		if(this.isVisible())
			this.setVisible(false);
	},
	addActionListener:function(at,ah,el){
		var E=YAHOO.util.Event,_this=this;
		if(!YAHOO.lang.isFunction(ah))
			return;//invalid
		if(at==JOptionPanel.ACTION_OK){
			this.okListeners.push(ah);
			if(el){
				E.on(el,"click",function(){
					_this.fireActionOK();
				});
			}
		}else if(at==JOptionPanel.ACTION_CANCEL){
			this.cancelListeners.push(ah);
			if(el){
				E.on(el,"click",function(){
					_this.fireActionCancel();
				});
			}
		}
	},
	clear:function(){
		this.panel.getElementsByTagName("form")[0].reset();
	},
	fill:function(name,value){
		try{
			var f = this.panel.getElementsByTagName("form")[0];
			f[name].value = value;
			return true;
		}catch(e){
			return false;	
		}
	},
	getValue:function(key){
		try {
			var candidate = this.panel.getElementsByTagName("form")[0][key].value;
			return candidate?candidate:null;
		}catch (e) {
			return null;
		}
	},
	updateUI:function(m){
		var h4s = this.panel.getElementsByTagName("h4");
		h4s[0].innerHTML = m;
		h4s[1].innerHTML = m;
	}
});

var JConfirmDialog = function(panel){
	JConfirmDialog.superclass.constructor.call(this,panel);
	
	this._listener = null;
	this._exeConfirm = null;
	this._init();
}
YAHOO.extend(JConfirmDialog,JTFrame,{
	_init:function(){
		var _this = this;
		this.addComponentListener(function(mask){
			GUIAssistant.getCanvas().setVisible(mask == JTFrame.COMPONENT_SHOWN);
			if(_this._listener){
				_this._listener.call(this,mask);
			}
		});
		document.getElementById("jConfirmDialogClose").onclick = function(){
			_this.setVisible(false);
		}
		document.getElementById("jConfirmDialogCancel").onclick = function(){
			_this.setVisible(false);
		}
		document.getElementById("jConfirmDialogOK").onclick = function(){
			if(_this._exeConfirm)
				_this._exeConfirm.call(this);
			_this.setVisible(false);
		}
	},
	setMessage:function(msg){
		document.getElementById("jConfirmDialogMessage").innerHTML = msg;
	},
	setActionListener:function(l){
		if(typeof l != "function")
			return;
		this._exeConfirm = l;
	}
});

var JMessageBox = function(panel){
	JMessageBox.superclass.constructor.call(this,panel);
	this.messageContainer = null;
	this.init();
}
YAHOO.extend(JMessageBox,JTFrame,{
	init:function(){
		var _this = this;
		this.addComponentListener(function(mask){
			GUIAssistant.getCanvas().setVisible(mask == JTFrame.COMPONENT_SHOWN);
		});
		var anchor = this.panel.getElementsByTagName("a")[0];
		anchor.onclick = function(){
			_this.setVisible(false);
		}
		var ok = this.panel.getElementsByTagName("input")[0];
		ok.onclick = function(){
			_this.setVisible(false);
		}
	},
	setMessage:function(s){
		if(!s)
			return;
		if(!this.messageContainer){
			this.messageContainer = this.panel.getElementsByTagName("p")[0];
		}
		this.messageContainer.innerHTML = s;
	}
});

var GUIAssistant = {
	_canvas:null,
	_masks:{},
	_confirmDialog:null,
	_messageBox:null,
	_guiTip:null,
	_messenger:null,
	getCanvas:function(){
		if(this._canvas == null){
			var d = document.createElement("div"),D=YAHOO.util.Dom,E=YAHOO.util.Event,_this=this;
			document.body.appendChild(d);
			d.className = "canvas";
			D.setStyle(d,"width",D.getDocumentWidth()+"px");
			D.setStyle(d,"height",D.getDocumentHeight()+"px");
			this._canvas = new JTFrame(d);
			E.on(window,"resize",function(){
				_this._canvas.setSize(D.getDocumentWidth(),D.getDocumentHeight());
			});
		}
		return this._canvas;
	},
	getGUITip:function(panel){
		if(this._guiTip == null){
			var html = document.createElement("div"),_this=this,D=YAHOO.util.Dom,E=YAHOO.util.Event;
			document.body.appendChild(html);
			html.id = "guiTip";
			this._guiTip = new GUITip(html);
			this._guiTip.setLocation(((D.getViewportWidth()-this._guiTip.getSize()[0])/2),10);
			E.on(window,"resize",function(){
				_this._guiTip.setLocation(
					((D.getViewportWidth()-_this._guiTip.getSize()[0])/2),
					10
				);
			});
		}
		return this._guiTip;
	},
	getMessenger:function(){
		if(!this._messenger){
			this._messenger = new JMessenger(),_this=this,D=YAHOO.util.Dom,E=YAHOO.util.Event;
			this._messenger.setLocation(((D.getViewportWidth()-this._messenger.defaultUIWidth)/2),0);
			E.on(window,"resize",function(){
				_this._messenger.setLocation(
					((D.getViewportWidth()-_this._messenger.defaultUIWidth)/2),
					0
				);
			});
		}
		return this._messenger;
	},
	getMask:function(htmlTagId){
		if(typeof htmlTagId != "string")
			return;
		if(typeof this._masks[htmlTagId] == "undefined"){
			this._masks[htmlTagId] = new JTFrame(D.get(htmlTagId));
		}
		return this._masks[htmlTagId];
	},
	getConfirmDialog:function(){
		if(!this._confirmDialog){
			this._confirmDialog = new JConfirmDialog(document.getElementById("jConfirmDialog"));
			var D = YAHOO.util.Dom,E=YAHOO.util.Event,_this=this;
			this._confirmDialog.setLocation(
				(D.getViewportWidth()-this._confirmDialog.getSize()[0])/2,
				(D.getViewportHeight()-this._confirmDialog.getSize()[1])/2
			);
			E.on(window,"resize",function(){
				_this._confirmDialog.setLocation(
					(D.getViewportWidth()-_this._confirmDialog.getSize()[0])/2,
					(D.getViewportHeight()-_this._confirmDialog.getSize()[1])/2
				);
			});
		}
		return this._confirmDialog;
	},
	msgbox:function(s){
		if(!s)
			return;
		if(!this._messageBox){
			var panel = document.getElementById("jMessageBox");
			if(!panel)
				throw new Error("MessageBoxHTMLUINotFoundException");
			this._messageBox = new JMessageBox(panel);
			var D = YAHOO.util.Dom,E=YAHOO.util.Event,_this=this;
			this._messageBox.setLocation(
				(D.getViewportWidth()-this._messageBox.getSize()[0])/2,
				300
			);
			E.addListener(window,"resize",function(){
				_this._messageBox.setLocation(
					(D.getViewportWidth()-_this._messageBox.getSize()[0])/2,
					300
				);
			});
		}
		this._messageBox.setMessage(s);
		if(!this._messageBox.isVisible())
			this._messageBox.setVisible(true);
	}
}
