// JavaScript Document
///ClASS Window //////////////////////////////////////////////////////////////////////////////

Window = function ()
{
	var oDivBlock = document.createElement('div');
	var oTable = document.createElement('table');
	var oTopRow = oTable.insertRow(0);
	var oTopRowLeftCell = oTopRow.insertCell(0);
	var oTopRowRightCell = oTopRow.insertCell(1); 
	var oBottomRow = oTable.insertRow(1);
	var oBodyCell = oBottomRow.insertCell(0);
	//-------------------------------------------------------------------------
	var self = this;
	self.Client = document.createElement('div');
	self.ClientSize = new Size(300, 300);
	self.Position = new Point(100, 100);
	self.Caption = "PopWindow";
	self.IsCenterOfWindow = true;
	self.BorderColor = "#FE8B39";
	self.IsOpen = false;
	self.OnClose = null;
	//-------------------------------------------------------------------------
	var topBarHeight = 30;
	var closeBtnWidth = 15;
	var borderWidth = 8;
	//-------------------------------------------------------------------------
	oTable.id = "PopWindow" +  Math.round((Math.random() * 1000));
	oTable.cellPadding = 0;
	oTable.cellSpacing = 0;
	oTable.style.position = "absolute"; 
	oTable.style.display = "none"; 
	oTable.style.zIndex = 1000; 
	oTable.style.fontFamily = "Arial";
	oTable.style.fontSize = "10pt";
	oTable.style.cursor = "default";
	//-------------------------------------------------------------------------
	oTopRow.style.height = topBarHeight + "px"; 
	oTopRow.style.color = "white";
	oTopRow.style.cursor = "move";
	var drag = false;
	var topRowMousePos = new Point(0, 0);
	var pageMousePos = new Point(0, 0);
	oTopRow.onmousedown = function () {
		drag = true;
		topRowMousePos.X = pageMousePos.X - self.Position.X;
		topRowMousePos.Y = pageMousePos.Y - self.Position.Y;
	}
	addListener(document, "mouseup", function (e) {
		drag = false;
	}, false);
	addListener(document, "mousemove", function (e) {
		if (BROWSER_IE) { // grab the x-y pos.s if browser is IE
			pageMousePos.X = event.clientX + document.body.scrollLeft;
			pageMousePos.Y = event.clientY + document.body.scrollTop;
		} 
		else {  // grab the x-y pos.s if browser is NS
			pageMousePos.X = e.pageX;
			pageMousePos.Y = e.pageY;
		} 
		if(drag) { 
			self.Position.X = pageMousePos.X - topRowMousePos.X;
			self.Position.Y = pageMousePos.Y - topRowMousePos.Y;
			oTable.style.left = self.Position.X + "px";
			oTable.style.top = self.Position.Y + "px";
		}
	}, false);
	//-------------------------------------------------------------------------
	oTopRowRightCell.innerHTML = "x";
	oTopRowRightCell.align = "center";
	oTopRowRightCell.style.width = closeBtnWidth + "px";
	oTopRowRightCell.style.fontWeight = "bold"; 
	oTopRowRightCell.style.cursor = "pointer";
	oTopRowRightCell.onclick = function () { 
		if(self.OnClose) { self.OnClose(); }
		self.Hide();
	}
	oTopRowRightCell.onmouseover = function () { 
		this.style.color = "black"; 
	}
	oTopRowRightCell.onmouseout = function () { 
		this.style.color = ""; 
	}
	//-------------------------------------------------------------------------
	oBodyCell.colSpan = 2;
	oBodyCell.appendChild(self.Client);
	//-------------------------------------------------------------------------
	oDivBlock.style.position = "absolute"; 
	oDivBlock.style.display = "none"; 
	oDivBlock.style.left = "0px"; 
	oDivBlock.style.top = "0px"; 
	oDivBlock.style.zIndex = 999; 
	oDivBlock.style.backgroundColor = "black";
	oDivBlock.style.filter = "Alpha(Opacity=60)";
	oDivBlock.style.MozOpacity = 0.6;

	//=========================================================================
	self.onLoad = function ()
	{
		document.body.appendChild(oDivBlock);
		document.body.appendChild(oTable);	
	}
	
	addListener(window, "load", function () { self.onLoad(); }, false);
	addListener(window, "scroll", function () { self.Paint(); }, false);
	
	//=========================================================================
	self.Paint = function() 
	{
		var page_size = getPageSize();
		
		oDivBlock.style.width = page_size.Width + "px";
		oDivBlock.style.height = page_size.Height + "px";
		
		oTable.style.backgroundColor = "white";
		oTable.style.borderLeft = borderWidth + "px solid "+self.BorderColor; 
		oTable.style.borderRight = borderWidth + "px solid "+self.BorderColor; 
		oTable.style.borderBottom = borderWidth + "px solid "+self.BorderColor; 
		if (self.IsCenterOfWindow) {
			var scroll_pos = getScrollPosition();
			var window_client_size = getClientSize();
			self.Position.X = (scroll_pos.X  + Math.round((window_client_size.Width - (self.ClientSize.Width + 2 * borderWidth)) / 2));
			self.Position.Y = (scroll_pos.Y + Math.round((window_client_size.Height - (self.ClientSize.Height + borderWidth + topBarHeight)) / 2));
		}
		oTable.style.left = self.Position.X + "px";
		oTable.style.top = self.Position.Y + "px";
			
		oTopRow.style.backgroundColor = self.BorderColor; 
		oTopRowLeftCell.style.width = (self.ClientSize.Width - closeBtnWidth) + "px";
		oTopRowLeftCell.innerHTML = self.Caption;
	
		self.Client.style.width = self.ClientSize.Width + "px";
		self.Client.style.height = self.ClientSize.Height + "px";
		self.Client.style.overflow = "auto";
	}
	
	//=========================================================================
	self.Show = function() 
	{
		self.Paint();
		oDivBlock.style.display = "block";
		oTable.style.display = "block";
		self.IsOpen = true;
	}

	//=========================================================================
	self.Hide = function() 
	{
		oDivBlock.style.display = "none";
		oTable.style.display = "none";
		self.IsOpen = false;
	}
}