﻿/*
 * Defines a "pseudo" namespace for the com.heartsonfire initialization functions.
 */
if(typeof com=="undefined"){
	var com = {};
}
if(typeof com.heartsonfire=="undefined"){
	com.heartsonfire = {};
}

/*
 * UPDATE THE FOLLOWING VARIABLES TO THE APPROPRIATE PATHS ON heartsonfire.com
 */
var nocache = new Date(); 
nocache = Math.floor( nocache.getTime() / (1000 * 60 * 60) ); 
 
var pathToTemplateSWF = "/swf/index.swf?nocache=" + nocache;

/*
 * THE DOM OBJECT TO BE REPLACED BY FLASH
 */
var targetDivForSWF = "swfcontent";

var swfID = "swfcontent_swf";

/*
 * LOADS THE PAGE CONTENT AS THE SOURCE FOR THE FLASH NAVIGATION
 */

var pathToCurrentPageXHTML = escape(location.pathname + location.search + location.hash);

/*
 * This is the path to the SWFObject ExpressInstall SWF so that users with Flash 6 or higher 
 * are given the option to upgrade.
 */
var pathToExpressInstallSWF = "/swf/expressinstall/expressinstall.swf";

var pathToGetFlash = "/getflash.php";

/*
 * Replaces HTML content in document with Flash content.
 */
com.heartsonfire.initSWF = function(){
	// hide the div
	document.getElementById(targetDivForSWF).style.visibility = "hidden";

// calculate the flash content size  
	var windowHeight = com.heartsonfire.getWindowHeight();
	var windowWidth = com.heartsonfire.getWindowWidth();
	var containerDiv = document.getElementById("container");
	
	/*
	if(windowHeight<598) {
		containerDiv.style.height = "598px";
	}
	if(windowWidth<1004) {
		containerDiv.style.width = "1004px";
	}
	*/
	
	// create the swf object/embed tag
	var swfObject = new SWFObject(pathToTemplateSWF, swfID, "1004", "598", "9.0.28", "#110519");
	// swfObject.useExpressInstall(pathToExpressInstallSWF);
	
	var hash = (/#/g.test(location.href));
	
	var xiRedirectUrl = location.href;
	
	if(hash) {
		xiRedirectUrl = com.heartsonfire.SWFRedirect.redirect(hash);
	}
	
	// swfObject.setAttribute('xiRedirectUrl', xiRedirectUrl); 
	// swfObject.setAttribute('redirectUrl', pathToGetFlash); 
	swfObject.addParam("loop", "false");
	swfObject.addParam("play", "true");
	swfObject.addParam("wmode", "transparent");

	swfObject.addVariable("xhtmlpath", pathToCurrentPageXHTML);
	
	if(webServiceURL){
		swfObject.addVariable("webServiceURL", webServiceURL);
	}
	
	
	swfObject.write(targetDivForSWF);
	
	// show the div
	document.getElementById(targetDivForSWF).style.visibility = "visible";
	
	// focus on the swf
	if(document.getElementById(swfID)){
		document.getElementById(swfID).focus();
	}
}

com.heartsonfire.getWindowHeight = function() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} 
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight=document.documentElement.clientHeight;
		} 
		else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

com.heartsonfire.getWindowWidth = function() {
	var windowWidth=0;
	if (typeof(window.innerWidth )=='number') {
		windowWidth = window.innerWidth ;
	} 
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} 
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

com.heartsonfire.setLocaleJS = function(locale) {
	cookiejar.bake("locale");
	var params = {"country":locale.country, "language":locale.language};
	subcookiejar.bake("locale", params);
}

com.heartsonfire.getLocaleFromCookie = function() {
	var locale = {	"country" : subcookiejar.fetch("locale","country"), 
				  	"language" : subcookiejar.fetch("locale","language") };
	return locale;
}

com.heartsonfire.getLocaleFromPath = function() {
	var locale = {};
	var pathArray = location.hash.substring(1).split("/");
	if(pathArray.length<2){
		pathArray = location.pathname.substring(1).split("/");
	}
	if(pathArray[0] && pathArray[0] != "" && pathArray[0].length<=3){
		locale.country = pathArray[0];
		if(pathArray[1] && pathArray[1] != "" && pathArray[0].length<=5){
			locale.language = pathArray[1];
		}
	}
	return locale;
}

com.heartsonfire.getLocaleJS = function() {
	var locale = getLocaleFromPath();
	if(!locale.country || !locale.language){
		locale = getLocaleFromCookie();
	}
	return locale;
}

com.heartsonfire.getLocation = function() {
	var loc = location.pathname + location.search;
	return loc;
}

var setLocaleJS = com.heartsonfire.setLocaleJS;
var getLocaleJS = com.heartsonfire.getLocaleJS;
var getLocaleFromPath = com.heartsonfire.getLocaleFromPath;
var getLocaleFromCookie = com.heartsonfire.getLocaleFromCookie;
var getLocation = com.heartsonfire.getLocation;

com.heartsonfire.SWFRedirect = {
	init : function() {
		
		if (!so) {
			// If no SWFObject on the page (subpages), create a dummy one.
			// Actually, all we need is the version, so everything else can be 0...
			var so = new SWFObject(0, "testVersion", 0, 0, "9.0.28");
		}
				
		var versionIsValid = so.installedVer.versionIsValid(so.getAttribute("version")),
			hash = (/#/g.test(location.href));
		
		if (versionIsValid) {
			// if the version is valid, enable swf
			com.heartsonfire.SWFRedirect.enable();
		}
		
		
		// The magic. If either of the following scenarios, trigger the redirect:
		// 1. Flash is installed and is the correct version, but no anchor is found.
		// 2. An anchor is present, but Flash is not installed or not the correct version.
		if ((!hash && versionIsValid) || (hash && !versionIsValid)) {
			document.getElementsByTagName("body")[0].style.visibility = "hidden";
			var link = com.heartsonfire.SWFRedirect.redirect(hash);
			
			if(link != location.href && (link+"#") != location.href){
				// parameters or a has were passed and the cleaned up link is not the same as the current href,
				// replace the location
				location.replace(link);
			} else {
				// otherwise show the swf
				com.heartsonfire.SWFRedirect.enable();
			}
		} else {
			// we're cool, show the swf
			com.heartsonfire.SWFRedirect.enable();
		}
	},
	redirect : function(hash) {
		var protocol = location.protocol + "//",
			host = location.host,
			path = location.href.split(host)[1],
			query = location.search,
			append = "";
		if(query){
			path = path.split(query)[0];
			
			if(path == "/index.php" || path == "/index.html" || path == "/") {
				if(!hash){
					return (location.href);
				}
				path = "/";
			}
			
			var queryParamIds = [	"busrel",
									"busRel",
									"BusRel",
									"BUSREL",
									
									"stid",
									"stId",
									"stID",
									"STID",
									
									"chid",
									"chId",
									"chID",
									"CHID",
									
									"dropahint",
									
									"utm_source",
									"utm_medium",
									"utm_content",
									"utm_campaign",
									"item",
									"type",
									
									"dnid",
									"dnId",
									"dnID",
									"DNID"
								];
			
			var qArray = new Array();
			
			for(var i=0; i<queryParamIds.length; i++){
				var queryParamId = queryParamIds[i];
				if(getQueryParamValue(queryParamId).length>0){
					qArray.push( ( queryParamId + "=" + getQueryParamValue(queryParamId) ) );
					if(queryParamId!="type"){
						query = query.split( queryParamId + "=" + getQueryParamValue(queryParamId)).join("");
					}
				}
			}
			
			while(query.indexOf("&&")!=-1){
				query = query.split("&&").join("&");
			}
			
			if(query=="?&" || query=="?"){
				query="";
			}
			if(qArray.length>0){
				append = "?";
				append += qArray.join("&");
			} else {
				append = "";
			}
			
			path += query;
			
			//alert("path : " + path);
			//alert("append : " + append);
			//alert("query : " + query);
		} else {
			//alert("no query");
		}
		
		if (path == "/" || path == "/#") {
			return (protocol + host + "/" + append);
		}
		
		if (hash) {
			path = path.replace(/#\/?/g, "");
		} else if (!(/\//.test(path.charAt(0)))) {
			path = "/" + path;
		}
		
		return (protocol + host + append + (hash ? "" : "#") + path);
	},
	enable : function() {
		document.getElementsByTagName("body")[0].style.visibility = "visible";
		com.heartsonfire.initSWF();
	}
}

/*
 * Attaches window onload event handler
 */
DomLoaded.load( com.heartsonfire.SWFRedirect.init );

function hideIfFlashCompliant(){
	if (!so) {
			// If no SWFObject on the page (subpages), create a dummy one.
			// Actually, all we need is the version, so everything else can be 0...
			var so = new SWFObject(0, "testVersion", 0, 0, "9.0.28");
		}
		var versionIsValid = so.installedVer.versionIsValid(so.getAttribute("version")),
			hash = (/#/g.test(location.href));
		
		// The magic. If either of the following scenarios, trigger the redirect:
		// 1. Flash is installed and is the correct version, but no anchor is found.
		// 2. An anchor is present, but Flash is not installed or not the correct version.
		if (versionIsValid) {
			if(document.getElementsByTagName){
				if(document.getElementsByTagName("body")[0]){
					document.getElementsByTagName("body")[0].style.visibility = "hidden";
				}
			} else if(document.body){
				document.body.style.visibility =  "hidden";
			}
		}
}

function urchinTrackerProxy(action){
	if(!urchinTracker){
		return;
	}
	urchinTracker(action);
}
