/**
 * Fixes page so only content scrolls.
 *
 * @package    Ujigami
 * @subpackage ttKeyboard.js
 * @version    1.2
 * @copyright  Copyright (c) 2002-2010 - Tutelar Technologies Inc. {@link http://www.watchover.us}
 * @author     ejd <ejd@watchover.us>
 */

(function() { // create namespace closure
	function swResizeAddEvent(sourceElement, eventName, callbackFn, useCapture)
	{
		if (typeof(sourceElement) == "string") {
			sourceElement = document.getElementById(sourceElement);
		}
		if (sourceElement == null || callbackFn == null) {
			return false;
		}
	  // Mozilla/W3C listeners?
	  if (sourceElement.addEventListener) {
			sourceElement.addEventListener(eventName, callbackFn, useCapture);
			return true;
	  }
	  // IE-style listeners?
	  if (sourceElement.attachEvent && sourceElement.attachEvent("on" + eventName, callbackFn)) {
			return true;
	  }
	  return false;
	};

	var cPanel = document.getElementById('ttContainer');
	var cParent = cPanel.parentNode;
	var ujigami = document.getElementById('ujigami');

	function resizePanel(onload)
	{
		var prevFocus = (document.activeElement && (document.activeElement.nodeName.toLowerCase() != 'body')) ? document.activeElement : false;
		var smallScreen = (screen.width < 700) || (screen.height < 500);	/* small screen devices */
		if (smallScreen ||
			(document.body.clientWidth < 700) || (document.body.clientHeight < 500)) {	/* reduced browser window size */
			cPanel.style.width = '';
			cPanel.style.height = '';
			if (onload && smallScreen) {
				var main = document.getElementById('ttLayoutMain');
				main.style.height = screen.availHeight + 'px';			// try shrinking page to screen size
				main.style.width = screen.availWidth + 'px';
				if (window.scrollTo) {
					window.scrollTo(main.offsetLeft, main.offsetTop);	// scroll iPod header off top
				}
			}
		} else {
			cPanel.style.display = 'none';
			cPanel.style.height = cParent.clientHeight + 'px';
			cPanel.style.width = cParent.clientWidth + 'px';
			cPanel.style.display = '';
			if ((document.body.scrollWidth > document.body.clientWidth) || (document.body.scrollHeight > document.body.clientHeight)) {
				cPanel.style.width = '';
				cPanel.style.height = '';
			}
		}
		if (prevFocus && prevFocus.focus) {
			prevFocus.focus();
		} else {
			if (onload && cPanel.focus) {
				cPanel.focus();
			}
		}
	}

	function doResize()
	{
		resizePanel();
	}

	function doLoad()
	{
		resizePanel(true);
	}

	swResizeAddEvent(window, 'resize', doResize);
	swResizeAddEvent(window, 'load', doLoad);
})(); // end namespace closure



