/**
* @author Michelangelo Capraro
*/


/**
 * this handles the scrolling functionality of the 19340ft span of page and communicates back to flash via external interface where exactly the page is scrolled to, then flash takes care of rendering content at that elevation
 */
var jsReady = false;

/**
 * this returns the height of the body in pixels
 */
function getBodyHeight() {
	var scrollHeight;
	if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrollHeight = document.body.scrollHeight;
		
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrollHeight = document.documentElement.scrollHeight;
	}
	//console.log("getBodyHeight: " + scrollHeight, isNaN(scrollHeight));
	
	if (isNaN(scrollHeight)) scrollTo(0, 1);
	
	return scrollHeight;
	//alert(scrollHeight);
}

/**
 * this simply returns true of the javascript is ready and the giant page has been rendered (all 16 million pixels of it)
 */
function isReady() {
	return jsReady;
}

/**
 * looks at the actual scroll position of the web page and passes that to flash to render content appropriately
 */
function updateScroll() {
	var scrollY;
	if (typeof( window.pageYOffset ) == 'number') {
		//Netscape compliant
		scrollY = window.pageYOffset;	
		
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrollY = document.body.scrollTop;
		
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrollY = document.documentElement.scrollTop;
	}
	var swf = getFlashMovieObject("flashcontent");
	if (swf.updateScroll != null) swf.updateScroll(scrollY);
	
	//console.log(document.location.hash);
	
	//console.log(scrollY);
	
	if (navigator.userAgent.indexOf("Safari") >= 0 && navigator.userAgent.indexOf("Mac") >= 0) forceSafariRefresh();

}

/**
 * this scrolls to a particular pixel on the page, called from flash
 * @param {Object} pixels
 */
function scrollToPixel(pixels) {
	if (navigator.userAgent.indexOf("Firefox") >= 0 && navigator.userAgent.indexOf("Mac") >= 0) {
		setTimeout("scrollTo(0, " + pixels + ");", 5);
	} else {
		scrollTo(0, pixels);
	}
}

/**
 * this initializes the page, once the page is fully rendered (the html portion of it) then we know we can properly scroll the page with flash
 */
function pageInit(){
	if (isBrowserValid() && swfobject.hasFlashPlayerVersion("9.0.124")) {
		setTimeout("scrollToPixel(1)", 50); // we need to go somewhere more than pixel 0, otherwise the page wont init!
		jsReady = true;
	} else {
		document.getElementById("topHalf").style.height = "1px";
		document.getElementById("bottomHalf").style.top = "1px";
		document.getElementById("bottomHalf").style.height = "1px";
		document.getElementById("flashcontent").style.position = "relative";
		document.getElementById("flashcontent").style.overflow = "visible";
		document.getElementById("flashcontent").style.width = "auto";
		document.getElementById("flashcontent").style.height = "auto";
	}
}

/**
 * returns the swf object reference on the page
 * @param {Object} movieName
 */
function getFlashMovieObject(movieName){
	return document.getElementById(movieName);
}

var refreshToggle = true;
var refreshCounter = 0;
/**
 * a hack to address a safari bug where the whole flash page disappears. still unsure as to why, 
 * but thinking it might be a drawing issue with the flash player in safari mac
 */
function forceSafariRefresh(){
	refreshCounter++;
	if (refreshCounter % 20 == 0) {
		var height = "100%";
		if (refreshToggle) {
			height = "99.99%";
			refreshToggle = false
		}
		else {
			refreshToggle = true;
		}
		document.getElementById("flashcontent").style.height = height;
	}
}


var quakeDuration=600; 
var quakeCounter=0; 
/**
 * shake the browser!
 */
function shakeBrowserWindow()
{
	var deltaX=1;
	if (window.moveBy) {	
		for (quakeCounter=0; quakeCounter<quakeDuration; quakeCounter++) {
			if ((quakeCounter%4)==0) {
				window.moveBy(deltaX, 0);
				
			} else if ((quakeCounter%4)==2) {
				window.moveBy(-deltaX, 0);
			}
			if ((quakeCounter%50)==0) {
				if (quakeCounter < quakeDuration/2) 
					deltaX++;
				else
					deltaX--;
			}
		}		
	}
}

/**
 * this forces opoup window calls to execute when called and not when other URLs are placed on the SWFAddress stack
 */
function flushURLStack()
{
	var testVar = "";
}





