/** CenterScale by Øyvind Nordhagen / Allegro Interaktiv.
  *
  * @copyright	2009 Allegro Interaktiv AS
  * @version	1.0
  * @updated	21.1.2009
  */

var scaleeName;					// String id of the scalee, used to find reference to scalee
var scalee;						// The element to be scaled
var originalWidth;				// Original width of scalee, used to calculate height fraction
var originalHeight;				// Original heiht of scalee, used to calculate height fraction
var avoidElements;				// Array of page elements to read height from. Result is reservedHeight
var reservedHeight = 0;			// Any number of pixels to deduct from the allowed height of scalee
var readAvoidHeightEachPass;	// To allow for continous update of the available height if avoidElements are scalable as well
var runOnLoaded;				// To allow one initial run once DOM is loaded
var proportionalScaling;		// Scale content proportionally or fill available space
var firstRun = true;

/** Function, called on resizing the browser window
  */

function resize()
{
	var viewportwidth;
	var viewportheight;
	var window2 = window;
	var scalee2 = scalee;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window2.innerWidth != 'undefined')
	{
	     viewportwidth = window2.innerWidth;
	     viewportheight = window2.innerHeight;
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
	      viewportwidth = document.documentElement.clientWidth;
	      viewportheight = document.documentElement.clientHeight;
	}
	
	// older versions of IE
	
	else
	{
	      viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	      viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	if (readAvoidHeightEachPass)
	{
		for (var i = 0; i < avoidElements.length; i++)
		{
			reservedHeight += document.getElementsById( avoidElements[i] ).height;
		}		
	}
	
	scalee2.height = viewportheight - reservedHeight ;
	
	// Resize proportionally, width follows height
	if (proportionalScaling)
	{
		scalee2.width = (scalee2.height / originalHeight) * originalWidth;
	}
	// Resize improportionally
	else
	{
		scalee2.width = viewportwidth;
	}

	
	// Calculate side margins to maintain center and apply
	var newMargin = String(Math.round((scalee.width - viewportwidth) / 2) * -1) + 'px';
	scalee2.style.marginLeft = newMargin;
	scalee2.style.marginRight = newMargin;
	
	if (firstRun == true)
	{
		scalee2.sendSizeToFlash(scalee2.width, scalee2.height);
		firstRun = false;
	}

	
	// If a trace text field is present output the calculated margin
	if (document.getElementById('out'))
	{
		document.getElementById('out').value = scalee.style.marginLeft;
	}
}

/** Function, called when DOM is ready.
  */

function initScaling()
{

	scalee = document.getElementById( scaleeName );

	if (!readAvoidHeightEachPass)
	{
		for (i = 0; i < avoidElements.length; i++)
		{
			reservedHeight += document.getElementById(avoidElements[i]).clientHeight;
		}		
	}
	
	originalWidth = scalee.width;
	originalHeight = scalee.height;
	
	if (runOnLoaded)
	{
//		window.onload = resize; <- funker ikke i webkit
	resize();
	}
	window.onresize = resize;
}


/** Function used from page to set up the scaling properties.
  *
  * @param		$scalee							String		String id of the scalee, used to find reference to scalee
  * @param		$runOnLoaded					String		To allow one initial run once DOM is loaded
  * @param		$avoidElements					String		Array of page elements to read height from. Result is reservedHeight
  * @param		$readAvoidHeightEachPass		String		To allow for continous update of the available height if avoidElements are scalable as well
  */
  
function setupScaler($scalee, $runOnLoaded, $avoidElements, $readAvoidHeightEachPass, $proportionalScaling)
{
	scaleeName = $scalee;
	runOnLoaded = $runOnLoaded;
	avoidElements = $avoidElements;
	readAvoidHeightEachPass = $readAvoidHeightEachPass;
	proportionalScaling = $proportionalScaling;

	initScaling();
}

function doScaling(){
	setupScaler('Main', true, avoids);
}


