_class("MiniDialog", "", function(){
	var KEY_ESC       = 27;
	this._init = function(){
		//对话框的双态特性，和PaneAppContent相似，主要用来屏蔽环境差异
		this._head = null;
		this._body = null;
		this._btnClose = null;
		this._caption = "对话框标题";
		this._self = null;
		this._doc = document;
		this._tpl = null;
		this._shadowSelf = null;
		this._fnselectMove = {};
	};
	this.create = function(_tpl_){
		this._tpl = _tpl_;
		var obj = document.createElement("div");
		obj.className = "wui-Dialog";
		obj.style.position = "absolute";
		this.init(obj);
		this.setBodyContent(this._tpl);
		obj.style.zIndex = 100;
		document.body.appendChild(obj);
		this.createShadow();
		document.body.appendChild(this._shadowSelf);
	};
	this.init = function(obj){
		this._self = obj;
		this._head = this._doc.createElement("div");
		this._head.className = "head";
		var label = this._doc.createElement("label");
		label.appendChild(this._doc.createTextNode("对话框标题"));
		this._head.appendChild(label);
		var link = this._doc.createElement("a");
		link.href = "#dlg_close";
		link.title = "关闭";
		this._btnClose = this._head.appendChild(link);
		this._self.appendChild(this._head);
		this._body = this._doc.createElement("div");
		this._body.className = "body";
		this._self.appendChild(this._body);
		this._containerNode = this._body;  //更改容器节点
		this._head._dlg = this;
		this._btnClose._dlg = this;
		this._btnClose.onselectstart = function(ev){return false;};
		this._btnClose.ondragstart = function(ev){return false;};
		this._btnClose.onclick = function(ev){return false;};
		this._btnClose.onmousedown = function(ev){
			ev = ev || window.event;
			this.onmouseup = function(ev){
				ev = ev || window.event;
				var target = ev.srcElement || ev.target;
				this.onmouseup = null;
				if(target == this){
					this._dlg.close();  //关闭对话框
				}
			};
			ev.cancelBubble = true;
		};
		this.moveDlg();
	};
	this.dispose = function(){
		if(this._disposed) return;
		this._shadowSelf = null;
		if(this._contentPane_flag){
			this._contentPane.dispose();
		}
		if(this._actionManager){
			this._actionManager.dispose();
			this._actionManager = null;
		}
		this._contentPane = null;
		this._btnClose.onmousedown = null;
		this._btnClose.onclick = null;
		this._btnClose.ondragstart = null;
		this._btnClose.onselectstart = null;
		this._btnClose._dlg = null;
		this._btnClose = null;
		this._body = null;
		this._head.onselectstart = null;
		this._head.onmousedown = null;
		this._head._dlg = null;
		this._head = null;
		this._app = null;
		this._ownerApp = null;
	};
	this.close = function(){
		try{
			if(this.onClose) this.onClose();
		}catch(ex){
		}
		if(this.doAction){
			this.doAction("cancel", this._btnClose);
		}
		this._shadowSelf.style.display = "none";
	};
	this.setCaption = function(v){
		this._caption = v;
		if(this._self){
			this._head.childNodes[0].innerHTML = this._caption;
		}
	};
	this.setBodyContent = function(html){
		this._body.innerHTML = html;
	};
	this.onKeyUp = function(ev){
		switch(ev.keyCode){
		case KEY_ESC:
			this.close();  //关闭对话框
			break;
		}
	};
	this.doAction = function(action, sender){
		switch(action){
		case "cancel": this.do_dlg_cancel(action, sender);break;
		default: return this._app.doAction.apply(this._app, arguments);
		}
		return false;
	};
	this.moveTo = function(x, y){
		this._self.style.left = x + "px";
		this._self.style.top = y + "px";
	};
	this.moveToCenter = function(){
		var viewSize = this.getViewSize();
		var x = (viewSize.w-50)/2 - (this._self.clientWidth-50)/2;
		var y = (viewSize.h- 50)/2 - (this._self.clientHeight-50)/2;
		this.moveTo(x, y);
	};
	this.getViewSize = function(){
		return {"w": Math.min(document.body.clientWidth, document.documentElement.clientWidth),
			    "h": Math.min(document.body.clientHeight, document.documentElement.clientHeight)}
	};
	this.do_dlg_cancel = function(action, sender){
		document.body.removeChild(this._self);
	};
	this.reset = function(data){
		var form = document.forms["privatemsg"];
		for(var k in data){
			if(form[k]){
				form[k].value = data[k];
			}
		}
		form.user_name.disabled = true;
		this._shadowSelf.style.display = "";
	};
	this.createShadow = function(){
		this._shadowSelf = this._doc.createElement("div");
		this._shadowSelf.className = "wui-ModalPanel";
		this._shadowSelf.style.opacity = "0.2";
		if(isIE()){
			this._shadowSelf.style.filter = "Alpha(Opacity=20)";
			this._iframe = this._doc.createElement("iframe");
			this._iframe.setAttribute("scrolling", "no");
			this._iframe.setAttribute("frameBorder", "0");
			this._iframe.setAttribute("frameSpacing", "0");
//			this._iframe.setAttribute("allowTransparency", "true");
//			this._iframe.style.display = "none";
			this._iframe.style.width = "100%";
			this._iframe.style.height = "100%";
			this._iframe.style.zIndex = 50;
			this._iframe.src = "about:blank";
			this._shadowSelf.appendChild(this._iframe);
		}
		this._shadowSelf.style.position = "absolute";
		this._shadowSelf.style.left = "0px";
		this._shadowSelf.style.top = "0px";
		this._shadowSelf.style.width = document.body.clientWidth + "px";
		this._shadowSelf.style.height = Math.max(
			document.body.offsetHeight
			, document.body.scrollHeight
			, document.body.clientHeight
			, document.documentElement.scrollHeight
			, document.documentElement.offsetHeight
			, document.documentElement.clientHeight
			) + "px";
		this._shadowSelf.style.zIndex = 50;
	};
	this.comPos = function(_left, _top, moveOffsetX, moveOffsetY){
		var _moveOffsetX;
		var _moveOffsetY;
		if(_left + this._self.offsetWidth + moveOffsetX >= parseInt(document.body.clientWidth) - 2)
			_moveOffsetX = parseInt(document.body.clientWidth)-this._self.offsetWidth-2;
		else if(_left+moveOffsetX < 0)
			_moveOffsetX= 0;
		else
			_moveOffsetX = _left+moveOffsetX;
		//竖直方向也不能移动出去
		if(_top + this._self.offsetHeight + moveOffsetY >= parseInt(this._shadowSelf.style.height) - 2)
			_moveOffsetY = parseInt(this._shadowSelf.style.height)-this._self.offsetHeight-2;
		else if(_top+moveOffsetY < 0)
			_moveOffsetY = 0;
		else
			_moveOffsetY = _top+moveOffsetY;
		this.moveTo(_moveOffsetX, _moveOffsetY);
	}
	this.moveDlg = function(){
		var _this = this;
		this.hookEvent(this._head, "mousedown", function(ev){
			_this.addEvent(ev, _this._head, _this.fnStop);
			var _x = ev.clientX;
			var _y = ev.clientY;
			var _left = parseInt(_this._self.style.left);
			var _top = parseInt(_this._self.style.top);
			_this._fnselectMove = function(ev){
				window.getSelection ? window.getSelection().removeAllRanges():document.selection.empty();
				var moveOffsetX = parseInt(ev.clientX) - parseInt(_x);
				var moveOffsetY = parseInt(ev.clientY) - parseInt(_y);
				_this.comPos(_left, _top, moveOffsetX, moveOffsetY);
			};
			_this.hookEvent(document, "mousemove", _this._fnselectMove);
		});
		this.hookEvent(document, "mouseup", _this.fnStop = function(ev){
			if(typeof(_this)!="undefined" && ev.srcElement != ""){
				_this.removeEvent(ev, _this._head, _this.fnStop);
			}
			_this.unhookEvent(document, "mousemove",_this._fnselectMove);
		});
	}
	//兼容事件的操作Compatible Event之绑定
	this.addEvent = function(ev, obj ,callback){
		if(obj.setCapture){//IE
			this.hookEvent(obj, "losecapture", callback);
			obj.setCapture();
			ev.cancelBubble = true; //IE
		}else if(window.captureEvents){//标准DOM
			ev.stopPropagation();
			this.hookEvent(window, "blur", this.fnStop);
			ev.preventDefault();
		}
	};
	//兼容事件的操作Compatible Event之删除
	this.removeEvent = function(ev, obj, callback){
		if(typeof(obj) != "undefined"){
			if(obj.releaseCapture){//IE
				this.unhookEvent(obj, "losecapture", callback);
				obj.releaseCapture();
			}
		}else if(window.releaseEvents){//标准DOM
			this.unhookEvent(window, "blur", this.fnStop);
		}
	};
	this.hookEvent = function(element, eventName, callback){
		if(typeof(element) != "undefined"){
			if(element.addEventListener){
				element.addEventListener(eventName, callback, false);
			}else if(element.attachEvent){
				element.attachEvent("on" + eventName, callback);
			}
		}
	};
	this.unhookEvent = function(element, eventName, callback){
		if(typeof(element) != "undefined"){
			if(element.removeEventListener){
				element.removeEventListener(eventName, callback, false);
			}else if(element.detachEvent){
				element.detachEvent("on" + eventName, callback);
			}
		}
	};
});