/**
 * Slightly modified form of the stuff found at http://sonspring.com/journal/jquery-iframe-sizing
 *
 * Requires jQuery.
 *
 * ddm
 **/

$(document).ready(function(){

	getViewportHeight = function ()
						{
							//alert("client height = " + document.body.clientHeight + "\nscroll height = " + document.body.scrollHeight + "\noffset height = " + document.body.offsetHeight);
							//return document.body.clientHeight;
							 var viewportheight;
							 
							 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
							 if (typeof window.innerWidth != 'undefined')
							 {
								  viewportheight = window.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.clientHeight !=
								 'undefined' && document.documentElement.clientHeight != 0)
							 {
								   viewportheight = document.documentElement.clientHeight
							 }
							 
							 // older versions of IE						 
							 else
							 {
								   viewportheight = document.getElementsByTagName('body')[0].clientHeight
							 }

							return viewportheight;
						}

	// Set specific variable to represent all iframe tags.
	var iFrames = document.getElementsByTagName('iframe');

	// Resize heights.
	function iResize()
	{
		// Iterate through all iframes in the page.
		for (var i = 0, j = iFrames.length; i < j; i++)
		{
			// Set inline style to equal the body height & width of the iframed content.
			iFrames[i].style.height = getViewportHeight()-100 + 'px';
			iFrames[i].style.width = iFrames[i].contentWindow.document.body.scrollWidth+20 + 'px';
			iFrames[i].dialog('position', 'center', iFrames[i].dialog('position', 'center'));
		}
	}

	// Check if browser is Safari or Opera.
	if ($.browser.safari || $.browser.opera)
	{
		// Start timer when loaded.
		$('iframe').load(function()
			{
				setTimeout(iResize, 0);
			}
		);

		// Safari and Opera need a kick-start.
		for (var i = 0, j = iFrames.length; i < j; i++)
		{
			var iSource = iFrames[i].src;
			iFrames[i].src = '';
			iFrames[i].src = iSource;
		}
	}
	else
	{
		// For other good browsers.
		$('iframe').load(function()
			{
				//Set width to best guess in case browser security chokes
				try{
					width = this.contentWindow.document.body.scrollWidth+20;
				} catch (ex) {
					width = 789;
				}
				// Set inline style to equal the body height & width of the iframed content.
				this.style.height = parent.getViewportHeight()-100 + 'px';
				this.style.width = width + 'px';
				$(this).dialog('position', 'center', $(this).dialog('position', 'center'));
			}
		);
	}
});

