/**
 *
 * The utilities are here to help in embedding flash and doing flash-type things
 *
 * @author: Michael Sablone
 * @version: 1.1
 *
 * Namespaces
 * create some namespaces to avoid collision and just be nice and tidy
 */
if (typeof classes == "undefined") classes = new Object();
if (typeof utilities == "undefined") utilities = new Object();

/**
 *
 * bypassFlash
 * this sets a cookie that can be read in globally, during any session of browsing
 * if you set {url}/index.html?flash=false, it will be there on the next page.
 * you can use this to globally disable flash on one page and then keep it there
 * thourghout.
 * it assumes flash as true, otherwise, this function wouldnt need to exists if the
 * website were all html only
 *
 * @return			Boolean -- if it should, in fact bypass flash
 *
 */
utilities.bypassFlash = function () {
	var Cookie = classes.Cookie;
	var value = swfobject.getQueryParamValue("flash");
	if (value!="") { // it's being actively set via the url so save and return
		Cookie.set("flash", value);
		return value=="false";
	}
	value = Cookie.get("flash");
	if (value!="") { // see if we have anything saved first
		return value=="false";
	}
	return false; // if we have gotten here, assume nothing has been set or saved, so flash 99% good
};

/**
 *
 * getElementsByClassName
 *
 * @param className:String
 * @param tag:String
 * @param elm:Object
 * @return:Array of mathcing elements
 *
 */
utilities.getElementsByClassName = function (className, tag, elm) {
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
};

/**
 *
 * getBrowserDimensions
 * return the inner dimensions of the browser.  again, this is from
 * www.quirksmode.org -- love that guy
 *
 * @return 			Array of width and height
 *
 */
utilities.getBrowserDimensions = function () {
	var x,y;
	if (self.innerHeight) {// all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement &&document.documentElement.clientHeight) {// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) {// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return ([x, y]);
};

/**
 *
 * loadStyleSheet
 * appends a new stylesheet to the browser
 *
 * @return 			Array of width and height
 *
 */
utilities.loadStyleSheet = function (uri) {
	if (document.createStyleSheet) {
		document.createStyleSheet(uri);
	} else {
		var element = document.createElement('link');
		element.type = 'text/css';
		element.rel = 'stylesheet';
		element.href = uri;
		element.media = 'screen';
		document.getElementsByTagName("head")[0].appendChild(element);
	}
};

/**
 *
 * FlashSite singleton
 *
 */
if(typeof classes.FlashSite == "undefined") new function () {
	classes.FlashSite = this;
	/* Constructor
		*/
	var delay_ints = new Object();
	var tracker_ua = "";
	var wmode = "";
	var swf_color = null;
	var resize_type = "None";
	var metrics = new Array();
	var bypass_flash = utilities.bypassFlash();
	var browser_title = "";
	var page_hash = "";
	var do_redirect = false;
	var inited = false;
	var alt_style = "";
	/* this will return false if a persistent cookie is set to disable flash
		or if a miminum flash version is not available
		*/
	function flashEnabled () {
		//return false;
		if (bypass_flash) return false;
		if (swfobject.hasFlashPlayerVersion("6.0.79")==false) return false;
		return true;
	};
	/* this gets called from the start() function
		essentially allowing you to setup all the stuff, and then initializing the singleton instance
		*/
	function init () {
		/* kill it if we're not using flash, or if we've already done this
			*/
		if (inited==true) return;
		inited = true;
		if (flashEnabled()==false) {
			var hash = classes.History.getHistory();
			if (hash!="") {
				document.location.href = "./" + "?" + page_hash;
				return;
			}
			if (tracker_ua!="") document.write(unescape('%3Cscript type="text/javascript"%3Etry{var pageTracker=_gat._getTracker("') + tracker_ua + unescape('");pageTracker._trackPageview();}catch(err){}%3C/script%3E'));
			if (alt_style!="") utilities.loadStyleSheet(alt_style);
			return;
		}
		/* do any redirecting if we're in an internal page
			*/
		if (do_redirect==true) {
			document.location.href = "./" + "#" + page_hash;
			return;
		}
		/* take this moment to clear the page title
			*/
		doTitle("");
		/* if we're here, it means flash is a go, so let's setup some full-screen flash stuff
			also lets hide the container's data until the dom loads and we can replace it
			*/
		swfobject.createCSS("*", "margin:0;padding:0;");
		swfobject.createCSS("html", "width:100%;height:100%;");
		swfobject.createCSS("body", "width:100%;height:100%;");
		swfobject.createCSS("#container", "display:none");
		if (swf_color!=null) swfobject.createCSS("body", "background-color:"+swf_color);
		/* if we're forcing the size, add the event handler on the window
			*/
		if (resize_type=="ForceSize") window.onresize = forceSize;
		/* and then embed everything once the dom loads
			*/
		swfobject.addDomLoadEvent(embed);
	};
	function embed () {
		/* we're going to be replacing "container"
			and since swfobject 2.0 replaces uses outerhtml instead of innerhtml
			we're gonna make a new div specifically for replacement
			*/
		var container = document.getElementById("container");
		container.style.display = "block";
		container.innerHTML = '<div id="container_replacement"></div>';
		/* embed the main swf
			*/
		var flashvars = {};
		var params = {};
		var attributes = {
			id: "loader",
			allowfullscreen: "true"
		};
		if (swf_color!=null) params.bgcolor = swf_color;
		if (wmode!="") params.wmode = wmode;
		swfobject.embedSWF("core/swf/loader.swf", "container_replacement", "100%", "100%", "9.0.0", "core/swf/expressinstall.swf", flashvars, params, attributes);
		/* setup thie history
			*/
		classes.History.SWFID("loader");
		classes.Unfocus.addEventListener('onHistory', history);
		/* embed the soundtrack player
			*/
		var element = document.createElement("div");
		element.id = "dynamic_music_player";
		element.style.height = "0px";
		element.style.overflow = "hidden";
		document.body.appendChild(element);
		var dynamic_music_player = document.getElementById("dynamic_music_player");
		dynamic_music_player.innerHTML = '<div id="dynamic_music_player_inner"></div>';
		swfobject.embedSWF("core/swf/musicplayer.swf", "dynamic_music_player_inner", "320", "240", "9.0.0");
		/* lets auto-call the first history call
			*/
		var hash = classes.History.getHistory();
		history(hash);
		/* if we're forcing the size, lets do it once on initial page load
			*/
		if (resize_type=="ForceSize") forceSize();
	};
	/* do some various browser things each time the hsitory (content) changes
		*/
	function history (hash) {
		if (tracker_ua!="") {
			var tracker = _gat._getTracker(tracker_ua);
			var track_hash = hash=="" ? "" : "?page=" + hash + "/";
			tracker._trackPageview(track_hash);
		}
		if (resize_type=="AutoHeight"||resize_type=="ExactHeight") {
			window.scrollTo(0,0);
		}
		if (hash=="") hash = page_hash;
	};
	/* the internal title event function
		bascially just does title case for everything
		*/
	function doTitle (hash) {
		document.title = hash;
	};
	/* the internal resize event function
		*/
	function doResize (w, h) {
		if (resize_type=="AutoHeight") autoHeight(h);
		else if (resize_type=="ExactHeight") exactHeight(h);
	};
	/* because the resize mechanisms do a continuous call, calling windows functions
		like focus and scroll cause crashing.  lets's buffer that so it's once a sec max
		*/
	function delayWindowFocus () {
		clearTimeout(delay_ints.focus);
		delay_ints.focus = setTimeout('window.focus();', 1000);
	};
	/* this will force a specific size for the swf
		it will try to go 100% width and height, but otherwise use the minimums
		*/
	function forceSize () {
		var stage = utilities.getBrowserDimensions();
		var element = document.getElementById("container");
		var loader = document.getElementById("loader");
		var width_minimum = metrics[0];
		var height_minimum = metrics[1];
		var width = "100%";
		var height = "100%";
		if (stage[0]<width_minimum) width = width_minimum + "px";
		if (stage[1]<height_minimum) height = height_minimum + "px";
		if (element) {
			element.style.width = width;
			element.style.height = height;
		}
		if (loader) {
			loader.style.width = width;
			loader.style.height = height;
		}
		delayWindowFocus();
	};
	/* this function checks to see if the container size is bigger than the browser stage
		otherwise, it uses the stage size
		*/
	function autoHeight (h) {
		var stage = utilities.getBrowserDimensions();
		var element = document.getElementById("container");
		var loader = document.getElementById("loader");
		var height;
		var height_offset = metrics[0];
		var height_minimum = metrics[1];
		height = parseInt(h);
		height = isNaN(height) ? 0 : height;
		height += height_offset;
		if (height<height_minimum) height = height_minimum;
		if (height<stage[1]) height = "100%";
		else height = height + "px";
		element.style.height = height;
		loader.style.height = height;
		delayWindowFocus();
	};
	/* sets the size of the container to the exact size
		*/
	function exactHeight (h) {
		var element = document.getElementById("container");
		var loader = document.getElementById("loader");
		var height;
		var height_offset = metrics[0];
		var height_minimum = metrics[1];
		height = parseInt(h);
		height = isNaN(height) ? 0 : height;
		height += height_offset;
		if (height<height_minimum) height = height_minimum;
		height = height + "px";
		element.style.height = height;
		loader.style.height = height;
		delayWindowFocus();
	};
	/* public functions
		*/
	this.start = function() {
		init();
	};
	this.setWmode = function(mode) {
		wmode = mode;
	};
	this.resizeFunction = function(w, h) {
		doResize(w, h);
	};
	this.setSwfColor = function(str) {
		swf_color = str;
	};
	this.setPageHash = function(str) {
		page_hash = str;
	};
	this.setDoRedirect = function(bool) {
		do_redirect = bool;
	};
	this.setResizeStyle = function(str) {
		resize_type = str;
	};
	this.setResizeMetrics = function() {
		metrics = new Array();
		for (var i=0; i<arguments.length; ++i) metrics.push(arguments[i]);
	};
	this.setTrackingUA = function(str) {
		if (!str||str=="") return false;
		tracker_ua = str;
		document.write(unescape("%3Cscript src='" + (("https:" == document.location.protocol) ? "https://ssl." : "http://www.") + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	};
	this.setAltStylesheet = function(str) {
		alt_style = str;
	};
	this.setTitleFunction = function(str) {
		doTitle(str);
	};
	this.getUserAgent = function() {
		return navigator.userAgent;
	};
	this.isAvailable = function() {
		return flashEnabled();
	};
};

/**
 *
 * some shortcuts to the class
 *
 */
FlashSite = classes.FlashSite;
