/* 
	$Id: checkWindowSize.js,v 1.1 2007/07/04 06:22:44 simon Exp $
	Author : Simon Haddon simon@sibern.com.au
	Purpose: Functions to get the dimensions of the browser window .
	         Now allows a R/H Margin as well for the setIframe

	Help from
		http://www.quirksmode.org/viewport/compatibility.html
		http://guymal.com/mycode/100_percent_iframe/
*/

var checkWindowSizeList = new Array();
var checkWindowSizeListId = 0;
var checkWindowSizeThreshold = 20;

function getWindowWidth () 
{
	var x;

	if (self.innerHeight) // all except Explorer
		x = self.innerWidth;
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
	else if (document.body) // other Explorers
		x = document.body.clientWidth;

	return x;
}

function getWindowHeight () 
{
	var y;

	if (self.innerHeight) // all except Explorer
		y = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
		y = document.documentElement.clientHeight;
	else if (document.body) // other Explorers
		y = document.body.clientHeight;

	return y;
} 

function calcHeight(iFrameName)
{
  //find the height of the internal page
  var the_height = getWindowHeight();

  //change the height of the iframe
  document.getElementById(iFrameName).height= the_height;
  document.getElementById(iFrameName).style.height= the_height;
  //alert('Calculated height as '+the_height);
}

function findPosX(obj)
{
	// The script is very simple. Hand it the object whose position should be calculated 
	// and set the variable curleft to 0:
	var curleft = 0;

	// If the browser supports offsetParent
	if (obj.offsetParent)
	{
		// go into a loop that continues as long as the object has an offsetParent:
		// The while loop repeats this process as long as the element has an offsetParent. 
		// When it has no more offsetParent, the HTML element is reached and we have the position 
		// relative to it (in other words: relative to the entire document).
		while (obj.offsetParent)
		{
			// Add the offsetLeft of the element relative to the offsetParent to curleft and 
			// set the object to this offsetParent.
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		// As to browser supporting x (Netscape 4):
		// take the x property of the link (it doesn't work on any other element).
		curleft += obj.x;

	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// Array hunt
function findIframeIndex(iFrameName)
{
	var x = 0;
	var retVal = -1;

	for (x = 0; x < checkWindowSizeList.length; x++)
	{
		if (checkWindowSizeList[x]['id'] == iFrameName)
			retVal = x;
			
	}
	return retVal;
}

// Check a threshold value to see if we need to redraw the map or not
function redrawContents(iFrameName, newFrameHeight ,newFrameWidth)
{
	var indexPos = findIframeIndex(iFrameName);
	var retVal = true;

	if (indexPos < 0)
		retVal = true;
	else if (checkWindowSizeList[indexPos]['ifThresholdExceeded'])
	{
		// If the window requires a threshold to be exceeded then ok.
		if (Math.abs(checkWindowSizeList[indexPos]['frameHeight'] - newFrameHeight) < checkWindowSizeThreshold
				&& Math.abs(checkWindowSizeList[indexPos]['frameWidth'] - newFrameWidth) < checkWindowSizeThreshold)
			retVal = false;
		else
			retVal = true;
	}
	else
		retVal = true;
	
	return retVal;
}

// Store the parameters for later window resize checking
function setParams(iFrameName, frameHeight, frameWidth, ifThresholdExceeded)
{
	var indexPos = findIframeIndex(iFrameName);
	
	if (indexPos < 0)
	{
		checkWindowSizeList[checkWindowSizeListId] = new Array(4);
		checkWindowSizeList[checkWindowSizeListId]['id'] = iFrameName;
		checkWindowSizeList[checkWindowSizeListId]['frameHeight'] = frameHeight;
		checkWindowSizeList[checkWindowSizeListId]['frameWidth'] = frameWidth;
		checkWindowSizeList[checkWindowSizeListId]['ifThresholdExceeded'] = ifThresholdExceeded;
		checkWindowSizeListId = checkWindowSizeListId + 1;
	}
	else
	{
		checkWindowSizeList[indexPos]['frameHeight'] = frameHeight;
		checkWindowSizeList[indexPos]['frameWidth'] = frameWidth;
		checkWindowSizeList[indexPos]['ifThresholdExceeded'] = ifThresholdExceeded;
	}
}

// Set an iFrame size and optionally go to the associated url
function setIframe(offsetElementName, iFrameName, url, footer, rhMargin, ifThresholdExceeded)
{
	var frameWidth = getWindowWidth();
	var frameHeight = getWindowHeight();
	//var heightOffset = document.getElementById(offsetElementName).offsetTop;
	var heightOffset = findPosY(document.getElementById(offsetElementName));
	var widthOffset = findPosX(document.getElementById(offsetElementName));
	var defaultFooter = 30;
	var defaultMargin = 170;
	var newURL = "";
	if (footer == 0)
	  footer = defaultFooter;
	if (rhMargin == 0)
	  rhMargin = defaultMargin;
	if (ifThresholdExceeded == undefined)
		ifThresholdExceeded = false;
	  
	//alert('offsetElementName = ' +offsetElementName + ' iFrameName = ' + iFrameName+
	//      '\nFrameHeight = '+frameHeight+' offset = '+heightOffset+' footer = '+footer+
	//      '\nFrameWidth = '+frameWidth+' offset = '+widthOffset+' R/H Margin = '+rhMargin);

	frameHeight = frameHeight - heightOffset - footer;
	frameWidth = frameWidth - widthOffset - rhMargin;
	document.getElementById(iFrameName).height= frameHeight;
	document.getElementById(iFrameName).width = frameWidth;
	//fh = document.getElementById(iFrameName).height;
	//fw = document.getElementById(iFrameName).width;
	//alert('New frameHeight = '+fh+', new frameWidth = '+fw);
	
	if (url != "" && redrawContents(iFrameName, frameHeight ,frameWidth))
	{
		newURL = url + "&frameWidth=" + frameWidth + "&frameHeight=" + frameHeight;
		document.getElementById(iFrameName).src = newURL;
	}

	// Store the params
	setParams(iFrameName, frameHeight, frameWidth, ifThresholdExceeded);
	
}

