﻿// JavaScript Document
// Jfp 08-12-15
// Dialog

var Class = { //定义通用类创建对象
	create: function() {
			return function() {
				return this.initialize.apply(this, arguments);
			}
	}
}

Object.extend = function(destination, source) { //对象原型扩展
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0, length = iterable.length; i < length; i++)
      results.push(iterable[i]);
    return results;
  }
}

function $C(tag) { //创建一个页面元素tag
	return document.createElement(tag);
}

function $To(o,p) { //把o添加到p对象中
	if(o&&p){
		p.appendChild(o);
		return o;
	}
	else{
		return false;
	}
}

var __$EventName = new function(){
	var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
	this.mousemove   = isIE?'onmousemove': 'mousemove';
	this.mouseup     = isIE?'onmouseup': 'mouseup';
};

var arrIndexOf = function(arr, obj) { //扩展Array
    for(var i = 0; i < arr.length; i++) { 
        if (arr[i] === obj) { 
            return i; 
        } 
    } 
    return -1;
};


var arrRemove = function(arr, obj) {
    var index = arrIndexOf(arr, obj);   

    if (index !== -1) {   
        arr.splice(index, 1);   
    }
};

if(window.HTMLElement){
	HTMLElement.prototype.__defineGetter__("children", 
     function () { 
         var returnValue = new Object(); 
         var number = 0; 
         for (var i=0; i<this.childNodes.length; i++) { 
             if (this.childNodes[i].nodeType == 1) { 
                 returnValue[number] = this.childNodes[i]; 
                 number++; 
             } 
         } 
         returnValue.length = number; 
         return returnValue; 
     } 
 	);
}

Function.prototype.bind = function() { //将方法绑定到指定对象上执行
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}
Function.prototype.bindWithEvent = function(object) { //将方法绑定到指定对象上执行，并传递Event对象
  var __method = this, args = $A(arguments), object = args.shift();
  return function(event) {
    return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments)));
  }
}

function $attachEvent(obj,eventName,oListener){ //绑定事件监听方法
    if(obj.attachEvent){
        obj.attachEvent(eventName,oListener);
    }
    else if(obj.addEventListener){
        obj.addEventListener(eventName,oListener, false);
    }
    else{
        return false;
    }
}
function $detachEvent(obj,eventName,oListener){ //删除事件监听方法
    if(obj.detachEvent){
        obj.detachEvent(eventName,oListener);
    }
    else if(obj.removeEventListener){
        obj.removeEventListener(eventName,oListener, false);
    }
    else{
        return false;
    }
}

function isNumber(v) {
	if(!v) return false;
	return !isNaN(v);
}


var Dialog = Class.create();
Object.extend(Dialog.prototype, {
	//title: 标题
	//width: 对话框宽度
	//height: 对话框高度
	initialize : function(title, width, height) { //构造函数
		this.tab = [];
		this.zIndex = 100;
		this.resizeBut = [];
		this.canMini = true;
		this.canClose = true;
		this.miniKeepWidth = true; //最小化时是否保留对话框宽度
		this.left = this.minL = 0; this.top = this.minT = 0;
		this.dialogWidth = this.minW = 120; this.dialogHeight = this.minH = 60;
		this.outDialog();
		if(typeof(title) == "string") this.setTitle(title);
		if(typeof(width) == "number") this.dialogWidth = width;
		if(typeof(height) == "number") this.dialogHeight = height;
		this.setDialogSize(this.dialogWidth,this.dialogHeight);
	},
	outDialog : function() { //输出对话框主体部分
		var div,table,tr,td,title,tab,mainBody;
		
		div = $C("div"); div.className = "dialogDiv"; this.div = div; //主DIV
		$To(div,document.body);
		
		//添加Dialog表格
		table = $C("table");
		table.border = 0; table.cellPadding = 0; table.cellSpacing = 0;
		
		tr = table.insertRow(-1); //上边框
		td = tr.insertCell(-1); td.className = "dialogB1"; td.rsCur = "nw-resize"; this.resizeBut[0] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 1; this.cT = 1; this.cW = -1; this.cH = -1;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		td = tr.insertCell(-1); td.className = "dialogB2"; td.rsCur = "n-resize"; this.resizeBut[1] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 0; this.cT = 1; this.cW = 0; this.cH = -1;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		td = tr.insertCell(-1); td.className = "dialogB3"; td.rsCur = "ne-resize"; this.resizeBut[2] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 0; this.cT = 1; this.cW = 1; this.cH = -1;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		
		tr = table.insertRow(-1); //标题栏
		td = tr.insertCell(-1); td.className = "dialogB4"; td.rsCur = "w-resize"; this.resizeBut[3] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 1; this.cT = 0; this.cW = -1; this.cH = 0;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		title = tr.insertCell(-1); title.vAlign = "top"; title.className = "dialogTitle";
		td = tr.insertCell(-1); td.className = "dialogB5"; td.rsCur = "e-resize"; this.resizeBut[4] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 0; this.cT = 0; this.cW = 1; this.cH = 0;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		
		tr = table.insertRow(-1); this.dialogBodyLine = tr; //窗体主体
		td = tr.insertCell(-1); td.className = "dialogB6"; td.rsCur = "w-resize"; this.resizeBut[5] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 1; this.cT = 0; this.cW = -1; this.cH = 0;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		td = tr.insertCell(-1);
		mainBody = $To($C("div"),td); mainBody.className = "dialogBg"; mainBody.vAlign = "top"; this.mainBody = mainBody; //mainBody
		td = tr.insertCell(-1); td.className = "dialogB7"; td.rsCur = "e-resize"; this.resizeBut[6] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 0; this.cT = 0; this.cW = 1; this.cH = 0;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		
		tr = table.insertRow(-1); //底边框
		td = tr.insertCell(-1); td.className = "dialogB8"; td.rsCur = "sw-resize"; this.resizeBut[7] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 1; this.cT = 0; this.cW = -1; this.cH = 1;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		td = tr.insertCell(-1); td.className = "dialogB9"; td.rsCur = "s-resize"; this.resizeBut[8] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 0; this.cT = 0; this.cW = 0; this.cH = 1;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		td = tr.insertCell(-1); td.className = "dialogB10"; td.rsCur = "se-resize"; this.resizeBut[9] = td;
		td.onmousedown = function(event){
			if(!this.canResize) return;
			this.cL = 0; this.cT = 0; this.cW = 1; this.cH = 1;
			this.down.bindWithEvent(this)(event)
		}.bindWithEvent(this);
		$To(table,div);
		
		//添加Title表格
		table = $C("table");
		table.border = 0; table.cellPadding = 0; table.cellSpacing = 0; table.style.width = "100%";
		
		tr = table.insertRow(-1);
		td = tr.insertCell(-1); td.className = "dialogTitleTemp"; td.onselectstart = function(){return false};
		td.onmousedown = function(){if(this.canMove)this.firstDown = true}.bind(this);
		td.onmousemove = function(event){
			if(this.firstDown){
				this.cL = 1; this.cT = 1; this.cW = 0; this.cH = 0;
				this.down.bindWithEvent(this)(event)
			}
		}.bindWithEvent(this);
		td.onmouseup = function(){this.firstDown = false}.bind(this);
		td.ondblclick = this.mini.bind(this);
		div = $To($C("div"),td); div.className = "dialogTitleValue"; this.title = div; //标题
		td = tr.insertCell(-1); td.className = "dialogCloseButTemp"; td.vAlign = "top";
		div = $To($C("div"),td); div.className = "dialogBut dialogCloseBut_def"; this.closeBut = div; //关闭按钮
		div.onmouseover = function(){this.className = "dialogBut dialogCloseBut_hover"};
		div.onmouseout = function(){this.className = "dialogBut dialogCloseBut_def"};
		div.onclick = this.close.bind(this);
		div = $To($C("div"),td); div.className = "dialogBut dialogMinBut_def"; this.miniBut = div; //最小化按钮
		div.onmouseover = function(){this.className = "dialogBut " +
							((this.className.indexOf("Min") > -1) ? "dialogMinBut_hover" : "dialogMaxBut_hover")};
		div.onmouseout = function(){this.className = "dialogBut " + 
							((this.className.indexOf("Min") > -1) ? "dialogMinBut_def" : "dialogMaxBut_def")};
		div.onclick = this.mini.bind(this);
		$To(table,title);
	},
	addInnerBody : function() { //添加内表格
		var div,table,tr,td,tab;
	
		table = $C("table"); this.mainBodyTalbe = table;
		table.border = 0; table.cellPadding = 0; table.cellSpacing = 0;
		
		tr = table.insertRow(-1); //dialogBlank
		td = tr.insertCell(-1); td.className = "dialogBlank";
		td = tr.insertCell(-1);
		td = tr.insertCell(-1);
		
		tr = table.insertRow(-1); this.topBorder = tr; //备用边框
		td = tr.insertCell(-1); td.className = "dialogCB1";
		td = tr.insertCell(-1); td.className = "dialogCB2";
		td = tr.insertCell(-1); td.className = "dialogCB3";
		
		tr = table.insertRow(-1); this.tabLine = tr; //Tab行
		td = tr.insertCell(-1); td.className = "dialogTCB1";
		tab = tr.insertCell(-1); tab.className = "dialogTCB2";
		tab.onselectstart = function(){return false};
		td = tr.insertCell(-1); td.className = "dialogTCB3";
		tr.style.display = "none";
		
		tr = table.insertRow(-1); //Body行
		td = tr.insertCell(-1); td.className = "dialogCB4";
		td = tr.insertCell(-1);
		div = $To($C("div"),td); div.className = "dialogBody"; div.vAlign = "top"; this.body = div; //Body
		td = tr.insertCell(-1); td.className = "dialogCB5";
		
		tr = table.insertRow(-1); //底边
		td = tr.insertCell(-1); td.className = "dialogCB6";
		td = tr.insertCell(-1); td.className = "dialogCB7";
		td = tr.insertCell(-1); td.className = "dialogCB8";
		$To(table,this.mainBody);
		
		//添加Tab表格
		table = $C("table");
		table.border = 0; table.cellPadding = 0; table.cellSpacing = 0; table.style.width = "100%";
		
		tr = table.insertRow(-1);
		td = tr.insertCell(-1);
		div = $To($C("div"),td); div.className = "dialogTab"; this.tabScroll = div; //tabScroll
		div = $To($C("div"),div); div.className = "dialogTabTemp"; this.tabTemp = div; //tabTemp
		td = tr.insertCell(-1); td.className = "dialogTabRollTemp";
		div = $To($C("div"),td); div.className = "dialogTabRoll dialogTabRollL_def"; this.rollL = div; //左滚动
		div.onmouseover = function(){this.className = "dialogTabRoll dialogTabRollL_hover"};
		div.onmouseout = function(){this.className = "dialogTabRoll dialogTabRollL_def"};
		div.onmousedown = function(){this.onDown = true; this.direction = false; this.tabRoll.bind(this)()}.bind(this);
		div.onmouseup = function(){this.onDown = false}.bind(this);
		div = $To($C("div"),td); div.className = "dialogTabRoll dialogTabRollR_def"; this.rollR = div; //右滚动
		div.onmouseover = function(){this.className = "dialogTabRoll dialogTabRollR_hover"};
		div.onmouseout = function(){this.className = "dialogTabRoll dialogTabRollR_def"};
		div.onmousedown = function(){this.onDown = true; this.direction = true; this.tabRoll.bind(this)()}.bind(this);
		div.onmouseup = function(){this.onDown = false}.bind(this);
		$To(table,tab);
		
		this.setDialogSize(this.dialogWidth,this.dialogHeight);
	},
	setBodySize : function(w,h) { //设置Body宽度
		if(typeof(w) != "number" && typeof(h) != "number") return;
		
		if(this.div.style.display == "none"){
		    var hiddened = true;
		    this.div.style.visibility = "hidden";
		    this.div.style.display = "block";
		}
		if(this.minied) {
		    var minied = true;
		    this.mini(false);
		}
		
		if(this.body){
			this.mainBody.style.width = "";
			this.mainBody.style.height = "";
			if(typeof(w) == "number") {
				this.body.style.width = w + "px";
				this.dialogWidth = w + 12;
			}
			if(typeof(h) == "number") {
				this.body.style.height = h + "px";
				this.dialogHeight = h + (this.tabLine.style.display == "none"? 34 : 53);
			}
		}else {
			if(typeof(w) == "number")	{
				this.mainBody.style.width = w + "px";
				this.dialogWidth = w + 8;
			}
			if(typeof(h) == "number") {
				this.mainBody.style.height = h + "px";
				this.dialogHeight = h + 28;
			}
		}
		if(typeof(w) == "number") {
			this.div.style.width = this.dialogWidth + "px";
			this.bodyWidth = w;
			this.title.style.width = (w - 36) + "px";
		}
		if(typeof(h) == "number") {
			this.div.style.height = this.dialogHeight + "px";
			this.bodyHeight = h;
		}
		
		if(minied) this.mini(true);
		if(hiddened) {
		    this.div.style.visibility = "visible";
		    this.div.style.display = "none";
		}
		
		this.setTabWidth();
		
		if(this.onResized) this.onResized();
	},
	setDialogSize : function(w, h) { //设置对话框宽度
		if(typeof(w) != "number" && typeof(h) != "number") return;

		if(this.div.style.display == "none"){
		    var hiddened = true;
		    this.div.style.visibility = "hidden";
		    this.div.style.display = "block";
		}
		if(this.minied) {
		    var minied = true;
		    this.mini(false);
		}
		
		if(this.body){
			this.mainBody.style.width = "";
			this.mainBody.style.height = "";
			if(typeof(w) == "number") this.body.style.width = (w - 12) + "px";
			if(typeof(h) == "number") this.body.style.height = (h - (this.tabLine.style.display == "none"? 34 : 53)) + "px";
		}else {
			if(typeof(w) == "number") this.mainBody.style.width = (w - 8) + "px";
			if(typeof(h) == "number") this.mainBody.style.height = (h - 28) + "px";
		}
		if(typeof(w) == "number") {
			this.dialogWidth = w;
			this.div.style.width = w + "px";
			this.title.style.width = (w - 48) + "px";
		}
		if(typeof(h) == "number") {
			this.dialogHeight = h;
			this.div.style.height = h + "px";
		}
		
		if(minied) this.mini(true);
		if(hiddened) {
		    this.div.style.visibility = "visible";
		    this.div.style.display = "none";
		}
		
		this.setTabWidth();
		
		if(this.onResized) this.onResized();
	},
	setTabWidth : function() { //设置Tab宽度
		if(!this.body) return;
		
		if(this.div.style.display == "none"){
		    var hiddened = true;
		    this.div.style.visibility = "hidden";
		    this.div.style.display = "block";
		}
		if(this.minied) {
		    var minied = true;
		    this.mini(false);
		}
		
		this.tabScroll.style.width = "1px";
		if(!this.body.offsetWidth || this.body.offsetWidth < 0) return; 
		this.tabScroll.style.width = (this.body.offsetWidth - 30) + "px";
		var tabs = this.tab;
		var w = 0;
		this.tabTemp.style.width = "10000px";
		for(var i=0;i<tabs.length;i++){
			tabs[i].width = tabs[i].tab.children[0].offsetWidth + tabs[i].tab.children[1].offsetWidth + 2;
			w += tabs[i].width;
		}
		this.tabTemp.style.width = w + "px";

		if(this.tabTemp.offsetWidth > this.tabScroll.offsetWidth && this.tabScroll.scrollLeft > 0)
			this.rollL.style.visibility = "visible";
		else this.rollL.style.visibility = "hidden";
		if(this.tabTemp.offsetWidth > this.tabScroll.offsetWidth &&
			 											this.tabScroll.scrollLeft < this.tabScroll.scrollWidth - this.tabScroll.offsetWidth)
			this.rollR.style.visibility = "visible";
		else this.rollR.style.visibility = "hidden";
		
		if(minied) this.mini(true);
		if(hiddened) {
		    this.div.style.visibility = "visible";
		    this.div.style.display = "none";
		}
	},
	moveTo : function(left, top) { //将对话框移动到指定位置
		if(typeof(left) != "number" && typeof(top) != "number") return;
		var dis = this.div.style.display;
		this.div.style.display = "none";
		if(typeof(left) == "number") this.div.style.left = left + "px";
		if(typeof(top) == "number")this.div.style.top = top + "px";
		this.div.style.display = dis;
		
		if(this.onMoveEnd) this.onMoveEnd();
	},
	tabRoll : function() { //滚动tab
		if(this.onDown){
			if(this.direction){
				if(this.rollL.style.visibility == "hidden") this.rollL.style.visibility = "visible"; 
				if(this.tabScroll.scrollLeft + 3 < this.tabScroll.scrollWidth - this.tabScroll.offsetWidth){
					this.tabScroll.scrollLeft += 3;
					setTimeout(this.tabRoll.bind(this),5);
				}else {
					this.tabScroll.scrollLeft = this.tabScroll.scrollWidth - this.tabScroll.offsetWidth;
					this.rollR.style.visibility = "hidden";
				}
			}else {
				if(this.rollR.style.visibility == "hidden") this.rollR.style.visibility = "visible"; 
				if(this.tabScroll.scrollLeft - 3 > 0){
					this.tabScroll.scrollLeft -= 3;
					setTimeout(this.tabRoll.bind(this),5);
				}else {
					this.tabScroll.scrollLeft = 0;
					this.rollL.style.visibility = "hidden";
				}
			}
		}
	},
	mini : function(minied) { //在mini和default间切换,如果指定参数minied,则按minied设置
		if(!this.canMini) return;
		if(minied == true || (typeof(minied) != "boolean" && !this.minied)) {
			if(this.minied) return;
			
			if (this.onBeforeMini) {
				if(this.onBeforeMini() == false) return;
			}
			
			this.dialogBodyLine.style.display = "none";
			this.miniBut.className = "dialogBut dialogMaxBut_def";
			if(!this.miniKeepWidth) {
				this.tTitleWidth = this.title.offsetWidth;
				this.title.style.width = "52px";
				this.tWidth = this.div.offsetWidth;
				this.div.style.width = "";
				this.dialogWidth = this.div.offsetWidth;
				this.div.style.width = this.dialogWidth + "px";
			}
			this.tHeight = this.div.offsetHeight;
			this.div.style.height = "";
			this.dialogHeight = this.div.offsetHeight;
			this.div.style.height = this.dialogHeight + "px";
			this.tCanResize = this.canResize;
			this.setCanResize(false);
			this.minied = true;
			
			if(this.onMinied) this.onMinied();
		}else {
			if(!this.minied) return;
			
			if(this.onBeforeRevert){
				if(this.onBeforeRevert() == false) return;
			}
			
			this.dialogBodyLine.style.display = "";
			this.miniBut.className = "dialogBut dialogMinBut_def";
			if(!this.miniKeepWidth){
				this.div.style.width = this.tWidth + "px";
				this.dialogWidth = this.tWidth;
				this.title.style.width = this.tTitleWidth + "px";
			}
			this.div.style.height = this.tHeight + "px";
			this.dialogHeight = this.tHeight;
			this.setCanResize(this.tCanResize);
			this.minied = false;
			
			if(this.onReverted) this.onReverted();
		}
		
		if(this.onResized) this.onResized();
	},
	show : function(left, top) { //显示Dialog
		if(this.div){
			if (this.onBeforeShow) {
				if(this.onBeforeShow() == false) return;
			}
			
			if(typeof(left) == "number") this.left = left;
			if(typeof(top) == "number") this.top = top;
			this.moveTo(this.left, this.top);
			this.div.style.display = "block";
			this.showed = true;
			if(this.minied)	this.mini(false);
			this.setDialogSize(this.dialogWidth, this.dialogHeight);
			
			if(this.onShowed) this.onShowed();
		}
	},
	hidden : function() { //隐藏Dialog
		if (this.onBeforeHidden) {
			if(this.onBeforeHidden() == false) return;
		}
		
		this.div.style.display = "none";
		this.showed = false;
		
		if(this.onHiddened) this.onHiddened();
	},
	close : function() { //关闭Dialog
		if(!this.canClose || this.closed) return;
		
		if (this.onBeforeClose) {
			if(this.onBeforeClose() == false) return;
		}
		
		this.beginCloseing = true;
		this.div.style.display = "none";
		this.removeAllTab();
		this.tab = null;
		this.resizeBut = null;
		this.div.parentNode.removeChild(this.div);
		if(this.moveLine) this.moveLine.parentNode.removeChild(this.moveLine);
		
		this.closed = true;
		this.beginCloseing = false;
		
		if(this.onClosed) this.onClosed();
	},
	setZIndex : function(zIndex) {
		if(typeof(zIndex) != "number") return;
		this.zIndex = zIndex;
		this.div.style.zIndex = zIndex;
		if(this.moveLine) this.moveLine.style.zIndex = zIndex + 1;
	},
	setCanResize : function(canResize) { //设置对话框是否能够被调整大小
		if(!this.resizeBut || typeof(this.resizeBut) != "object" || !this.resizeBut.length) return;
		this.canResize = canResize;
		if(canResize){
			for(var i=0; i<this.resizeBut.length; i++) {
				this.resizeBut[i].style.cursor = this.resizeBut[i].rsCur;
			}
		}else {
			for(var i=0; i<this.resizeBut.length; i++) {
				this.resizeBut[i].style.cursor = "default";
			}
		}
	},
	setCanMove : function(canMove) { //设置对话框是否可以移动
		this.canMove = canMove;
	},
	setCanMini : function(canMini) { //设置对话框是否可以最小化
		this.canMini = canMini;
		if(canMini)
			this.miniBut.style.display = "block";
		else
			this.miniBut.style.display = "none";
	},
	setCanClose : function(canClose) { //设置对话框是否可以关闭
		this.canClose = canClose;
		if(canClose)
			this.closeBut.style.display = "block";
		else
			this.closeBut.style.display = "none";
	},
	setTitle : function(title) { //设置文档标题
		if(typeof(title) != "string") title = "";
		this.title.innerHTML = title;
		this.title.title = title;
		this.titleValue = title;
	},
	setMainBody : function(body,append) {//设置对话框mainBody，如果有内表格将被清除
		if(this.onBeforeMainBodyChange) {
			if(this.onBeforeMainBodyChange() == false) return;
		}
	
		if(this.mainBodyTalbe) {
			this.mainBody.removeChild(this.mainBodyTalbe);
			this.mainBodyTalbe = null;
		}
		if(typeof(body) == "string"){
			if(append)
				this.mainBody.innerHTML += body;
			else
				this.mainBody.innerHTML = body;
		}else if(typeof(body) == "object"){
			if(!append){
				for(var i=0; i<this.mainBody.children.length; i++)
					this.mainBody.removeChild(this.mainBody.children[i]);
			}
			this.mainBody.appendChild(body);
		}
		
		if(this.onMainBodyChanged) this.onMainBodyChanged();
	},
	appendMainBody : function(body) { //向mainBody中追加内容
		this.setMainBody(body, true);
	},
	setBody : function(body,append) { //设置对话框Body，如果有Tab，Tab将被清除
		if(this.onBeforeBodyChange) {
			if(this.onBeforeBodyChange() == false) return;
		}
	
		if(!this.body) this.addInnerBody();
		this.removeAllTab();
		if(typeof(body) == "string"){
			if(append)
				this.body.innerHTML += body;
			else
				this.body.innerHTML = body;
		}else if(typeof(body) == "object"){
			if(!append){
				for(var i=0; i<this.body.children.length; i++)
					this.body.removeChild(this.body.children[i]);
			}
			this.body.appendChild(body);
		}
		
		if(this.onBodyChanged) this.onBodyChanged();
	},
	appendBody : function(body) { //向Body中追加内容
		this.setBody(body, true);
	},
	appendTab : function(title, body, canClose) { //添加一个Tab
		if(typeof(title) != "string") title = "";
		if(!this.body) this.addInnerBody();
		var div,label,R,CB;
		var newTab = this.tab[this.tab.length] = {};
		div = $To($C("div"),this.tabTemp); div.className = "dialogTab_def"; newTab.tab = div;
		div.onclick = this.selectTab.bind(this,newTab);
		label = $To($C("div"), div); label.className = "dialogTabLabel"; label.innerHTML = title; label.title = title;
		newTab.title = label; newTab.titleValue = title;
		R = $To($C("div"), div); R.className = "dialogTabR";
		CB = $To($C("div"), R); CB.className = "dialogTabCloseBut dialogTabCloseBut_def"; newTab.closeBut = CB;
		this.canClose = typeof(canClose) == "undefined" || canClose;
		if(!this.canClose) CB.style.display = "none";
		CB.onmouseover = function(){this.className = "dialogTabCloseBut dialogTabCloseBut_hover"};
		CB.onmouseout = function(){this.className = "dialogTabCloseBut dialogTabCloseBut_def"};
		CB.onclick = this.removeTab.bind(this,newTab);
		
		div = $To($C("div"),this.body); div.style.display = "none";
		if(typeof(body) == "object") div.appendChild(body);
		else if(typeof(body) == "string") div.innerHTML = body;
		else div.innerHTML = "";
		newTab.body = div;
		
		if(this.tabLine.style.display == "none") {
			this.topBorder.style.display = "none";
			this.tabLine.style.display = "";
			if(this.minied) {
			    this.onReverted = function() {
			        this.setDialogSize(this.dialogWidth,this.dialogHeight);
			    }.bind(this);
			}else
			    this.setDialogSize(this.dialogWidth,this.dialogHeight);
		}else if(this.showed) {
		    this.setTabWidth();
		}
		if(!this.curTab) this.selectTab(newTab);

		return newTab;
	},
	setTabTitle : function(tab, title) { //设置指定Tab的title
		if(typeof(tab) == "number") {
			if(this.tab.length <=0 || tab < 0 || tab > this.tab.length -1) return;
			tab = this.tab[tab];
		}
		if(!tab) return;
		if(typeof(title) != "string") title = "";
		tab.title.innerHTML = title;
		tab.title.title = title;
		tab.titleValue = title;
		this.setTabWidth();
	},
	setTabBody : function(tab, body, append) { //设置指定Tab的body
		if(typeof(tab) == "number") {
			if(this.tab.length <=0 || tab < 0 || tab > this.tab.length -1) return;
			tab = this.tab[tab];
		}
		if(!tab) return;
		
		if(tab.onBeforeBodyChange) {
			if(tab.onBeforeBodyChange() == false) return;
		}
		
		if(!append){
			if(typeof(body) == "object") {
				for(var i=0; i<tab.body.children.length; i++)
					tab.body.removeChild(tab.body.children[i]);
				tab.body.appendChild(body);
			}else if(typeof(body) == "string") tab.body.innerHTML = body;
			else tab.body.innerHTML = "";
		}else {
			if(typeof(body) == "object") tab.body.appendChild(body);
			else if(typeof(body) == "string") tab.body.innerHTML += body;
		}
		
		if(tab.onBodyChanged) tab.onBodyChanged();
	},
	appendTabBody : function(tab, body) { //追加指定Tab的body
		this.setTabBody(tab, body, true);
	},
	setTab : function(tab, title, body, append) { //设置指定Tab
		if(typeof(tab) == "number") {
			if(this.tab.length <=0 || tab < 0 || tab > this.tab.length -1) return;
			tab = this.tab[tab];
		}
		if(!tab) return;
		if(typeof(title) == "string") this.setTabTitle(tab, title);
		if(typeof(body) != "undefined") this.setTabBody(tab, body, append);
	},
	setTabCanClose : function(tab, canClose) { //设置指定Tab是否允许关闭
		if(typeof(tab) == "number") {
			if(this.tab.length <=0 || tab < 0 || tab > this.tab.length -1) return;
			tab = this.tab[tab];
		}
		if(!tab) return;
		tab.canClose = canClose;
		tab.closeBut.style.display = canClose ? "block" : "none";
	},
	removeTab : function(tab) { //删除一个Tab,可传入index或Tab对象
		if(typeof(tab) == "number") {
			if(this.tab.length <=0 || tab < 0 || tab > this.tab.length -1) return;
			tab = this.tab[tab];
		}
		if(!tab) return;
		
		if (tab.onBeforeClose) {
			if(tab.onBeforeClose() == false) return;
		}

		if(this.curTab == tab) {
			var index = arrIndexOf(this.tab, tab);
			
			if(index > 0) index --;
			else if(index < this.tab.length - 1) index ++;
			else index = -1;
			
			if(index != -1) this.selectTab(index);
		}
		this.tabTemp.removeChild(tab.tab); tab.tab = null;
		this.body.removeChild(tab.body); tab.body = null;
		arrRemove(this.tab, tab);
		
		if(!this.beginCloseing)this.setTabWidth();
		
		if(tab.onClosed) tab.onClosed();
		tab = null;
		
		if(this.tab.length <= 0 && !this.beginCloseing) this.close();
	},
	removeAllTab : function() { //删除全部Tab
		if(this.tab.length == 0) return;
		var Len = this.tab.length;
		for(var i = 0; i < Len; i++){
			this.removeTab(this.tab[0]);
		}
		this.tab = [];
	},
	selectTab : function(tab) { //将某个Tab设为当前Tab,可传入index或Tab对象
		if(typeof(tab) == "number") {
			if(this.tab.length <=0 || tab < 0 || tab > this.tab.length -1) return;
			tab = this.tab[tab];
		}
		if(!tab) return;
		
		if (tab.onBeforeSelect) {
			if (tab.onBeforeSelect() == false) return;
		}
		
		if(this.curTab != tab || !this.curTab) {
		    if(this.curTab) {
			    this.curTab.tab.className = "dialogTab_def";
			    this.curTab.body.style.display = "none";
			    this.curTab.body.style.width = "1px";
			    this.curTab.body.style.height = "1px";
			    this.curTab = null;
		    }
		    tab.tab.className = "dialogTab_hover";
		    tab.body.style.display = "block";
		    tab.body.style.width = "";
		    tab.body.style.height = "";
		}
		
		var oL = 0;
		for(var i=0; i<this.tab.length; i++) {
			if(this.tab[i] != tab)
				oL += this.tab[i].width;
			else
				break;
		}
		var dif = oL + tab.width - this.tabScroll.scrollLeft - this.tabScroll.offsetWidth;
		if(dif > 0) this.tabScroll.scrollLeft += dif;
		dif = oL - this.tabScroll.scrollLeft;
		if(dif < 0) this.tabScroll.scrollLeft += dif;
		
		this.rollR.style.visibility = (this.tabScroll.scrollLeft >= this.tabScroll.scrollWidth - this.tabScroll.offsetWidth) ? "hidden" : "visible";
        this.rollL.style.visibility = (this.tabScroll.scrollLeft <= 0) ? "hidden" : "visible";
		
		this.curTab = tab;
		
		if(tab.onSelected) tab.onSelected();
	},
	down : function(event) { //当调整对话框大小，位置时鼠标按下时调用方法
		this.onDown = true;
		var moveLine,div = this.div;
		if(!this.moveLine){
			moveLine = this.moveLine = $To($C("div"),div.parentNode); moveLine.className = "moveLine";
			moveLine.style.zIndex = this.zIndex + 1000;
		}else moveLine = this.moveLine;
		
		this.downX = event.clientX;
		this.downY = event.clientY;
		this.downL = div.offsetLeft;
		this.downT = div.offsetTop;
		this.downW = div.offsetWidth;
		this.downH = div.offsetHeight;
		moveLine.style.left = this.downL + "px";
		moveLine.style.top = this.downT + "px";
		moveLine.style.width = (this.downW - 2) + "px";
		moveLine.style.height = (this.downH - 2) + "px";
		moveLine.style.display = "block";
		if(moveLine.setCapture) {
			moveLine.setCapture();
			if(!moveLine.onmousemove){
				moveLine.onmousemove = this.move.bindWithEvent(this);
				moveLine.onmouseup = this.up.bindWithEvent(this);
			}
		}else{
		    if(!this.dialogEventDiv){
		        this.dialogEventDiv = $To($C("div"), document.body);
		        this.dialogEventDiv.className = "dialogEventDiv";
		        this.dialogEventDiv.style.zIndex = this.zIndex + 1001;
		        this.dialogEventDiv.style.cursor = (event.srcElement || event.target).style.cursor;
			    $attachEvent(this.dialogEventDiv, __$EventName.mousemove, this.moveMethod = this.move.bindWithEvent(this));
			    $attachEvent(this.dialogEventDiv, __$EventName.mouseup, this.upMethod = this.up.bindWithEvent(this));
			}
		}
	},
	move : function(event) { //当调整对话框大小，位置时鼠标移动时调用方法
		if(this.onDown){
			var L,T,W,H,moveLine = this.moveLine;
			var mL = this.minL,mT = this.minT,mW = this.minied?100:this.minW,mH = this.minied?26:this.minH;
			var cX = event.clientX - this.downX,cY = event.clientY - this.downY;
			L = this.downL + cX * this.cL;
			T = this.downT + cY * this.cT;
			W = this.downW + cX * this.cW;
		  	H = this.downH + cY * this.cH;
			if(L < mL) {
				L = mL;
				W = this.downW + (L - this.downL) * this.cW;
			}
			if(W < mW) {
				W = mW;
				L = this.downL - (W - this.downW) * this.cL;
			}
			if(T < mT){
				T = mT;
				H = this.downH + (T - this.downT) * this.cH;
			}
			if(H < mH){
				H = mH;
				T = this.downT - (H - this.downH) * this.cT;
			}
			moveLine.style.left = L + "px";
			moveLine.style.top = T + "px";
			moveLine.style.width = (W - 2) + "px";
			moveLine.style.height = (H - 2) + "px";
		}
	},
	up : function(event) { //当调整对话框大小，位置时鼠标松开时调用方法
		if(this.onDown){
			var L,T,moveLine = this.moveLine;
			var mL = this.minL,mT = this.minT,mW = this.minied?100:this.minW,mH = this.minied?26:this.minH;
			var cX = event.clientX - this.downX,cY = event.clientY - this.downY;
			L = this.downL + cX * this.cL;
			T = this.downT + cY * this.cT;
			W = this.downW + cX * this.cW;
		    H = this.downH + cY * this.cH;
			if(L < mL) {
				L = mL;
				W = this.downW + (L - this.downL) * this.cW;
			}
			if(W < mW) {
				W = mW;
				L = this.downL - (W - this.downW) * this.cL;
			}
			if(T < mT){
				T = mT;
				H = this.downH + (T - this.downT) * this.cH;
			}
			if(H < mH){
				H = mH;
				T = this.downT - (H - this.downH) * this.cT;
			}
			this.div.style.display = "none";
			if(this.downL != L || this.downT != T) this.moveTo(L, T);
			if(!this.minied && (this.downW != W || this.downH != H)) this.setDialogSize(W, H);
			this.div.style.display = "block";
			if(moveLine.releaseCapture){
				moveLine.releaseCapture();
			}else{
			    if(this.dialogEventDiv){
				    $detachEvent(this.dialogEventDiv, __$EventName.mousemove, this.moveMethod);
				    $detachEvent(this.dialogEventDiv, __$EventName.mouseup, this.upMethod);
				    this.dialogEventDiv.style.display = "none";
				    this.dialogEventDiv.parentNode.removeChild(this.dialogEventDiv);
				    this.dialogEventDiv = null;
				}
			}
			moveLine.style.display = "none";
			this.onDown = false;
			this.firstDown = false;
		}
	}
});
