/**
 *
 * "The Essentials"
 * a collection by Theta Interactive Incorporated
 *
 * The "essentials" javascript is a collection of javascript classes and utility functions
 * that can help do some cool stuff.
 *
 * @author: Michael Sablone
 * @version: 1.1
 *
 * Namespaces
 * create some namespaces to avoid collision and just be nice and tidy
 *
 */
if(typeof thetainteractive == "undefined") var thetainteractive = new Object();
if(typeof thetainteractive.classes == "undefined") thetainteractive.classes = new Object();

/**
 *
 * Utitity functions
 * an assrtment of browser functions and stuff grab bag of goodies
 *
 */
if(typeof thetainteractive.utilities == "undefined") thetainteractive.utilities = {
	/**
	 *
	 * getBrowserDimensions
	 * return the inner dimensions of the browser.  again, this is from
	 * www.quirksmode.org -- love that guy
	 *
	 * @return 			Array of width and height
	 *
	 */
	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]);
	},
	/**
	 *
	 * getRequestParameter
	 * return value of a name-value pair in the query.  will look values in the query OR the hash
	 * but not both.
	 *
	 * @param name		String - the name in the name-value pair
	 * @return			String - the value in the name-value pair or empty string if no matches
	 *
	 */
	getRequestParameter: function (name) {
		var query = document.location.search || document.location.hash;
		if(query){
			var startIndex = query.indexOf(name+"=");
			var endIndex = (query.indexOf("&", startIndex) > -1) ? query.indexOf("&", startIndex) : query.length;
			if (query.length > 1 && startIndex > -1) return query.substring(query.indexOf("=", startIndex)+1, endIndex);
		}
		return "";
	},
	/**
	 *
	 * forceRoot
	 * this is a snugsite function.  this should be moved to a different location.
	 * it's really not generic enough to be here 
	 *
	 */
	forceRoot: function () {
	
		if (INDEX_URL!="index.html") document.location.href = INDEX_URL + "#" + PAGE_TITLE + "-"  + PAGE_CID;
	
	},
	/**
	 *
	 * 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
	 *
	 */
	bypassFlash: function () {
	
		var Cookie = thetainteractive.classes.Cookie;
		var value = thetainteractive.utilities.getRequestParameter("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
	
	},
	/**
	 *
	 * getBrowserCode
	 * look, there are way too many browser snffers out there.  this one probably sucks, but hey, at least
	 * it's simple
	 *
	 * @return			String - a two letter code representing the browser type
	 *					"SF" is safari
	 *					"MZ" is anything based on mozilla: firefox, netscape, etc.
	 *					"IE" is anything internet explorer: ie, opera, and probably aol, who knows (and who cares)
	 *
	 */
	getBrowserCode: function () {
	
		if (navigator.userAgent.toLowerCase().indexOf('safari') != -1) return "SF";
		else if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) return "MZ";
		else return "IE";
		
	}
}

/**
 *
 * Cookie singleton
 * comes in three flavors: set, get and kill -- pretty simple
 *
 * i ripped most of the logic from www.quirksmode.org -- love that guy.
 *
 */
 if(typeof thetainteractive.classes.Cookie == "undefined") new function () {
	thetainteractive.classes.Cookie = this;
	/**
	 *
	 * Cookie.set
	 * set a session cookie
	 *
	 * @param name		String -- the name of the cookie name-value pair
	 * @param value		String -- the value of the cookie name-value pair
	 * @param days		Number -- the value of the cookie name-value pair
	 *
	 */
	this.set = function(name, value, days) {
		var expires = ""
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	};
	/**
	 *
	 * Cookie.get
	 * get a previously saved session cookie
	 *
	 * @param name		String -- the name of the cookie name-value pair
	 * @return			String -- the value of the name-pair in the form a of a string
	 *						or empty string if no value present
	 *
	 */
	this.get = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i< ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return "";
	};
	/**
	 *
	 * Cookie.kill
	 * delete a previously saved session cookie
	 *
	 * @param name		String -- the name of the cookie name-value pair to delete
	 *
	 */
	this.kill = function(name) {
		this.set(name,"",-1)
	};
}

/**
 *
 * SuckerFish Singleton.
 * automatically registers a parse function for ie.  runs through all the nav items and
 * adds the "sfhover" mouse events
 *
 * please visit http://www.htmldog.com/articles/suckerfish/dropdowns/ for the real deal
 *
 */
if(typeof thetainteractive.classes.Suckerfish == "undefined") new function() {
	thetainteractive.classes.Suckerfish = this;
	function parse () {
		var listitems;
		var navblock = document.getElementById("nav");
		if (navblock!=null) {
			listitems = navblock.getElementsByTagName("li");
			for (var i=0; i<listitems.length; i++) {
				listitems[i].onmouseover = function() {
					this.className += " sfhover";
				}
				listitems[i].onmouseout = function() {
					this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
				}
			}
		}
	}
	if (window.attachEvent) window.attachEvent("onload", parse);
}

/**
 *
 * History Singleton.
 * i should really re-write this to a simplified single class instead of this protype version which is a biit bulky
 * but anthony might kill me if i spend another day on this.
 * besides, it's got probelms i'd like to fix in the next version when we switch to AS2 and flash 8.
 * that should solve some of the flash->IE issues posed with the hash mark shit.  i think ExternalInterface might
 * solve these issues.
 *
 * BUT, for now, the functionality is lumped into three different browser groups:
 *
 * MZ - works flawlessly.  back button and deep linking are enabled all at run-time without the use of frames.
 *	makes me wonder why other browsers suck so badly.
 *
 * IE - this *should* work as nice as the mozilla flavor.  however, a weird limitation of flash to javascript communication
 *	only allws for setting the hash once.  *unless* you use some stupid VBscript bullshit.  really really dumb.  i would love
 *	to enable the deep linking, but alas, cannot without it getting ugly.  so, for now, it only supports the back button and
 *	first -load deep linking.
 *
 * SF - the red-headed stepchild of the flash accessibility family.  no deep-linking, no dynamic history states.  so, what we
 *	are left with is a back-button-only functionality.  and we have to use a static history html file to do so.  run-time frame
 *	source changes dont register history states, and you cant set the has without an infinite load loop.  so no deep linking
 *	either.  in fact, once a first-load is run, we cant even delete the hash to limit any confusion, should the page change.
 *
 * @requires				essentials.js or essentials.compressed.js -- this javascript file
 * @requires				flashobject.js -- http://blog.deconcept.com/flashobject/
 * @requires				History.as -- an actionscript class see it's documentation to implement
 * @requires				history.html -- a static html file for safari users
 *
 */
thetainteractive.classes.History = function() {
	this.hashstring = thetainteractive.classes.History.getHashString();
	this.key = "";
	this.eid = "";
	this.HSID = null;
	this.historyflash = "core/swf/historyclient.swf";
	this.historyhtml = '<html><head><title>%TITLE%</title><style>*{margin:0px,padding:0px;}</style></head><body><object type="application/x-shockwave-flash" data="core/swf/historyclient.swf%QUERY%" width="100%" height="100%"><param name="movie" value="core/swf/historyclient.swf%QUERY%" /></object></body></html>';
	this.browsertype = thetainteractive.utilities.getBrowserCode();
}
thetainteractive.classes.History.prototype = {
	//--------------------------------------------------
	// PRIVATE METHODS
	//--------------------------------------------------
	setup: function () {
		if (this.browsertype=="MZ") this.initializeHashSystem();
		else this.initializeFrameSystem();
	},
	startup: function () {
		if (this.browsertype=="IE") this.clearHash();
		if (this.browsertype=="MZ") this.startPollingLocation();
		if (this.hashstring!="") this.setHistoryState(this.hashstring);
	},
	setInitialHash: function (hash) {
		if (this.hashstring=="") this.hashstring = hash;
	},
	setElementID: function (eid) {
		this.eid = eid;
	},
	setKey: function (key) {
		this.key = key;
	},
	setHistoryState: function (hashstring) {
		this.hashstring = hashstring;
		this["set" + this.browsertype + "HistoryState"]();
	},
	getHistoryState: function () {
		var hashstring = thetainteractive.classes.History.getHashString();
		if (hashstring!=this.hashstring) this.setHistoryState(hashstring);
	},
	invalidate: function () {
		clearInterval(this.HSID);
		this.HSID = null;
	},
	initializeHashSystem: function () {
		var elementname = "history_runtime_divisional";
		if (this.eid=="") {
			var element = document.createElement("div");
			element.id = elementname;
			element.style.width = "300px";
			element.style.height = "150px";
			element.style.overflow = "hidden";
			document.body.appendChild(element);
			this.eid = elementname;
		}
	},
	startPollingLocation: function () {
		if (this.HSID==null) this.HSID = setInterval("thetainteractive.classes.History.inspect()", 500);
	},
	initializeFrameSystem: function () {
		var framename = "history_runtime_iframe";
		var element = this.eid=="" ? document.body : document.getElementById(this.eid);
		var frame = document.getElementById(framename);
		if (frame==null) {
			frame = document.createElement("iframe");
			frame.name = framename;
			frame.id = framename;
			frame.setAttribute("scrolling", "no");
			frame.setAttribute("frameborder", "0");
			element.appendChild(frame);
		}
	},
	/*createHashElement: function (hashstring) {
		var child, element = this.eid=="" ? document.body : document.getElementById(this.eid);
		child = document.createElement("a");
		child.setAttribute("href", "#"+this.hashstring);
		child.style.border = "1px blue dotted";
		child.innerHTML = "";
		child.innerHTML = "link to hash: " + this.hashstring;
		element.appendChild(child);
		child = document.createElement("div");
		child.id = this.hashstring;
		child.style.border = "1px red dotted";
		child.innerHTML = "";
		child.innerHTML = "hash: " + this.hashstring;
		element.appendChild(child);
	},*/
	clearHash: function () {
		if (document.location.hash) document.location.hash = "";
	},
	/*createVBCatcher: function (id) {
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
		document.write('on error resume next \n');
		document.write('Sub ' + id + '_FSCommand(ByVal command, ByVal args)\n');
		document.write(' call alert(args)\n');
		document.write('end sub\n');
		document.write('</SC' + 'RIPT\> \n');
	},*/
	setMZHistoryState: function () {
		document.location.hash = this.hashstring;
		var historyflash = new FlashObject(this.historyflash, null, "100%", "100%", "6.0.79");
		historyflash.addVariable("key", this.key);
		historyflash.addVariable("hashstring", this.hashstring);
		historyflash.write(this.eid);
	},
	setIEHistoryState: function () {
		var frame = window.frames["history_runtime_iframe"];
		var historyhtml = this.historyhtml.replace(/%TITLE%/g, document.title + this.hashstring.split("/").join(" ")).replace(/%QUERY%/g, "?key=" + this.key + "&hashstring=" + this.hashstring);
		frame.document.open('text/html');
		frame.document.write(historyhtml);
		frame.document.close();
	},
	setSFHistoryState: function () {
		var frame = document.getElementById("history_runtime_iframe");
		frame.src = 'core/history.html?key=' + this.key + '&hashstring=' + this.hashstring;
	}
}
//--------------------------------------------------
// STATIC PUBLIC METHODS
//--------------------------------------------------
thetainteractive.classes.History.getHashString = function () {
	var hash = document.location.hash
	return hash ? hash.substring(1) : "";
}
thetainteractive.classes.History.initialize = function (hash, eid) {
	var instance = thetainteractive.classes.History.getInstance();
	if (hash) instance.setInitialHash(hash);
	if (eid) instance.setElementID(eid);
	instance.setup();
}
thetainteractive.classes.History.start = function (key) {
	var instance = thetainteractive.classes.History.getInstance();
	if (key) instance.setKey(key);
	instance.startup();
}
thetainteractive.classes.History.setHistory = function (hashstring) {
	var instance = thetainteractive.classes.History.getInstance();
	instance.setHistoryState(unescape(hashstring));
}
thetainteractive.classes.History.invalidate = function () {
	var instance = thetainteractive.classes.History.getInstance();
	instance.invalidate();
}
thetainteractive.classes.History.inspect = function () {
	var instance = thetainteractive.classes.History.getInstance();
	instance.getHistoryState();
}
thetainteractive.classes.History.getInstance = function () {
	if (thetainteractive.classes.History._instance==undefined) thetainteractive.classes.History._instance = new thetainteractive.classes.History();
	return thetainteractive.classes.History._instance;
}