/*
DHTML Window Manager Class 360
Written By: Ken Colton <kcolton@360hubs.com>
*/

var WindowManager360 = new Object();

WindowManager360.moveWindow = null;
WindowManager360.moveOffsetX = 0;
WindowManager360.moveOffsetY = 0;

WindowManager360.zIndex = 10;

WindowManager360.windowSet = Array();
WindowManager360.concealWindows = 1;

WindowManager360.mouseUp = function()
{
	WindowManager360.moveWindow = null;
}

WindowManager360.mouseMove = function(ev)
{
	ev = ev || window.event;
	
	if(WindowManager360.moveWindow != null)
	{	
		var mousePos = WindowManager360.mouseCoords(ev);	
		
		xLeft(WindowManager360.moveWindow.windowNode, mousePos.x - WindowManager360.moveOffsetX);
		xTop(WindowManager360.moveWindow.windowNode, mousePos.y - WindowManager360.moveOffsetY);
	}
	
}


WindowManager360.mouseCoords = function(ev)
{
	if(ev.pageX || ev.pageY)
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

WindowManager360.showWindows = function()
{
	for(var i = 0; i < WindowManager360.windowSet.length; i++)
	{
		WindowManager360.windowSet[i].windowNode.style.display = "block";
	}
	WindowManager360.concealWindows = 0;
}

WindowManager360.hideWindows = function()
{
	for(var i = 0; i < WindowManager360.windowSet.length; i++)
	{
		WindowManager360.windowSet[i].windowNode.style.display = "none";
	}
	WindowManager360.concealWindows = 1;
}
	
WindowManager360.Window = function(contentNode, windowTheme, windowTitle, closeCallback, windowWidth, min, hidden)
{
	//Set the content node reference, we will be wrapping this in a window
	this.contentNode = contentNode;
	
	contentNode.style.display = "block";
	
	//Set close window callback
	if(closeCallback)
		this.closeCallback = closeCallback;
	
	//Create the window node
	this.windowNode = document.createElement("div");
	this.windowNode.className = windowTheme;
	this.windowNode.style.zIndex = WindowManager360.zIndex;
	WindowManager360.zIndex++;
	
	if(WindowManager360.concealWindows == 1)
		this.windowNode.style.display = "none";
		
	if(hidden)
		this.windowNode.style.display = "none";
	
	document.body.appendChild(this.windowNode);
	
	//Assign the window node its position based on where the content node used to live in the DOM
	xLeft(this.windowNode, xLeft(this.contentNode)); 	
	xTop(this.windowNode, xTop(this.contentNode));

	
	if(windowWidth)
		xWidth(this.windowNode, windowWidth);
	else
		xWidth(this.windowNode, xWidth(this.contentNode));
	
	xLeft(this.contentNode, 0);
	xTop(this.contentNode, 0);
	
	//Create the titlebar node
	this.titleNode = document.createElement("div");
	this.windowNode.appendChild(this.titleNode);
	this.titleNode.className = "titlebar";
	
	this.titlePadding = document.createElement("div");
	this.titlePadding.className = "padding";
	this.titleNode.appendChild(this.titlePadding);
	
	if(min)
	{
		//Create the close node
		this.closeNode = document.createElement("div");
		this.titleNode.appendChild(this.closeNode);
		this.closeNode.className = "minbutton";

		var closeText = document.createTextNode("Hide");
		this.closeNode.appendChild(closeText);
	}
	else
	{
	
		//Create the close node
		this.closeNode = document.createElement("div");
		this.titleNode.appendChild(this.closeNode);
		this.closeNode.className = "closebutton";

		var closeText = document.createTextNode("X");
		this.closeNode.appendChild(closeText);
	}
	
	//Add in the title text
	var titleText = document.createTextNode(windowTitle);
	this.titlePadding.appendChild(titleText);
	
	this.windowNode.appendChild(this.contentNode);
	this.contentNode.style.position = "relative";
	
	//Add a custom attribute to the window DOM reference so that we can get back to our object
	this.windowNode.Window = this;
	
	//Event Handlers
	this.titleNode.onmousedown = this.beginDrag;
	this.closeNode.onclick = this.closeWindow;
	
	WindowManager360.windowSet.push(this);
}

WindowManager360.Window.prototype.beginDrag = function(ev)
{	
	ev = ev || window.event;
	
	var mousePos = WindowManager360.mouseCoords(ev);
	
	this.parentNode.style.zIndex = WindowManager360.zIndex;
	WindowManager360.zIndex++;
	
	WindowManager360.moveWindow = this.parentNode.Window;
	WindowManager360.moveOffsetX = mousePos.x - xLeft(this.parentNode);
	WindowManager360.moveOffsetY = mousePos.y - xTop(this.parentNode);
}

WindowManager360.Window.prototype.closeWindow = function(forceClose)
{
	

	if(this.parentNode && this.parentNode.className && this.parentNode.className == "titlebar")
	{
	
		if(this.className == "minbutton")
		{
			this.parentNode.parentNode.Window.contentNode.style.display = "none";
			
			this.innerHTML = "Show";
			this.onclick = this.parentNode.parentNode.Window.maximize;
			
			var windowLoader =  new net.ContentLoader(rootURL + 'modules/im360/ajax/im360toggle.php', null, null, 'POST', 'toggle=hidechat&value=1');
		}
		else
		{
			this.parentNode.parentNode.style.display = "none";

			if(this.parentNode.parentNode.Window.closeCallback)
				this.parentNode.parentNode.Window.closeCallback.call(this);
			
		}
			
	}
	else
	{
		if(this.closeNode.className == "minbutton" && !forceClose)
		{
			this.contentNode.style.display = "none";

			this.closeNode.innerHTML = "Show";
			this.closeNode.onclick = this.maximize;

			var windowLoader =  new net.ContentLoader(rootURL + 'modules/im360/ajax/im360toggle.php', null, null, 'POST', 'toggle=hidechat&value=1');
		}
		else
		{
			this.windowNode.style.display = "none";

			if(this.closeCallback)
				this.closeCallback.call(this);
		}
	}
}

WindowManager360.Window.prototype.maximize = function()
{
	this.parentNode.parentNode.Window.contentNode.style.display = "block";
				
	this.innerHTML = "Hide";
	this.onclick = this.parentNode.parentNode.Window.closeWindow;
	
	var windowLoader =  new net.ContentLoader(rootURL + 'modules/im360/ajax/im360toggle.php', null, null, 'POST', 'toggle=hidechat&value=0');
}

WindowManager360.Window.prototype.openWindow = function()
{
	this.windowNode.style.display = "block";
}