//==================================================
//
// iWin Object 視窗物件
// 版權所有 (C) 2007/03 許世衛 http://talk.away.com.tw/
// 遵循 GNU GPL 使用規範，修改或散佈時請保留這一段版權聲明。
//

function iWinObject (prefix, idDiv) {

	if (!prefix) prefix = "iWin_";
	this.prefix  = prefix;
	this.objname = this.prefix+"iWin";
	this.Img     = "/iWin/";

	var div = null;
	if (!idDiv) {
		idDiv = this.prefix+"iWinDiv";
		div = document.createElement("div");
		div.setAttribute("id", idDiv);
		document.getElementById("iWinTopDiv").appendChild(div);
	}
	else
		div = document.getElementById(idDiv);
	this.idDiv = idDiv;
	this.Div   = div;
	this.Div.style.position = "absolute";
	this.Div.style.left     = 0;
	this.Div.style.top      = 0;
	this.Div.style.display  = "none";

	this.caption = "新視窗";
	this.width   = 400;
	this.height  = 300;
	this.isBlurParent = 0;
	this.iWinStyle_ = this.iWinStyle0_;

	this.BlurParent = 1;

	this.onClose = null;

	this.contentDivName = "";
	this.isMove = 0; this.dx = 0; this.dy = 0; this.tmpMouseMove = null;
	eval("window."+this.objname+" = this;");
}

iWinObject.prototype.open_ = function (caption, w, h) {

	caption = this.setCaption_(caption);
	w = this.setWidth_(w); h = this.setHeight_(h);
	if (!document.getElementById(this.prefix+"iWinTable")) {
		if (this.contentDivName!="") {
			document.getElementById(this.contentDivName).style.display = "none";
			document.getElementById("iWinTopDiv").appendChild(document.getElementById(this.contentDivName));
			this.contentDivName = "";
		}
		this.Div.innerHTML = this.iWinStyle_(this, caption, w, h);
	}

	this.alignCenter_(this.Div);
	this.show_();

	if (this.BlurParent) this.blurParent_(1);
}

iWinObject.prototype.close_ = function () {
	if (this.onClose) this.onClose(this);
	this.Div.style.display = "none";
	if (this.isBlurParent==1) this.blurParent_(0);
}

iWinObject.prototype.hide_ = function () {
	if (this.onClose) this.onClose(this);
	this.Div.style.display = "none";
	if (this.isBlurParent==1) this.blurParent_(0);
}

iWinObject.prototype.show_ = function () {
	document.getElementById(this.prefix+"iWinCaption").innerHTML  = this.caption;
	document.getElementById(this.prefix+"iWinTable").style.width  = this.width;
	document.getElementById(this.prefix+"iWinTable").style.height = this.height;
	this.Div.style.display = "";
}

iWinObject.prototype.setCaption_ = function (caption) {
	if (typeof(caption)=="string") this.caption = caption;
	return this.caption;
}

iWinObject.prototype.setWidth_ = function (w) {
	if (typeof(w)=="number") this.width = w;
	return this.width;
}

iWinObject.prototype.setHeight_ = function (h) {
	if (typeof(h)=="number") this.height = h;
	return this.height;
}

iWinObject.prototype.setPos_ = function (x, y) {
	this.Div.style.left = x;
	this.Div.style.top  = y;
}

iWinObject.prototype.setSize_ = function (w, h) {
	var div = document.getElementById(this.prefix+"iWinTable");
	div.style.width  = w;
	div.style.height = h;
}

iWinObject.prototype.setContentHTML_ = function (s) {
	if (this.contentDivName!="") {
		document.getElementById(this.contentDivName).style.display = "none";
		document.getElementById("iWinTopDiv").appendChild(document.getElementById(this.contentDivName));
		this.contentDivName == "";
	}
	document.getElementById(this.prefix+"iWinContent").innerHTML = s;
}

iWinObject.prototype.setContentDIV_ = function (divname) {
	var div  = document.getElementById(this.prefix+"iWinContent");
	var div2 = document.getElementById(divname);
	if (this.contentDivName!="") {
		document.getElementById(this.contentDivName).style.display = "none";
		document.getElementById("iWinTopDiv").appendChild(document.getElementById(this.contentDivName));
	}
	div.innerHTML = "";
	this.contentDivName = divname;
	div.appendChild(div2);
	div2.style.display = "";
}

iWinObject.prototype.iWinStyle0_ = function (iWin, caption, w, h) {

	if (typeof(caption)!="string") caption = iWin.caption;
	if (typeof(w)!="number") w = iWin.width;
	if (typeof(h)!="number") h = iWin.height;

	var s = ""
	+"<table id='"+iWin.prefix+"iWinTable' border=0 cellpadding=0 cellspacing=0 class='iWinBorder' style='width:"+w+"px; height:"+h+"px;'><tr><td valign=top>"
		+"<table width=100% border=0 cellpadding=1px cellspacing=0 class='iWinCaption'><tr>"
			+"<td width=4px><img src='"+iWin.Img+"space.gif' width=4px border=0><br></td>"
			+"<td width=18px><img src='"+iWin.Img+"space.gif' border=0 onMouseOver='this.src=\""+iWin.Img+"space.gif\";' onMouseOut='this.src=\""+iWin.Img+"space.gif\";'><br></td>"
			+"<td align=left class='iWinCaptionText' id='"+iWin.prefix+"iWinCaption' onMouseDown='window."+iWin.objname+".onMouseDown_(event)' onMouseUp='window."+iWin.objname+".onMouseUp_()'>"
				+caption+"</td>"
			+"<td width=18px><img src='"+iWin.Img+"close.gif' border=0 onMouseOver='this.src=\""+iWin.Img+"closeover.gif\";this.style.cursor=\"pointer;\"' onMouseOut='this.src=\""+iWin.Img+"close.gif\";' onClick='window."+iWin.objname+".hide_()'><br></td>"
			+"<td width=4px><img src='"+iWin.Img+"space.gif' width=4px border=0><br></td>"
		+"</tr></table>\r\n"
		+"<div id='"+iWin.prefix+"iWinContent'></div>\r\n"
	+"</td></tr></table>\r\n"
	return s;
}

iWinObject.prototype.alignCenter_ = function (div) {
	if (document.all) {
		var top  = Math.round((document.body.clientHeight-this.height) / 2)+document.body.scrollTop;
		var left = Math.round((document.body.clientWidth-this.width)   / 2)+document.body.scrollLeft;
	}
	else {
		var top  = Math.round((window.innerHeight-this.height) / 2)+window.pageYOffset;
		var left = Math.round((window.innerWidth-this.width)   / 2)+window.pageXOffset;
	}
	div.style.top  = top;
	div.style.left = left;
}

iWinObject.prototype.onMouseDown_ = function (e) {
	this.isMove = 1;

	var x = 0, y = 0;
	if (document.all) {
		x = event.clientX + document.body.scrollLeft;
		y = event.clientY + document.body.scrollTop;
	}
	else {
		x = e.pageX;
		y = e.pageY;
	}

	this.dx = parseInt(this.Div.style.left) - x;
	this.dy = parseInt(this.Div.style.top)  - y;

	window.iWinObjName   = this.objname;
	this.tmpMouseMove    = document.onmousemove;
	document.onmousemove = this.onMouseMove_;
}

iWinObject.prototype.onMouseUp_ = function () {
	this.isMove = 0;
	document.onmousemove = this.tmpMouseMove;
}

iWinObject.prototype.onMouseMove_ = function (e) {
	if (typeof(window.iWinObjName)!="string" || window.iWinObjName=="")
		return;

	eval("var iWin = window."+window.iWinObjName);

	if (iWin.isMove != 1) return;

	var x = 0, y = 0;
	if (document.all) {
		x = event.clientX + document.body.scrollLeft;
		y = event.clientY + document.body.scrollTop;
	}
	else {
		x = e.pageX;
		y = e.pageY;
	}

	iWin.Div.style.left = x + iWin.dx;
	iWin.Div.style.top  = y + iWin.dy;
}

iWinObject.prototype.blurParent_ = function (isBlur) {
	var blur = document.getElementById("iWinBlurDiv");
	blur.style.width   = (document.all) ? document.body.clientWidth  : "100%";
	blur.style.height  = (document.all) ? document.body.scrollHeight : window.innerHeight + window.scrollMaxY;
	blur.style.display = (isBlur==0) ? "none" : "";
	this.isBlurParent  = isBlur;
}

document.write("<div id='iWinTopDiv'  style='position:absolute;left:0px;top:0px;z-index:1000;'></div>");
document.write("<div id='iWinBlurDiv' style='position:absolute;background-color:#000000;left:0px;top:0px;display:none;z-index:999;opacity:0.2;filter:alpha(opacity=20);'></div>");

var iWin = new iWinObject();
