/* Mag 200901: Omniture Site Catalyst
 * This function sets variables for Site Catalyst from Omniture
 * The file /includes/sitecatalyst.jsp uses these variables
 */
var omnitureRSID = "whichdev";//Default value if none is set on the third party page
if (window.omnitureAccount && (omnitureAccount() != "")) {var omnitureRSID = omnitureAccount();}//Available in /includes/siteCatalyst.jsp
var s=s_gi(omnitureRSID);

//This function tracks carousel link clicks and is called from pages with carousels. 
//selectedLink is the selector for the selected link
function omnitureCarouselTracker(selectedLink)
{
	var arrOmniture=setOmnitureVariables(omniturePageName);
	var selectedLink = $(selectedLink);
	//Carousel Custom Links
	//----------------------
	var carouselLinkText = "";
	var carouselSection = "";
	var buttonText = "";
	var buttonPosition = "";
	//Setting custom link capture for image carousels. Note this relies on html format from: https://whichonline.onjira.com/browse/OD-1213
	if (arrOmniture["section"]!="") {carouselSection = ": " + arrOmniture["section"];}
	else {carouselSection = ": " + document.location;}
	if (selectedLink.parent("li").attr("class")!="") {buttonPosition = ": " + selectedLink.parent("li").attr("class").toLowerCase() + " button"}
	if (selectedLink.html()!="") {buttonText = ": " + selectedLink.html();} 
	carouselLinkText = "carousel" + carouselSection + buttonPosition + buttonText;
	carouselLinkText = carouselLinkText.toLowerCase();
	s.tl(this,'o',carouselLinkText);      
}

//Sets custom links on navigation items
function setOmnitureCustomLinks(arrOmniture)
{
	//Top Nav Custom Links
	//--------------------
	$("#user-controls")
	
	//Setting custom link capture for login on top navigation
	.find("#om-login-tn").click(function (){s.tl(this,'o','log in: top navigation: button');}).end()
	
	//Setting custom link capture for signup on top navigation
	.find("#om-signup-tn").click(function (){s.tl(this,'o','sign up: top navigation');}).end()
	
	
	//Setting custom link capture for search on top navigation
	.find("#om-search-tn").click(function (){ 
		s.tl(this,'o','search: top navigation');
		arrOmniture["events"] += ",event1";     
    }).end()
	
	//Setting custom link capture for log out on top navigation
	.find("#om-logout-tn").click(function (){s.tl(this,'o','log out: top navigation: button');}).end()
	
	//Promo Signup/Login Custom Links
	//--------------------------------
	var promoText = "";
	$("div.subscription-panel").find("a.om-signup-promo").click(function (){
		promoText = "signup: promo: right";
		if ($("div.subscription-panel h2").html()!=""){
			promoText = promoText + ": " + $("div.subscription-panel h2").html();
		}
		if ($(this).html()!=""){
			promoText = promoText + ": " + $(this).find("span").html();
		}
		promoText = promoText.toLowerCase();
		s.tl(this,'o',promoText);
	}).end()
	
	$("div.subscription-panel-b")
	.find("a.om-signup-promo").click(function (){
		promoText = "signup: promo: full";
		if ($("div.subscription-panel-b h2").html()!=""){
			promoText = promoText + ": " + $("div.subscription-panel-b h2").html();
			promoText = promoText.replace("<span>", "");
			promoText = promoText.replace("</span>", "");
		}
		if ($(this).html()!=""){
			promoText = promoText + ": " + $(this).find("span").html();
		}
		promoText = promoText.toLowerCase();
		s.tl(this,'o',promoText);
	}).end()
	.find("a.om-login-promo").click(function (){
		promoText = "login: promo: full";
		if ($("div.subscription-panel-b h2").html()!=""){
			promoText = promoText + ": " + $("div.subscription-panel-b h2").html();
			promoText = promoText.replace("<span>", "");
			promoText = promoText.replace("</span>", "");
		}
		if ($(this).html()!=""){
			promoText = promoText + ": " + $(this).find("span").html();
		}
		promoText = promoText.toLowerCase();
		s.tl(this,'o',promoText);
	}).end()
	
	return arrOmniture;
}

//These are required by the Switch site only
function setSwitchVariables(arrOmniture) {

	//Events needs to be fired when a user starts the switch process after homepage
	if (!window.omnitureSwitchStarted) {omnitureSwitchStarted = false}
	if (arrOmniture["pageName"].search("which switch: current situation")!=-1 && omnitureSwitchStarted) {
		arrOmniture["events"] += ",event7";
	}
	
	//Events needs to be fired when a user reaches the results page
	if (arrOmniture["pageName"].search("which switch: results")!=-1) {
		arrOmniture["events"] += ",event5";
	}
	
	//Events needs to be fired when a user reaches the save quote page
	if (arrOmniture["pageName"].search("which switch: save results")!=-1) {
		arrOmniture["events"] += ",event16";
	}
	
	//Events & data captures need to be fired when a user reaches the confirmation page
	if (arrOmniture["pageName"].search("which switch: confirmation")!=-1) {
		arrOmniture["events"] += ",event4,event18";
		
		//Sets the region of the switch		
		if (!window.omnitureRegion) {omnitureRegion = ""}
		arrOmniture["prop35"] = omnitureRegion;
		
		//Sets the saving of the switch
		if (!window.omnitureSavings) {omnitureSavings = ""}
		arrOmniture["prop43"] = omnitureSavings;

		//Sets the variables specific to fuel type
		if (!window.omnitureOldGasSupplier) {omnitureOldGasSupplier = ""}
		if (!window.omnitureOldElectricitySupplier) {omnitureOldElectricitySupplier = ""}
		
		//Sets the same supplier switched from variable
		if ((omnitureOldGasSupplier != "") && (omnitureOldGasSupplier == omnitureOldElectricitySupplier)) {
			arrOmniture["prop39"] = "yes";
		} else if ((omnitureOldGasSupplier != "") && (omnitureOldGasSupplier != omnitureOldElectricitySupplier) && (omnitureOldElectricitySupplier != "")) {
			arrOmniture["prop39"] = "no";
		}
		
		//Sets the tariff user has switched to
		if (!window.omnitureTariff) {omnitureTariff = ""}
		arrOmniture["prop40"] = omnitureTariff;
		
		//Sets the old tariff(s) the user has switched from
		if (!window.omnitureOldGasTariff) {omnitureOldGasTariff = ""}
		arrOmniture["prop41"] = omnitureOldGasTariff;
		
		if (!window.omnitureOldElectricityTariff) {omnitureOldElectricityTariff = ""}
		arrOmniture["prop42"] = omnitureOldElectricityTariff;
		
		//Sets the energy supplier user has switched to
		if (!window.omnitureEnergySupplier) {omnitureEnergySupplier = ""}
		arrOmniture["prop32"] = omnitureEnergySupplier;
		
		//Initialises the bill sizes
		if (!window.omnitureBillSizeGas) {omnitureBillSizeGas = ""}
		if (!window.omnitureBillSizeElectrictity) {omnitureBillSizeElectrictity = ""}
		
		//Set to total time a user spent from the homepage to the confirmation page
		if (!window.secondsTotalElapsed) {secondsTotalElapsed = 0}
		arrOmniture["prop17"] = secondsTotalElapsed;
		
		if (!window.omnitureProcessedEnergyType) {omnitureProcessedEnergyType = ""}
		if (omnitureProcessedEnergyType == "ELECTRICITY") {
			arrOmniture["prop31"] = "electricity"; //Switch fuel type
			arrOmniture["prop49"] = "electricity:"+omnitureOldElectricitySupplier; //Switch from supplier by fuel type combo
			arrOmniture["prop50"] = "electricity:"+omnitureOldElectricityTariff; //Switch from tariff by fuel type combo
			arrOmniture["prop34"] = omnitureOldElectricitySupplier; //Old electricity supplier
			arrOmniture["events"] += ",event10,event13";
			//Sets the bill electricity size value
			if (omnitureBillSizeElectrictity=="true") {
				arrOmniture["prop38"] = "yes";
			} else if (omnitureBillSizeElectrictity=="false") {
				arrOmniture["prop38"] = "no";
			}
			arrOmniture["prop44"] = arrOmniture["prop38"]; //Assigns the electricity bill size prop to the generic bill size prop
			arrOmniture["prop25"] = omnitureOldElectricitySupplier; //Assigns the old electricity supplier to the "Energy Supplier Switch From Single/Dual Report" variable
		} 
		else if (omnitureProcessedEnergyType == "GAS") {
			arrOmniture["prop31"] = "gas"; //Switch fuel type
			arrOmniture["prop49"] = "gas:"+omnitureOldGasSupplier; //Switch from supplier by fuel type combo
			arrOmniture["prop50"] = "gas:"+omnitureOldGasTariff; //Switch from tariff by fuel type combo
			arrOmniture["prop33"] = omnitureOldGasSupplier; //Old gas supplier
			arrOmniture["events"] += ",event10,event14";
			//Sets the bill gas size value
			if (omnitureBillSizeGas=="true") {
				arrOmniture["prop37"] = "yes";
			} else if (omnitureBillSizeGas=="false") {
				arrOmniture["prop37"] = "no";
			}
			arrOmniture["prop44"] = arrOmniture["prop37"]; //Assigns the gas bill size prop to the generic bill size prop
			arrOmniture["prop25"] = omnitureOldGasSupplier; //Assigns the old gas supplier to the "Energy Supplier Switch From Single/Dual Report" variable
		}
		else if (omnitureProcessedEnergyType == "DUAL_FUEL") {
			arrOmniture["prop31"] = "dual"; //Switch fuel type
			if ((omnitureOldGasSupplier == omnitureOldElectricitySupplier) && (omnitureOldElectricitySupplier != "")) {//Same supplier switched from, flag as dual switch
				arrOmniture["prop49"] = "dual:"+omnitureOldGasSupplier; //Switch from supplier by fuel type combo
			} else {
				arrOmniture["prop49"] = "both:"+omnitureOldGasSupplier+","+omnitureOldElectricitySupplier; //Switch from supplier by fuel type combo
			}
			if ((omnitureOldGasTariff == omnitureOldElectricityTariff) && (omnitureOldElectricityTariff != "")) {//Same tariff switched from, flag as dual switch
				arrOmniture["prop50"] = "dual:"+omnitureOldGasTariff; //Switch from tariff by fuel type combo
			} else {
				arrOmniture["prop50"] = "both:"+omnitureOldGasTariff+","+omnitureOldElectricityTariff; //Switch from tariff by fuel type combo
			}
			arrOmniture["prop34"] = omnitureOldElectricitySupplier; //Old electricity supplier
			arrOmniture["prop33"] = omnitureOldGasSupplier; //Old gas supplier
			arrOmniture["events"] += ",event11,event14,event13";
			//Sets the bill gas size value
			if (omnitureBillSizeGas=="true") {
				arrOmniture["prop37"] = "yes";
			} else if (omnitureBillSizeGas=="false") {
				arrOmniture["prop37"] = "no";
			}
			//Sets the bill electricity size value
			if (omnitureBillSizeElectrictity=="true") {
				arrOmniture["prop38"] = "yes";
			} else if (omnitureBillSizeElectrictity=="false") {
				arrOmniture["prop38"] = "no";
			}
			
			//When dual fuel is selected a few variables will need to be initialised twice, once for gas and once for electricity as below
			//============================================================================================================================
			//The bill size yes/no must be set for gas and electricity. Gas is done the normal way, but electricity is done by custom link inline on the confirmation page
			s.eVar44 = arrOmniture["prop44"] = arrOmniture["prop37"]; //Assigns the gas bill size prop to the generic bill size prop
			//The supplier switched from must be set for gas and electricity. Gas is done the normal way, but electricity is done by custom link inline on the confirmation page
			arrOmniture["prop25"] = omnitureOldGasSupplier; //Assigns the gas supplier switched from to the generic supplier switched from prop
		}
		
		//Sets the Economy 7 value user has switched to
		if (!window.omnitureEconomy7) {omnitureEconomy7 = ""}
		if (omnitureProcessedEnergyType == "GAS") {
			omnitureEconomy7 = "";//Don't log it
		}
		else {
			if (omnitureEconomy7=="true") {
				arrOmniture["prop36"] = "yes";
			} else if (omnitureEconomy7=="false") {
				arrOmniture["prop36"] = "no";
			}
		}

	}
	
	return arrOmniture;
}

function setOmnitureVariables(omPageName) {
	//Initialising variables array 
	var arrOmniture = new Array();
	arrOmniture["pageName"] = omPageName; 
	arrOmniture["server"] = "";
	arrOmniture["channel"] = "";
	arrOmniture["pageType"] = "";
	arrOmniture["state"] = "";
	arrOmniture["zip"] = "";
	arrOmniture["events"] = "";
	arrOmniture["products"] = "";
	arrOmniture["purchaseID"] = "";
	//Switch Specific
	arrOmniture["prop25"] = "";
	arrOmniture["prop28"] = "";
	arrOmniture["prop29"] = "";
	arrOmniture["prop31"] = "";
	arrOmniture["prop32"] = "";
	arrOmniture["prop33"] = "";
	arrOmniture["prop34"] = "";
	arrOmniture["prop35"] = "";
	arrOmniture["prop36"] = "";
	arrOmniture["prop37"] = "";
	arrOmniture["prop38"] = "";
	arrOmniture["prop39"] = "";
	arrOmniture["prop40"] = "";
	arrOmniture["prop41"] = "";
	arrOmniture["prop42"] = "";
	arrOmniture["prop43"] = "";
	arrOmniture["prop44"] = "";
	arrOmniture["prop49"] = "";
	arrOmniture["prop50"] = "";
	/*Some evars are reserved and do not appear here*/
	arrOmniture["hier1"] = "";
	
	if (omPageName!=""){
		
		var arrBreadcrumb = omPageName.split(":"); 
		var numChannelHierCount = arrBreadcrumb.length-2; 
		
		if (arrBreadcrumb.length > 0){
			//If the last array item of breadcrumbs has the value of "home" this means this page is a parent
			if ($.trim(arrBreadcrumb[arrBreadcrumb.length-1]) == "home"){
				arrOmniture["pageName"] = arrOmniture["pageName"].substring(0, omPageName.length-6); //Leave ": home" out of the page name as per http://whichonline.onjira.com/browse/TWO-13 to prevent continuity issues in the data
			}
			//Sets s.channel
			for (i=0;i<=numChannelHierCount;i++){
				arrOmniture["channel"] = arrOmniture["channel"] + arrBreadcrumb[i] + ":";
			}
			arrOmniture["channel"] = arrOmniture["channel"].slice(0,arrOmniture["channel"].length-1);
			
			//Sets s.hier1
			for (i=0;i<=numChannelHierCount;i++){
				arrOmniture["hier1"] = arrOmniture["hier1"] + arrBreadcrumb[i] + ",";
			}
			arrOmniture["hier1"] = arrOmniture["hier1"].replace(/, /g, ",");
			arrOmniture["hier1"] = arrOmniture["hier1"].slice(0,arrOmniture["hier1"].length-1);
			
			//Sets s.prop7 - s.prop9: sections and subsections
			for (i=1;i<=numChannelHierCount;i++){
				var iPos = "prop" + (i+6);
				arrOmniture[iPos] = arrBreadcrumb[i];
			}
		}
		
		arrOmniture["section"] = arrBreadcrumb[0];
	}
	arrOmniture = setOmnitureCustomLinks(arrOmniture);
	if (omPageName.search("which switch:")!=-1) {
		arrOmniture = setSwitchVariables(arrOmniture);//Specific to Switch only
	}
	//Returns array of variable values needed by Site Catalyst
	return arrOmniture;
}
