//----------------------- NEW -----------------------------------// 

//Layout object
function TLayout()
{
	//Settings
	this.bClearMenuOnLoad = false;	//Clears top menu on each new page request (except silent requests), should be false under dedicated mode
	
	//Centered layout? (If false then it floats to the left)
	this.bCenteredLayout = true;
	
	//Defaults
	this.nDefaultBodyWidth = 800;	//Needs to be set to the defaults of the CSS layout (divMasterBody)
	this.nDefaultBodyHeight = 0;
	this.nDefaultInnerBodyHeight = 0;
	this.nDefaultFooterWidth = 738;	//Needs to be set to the defaults of the CSS layout (divFooter)
	this.nDefaultDocumentWidth = 0;	
	
	//Do not touch
	this.bFirstLoad = false;
	this.tCKList = Array();
	this.nCK = 0;
	this.tTempQueue = Array();
	this.nTempQueue = 0;	//Number of items in the temporary queue which need to be removed from the DOM on net page load
		
	//Stores info about collapseable divs
	this.aCollapseInfo = Array();		
	
	//Returns co-ordinates which might be useful for apps which require popup boxes
	this.fnGetFreeRight = function()
	{
		return $("#masterBody").offset().left + $("#masterBody").width();
	}
		
	//Adjusts layout (keeps it in size with documents height and width
	this.fvAdjust = function()
	{
		//Centers the layout if required
		if(this.bCenteredLayout)
		{
			nWidth = (($(document).width() - Layout.nDefaultBodyWidth) / 2) - $("#navigationLeft").width();
			
			try
			{
			document.getElementById("spaceColumn").style.width = nWidth.toString() + "px";		
			document.getElementById("spaceColumn").innerHTML = "&nbsp;";			
			} catch (e) {}
			
			nLeft = $("#navigationLeft").width() + $("#navigationLeft").offset().left;
			
			$("#masterBody").css("left", nLeft + "px");
			$("#divMenu").css("left", (nLeft+2) + "px");
			
			//Make sure the header nav doesn't float to a strange location
			if(nWidth <= 0)
				nWidth = 0;
							
			$("#headerNav").css("left", nWidth + "px");
			
			//Min width of the header strip
			document.getElementById("header").style.minWidth = nWidth + $("#navigationLeft").width() + 1 + Layout.nDefaultBodyWidth + "px";
			
			//Center miscellaneous stuff
			$("#subPage").css("left", nLeft + "px");
			
			nLeft += (this.nDefaultBodyWidth / 2) - ($("#errorbox").width() /2);
			
			//$("#errorbox").css("left", nLeft + "px");
			//$("#infobox").css("left", nLeft + "px");
			//$("#debugbox").css("left", nLeft + "px");			
		}		
		
	}

	//Adds an item to temp quue
	this.fvAddToFlushQueue = function(p_sDiv)
	{
		this.nTempQueue++;
		this.tTempQueue[this.nTempQueue] = p_sDiv;
	}
	
	//Removes an item from the flush queue
	this.fvRemoveFromFlushQueue = function(p_sDiv)
	{
		for(nX=0;nX<this.nTempQueue;nX++)
		{
			if(this.tTempQueue[nX] == p_sDiv)
			{
				this.tTempQueue[nX] = "[removed]";
			}
		}
	}
	
	//Flushes temp queue
	this.fvFlushTempQueue = function(p_sSkip)
	{
		if(p_sSkip == null)
			p_sSkip = "";
			
		if(this.nTempQueue > 0)
		{
			for(nX=1;nX<=this.nTempQueue;nX++)
			{		
				try
				{
					if(p_sSkip.indexOf( this.tTempQueue[nX] ) == -1)
						$("#" + this.tTempQueue[nX]).remove();
				}
				catch (e) {}
			}
		}
	}	
		
	//Blurs and darkens the screen
	this.fvBlurScreen = function()
	{
		$("#" + divBlackbox).width($(document).width());
		$("#" + divBlackbox).height($(document).height());
		$("#" + divBlackbox).css('filter', 'alpha(opacity=60)');	//Needed for IE			
		$("#" + divBlackbox).fadeIn('slow');
	}
	
	//Undo's the above
	this.fvUnblurScreen = function()
	{	
		$("#" + divBlackbox).fadeOut('slow');
	}
	
	//Adjusts the body container width
	this.fvAdjustBodyWidth = function(p_sWidth)
	{
		nDif = this.nDefaultFooterWidth + (p_sWidth - this.nDefaultBodyWidth);	
	
		$("#" + divMasterBody).width(p_sWidth);
		$("#" + divBody).width(p_sWidth);
		$("#" + divFooter).width(nDif);
	}

	//Adjusts the body container height
	this.fvAdjustBodyHeight = function(p_sHeight)
	{
		$("#" + divBody).css("min-height", p_sHeight + "px");
		this.fvAdjust(true);
	}	
	
	
	//Returns the scrollbar's width (if any)
	this.fnScrollbarWidth = function() 
	{
    	var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
   
   		// Append our div, do our calculation and then remove it
		$('body').append(div);
		var w1 = $('div', div).innerWidth();
		div.css('overflow-y', 'scroll');
		var w2 = $('div', div).innerWidth();
		$(div).remove();
		return (w1 - w2);
	}	
	
	//Resets divs to default
	this.fvResetDivs = function()
	{
		tDependancies.clearAllDivs();	
		this.fvFlushTempQueue();		
		$("#" + divMasterBody).width(this.nDefaultBodyWidth);
		$("#" + divBody).width(this.nDefaultBodyWidth);
		$("#" + divFooter).width(this.nDefaultFooterWidth);					
		//$("#" + divBody).height(this.nDefaultInnerBodyHeight);	
		$("#" + divBody).css("min-height", this.nDefaultInnerBodyHeight + "px");
	}
	
	//Clears menu
	this.fvClearMenu = function()
	{	
		if(this.bClearMenuOnLoad == true)
		{	
			try
			{
				$("#" + divMenu).html(""); 
			} 
			catch (e) {} //Clear menu bar
		}
	}
	
	//Clears out divs which should not be displayed if the user is not authenticated
	this.fvClearDivs = function()
	{
		$("#" + divProfilePic).html("");
		$("#" + divApps).html("");
		$("#" + divNotifications).html("");
	}
	
	//Support for dynamically adding CKEditor
	this.CKElement = function(p_sName, p_sWidth, p_sHeight) 
	{
		this.sName = p_sName;
		this.sWidth = p_sWidth;
		this.sHeight = p_sHeight;
	}	
	
	//Adds an object into the CK collection 
	this.fvAddToCK = function(p_sName, p_sWidth, p_sHeight)
	{	
		this.tCKList[this.nCK] = new this.CKElement(p_sName, p_sWidth, p_sHeight);
		this.nCK++;
	}
	
	

	//Triggered when the CK editor loads
	this.fvCKLoad = function()
	{
		for(nX=0;nX<this.nCK;nX++)
		{
			try
			{
				CKEDITOR.replace( this.tCKList[nX].sName, {height:this.tCKList[nX].sHeight, width:this.tCKList[nX].sWidth} );
			}
			catch (e) {}
		}
		this.tCKList= Array();
		this.nCK=0;
	}
	
	//Clears a collapseable div
	this.fvClearCollapseDiv = function(p_sCollapseImg)
	{
		try
		{
			this.aCollapseInfo[p_sCollapseImg] = null;	
		}
		catch (e)
		{
			
		}
	}
	
	//Collapsable div
	this.fvCollapseDiv = function(p_sCollapseImg, p_sCollapseDiv, p_sMaxImage)
	{
		try
		{
						
			if( this.aCollapseInfo[p_sCollapseImg][2] == true)
			{
				this.aCollapseInfo[p_sCollapseImg][2] = false;
				$("#" + this.aCollapseInfo[p_sCollapseImg][1]).show();
				document.getElementById(p_sCollapseImg).src = this.aCollapseInfo[p_sCollapseImg][0];				
			}
			else
			{
				this.aCollapseInfo[p_sCollapseImg][2] = true;
				$("#" + this.aCollapseInfo[p_sCollapseImg][1]).hide();	
				document.getElementById(p_sCollapseImg).src = p_sMaxImage;
			}
			
		}
		catch (e)
		{

			this.aCollapseInfo[p_sCollapseImg] = Array();
			this.aCollapseInfo[p_sCollapseImg][0] = document.getElementById(p_sCollapseImg).src;
			this.aCollapseInfo[p_sCollapseImg][1] = p_sCollapseDiv;
			this.aCollapseInfo[p_sCollapseImg][2] = true; //Collapsed
			document.getElementById(p_sCollapseImg).src = p_sMaxImage;
			$("#" + this.aCollapseInfo[p_sCollapseImg][1]).hide();
									
		}
	}	
		
	//Default events
	window.onresize = function()
	{
		Layout.fvAdjust();					
	}
	
	window.onload = function()
	{
		if(Layout.bFirstLoad == false)
		{
			Layout.bFirstLoad = true;
			
			//Initialize sizes
			Layout.nDefaultDocumentWidth = $(document).width();
			Layout.nDefaultBodyWidth = $("#" + divMasterBody).width();
			Layout.nDefaultBodyHeight = $("#" + divMasterBody).height();
			Layout.nDefaultFooterWidth = $("#" + divFooter).width();
			Layout.nDefaultInnerBodyHeight = $("#" + divBody).height();
			Layout.nDefaultDocumentWidth = 0;
			
			Layout.fvAdjust();
				
			Main();
		
			//Div default settings
			$("#" + divBlackbox).fadeOut();	
			
			setInterval("fvConnectJS()", 1000);
		}
		
		Layout.fvAdjust();
	}	
}


//Page - div dependancy object
function Dependancy(sURL)
{
	this.URL = sURL;
	this.dependantDivs = new Array();
	this.nDivs = 0;
	this.sOriginals = new Array();
	
	this.addDiv = function(p_sName, p_sOriginal)
	{
		this.nDivs += 1;
		this.dependantDivs[this.nDivs] = p_sName;
		this.sOriginals[this.nDivs] = p_sOriginal;
	}
	
	this.getDiv = function(p_nDiv)
	{
		return this.dependantDivs[p_nDiv];
	}
	
	//Clears all divs
	this.clearDivs = function()
	{
		for(i=1;i<=this.nDivs;i++)
		{
				document.getElementById(this.dependantDivs[i]).innerHTML = this.sOriginals[i];
		}
	}
}

//Dependancies table
function Dependancies()
{
	this.oDO = new Array();
	this.nDeps = 0;
	
	this.addDependancy = function (p_sURL)
	{
		this.nDeps += 1;
		this.oDO[this.nDeps] = new Dependancy(p_sURL);	
	}
	
	this.addDiv = function(p_sDiv, p_sOriginal)
	{
		this.oDO[this.nDeps].addDiv(p_sDiv, p_sOriginal);
	}
	
	this.getDeps = function(p_sURL)
	{
		for(i=1;i<=this.nDeps;i++)
		{
			//Match the URL with the one specified
			if(p_sURL == this.oDO[i].URL)
			{
				return this.oDO[i].dependantDivs;
			}
		}
	}
	
	this.clearDivs = function(p_sURL)
	{
		for(i=1;i<=this.nDeps;i++)
		{
			//Match the URL with the one specified
			if(p_sURL == this.oDO[i].URL)
			{
				this.oDO[i].clearDivs();
			}
		}
	}	

	this.clearAllDivs = function()
	{
		for(i=1;i<=this.nDeps;i++)
		{

			this.oDO[i].clearDivs();
		}
	}
}

//GLOBAL INSTANCES OF THE LAYOUT & DEPENDANCIES SYSTEM GO HERE
//Layout wrapper initialization
Layout = new TLayout();

//Global dependancies table
tDependancies = new Dependancies();

