// Helper JavaScript to support dynamic sizing of our Flash applications
// Created Sept 11, 2008 by Aris

// Declare nativeWidth and nativeHeight outside, before including this file
// var nativeWidth = 1000;
// var nativeHeight = 600;

var lastId;
var currentMetaId;
var isHelpPaneOn = false;
var optimalOneObj;
var contentHeight = 0;

var isVariableWidth = false;

var _isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var _isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var _isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var _isFirefox = (navigator.userAgent.indexOf("Firefox") != -1) ? true : false;
var _isNetscape = (navigator.userAgent.indexOf("Netscape") != -1) ? true : false;
var _isNetscape7 = (navigator.userAgent.indexOf("Netscape/7") != -1) ? true : false;
var _isSafari = (navigator.userAgent.indexOf("Safari") != -1) ? true : false;
var _isAvant = (navigator.userAgent.indexOf("Avant Browser") != -1) ? true : false;
var _isLoaded = false;

var heightTimerInterval = null;
var widthTimerInterval = null;

var dummyMessage = "@o$m#&";
var dummyTextArea;

window.onresize = handleWindowResize;
//setInterval("checkForWalkthroughRequest()", 200); // aris 26 feb 2010.

var currY = 0
function locateBrowserScrollbarCurrY()
{
	var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
	h =  h ? h : 0;
	
	if (currY!=h)
	{
		currY = h;
		seto1Variable("browserScrollbarCurrY", currY);
	}
}

function seto1Variable(variableName, variableValue) {
	var o1Object = null;
	
	if(navigator.userAgent.indexOf("MSIE") == -1 && document.embeds) 
	{
		o1Object = document.embeds["optimalOne"]
	} else if (document.getElementById) {
		o1Object = document.getElementById("optimalOne");
	}

	if(o1Object != null) {
		o1Object.SetVariable(variableName, variableValue);
	}
}

function setup() {
	seto1Variable("onLoad", "1"); // Related to onLoad bug. Look in purple intranet for details.
	
	if(!_isIE && document.embeds) {
		optimalOneObj = document.embeds["optimalOne"]
	} else if (document.getElementById) {
		optimalOneObj = document.getElementById("optimalOne");
	}
	
	if(document.all && !document.getElementById) {
		dummyTextArea = document.all["dummyTextArea"];
	} else if(document.getElementById) {
       dummyTextArea = document.getElementById("dummyTextArea");
 	}

	if(dummyTextArea != null) {
		dummyTextArea.value = dummyMessage;
	}
	window.onfocus = _onWindowFocus;
	_isLoaded = true;

	resizeVerticallyToFitBrowser();
	focusOnApplication();
}

function unsetKeyListeners() 
{
	if(dummyTextArea != null && dummyTextArea != undefined) {
		dummyTextArea.onkeydown = null;
		dummyTextArea.onkeypress = null;
	}
}

function _docKeyDown()
{
    if (window.event && event.keyCode == 9)
        event.returnValue = false;
}

function _onWindowFocus()
{
	focusOnApplication();
}

function focusOnApplication()
{
	if (_isIE || _isAvant) {
		if(optimalOneObj != null) {
			optimalOneObj.focus();
		}
	} else {
		// The following code causes the scroll to go back to the top
		//dummyTextArea.focus();
	}
}

function setDocumentFocus()
{
    if (_isFirefox || _isSafari)
        dummyTextArea.focus();
    else if (_isIE || _isAvant)
        document.focus();
    else if (!_isOpera && !_isNetScape)
        window.focus();
}

function _keyDown(e) {
	var event = e || window.event;
	var keycode = event.keyCode;
	
	if(keycode == 8 || keycode == 9 || keycode == 37 || keycode == 39 || keyCode == 13) 
	{
		if(optimalOneObj != null) {
			optimalOneObj.SetVariable("currentkeycode", keycode);
		}
		if(keycode == 9 || keycode == 8) {
			if (_isFirefox || _isSafari) {
				dummyTextArea.focus();
				
				if (typeof e.preventDefault == "function") {
			        event.preventDefault();	
			    }				
			}
		}
	}
}

function _keyPress(e) {
	var event = e || window.event;
	var keycode = event.keyCode;
	
	if(keycode == 0) {
		keycode = ((typeof event.charCode == "number") && Number(event.charCode) != 0) ? event.charCode : keycode;
	} else if(keycode == 8 || keycode == 9 || keycode == 37 || keycode == 39) {
		return;
	}
	
	if(optimalOneObj != null) {
		var currentChar = String.fromCharCode(keycode);
		optimalOneObj.SetVariable("currentchar", currentChar);
	}
	
    if (_isIE) {
		event.returnValue = false;	    	
    } else if (typeof e.preventDefault == "function") {
        event.preventDefault();	
    }
}

// (aris feb 25,2010) Get the amount of space available in the browser window
// Specific behaviors:
// Chrome: If the horizontal scrollbar is present, the scrollbar height is included.
// Firefox: If the horizontal scrollbar is present, the scrollbar height is included.
// IE: If the horizontal scrollbar is present, the scrollbar height is NOT included.
function getViewableHeight()
{
	// code from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	var viewableHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		viewableHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		viewableHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		viewableHeight = document.body.clientHeight;
	} else {
		// ??
	}

	return viewableHeight;
}

function getViewableWidth()
{
	var viewableWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		viewableWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		viewableWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		viewableWidth = document.body.clientWidth;
	} else {
		// ??
	}

	return viewableWidth;
}

function getFooterHeight()
{
	var footerHeight = 0;
	
	if(document.all && !document.getElementById) {
		var theObject = document.all["customFooter"];
		if(theObject != null) {
			footerHeight = document.all["customFooter"].style.pixelHeight;
		}
	} else if(document.getElementById) {
		var theObject = document.getElementById("customFooter");
		if(theObject != null) {
			footerHeight = document.getElementById("customFooter").style.height;
		}
	}
	
	// By this point, the browsers are as follows:
	// IE: 70px
	// Firefox: 70px
	// Chrome: 70px
	
	//now clean up footerHeight if necessary
	if(footerHeight != undefined && footerHeight != null) {
		if(footerHeight.indexOf != null) {
			if(footerHeight.indexOf("px") != -1) {
				footerHeight = footerHeight.substring(0, footerHeight.indexOf("px"));
			}
		}
	} else {
		footerHeight = 0;
	}
	
	return footerHeight;
}

function resizeVerticallyToFitBrowser() 
{
	var proposedFlashStageHeight = getViewableHeight() - getFooterHeight();
	setFlashHeight(proposedFlashStageHeight);
}

function handleWindowResize() {
	var viewableHeight = getViewableHeight();
	seto1Variable("viewableHeight", viewableHeight);
	
	resizeVerticallyToFitBrowser();
	
	if(isVariableWidth) {
		setFlashWidth(getViewableWidth() - 20); // Take into account the scrollbar
	}
}

// This function is designed to be called from actionscript when the content height changes.
function setCurrentContentHeight(h) {
	contentHeight = h;
	resizeVerticallyToFitBrowser();
}

// This already respects nativeHeight, i.e. it won't allow Flash to get less than nativeHeight, so no shrinking occurs
function setFlashHeight(h) {
	if(h < nativeHeight) h = nativeHeight;
	if(contentHeight > nativeHeight && h < contentHeight) h = contentHeight;
	
	if(heightTimerInterval != null) {
		clearTimeout(heightTimerInterval);
	}
	
	heightTimerInterval = setTimeout("delayedSetFlashHeight(" + h + ");", 200);
}

function delayedSetFlashHeight(h) {
	heightTimerInterval = null;
	if(document.all && !document.getElementById) {
 		document.all['flashContent'].style.pixelHeight = h;
	}else{
		document.getElementById('flashContent').style.height = h;
	}
}

function setFlashWidth(w) {
	if(w < nativeWidth) w = nativeWidth;
	
	if(widthTimerInterval != null) {
		clearTimeout(widthTimerInterval);
	}
	
	widthTimerInterval = setTimeout("delayedSetFlashWidth(" + w + ");", 200);
}

function delayedSetFlashWidth(w) {
	widthTimerInterval = null;
	if(document.all && !document.getElementById) {
 		document.all['flashContent'].style.pixelWidth = w;
	}else{
		document.getElementById('flashContent').style.width = w;
	}
	
	seto1Variable("viewableWidth", w);
}

function setFlashSize(w,h) {
	setFlashWidth(w);
	setFlashHeight(h);
}

function setFlashContainerSize(w,h) {
	if(document.all && !document.getElementById) {
		document.all['flashContainer'].style.pixelWidth = w;
	} else {
		document.getElementById('flashContainer').style.width = w;
	}
}
//added 22 April.2009 for HelpPane Development

function requestWalkthrough(walkthroughId) {
	if(navigator.userAgent.indexOf("MSIE") == -1) {
		var optimalOneObj = document.embeds["optimalOne"];
        if(optimalOneObj != null) {
			optimalOneObj.SetVariable("playWalkthroughFor", walkthroughId);
		}
	} else {
		var optimalOneObj = document.getElementById("optimalOne");
		if(optimalOneObj != null) {
			optimalOneObj.SetVariable("playWalkthroughFor", walkthroughId);
		}
	}
}

function checkForWalkthroughRequest(){
	if(location.hash != lastId){
		lastId = location.hash;
		requestWalkthrough(lastId.split("!")[1]);
	}
}

function sendCurrentURLtoIframe(channelMetaId){
	var iwin;
	if(navigator.userAgent.indexOf("Safari") != -1){
	        iwin = frames["helpPane"];
	}else{
	        iwin = document.getElementById("helpPane").contentWindow;
	}
	iwin.location = "http://www.quickschools.com/help/helpPane/" + channelMetaId + ".html#" + window.location;
}



function sayHello() {
	alert("hello world");
}

function toggleHelpPane() 
{
	if (getHelpPaneWidth() == "250px")
	{
		setHelpPaneWidth("1px");
	} else {
		setHelpPaneWidth("250px");
	}
	handleWindowResize();	
}

function showHelpPane(channelMetaId) 
{
	setHelpPaneWidth("250px");
	isHelpPaneOn = true;
	handleWindowResize();
	sendCurrentURLtoIframe(channelMetaId);
}

function clearHelpPane()
{
	if(document.all && !document.getElementById) {
		document.all['helpPane'].clearAttributes;
	}else{
		document.getElementById('helpPane').clearAttributes;
	}	
}

function hideHelpPane()
{
	setHelpPaneWidth("0px");
	clearHelpPane();
	isHelpPaneOn = false;
	handleWindowResize();
}
function setHelpPaneWidth(w)
{
	if(document.all && !document.getElementById) {
		document.all['helpPane'].style.pixelWidth = w;
		document.all['helpPaneContainer'].style.pixelWidth = w;
	}else{
		document.getElementById('helpPane').style.width = w;
		document.getElementById('helpPaneContainer').style.width = w;
	}
}

function setHelpPaneHeight(h) 
{
	if(document.all && !document.getElementById) {
		document.all['helpPane'].style.pixelHeight = h;
	}else{
		document.getElementById('helpPane').style.height = h;
	}
}

function adjustFooter(top, width)
{
	if(document.all && !document.getElementById) {
		document.all['customFooter'].style.top = top;
		document.all['customFooter'].style.width = width;
	}else{
		document.getElementById('customFooter').style.top = top;
		document.getElementById('customFooter').style.width = width;
	}
}

function getHelpPaneWidth()
{
	var w;
	if(document.all && !document.getElementById) {
		w = document.all['helpPane'].style.pixelWidth;
	}else{
		w = document.getElementById('helpPane').style.width;
	}
	return w;
}

function getHelpPaneHeight()
{
	if(document.all && !document.getElementById) {
		return document.all['helpPane'].style.pixelHeight;
	}else{
		return document.getElementById('helpPane').style.height;
	}
}

function getFooterHeight2()
{
	var h;
	if(document.all && !document.getElementById) {
		h = document.all['customFooter'].style.offsetHeight;
	}else{
		h = document.getElementById('customFooter').style.offsetHeight;
	}
	return h;
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}

