/**
 * 1. Global Variables
 * 2. Setup the history
 * 3. Load the flash movie and setup the flash/javascript communication
 */



//#SECTION 1. Global Variables
	var flashMovieUrl = "flash/JS.swf";
	var movieElementId = "flashDiv";
//#END SECTION 1
//#########################################################################################################
//#SECTION 2. Setup history and flash/javascript communication
	var historyModuleStr = "\/";
	var historyModuleBookmarkedState = YAHOO.util.History.getBookmarkedState(historyModuleStr);
	var intervalId = 0;
	// If there is no bookmarked state, assign the default state: 
	var historyModuleInitialState = historyModuleBookmarkedState || "";
	var historyModuleNoXML = false;
	function historyModuleStateChangeHandler (state) {
		// Update the UI of your module according to the "state" parameter
		flashToUrl(state, historyModuleNoXML);
	}
	YAHOO.util.History.register(historyModuleStr, historyModuleInitialState, historyModuleStateChangeHandler);
	var historyModuleCurrentState;
	YAHOO.util.History.onReady(function () {
		historyModuleCurrentState = YAHOO.util.History.getCurrentState(historyModuleStr);
		// Send flash the URL to show at the beginning.
		// The hard part here is we don't know if the flash movie is loaded and ready
		// As a result, we must keep calling the flashToUrl
		intervalId = window.setInterval("flashToUrl(historyModuleCurrentState)", 100);
	});
	// Initialize the browser history management library.
	function initializeHistory() {
		//first load, record the page hit
		recordPageHit(historyModuleBookmarkedState);
		try {
			YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
		} catch (e) {
			// The only exception that gets thrown here is when the browser is
			// not supported (Opera, or not A-grade) Degrade gracefully...?
			// alert(e);
		}
	}
	YAHOO.util.Event.addListener(window, 'load', initializeHistory);
//#END SECTION 2
//#########################################################################################################
//#SECTION 3. Load the flash movie and setup the flash/javascript communication
	var flashIsLoaded = false;
	/**
	 * flash calls this function once it's navigated to a page
	 * This is our method for storing the history
	**/
	function navigatedTo(url, noXML) {
		//console.log("Navigated to " + url);
		try {
			historyModuleNoXML = noXML;
			YAHOO.util.History.navigate(historyModuleStr, url); 
		} catch (e) {
			//console.error("navigatedTo error: " + e);
		}
		recordPageHit(url);
	}
	
	function recordPageHit(url) {
		if(url=="home" || !url)
			url = "/";
		//console.log("Recording page hit on "+url);
		res = pageTracker._trackPageview(url);
	}
	/**
	 * this function calls flash to communicate that the back button has been used
	 * It passes the function the URL to pull
	**/
	function thisMovie(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName]
		} else {
			return document[movieName]
		}
	}
	//function that communicates with flash to send it to a url
	function flashToUrl(url, noXML) {
		//call the flash function to move to historyModuleCurrentState
		if(flashIsLoaded) {
			clearInterval(intervalId);
			if(url) {
				thisMovie(movieElementId).moveToUrl(url, noXML);
			} else {
				url = "home";
				thisMovie(movieElementId).moveToUrl("home", noXML);
			}
			//console.log('flashToUrl');
			//document.getElementById('testing').innerHTML += " <span style='background: green; color: white;'>YES</span>";
		} else {
			//alert('flash not called, not passed url '+url);
		}
	}
	/**
	 * Flash calls this function to indicate that the movie has loaded
	**/
	function setFlashLoaded() {
		flashIsLoaded = true;
	}
	//Load the flash movie
	// See: http://code.google.com/p/swfobject/wiki/SWFObject_2_0_documentation
	function SwfFlashObject(src, id, width, height)
	{
		this.src       = src;
		this.id        = id;
		this.width     = width;
		this.height    = height;
		this.version   = '8.0.0';
		this.bgcolor   = 'ffffff';
		//this.flashVars = {'lcId': 'flashProxyUnique'}
		var xmlPath = window.location.host;
		if(xmlPath.indexOf("www.")!=-1) {
			xmlPath = xmlPath.replace("www.", "xml.");
		} else {
			xmlPath = "xml."+xmlPath;
		}
		xmlPath = "http://"+xmlPath;
		this.flashVars = { 'xmlPath' : xmlPath}
		this.expressInstallSwfurl = '';
		this.params	= {allowScriptAccess:"always", scale:"noscale", bgcolor:"#FFFFFF", align:"left", quality:"high", movie:"flash/JS.swf"};
		this.attributes = {};
	}
	SwfFlashObject.prototype.load = function() {
		swfobject.embedSWF(this.src, this.id, this.width, this.height, this.version, this.expressInstallSwfurl, this.flashVars, this.params, this.attributes);
	}
	function loadFlashMovie() {
		fo = new SwfFlashObject(flashMovieUrl, movieElementId, "100%", "100%");
		if(swfobject.hasFlashPlayerVersion('8.0.0')) {
			fo.load();
		} else {
			document.getElementById("noflashcontent").style.display = "block";
		}
	}
	//Get size of the viewport
	function getViewportSize() { 
		var size = [0, 0]; 
		if (typeof window.innerWidth != "undefined") { 
			size = [window.innerWidth, window.innerHeight];
		} 
		else if (typeof document.documentElement != "undefined" && typeof document.documentElement.clientWidth != "undefined" && document.documentElement.clientWidth != 0) {
			size = [document.documentElement.clientWidth, document.documentElement.clientHeight]; 
		}
		else {
			size = [document.getElementsByTagName("body")[0].clientWidth, document.getElementsByTagName("body")[0].clientHeight]; 
		}
		return size; 
	}
	
	/**
	 * Flash calls this function to set the height of the Flash
	 * movie in the page. If the document height is too short,
	 * scrollbars are added.
	**/
	function setHeight( h ) {
		//console.log('set height to ' + h);
		var size = getViewportSize();
		var el = document.getElementById(movieElementId);
		if ( h < size[1] ) {
			el.style.height = "100%";
		} else {
			el.style.height = h + "px";
		}
		return el.style.height;
	}
	/**
	 * If the user enlarges the window after Flash
	 * has set a height, and that height turns out
	 * to be less than new browser height, we switch
	 * it back to 100%.
	 * (Flash handles this now)
	**/
	/*window.onresize = function() {
		var size = getViewportSize();
		var el = document.getElementById(movieElementId);
		if( el.offsetHeight < size[1] ) {
			el.style.height = "100%";
		}
	}*/
	
	YAHOO.util.Event.addListener(window, 'load', loadFlashMovie);
//#END SECTION 3
//#########################################################################################################
//#SECTION 4. Setup the function that changes the title and the links of the page
	function updateTitle(text) {
		document.title = text;
	}
	
	function updateLinks(linkArr) {
	}
//#END SECTION 4