/*
文件名:window.js
版  本:0.1.0
作  用:管理窗口
*/
Array.prototype.del = function(e)
{
	var bFind = false;
	for(var i = 0; i < this.length;i++)
	{
		if(!bFind)
		{
			if(this[i]==e)
			{
				bFind = true;
				this[i] = null;
			}
		}
		else
		{
			this[i-1] = this[i];
		}
	}
	if(bFind)
	{
		this.pop();
	}
};
//=================鼠标移动处理类=======================
var mouseMove = function(rc,eid){this.realClass="mouseMove";this.eID="";this.targetRealClass = rc;this.targetEID = eid;};
Object.extend(mouseMove.prototype,{
	oX:0,
	oY:0,
	x:0,
	y:0,
	button:0,
	isDown:false,
	targetRealClass:"",
	targetEID:"",
	mousedown:function(e)
	{
		e = e?e:event;
		var x = e.x?e.x:e.pageX;
		var y = e.y?e.y:e.pageY;
		if(!this.isDown)
		{
			this.oX = this.x = x;
			this.oY = this.y = y;
			this.button = e.button;
			var t = eval(this.targetRealClass + "._elements['"+this.targetEID+"']");
			if(t)
				if(t.setZIndex)
					t.setZIndex();
			//this.target.element.setCapture();
			this.isDown = true;
		}
	},
	mouseup:function(e)
	{
		e = e?e:event;
		if(this.isDown)
		{
			//this.target.element.releaseCapture();
			this.isDown = false;
		}
	},
	mousemove:function(e)
	{
		e = e?e:event;
		var x = e.x?e.x:e.pageX;
		var y = e.y?e.y:e.pageY;
		if(this.isDown&&x>0&&y>0)
		{
			var t = eval(this.targetRealClass + "._elements['"+this.targetEID+"']");
			if(t)
				if(t.move)
					t.move(x-this.oX,y-this.oY);
			this.oX = this.x = x;
			this.oY = this.y = y;
		}
	}
});

//=================Window 成员=======================
var Window = function(x,y,width,height,title)
{
	this.x = x?x:0;
	this.y = y?y:0;
	this.width = width?width:0;
	this.height = height?height:0;
	this.title = title?title:"窗口";
};

Object.extend(Window.prototype,{
		x:0,
		y:0,
		width:0,
		height:0,
		title:"",
		zIndex:0,
		Position:0,
		alpha:100,
		id:"",
		element:null,
		oTitle:null,
		oText:null,
		oButton:null,
		body:null,
		oFrame:null,
		sCss:"Window",
		onclose:null,
		onshow:null,
		oncreate:null,
		onload:null,
		mouse:null,
		setTitle:function(s)
		{
			this.title = s?s:this.title;
			if(this.oText)
				this.oText.innerHTML = this.title;
		},
		setClassName:function(s)
		{
			this.sCss = s?s:this.sCss;
			if(this.element)
			{
				this.element.className = this.sCss;
				this.oFrame.className = this.sCss+"_Frame";
			}
		},
		close:function()
		{
			if(this.onclose)
			{
				if(this.onclose())
					Window.destroy(this);
			}
			else
					Window.destroy(this);
		},
		show:function(b)
		{
			b = b==null?this.Position:b;
			if(b==true||b>=0)
			{
				if(!this.element)
					this.create();
				this.element.style.display = "";
				var offX = parseInt((document.documentElement.clientWidth - this.element.clientWidth)/2);
				var offY = parseInt((document.documentElement.clientHeight - this.element.clientHeight)/2);
				if(b!= 0)
				{
					if(b == 1)
					{
						this.x = (document.documentElement.scrollLeft + offX);
						this.y = (document.documentElement.scrollTop + offY);
					}
					else
					{
						if(b&2)
							this.x = (document.documentElement.scrollLeft + offX);
						if(b&4)
							this.y = (document.documentElement.scrollTop + offY);
						if(b&8)
							this.y = document.documentElement.scrollTop;
						if(b&16)
							this.x = (document.documentElement.scrollLeft + document.documentElement.clientWidth - this.element.clientWidth - 2);
						if(b&32)
							this.y = (document.documentElement.scrollTop + document.documentElement.clientHeight - this.element.clientHeight - 2);
						if(b&64)
							this.x = document.documentElement.scrollLeft;
					}
				}
				this.element.style.left = this.x + "px";
				this.element.style.top = this.y + "px";
				this.element.style.filter = "alpha(opacity="+this.alpha+")";
				this.element.style.opacity = this.alpha / 100;
				this.oFrame.style.left = this.element.style.left;
				this.oFrame.style.top = this.element.style.top;
				this.oFrame.style.width = this.element.clientWidth + "px";
				this.oFrame.style.height = this.element.clientHeight+"px";
				this.oFrame.style.display = "";
			}
			else
			{
				if(this.element)
				{
					this.element.style.display = "none";
					this.oFrame.style.display = "none";
				}
			}
			if(this.onshow)
				this.onshow();
		},
		move:function(iX,iY)
		{
			this.x += iX?iX:0;
			this.y += iY?iY:0;
			if(this.element)
			{
				this.element.style.left = this.x+"px";
				this.element.style.top = this.y+"px";
				this.oFrame.style.left = this.x+"px";
				this.oFrame.style.top = this.y+"px";
			}
		},
		moveTo:function(iX,iY)
		{
			this.x = iX?iX:this.x;
			this.y = iY?iY:this.y;
			if(this.element)
			{
				this.element.style.left = this.x+"px";
				this.element.style.top = this.y+"px";
				this.oFrame.style.left = this.x+"px";
				this.oFrame.style.top = this.y+"px";
			}
		},
		setSize:function(w,h)
		{
			this.width = w?w:this.width;
			this.height = h?h:this.height;
			if(this.body)
			{
				this.element.style.width = this.width+"px";
				this.body.style.width = this.width+"px";
				this.body.style.height = this.height+"px";
				this.oFrame.style.width =  this.width+"px";
				this.oFrame.style.height =  this.height+"px";
			}
		},
		setZIndex:function(z)
		{
			if(this.element)
			{
				this.element.style.zIndex = Window.setZIndex(this,z);
				this.oFrame.style.zIndex = this.element.style.zIndex - 1;
			}
		}
});

Window.prototype.create = function()
{
	if(!this.element)
	{
		this.id = Window.getNewID();
		this.mouse = new Array();
		this.mouse[0] = new mouseMove("Window",this.id);
		
		this.oFrame = document.createElement("iframe");
		this.oFrame.className = this.sCss+"_Frame";
		
		this.element = document.createElement("div");
		this.element.eid = this.id;
		this.element.realClass = "Window";
		this.element.className = this.sCss;
		this.element.style.display = "none";
		this.element.onclick = function(e){e=e?e:event;e.cancelBubble=true;Window._elements[this.eid].setZIndex();};
		
		this.oTitle = document.createElement("div");
		this.oTitle.className = "WT";
		this.oTitle.eid = this.id;
		this.oTitle.realClass = "Window";

		this.oText = document.createElement("div");
		this.oText.className = "TT";
		
		this.oButton = document.createElement("div");
		this.oButton.className = "TB";
		this.oButton.eid = this.id;
		this.oButton.onclick = function(e){e=e?e:event;e.cancelBubble=true;Window._elements[this.eid].close()};
		
		this.oTitle.appendChild(this.oText);
		this.oTitle.appendChild(this.oButton);
		this.element.appendChild(this.oTitle);
		
		this.body = document.createElement("div");
		this.body.className = "WB";
		
		this.element.appendChild(this.body);
		
		this.setTitle();
		
		this.element.style.zIndex = this.zIndex = Window.getMaxZIndex() + 1;
			
		Window._elements[this.id] = this;
		document.body.appendChild(this.oFrame);
		document.body.appendChild(this.element);
		
		this.moveTo();
		this.setSize();
	}
	
	if(this.oncreate)
		this.oncreate();
}

Window.prototype.destroy = function()
{
	if(this.element)
	{
		document.body.removeChild(this.element);
		document.body.removeChild(this.oFrame);
		this.element = null;
		this.oFrame = null;
		this.oText = null;
		this.oButton = null;
		this.oTitle = null;
		this.body = null;
		this.mouseMove = null;
	}
}
//=================Window 成员=======================

//=================Window 静态成员=======================
Window._elements = new Array();
Window._id = 0;
Window.destroy = function(w)
{
	w.destroy();
	Window._elements.del(w);
	w = null;
}
Window.setZIndex = function(w,z)
{
	if(!z)
	{
		z = Window.getMaxZIndex();
	}
	for(i in Window._elements)
		if(Window._elements[i].zIndex > w.zIndex&&Window._elements[i].zIndex<=z)
			Window._elements[i].setZIndex(Window._elements[i].zIndex - 1);
	w.zIndex = z;
	return z;
}
Window.getMaxZIndex = function()
{
	var z = 0;
	for(i in Window._elements)
		if(Window._elements[i].zIndex > z)
			z = Window._elements[i].zIndex;
	return z;
}
Window.getNewID = function()
{
	Window._id++;
	return "window_"+Window._id;
}
Window.getPosition = function(o)
{
	var p = o.offsetParent;
	var x = o.offsetLeft,y=o.offsetTop;
	while (p.tagName != "BODY"&&p.tagName != "HTML") {
		x += p.offsetLeft;
		y += p.offsetTop;
		p = p.offsetParent;
	}
	return {x:x,y:y};
} 

Window.Cancel = function(){return false;}

//显示位置
Window.SW_NORMAL = 0;
Window.SW_CENTER = 1;
Window.SW_HCENTER = 2;
Window.SW_VCENTER = 4;
Window.SW_TOP = 8;
Window.SW_RIGHT = 16;
Window.SW_BOTTOM = 32;
Window.SW_LEFT = 64;

//=================Window 静态成员=======================

//=================带按"确定\取消"按钮 Window=======================
var ButtonWindow = function(x,y,width,height,title)
{
	this.x = x?x:0;
	this.y = y?y:0;
	this.width = width?width:0;
	this.height = height?height:0;
	this.title = title?title:"对话框";
	this.buttons = new Array();
	this.buttonsName = new Array();
	for(var i = 5;i<arguments.length;i++)
		if(typeof(arguments[i]) == "string")
		{
			this.buttonsName[this.buttonsName.length] = arguments[i];
			this.buttons[this.buttons.length] = null;
		}
		
}
Object.extend(ButtonWindow.prototype,Window.prototype);
Object.extend(ButtonWindow.prototype,{
		oncreate:function()
		{
			for(var i = 0;i<this.buttonsName.length;i++)
			{
				this.buttons[i] = document.createElement("button");
				this.buttons[i].className = "WButton";
				this.buttons[i].innerHTML = this.buttonsName[i];
				this.element.appendChild(this.buttons[i]);
			}
			//this.oFrame.attachEvent("onmousemove",function(e){parent.document.body.onmousemove(e);});
		},
		onclose:function()
		{
			for(var i = 0;i<this.buttonsName.length;i++)
			{
				this.buttons[i] = null;
			}
			this.buttons = null;
			this.buttonsName = null;
			//document.body.detachEvent("onscroll",Window.Cancel);
			//document.body.detachEvent("onfocus",Window.Cancel);
			return true;
		},
		show:function(b)
		{
			b = b==null?this.Position:b;
			if(b==true||b>=0)
			{
				if(!this.element)
					this.create();
				this.element.style.zIndex = this.zIndex + 1;
				this.element.style.display = "";
				var offX = parseInt((document.documentElement.clientWidth - this.element.clientWidth)/2);
				var offY = parseInt((document.documentElement.clientHeight - this.element.clientHeight)/2);
				if(b!= 0)
				{
					if(b == 1)
					{
						this.x = (document.documentElement.scrollLeft + offX);
						this.y = (document.documentElement.scrollTop + offY);
					}
					else
					{
						if(b&2)
							this.x = (document.documentElement.scrollLeft + offX);
						if(b&4)
							this.y = (document.documentElement.scrollTop + offY);
						if(b&8)
							this.y = document.documentElement.scrollTop;
						if(b&16)
							this.x = (document.documentElement.scrollLeft + document.documentElement.clientWidth - this.element.clientWidth - 2);
						if(b&32)
							this.y = (document.documentElement.scrollTop + document.documentElement.clientHeight - this.element.clientHeight - 2);
						if(b&64)
							this.x = document.documentElement.scrollLeft;
					}
					this.element.style.left = this.x + "px";
					this.element.style.top = this.y + "px";
				}
				this.oFrame.style.zIndex = this.zIndex;
				this.oFrame.style.left = 0+"px";
				this.oFrame.style.top = 0+"px";
				this.oFrame.style.width = document.documentElement.scrollWidth + "px";
				this.oFrame.style.height = document.documentElement.scrollHeight+"px";
				this.oFrame.style.display = "";
				//document.body.attachEvent("onscroll",Window.Cancel);
				//document.body.attachEvent("onfocus",Window.Cancel);
			}
			else
			{
				if(this.element)
				{
					this.element.style.display = "none";
					this.oFrame.style.display = "none";
					//document.body.detachEvent("onscroll",Window.Cancel);
					//document.body.detachEvent("onfocus",Window.Cancel);
				}
			}
		},
		move:function(iX,iY)
		{
			this.x += iX?iX:0;
			this.y += iY?iY:0;
			if(this.element)
			{
				this.element.style.left = this.x+"px";
				this.element.style.top = this.y+"px";
			}
		},
		moveTo:function(iX,iY)
		{
			this.x = iX?iX:this.x;
			this.y = iY?iY:this.y;
			if(this.element)
			{
				this.element.style.left = this.x+"px";
				this.element.style.top = this.y+"px";
			}
		},
		setSize:function(w,h)
		{
			this.width = w?w:this.width;
			this.height = h?h:this.height;
			if(this.body)
			{
				this.element.style.width = this.width+"px";
				this.body.style.width = this.width+"px";
				this.body.style.height = this.height+"px";
			}
		}
});
//=================带按"确定\取消"按钮 Window=======================

//=================自动浮动 Window==================================
var FloatWindow = function(x,y,width,height,title,p,a)
{
	this.x = x?x:0;
	this.y = y?y:0;
	this.width = width?width:0;
	this.height = height?height:0;
	this.title = title?title:"浮动框";
	this.Position = p==null?0:p;
	this.alpha = a==null?100:a;
}
Object.extend(FloatWindow.prototype,Window.prototype);
Object.extend(FloatWindow.prototype,{
	action:0,
	targetW:0,
	targetH:0,
	targetA:0,
	timer:null,
	v:2,
	playExtend:function(w,h,a)
	{
		w = w==null?this.width:w;
		h = h==null?this.height:h;
		a = a==null?this.alpha:a;
		if(this.action==1)
		{
			var bStop = false;
			if(this.targetW != this.width)
			{
				var v = (this.targetW - this.width)/this.v;
				v = v<0?(v>-1?-1:v):(v<1?1:v);
				this.setSize(this.width + parseInt(v));
				bStop = (this.targetW == this.width);
			}
			else
				bStop = true;
				
			if(this.targetH != this.height)
			{
				var v = (this.targetH - this.height)/this.v;
				v = v<0?(v>-1?-1:v):(v<1?1:v);
				this.setSize(null,this.height + parseInt(v));
				bStop = bStop&&(this.targetH == this.height);
			}
			if(this.targetA != this.alpha)
			{
				var v = (this.targetA - this.alpha)/this.v;
				v = v<0?(v>-1?-1:v):(v<1?1:v);
				this.alpha +=parseInt(v);
				bStop = bStop&&(this.targetA == this.alpha);
			}
			this.show();
			if(bStop)
			{
				this.action = 0;
				if(this.timer!=null)
				{
					clearInterval(this.timer);
					this.timer = null;
				}
			}
		}
		else if(this.action==0&&(w!=this.width||h!=this.height||a!=this.alpha))
		{
			this.action = 1;
			this.targetW = w;
			this.targetH = h;
			this.targetA = a;
			FloatWindow.extend(this);
		}
	}
});

FloatWindow.extend = function(w)
{
	if(w.timer==null)
		w.timer = setInterval(function(){FloatWindow.extend(w)},10);
	else
		w.playExtend();
}
//=================自动浮动 Window==================================


//=================Tip Window==================================
var TipWindow = function(obj,width,height,time)
{
	this.targetElement = obj;
	this.width = width?width:0;
	this.height = height?height:0;
	this.showtime = time?time:5;
	this.playExtend(null,null,100);
}
Object.extend(TipWindow.prototype,FloatWindow.prototype);
Object.extend(TipWindow.prototype,{
			  content:"",
			  alpha:0,
			  v:5,
			  targetElement:null,
			  timer_show:null,
			  showtime:5,
			  oncreate:function()
			  {
				  this.element.removeChild(this.oTitle);
				  this.oTitle = null;
				  this.oButton = null;
				  var p = Window.getPosition(this.targetElement);
				  this.x = p.x;
				  this.y = p.y + this.targetElement.clientHeight + 10;
				  if(this.targetElement.tips!="")
				  {
				  	this.content = this.targetElement.tips;
				  }
				  this.body.innerHTML = this.content;				  
			  },
			  onshow:function()
			  {
				  if(this.element.style.display=="")
				  	if(this.timer_show==null)
				  	{
						var o = this;
						var closeWindow = function()
						{
							o.playExtend(null,null,0);
							var checkWindow = function()
							{
								if(o.alpha == 0)
								{
									o.targetElement.tip = null;
									o.close();
								}
								else
									setTimeout(checkWindow,100);
							}
							setTimeout(checkWindow,100);
						}
						this.timer_show = setTimeout(closeWindow,this.showtime*1000);
				  	}
			  }
});

//=================Tip Window==================================

function getElementOnEvent(e)
{
	e = e?e:event;
	var o = null;
	if(e.srcElement)
		o = e.srcElement;
	else
		o = e.target;
	if(o.eid&&o.realClass)
		if(o.eid!=""&&o.realClass!="")
		{
			return eval(o.realClass+"._elements['"+o.eid+"']");
		}
	return null;
}

var addMouseHalder = function()
{
	document.body._currentElement = null;
	document.body.onmousedown = function(e)
	{
		e = e?e:event;
		var o = document.body._currentElement = getElementOnEvent(e);
		if(o)
			if(o.mouse)
			{
				e.cancelBubble = true;
				if(o.mouse.length>0)
					for(var i = 0;i<o.mouse.length;i++)
						o.mouse[i].mousedown(e);
				else if(o.mouse.mousedown)
					o.mouse.mousedown(e);
			}
	}
	document.body.onmouseup = function(e)
	{
		e = e?e:event;
		var o = document.body._currentElement
		if(o)
			if(o.mouse)
			{
				e.cancelBubble = true;
				if(o.mouse.length>0)
					for(var i = 0;i<o.mouse.length;i++)
						o.mouse[i].mouseup(e);
				else if(o.mouse.mouseup)
					o.mouse.mouseup(e);
				document.body._currentElement = null;
			}
	}
	document.body.onmousemove = function(e)
	{
		e = e?e:event;
		var o = document.body._currentElement;
		if(o)
			if(o.mouse)
			{
				e.cancelBubble = true;
				if(o.mouse.length>0)
					for(var i = 0;i<o.mouse.length;i++)
						o.mouse[i].mousemove(e);
				else if(o.mouse.mousemove)
					o.mouse.mousemove(e);
			}
	}
}
