/**
 *	IFRAME JavaScript Class
 *
 *	Resizes the iframe to a 100% height and width that suits the website best.
 *	It fetches the height or width from the elements you specify and subtracts that
 *	amount from the clients' height or width. The outcome of that will be the height or width
 *	of the iframe on the page.
 *	
 *	When the width or height doesn't fit correctly you can specify a increment (make the iframe smaller)
 *	or decrement (make the iframe bigger) for the height or width to make it fit correctly.
 *	
 *	Created by: Stefan Leever
 *	Created on: 03-06-09
 *	
 *	Copyright Webdesigning.nl            
 *  
 */  

var iframe = {
	
	'init': function () {
		// Initialize all the values needed:
		iframe.widthElements 	= new Array();
		iframe.heightElements 	= new Array();
		iframe.decrementHeight	= 0;
		iframe.incrementHeight	= 0;
		iframe.decrementWidth	= 0;
		iframe.incrementWidth	= 0;
	},
	
	'setWidthElement': function (elementName)
	{
		// Fetch the current length of the iframe.
		var currentLength = iframe.widthElements.length;
		// Check the currentLength -1 and perform a few actions:
		if (Math.round(currentLength-1) == '-1')
			var length = 0;
		else if (Math.round(currentLength) == '1')
			var length = 1;
		else
			var length = Math.round(currentLength-1);
		// Set the elementName in the array.
		iframe.widthElements[length] = elementName;
	},
	
	'setHeightElement': function (elementName)
	{
		// Fetch the current length of the iframe.
		var currentLength = iframe.heightElements.length;
		// Check the currentLength -1 and perform a few actions:
		if (Math.round(currentLength-1) == '-1')
			var length = 0;
		else if (Math.round(currentLength) == '1')
			var length = 1;
		else
			var length = Math.round(currentLength-1);
		// Set the elementName in the array.
		iframe.heightElements[length] = elementName;
	},
	// To make the iframe smaller use this method:
	'setIncrementHeight': function (value)
	{
		iframe.incrementHeight = Math.round(iframe.incrementHeight + value);
	},
	// To make the iframe bigger use this method:
	'setDecrementHeight': function (value)
	{
		iframe.decrementHeight = Math.round(iframe.decrementHeight + value);
	},
	// To make the iframe smaller use this method:
	'setIncrementWidth': function (value)
	{
		iframe.incrementWidth = Math.round(iframe.incrementWidth + value);
	},
	// To make the iframe bigger use this method:
	'setDecrementWidth': function (value)
	{
		alert(value);
		iframe.decrementWidth = Math.round(iframe.decrementWidth + value);
	},
	
	'getClientXY': function()
	{
		var x,y;
		if (self.innerHeight) // all except Explorer
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		
		value = new Array();
		value[0] = x;
		value[1] = y;
		return value;
	},
	
	'resize': function ()
	{
		// Search for all the elements with the class page-content.
		var elements 		= document.getElementsByClassName('page-content');
		var iframeElement 	= elements[0]; // Fetch the first element. There's only 1 iframe on the page.
		// Check if the iframe is available.
		if (!iframeElement)
			return false;
		// Check if the element we've found is a IFRAME.
		if (iframeElement.nodeName != 'IFRAME')
			return false;
			
		// Fetch the client height:
		coordinates = iframe.getClientXY();
	
		// CALCULATE THE HEIGHT VALUE:
		
		var iframeHeight = iframe.calculateHeight(coordinates);
		
		// Set the new height for the iframe:
		iframeElement.style.height 	= iframeHeight+'px';
		iframeElement.height 		= iframeHeight;
		
		// CALCULATE THE WIDTH VALUE:
		
		var iframeWidth  = iframe.calculateWidth(coordinates);
		
		// Set the new width for the iframe:
		iframeElement.style.width 	= iframeWidth+'px';
		iframeElement.width 		= iframeHeight;
		
		iframeElement.scrollBottom;
	},
	
	'calculateHeight': function(coordinates)
	{
		// Subtract the height first from all the elements:
		var subtractHeight = 0; // Set the height.
		for (var i = 0; i < iframe.heightElements.length; i++)
		{
			subtractHeight = Math.round(subtractHeight + document.getElementById(iframe.heightElements[i]).offsetHeight);
		}
		
		// Check for the optional decrement or increment (increment has more priority then decrement, only 1 value can be specified).
		if (iframe.incrementHeight != 0 && iframe.decrementHeight == 0)
			subtractHeight = Math.round(subtractHeight + iframe.incrementHeight);
			
		if (iframe.decrementHeight != 0 && iframe.incrementHeight == 0)
			subtractHeight = Math.round(subtractHeight - iframe.decrementHeight);
		
		// Subtract the height from the Y:
		var iframeHeight = Math.round( coordinates[1] - subtractHeight );
		
		return iframeHeight;
	},
	
	'calculateWidth': function(coordinates)
	{
		// Subtract the height first from all the elements:
		var subtractWidth = 0; // Set the width.
		for (var i = 0; i < iframe.widthElements.length; i++)
		{
			subtractWidth = Math.round(subtractWidth + document.getElementById(iframe.widthElements[i]).offsetWidth);
		}
		// Check for the optional decrement or increment (increment has more priority then decrement, only 1 value can be specified).
		if (iframe.incrementWidth != 0 && iframe.decrementWidth == 0)
			subtractWidth = Math.round(subtractWidth + iframe.incrementWidth);
			
		if (iframe.decrementWidth != 0 && iframe.incrementWidth == 0)
			subtractWidth = Math.round(subtractWidth - iframe.decrementWidth);
			
		// Subtract the height from the Y:
		var iframeWidth = Math.round( coordinates[0] - subtractWidth );
		return iframeWidth;
	}
	
}

