/**
	Source: tk_frame_resizer.js
	Build: 2.2.13394

	The following code resizes an embedded <iframe> dynamically. This solution is
	fully cross-domain compatible.

	How It Works:
		Essentially, the parent (host) page resizes the <iframe> after it is told
		how tall the page in the <iframe> is.

		Because of the way browsers work, the parent page cannot know the size of
		the page in the <iframe> if they both refer to different domains, only the
		page in the <iframe> can tell. All browsers disallow cross-domain
		communication between pages. It's a security protection that browsers
		have.

		So, to communicate information to the host page, the embedded page uses
		another <iframe> embedded inside it. This other <iframe>, hidden from view,
		refers to the same domain as the top page and is used to communicate the
		height of the page in the first <iframe> to the top page.

		Resize notification is controlled by the page in the first <iframe>. At its
		convenience, it triggers the resizing by setting the src attribute of the
		second <iframe> to a page located on the same domain as the top page and to
		which the page height is appended as a parameter to the URL. When loaded,
		the hidden page calls a Javascript function on the top page to inform it
		of the new height of the <iframe>.

		Bare bone example:
			www.domain1.com/page.html:
				<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
				<head><!-- ... --></head>
				<body>
					<!-- ... -->
					<script type="text/javascript">
						function resizeFrame(height) {
							var iframe = document.getElementById('frame');
							iframe.height = nHeight+"px";
							iframe.style.height = nHeight+"px";
						};
					</script>
					<iframe id="frame" src="http://www.domain2.com/frame.html" allowTransparency="true" frameBorder="0" hspace="0" marginHeight="0" marginWidth="0" scrolling="no" vspace="0"></iframe>
					<!-- ... -->
				</body>
				</html>

			www.domain1.com/frameSizeNotifier.html:
				<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
				<head><!-- Empty! --></head>
				<body>
					<script type="text/javascript">
						var parts = window.location.search.split('=');
						var height = parseInt(parts[1]);
						window.top.resizeFrame(height);
					</script>
				</body>
				</html>

			www.domain2.com/frame.html:
				<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
				<head><!-- ... --></head>
				<body>
					<!-- ... -->
					<iframe id="frameSizeNotifier" style="position:absolute;top:0;left:0;height:1px;width:1px;visibility:hidden;"></iframe>
					<script type="text/javascript">
						var height = Math.max(document.body.offsetHeight, document.body.scrollHeight);
						document.getElementByID('frameSizeNotifier').src =
							'http://www.domain1.com/frameSizeNotifier.html?height='+height;
					</script>
				</body>
				</html>
	Copyright (c) 2005-2010 TreKnowledge Technologies Inc., All rights reserved.
*/

(function() {
	if (typeof TK_APP_WRAPPER_ID === 'undefined') {
		var TK_APP_WRAPPER_ID = 'tkAppWrapper';
	}

	if (typeof TK_APP_FRAME_ID === 'undefined') {
		var TK_APP_FRAME_ID = 'tkAppFrame';
	}

	// Resize effect options
	var resizeCtx = {
		duration: 1000, // ms
		interval: 40, // ms
		timer:    null,
		lastTime: 0,
		state:    0,
		target:   1
	};

	// Page scroll effect options
	var scrollCtx = {
		duration: 1000, // ms
		interval: 40, // ms
		timer:    null,
		lastTime: 0,
		state:    0,
		target:   1
	};

	/**
	*/
	function nextFxTween(oFx) {
		var now = new Date().getTime(),
			movement = ((now - oFx.lastTime)/oFx.duration) * (oFx.state < oFx.target ? 1 : -1);

		if (Math.abs(movement) >= Math.abs(oFx.state - oFx.target)) {
			oFx.state = oFx.target;
		}
		else {
			oFx.state += movement;
		}

		if (oFx.state === oFx.target) {
			clearInterval(oFx.timer);
			oFx.timer = null;
		}

		oFx.lastTime = now;

		return -(Math.cos(Math.PI * oFx.state)-1)/2;
	};

	/**
		Called by "tk_frame_size_notifier.html".
	*/
	window.tkResizeFrame = function(nHeight) {
		var wrapper = document.getElementById(TK_APP_WRAPPER_ID),
			frame    = document.getElementById(TK_APP_FRAME_ID);

		if (wrapper && frame && nHeight > 0) {
			// Hide overflow
			wrapper.style.overflow = 'hidden';

			// Clear top & bottom borders of both elements so they don't impact
			// the final height of both elements.
			wrapper.style.borderTopWidth =
				frame.style.borderTopWidth =
					wrapper.style.borderBottomWidth =
						frame.style.borderBottomWidth = '0px';

			// Set the height of both elements to be the same
			if (!wrapper.style.height) {
				wrapper.style.height = frame.style.height = wrapper.offsetHeight+'px';
			}

			var now = new Date().getTime();
			
			// Setup and start the page scroll effect
			var doc  = (!document.compatMode || document.compatMode === 'CSS1Compat') ? document.getElementsByTagName('html')[0] : document.body,
				fromY = window.pageYOffset || doc.scrollTop || 0,
				toY   = 0,
				el    = wrapper;

			do {
				toY += el.offsetTop  || 0;
				el = el.offsetParent;
			} while (el);

			// Only scroll back up if the top of the wrapper is out of view
			if (fromY > toY) {
				if (scrollCtx.timer) {
					clearInterval(scrollCtx.timer);
				}
				scrollCtx.state    = 0;
				scrollCtx.target   = 1;
				scrollCtx.lastTime = now;
				scrollCtx.timer    = setInterval(function() {
					var tween = nextFxTween(scrollCtx),
						fromY  = window.pageYOffset || doc.scrollTop || 0,
						y      = fromY + Math.round((toY - fromY) * tween);
					window.scrollTo(0, y);
				}, scrollCtx.interval);
			}

			// Setup and start the frame resize effect
			if (resizeCtx.timer) {
				clearInterval(resizeCtx.timer);
			}
			resizeCtx.state    = 0;
			resizeCtx.target   = 1;
			resizeCtx.lastTime = now;
			resizeCtx.timer    = setInterval(function() {
				var tween = nextFxTween(resizeCtx),
					height = wrapper.offsetHeight + Math.round((nHeight - wrapper.offsetHeight) * tween);
				wrapper.style.height = frame.style.height = height+'px';
			}, resizeCtx.interval);
		}
	};
})();
