// JavaScript Document

function WindowManager(doc)
{
	this._doc = doc;
	this._windows = new Array();	//All windows attached to the document tree
	this._windowsNA = new Array();		//All windows not attached to the document tree
	
	this._TOPMOSTZINDEX = 899;
	
	var _this = this;	
	this._funcPtrHandleZIndex = function(e) { _this.handleZIndex(e, wnd) };

	
	//this._funcPtrScrollJump = function(e) { _this.ScrollJump(e) };

}

WindowManager.prototype.AddWindow = function(wnd)
{
	//alert("WindowManager::AddWindow: id: " + wnd.GetId());
	var _this = this;
	wnd.GetElem().addEventListener("mousedown", function(e) { _this.handleZIndex(e, wnd) }, false);
	
	//Add handler for X button on titlebar
	var titlebarX = wnd.GetTitlebarX();
	//In case no titlebar was created
	if (titlebarX != null)
	{
		titlebarX.addEventListener("mousedown", function() { _this.HideWindow(wnd) }, false);
	}
	
	//this._windows.push(wnd);
	this._doc.body.removeChild(wnd.GetElem())
	this._windowsNA.push(wnd);
	
	//this.AppendWindow("Home");
}

WindowManager.prototype.AppendWindow = function(id)
{
	//alert(this._windowsNA.length + " " + this._windows.length);
	
	for(i=0; i < this._windowsNA.length; i++)
	{
		if (this._windowsNA[i].GetId() == id)
		{
			//Save the window
			var wnd = this._windowsNA[i];
			//Delete 
			this._windowsNA.splice(i,1);
			//Add
			this._windows.push(wnd);
			//Append to body
			this._doc.body.appendChild(wnd.GetElem());
			
			this.OnResize();
			
			wnd.Center();
			wnd.CenterVert();
			wnd.Fade(0.25,20);						
		}
	}
	
	this.SetFocus(id);
	
	//alert ("WindowManager::AppendWindow: Window with id: " + id + " not found umong non appended nodes.");
}

WindowManager.prototype.HideWindow = function(wnd)
{
	//alert("WindowManager::HideWindow: " + wnd);
	var _this = this;
	wnd.SetOnFadeDone( function() { _this.RemoveWindow(wnd) } );
	wnd.Fade(-0.25,20);		
}

WindowManager.prototype.RemoveWindow = function(wnd)
{
	//window.status = "RemoveWindow " + wnd.GetId();
	
	//this._windowsNA.push( this._doc.body.removeChild(wnd.GetElem()) );
	
	this._doc.body.removeChild(wnd.GetElem())
	this._windowsNA.push(wnd);
	
	//Delete 
	for(i=0; i < this._windows.length; i++)
	{
		if (this._windows[i].GetId() == wnd.GetId())
		{
			this._windows.splice(i,1);
		}
	}
}

WindowManager.prototype.OnResize = function()
{	
	//alert("WindowManager::OnResize");
	var wnd;
	var top, left;
	for(i=0; i < this._windows.length; i++)
	{
		//this._windows[i].Center();
		wnd = this._windows[i];
		top = wnd.GetTop();
		left = wnd.GetLeft();
		
		if ( !isNaN(top) && !isNaN(left) )
		{
			//Check if window is outside the bottom
			if ( top + wnd.GetHeight() > window.innerHeight )
				top = window.innerHeight - wnd.GetHeight();
			//Check if window is outside the right side
			if ( left + wnd.GetWidth() > window.innerWidth )
				left = window.innerWidth - wnd.GetWidth();
				
			if ( left < 0 )
				left = 0;
			if ( top < 0 )
				top = 0;
				
			//window.status = wnd.GetId() + " " + top + " " + left;
				
			wnd.SetTop(top);
			wnd.SetLeft(left);
		}
		//else
		//	alert("werwer");
	}
}

WindowManager.prototype.handleZIndex = function(e, wnd)
{
	//alert(e + " " + wnd.GetId());
	
	//Do nothing if window already is topmost
	//except when moving (zIndex == 1000), in this case it may not be topmost 
	if (wnd.GetZIndex() == this._TOPMOSTZINDEX) 
		return;
		
	this.SetFocus(wnd.GetId());
}

WindowManager.prototype.SetFocus = function(wndId)
{
	//Move wnd to first pos in the array, leave the rest
	var tmpWnd = null;
	var thisWnd;
	var tmpLength = this._windows.length;
	
	for(i=0; i < tmpLength; i++)
	{
		tmpWnd = this._windows.shift();
		if ( tmpWnd.GetId() != wndId )
		{
			this._windows.push(tmpWnd);
		}
		else
		{
			thisWnd = tmpWnd;
		}
		//alert("tmpWnd " + tmpWnd.GetId() + " " + tmpWnd.GetZIndex());
	}
	this._windows.unshift(thisWnd);
	
	var zIndex = this._TOPMOSTZINDEX;	//Windows should be in range 800 - 899
	for(i=0; i <this._windows.length; i++)
	{
		this._windows[i].SetZIndex(zIndex);	
		this._windows[i].SetDefaultZIndex(zIndex);	
		zIndex--;
	}
}

WindowManager.prototype.GetWindow = function(wndId)
{
	for(i=0; i < this._windows.length; i++)
	{
		if ( this._windows[i].GetId() == wndId )
		{
			return this._windows[i];
		}
	}
	
	for(i=0; i < this._windowsNA.length; i++)
	{
		if ( this._windowsNA[i].GetId() == wndId )
		{
			return this._windowsNA[i];
		}
	}
}