// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
// for Netscape 6/Mozilla by Thor Larholm me@jscript.dk
// Usage: include this code segment at the beginning of your document
// before any other Javascript contents.
if (typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
	HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode)
	{
		switch (where)
		{
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode, this);
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode, this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if (this.nextSibling)
				{
					this.parentNode.insertBefore(parsedNode,this.nextSibling);
				}
				else
				{
					this.parentNode.appendChild(parsedNode);
				}
				break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where, htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	}

	HTMLElement.prototype.insertAdjacentText = function(where, txtStr)
	{
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where, parsedText);
	}
}

var ___zindex = 1000;
function xWin(w, h, l, t, tit, msg, move_hook)
{
	___zindex += 1;
	var w_id = Math.random().toString().replace(/\./g, "_");
	this.id      = w_id;
	this.width   = w;
	this.height  = h;
	this.left    = l;
	this.top     = t;
	this.zIndex  = ___zindex;
	this.title   = tit;
	this.message = msg;
	this.obj     = null;
	this.bulid   = bulid;
	this.move_hook = move_hook;
	this.resize = function(nw, nh)
	{
		this.width 	= nw;
		this.height = nh;
		var ma = document.getElementById("xMsg" + w_id);
		var ha = document.getElementById("xMsgHeader" + w_id);
		var ba = document.getElementById("xMsgBody" + w_id);
		try {
			ma.style.width = this.width;
			ma.style.height = this.height;
			ha.style.width = this.width - 2*2;
			ba.style.height = this.height - 20-5;
		}catch(E){}
	};
	
	this.setTitle = function(str)
	{
		var tt = document.getElementById("title_" + w_id);
		tt.innerHTML = str;
	};

	this.setContent = function(str)
	{
		var tt = document.getElementById("content_" + w_id);
		tt.innerHTML = str;
	};
	
	this.setCloseHook = function(fn)
	{
		document.getElementById("close_" + w_id).onclick = fn;
	};
	
	this.close = function()
	{
		var ma = document.getElementById("xMsg" + w_id);
		ma.style.visibility = "hidden";
	};
	
	this.show = function()
	{
		var ma = document.getElementById("xMsg" + w_id);
		ma.style.visibility = "visible";		
	};
	
	this.setMoveHook = function(fn)
	{
		drag(document.getElementById("xMsgHeader" + w_id), fn);
	};
	this.bulid();
}

function bulid()
{
	var normal = "#F7921C";
	var str = ""
		+ "<div id='xMsg" + this.id + "' "
		+ "style='"
		+ "z-index:" + this.zIndex + ";"
		+ "width:" + this.width + ";"
		+ "height:" + this.height + ";"
		+ "left:" + this.left + ";"
		+ "top:" + this.top + ";"
		+ "background-color:" + normal + ";"
		+ "color:" + "#41535D" + ";"
		+ "font-size:12px;"
		+ "position:absolute;"
		+ "cursor:default;"
		+ "border:2px solid " + normal + ";"
		+ "'>"
		+ "<div id='xMsgHeader" + this.id + "' "
		+ "style='"
		+ "background-color:" + normal + ";"
		+ "width:" + (this.width-2*2) + ";"
		+ "height:20;"
		+ "color:white;'"
		+ ">"
		+ '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>'
		+ "<td><span style='font-size:12px; color:#FFFFFF;'>&nbsp;<b id='title_" + this.id + "'>" + this.title + "</b></span></td>"
		+ '<td align="right"><table border="0" cellpadding="0" cellspacing="0"><tr><td align="center" valign="top"><img src="images/c.gif" width="12" height="12" id="close_' + this.id + '" /></td></tr></table></td>'
		+ "</tr></table>"
		+ "</div>"
		+ "<div id='xMsgBody" + this.id + "' style='"
		+ "height:" + (this.height-20-5) + ";"
		+ "background-color:white;"
		+ "line-height:14px;"
		+ "word-break:break-all;"
		+ "padding:3px;"
		+ "'><span id='content_" + this.id + "'>" + this.message + "</span></div>"
		+ "</div>"
		+ "</div>";
	try {
		document.body.insertAdjacentHTML("beforeEnd", str);
		drag(document.getElementById("xMsgHeader" + this.id), this.move_hook);
		document.getElementById("close_" + this.id).onclick = this.close;
	}catch(E){}
}

function cls(obj)
{
	try {
		var win = document.getElementById(obj);
		win.style.visibility = "hidden";
	}catch(E){}
}

function drag(o, move_hook){
	o.onmousedown=function(a){
		var d=document;if(!a)a=window.event;
		var x=a.layerX?a.layerX:a.offsetX,y=a.layerY?a.layerY:a.offsetY;
		if(o.setCapture)
			o.setCapture();
		else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		if (o.parentNode.style.zIndex != ___zindex)
		{
			___zindex++;
			o.parentNode.style.zIndex = ___zindex;
		}
		d.onmousemove=function(a){
			if (!a){a = window.event;}
			var x0 = 0;
			var y0 = 0;
			if (!a.pageX)
			{
				x0 = a.clientX;
			}
			else
			{
				x0 = a.pageX;
			}
			
			if (!a.pageY)
			{
				y0 = a.clientY;
			}
			else
			{
				y0 = a.pageY;
			}
			
			var tx=x0-x, ty=y0-y;
			o.style.left=tx;
			o.style.top=ty;
			var win = o.parentNode;
			win.style.left = tx;
			win.style.top  = ty;
			if (move_hook != null)
			{
				move_hook(tx, ty);
			}
		};

		d.onmouseup=function(){
			if(o.releaseCapture)
				o.releaseCapture();
			else if(window.captureEvents)
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			d.onmousemove=null;
			d.onmouseup=null;
		};
	};
}
