/*			designer.js

set body to window height & width

- Get window height & width.
- Get header height & width (= nav menu).
- Difference is workheight.
- Locate object.
- for each parentNode until body, set height & width = workheight & workwidth.
- recalculate each time window or header (text size) is changed.

*/


//=========================
// execute on page load
//=========================
//AddEvent(window, "load", jsDesignerInit);


//=========================
// global variables
//=========================
// id of the Flash object
var sFlashObjectId = "tma";

var sHeaderId = "sitenav";

// global memory of the header object
var oDesignerHeader = null;

// global memory of the Flash object
var oDesignerObject = null;


//=========================
function jsDesignerInit()
//=========================
{
//alert("function jsDesignerInit()");

		if (!document.getElementById) return;
		if (!document.getElementsByTagName) return;

	// locate Flash object
	oDesignerObject = document.getElementById(sFlashObjectId);
		if (!oDesignerObject) return;
	
	// locate the header (if any)
	oDesignerHeader = document.getElementById(sHeaderId);

//alert("oDesignerHeader = " + oDesignerHeader);
	
	// resize the container nest each time the user resizes the window or the header (text size)
	AddEvent(window, "resize", jsResizeFlash);
	AddEvent(oDesignerHeader, "resize", jsResizeFlash);

	// now resize containers for the first time
	jsResizeFlash();
}


//=========================
function jsResizeFlash(evt)
//=========================
// When the window is resized, resize the Flash object's containers
{
/*
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;
*/

//alert("function jsResizeFlash()");

	var iWindowHeight = jsElementSize(window, 'height');
	var iWindowWidth = jsElementSize(window, 'width');

	var iHeaderHeight = jsElementSize(oDesignerHeader, 'height');
	var iHeaderWidth = jsElementSize(oDesignerHeader, 'width');
/*
alert("self.innerHeight = " + self.innerHeight +
	"\n" + "document.documentElement.clientHeight = " + document.documentElement.clientHeight +
	"\n" + "document.body.clientHeight = " + document.body.clientHeight );


alert("iWindowHeight = " + iWindowHeight + 
		"\n" + "iWindowWidth = " + iWindowWidth + 
		"\n" + "iHeaderHeight = " + iHeaderHeight + 
		"\n" + "iHeaderWidth = " + iHeaderWidth );
*/
alert("height = " + (iWindowHeight - iHeaderHeight * 2) + 
		"\n" + "width = " + (iWindowWidth  - iHeaderWidth) );

	// apply height & width to Flash embed
	var sObjectTag = "xEMBED";
	var aEmbed = document.getElementsByTagName(sObjectTag);
		if (aEmbed.length == 0)
		{
			sObjectTag = "OBJECT";
			aEmbed = document.getElementsByTagName(sObjectTag);
		}
//alert(sObjectTag + ".length = " + aEmbed.length);
	var oEmbed = aEmbed[0];
		oEmbed.style.height = (iWindowHeight - iHeaderHeight * 2) + "px";
		//oEmbed.style.width =  (iWindowWidth  - iHeaderWidth) + "px";
		//oEmbed.offsetheight = (iWindowHeight - iHeaderHeight * 2);
		//oEmbed.offsetwidth =  (iWindowWidth  - iHeaderWidth);
/*
	// apply height & width to Flash object's ancestors
	var oParent = oDesignerObject.parentNode;
	while (oParent.tagName != "BODY")
	{
		oParent.style.height = (iWindowHeight - iHeaderHeight * 2) + "px";
		oParent.style.width =  (iWindowWidth  - iHeaderWidth) + "px";

		oParent = oParent.parentNode;
	}
*/
//document.body.style.height = (iWindowHeight - iHeaderHeight * 2) + "px";
//document.body.style.width =  (iWindowWidth  - iHeaderWidth) + "px";
document.body.offsetheight = (iWindowHeight - iHeaderHeight * 2);
//document.body.offsetwidth =  (iWindowWidth  - iHeaderWidth);
}


//=========================
function jsElementSize(argElement, argDimension)
//=========================
{
	//alert("function jsElementSize(" + argElement + ", " + argDimension + ")");
	
	var iSize = 0;
	
		if (argElement)
		{
			if (argElement.tagName && argElement.tagName != "window")
			{
				switch (argDimension)
				{
					case 'height':
							if (argElement.pixelHeight)
							{
								iSize = argElement.pixelHeight;
//alert("argElement.pixelHeight = " + iSize);
							}
							else if (argElement.offsetHeight)
							{
								iSize = argElement.offsetHeight;
//alert("argElement.offsetHeight = " + iSize);
							}
							else
							{
								iSize = argElement.style.height;
//alert("argElement.style.height = " + iSize);
							}
					break;

					case 'width':
							if (argElement.pixelWidth)
							{
								iSize = argElement.pixelWidth;
//alert("argElement.pixelWidth = " + iSize);
							}
							else if (argElement.offsetWidth)
							{
								iSize = argElement.offsetWidth;
//alert("argElement.offsetWidth = " + iSize);
							}
							else
							{
								iSize = argElement.style.width;
//alert("argElement.style.width = " + iSize);
							}
					break;
				}
			}
			else
			{
				//alert("function jsElementSize(" + argElement + ", " + argDimension + ")");
				switch (argDimension)
				{
					case 'height':			
							if (document.documentElement && document.documentElement.clientHeight)			// Explorer 6 Strict Mode
							{
								iSize = document.documentElement.clientHeight;
//alert("document.documentElement.clientHeight = " + iSize);
							}
							else if (self.innerHeight) // all except Explorer
							{
								iSize = self.innerHeight;
//alert("self.innerHeight = " + iSize);
							}
							else if (document.body) // other Explorers
							{
								iSize = document.body.clientHeight;
//alert("document.body.clientHeight = " + iSize);
							}
						break;

					case 'width':			
							if (document.documentElement && document.documentElement.clientWidth)			// Explorer 6 Strict Mode
							{
								iSize = document.documentElement.clientWidth;
							}
							else if (self.innerWidth) // all except Explorer
							{
								iSize = self.innerWidth;
							}
							else if (document.body) // other Explorers
							{
								iSize = document.body.clientWidth;
							}
						break;
				}
			}
		}
	
	//alert("function jsElementSize(" + argElement + ", " + argDimension + ") = " + iSize);

	return iSize;
}


//=========================
function AddEvent(oElement, sEventName, fnFunction)
//=========================
{
	if (oElement)
	{
		if (oElement.attachEvent)
		{
			oElement.attachEvent("on" + sEventName, fnFunction);
		}
		else
		{
			oElement.addEventListener(sEventName, fnFunction, true);
		}
	}
}


// The following function is called from Flash to simulate a browser Back button click:
//=========================
function jsBack(argDefaultURL)
//=========================
{
	// if there's a history, go back one page
	if (window.history.length)
	{
		window.history.back();
	}
	// else if there's a default URL go there
	else if (argDefaultURL)
	{
		document.location = argDefaultURL;
	}
	// otherwise do nothing
}
