// OMNITURE Common functions

var onLoadFunctions = new Array();
var iloadFunction = 0;

// Pass each function that needs to load
function addOnLoad(func) {
    onLoadFunctions[iloadFunction] = func;
    iloadFunction++;
}
// Loops through all of the functions that were added
function loadAllFunctions() {
    for(i=0; i < onLoadFunctions.length; i++) {
        eval(onLoadFunctions[i]+"()");
    }
}
// Load all of the functions that you've set
window.onload = loadAllFunctions;

/* CALL EACH FUNCTION TO LOAD */
addOnLoad("loadFlash");


// Build the page name to pass to the brandbar or any other function requiring the current page name
function buildPgName() {
	var curr_url = window.location.href.split("//");
	var path_array = curr_url[1].split("/");
	// create page name object
	build_pgName = new Object;
	build_pgName.domain = path_array[0];
	build_pgName.dir = '';
	// assign domain
	// There was an error in the bliss share promotion so we are making an exception for the bliss promotion and renaming it deserve.
	if ( build_pgName.domain.match(/bliss\/share/) ) { 
		build_pgName.domain = "hersheys:deserve";
	}
	else {
		build_pgName.domain = "hersheys";
	}
	
	// build directory structure, append file name
	for ( i = 1; i <= path_array.length-1; i++ ) {
		if( path_array[i].indexOf("?") == -1) {
			build_pgName.dir = build_pgName.dir + ":" + path_array[i];
		}
		else {
			var tempSplit = path_array[i].split("?");
			build_pgName.dir = build_pgName.dir + ":" + tempSplit[0];
		}
	}
	// cut file extensions
	build_pgName.dir = build_pgName.dir.split(".");
	build_pgName.dir = build_pgName.dir[0]
	// if there is reference to "index" or "default", replace with "home"
	if ( build_pgName.dir.match(/index/) ) { build_pgName.dir = build_pgName.dir.replace(/index/, "home") }
	if ( build_pgName.dir.match(/default/) ) { build_pgName.dir = build_pgName.dir.replace(/default/, "home") }
	// if the last character is ":", replace with "home"
	if ( build_pgName.dir.charAt(build_pgName.dir.length-1) == ":" ) { build_pgName.dir = build_pgName.dir + "home" }
	// build page name structure
	curr_pgName = build_pgName.domain + build_pgName.dir;
	curr_pgName = curr_pgName.replace(/:#:/, ":");
}

// TRACK DOWNLOADS
function trackDownload(pDownloadType, pCategory, pName) {
	s.events = "event21";
	s.eVar6 = s.prop17 = pDownloadType + ":" + pCategory + ":" + pName;
	s.linkTrackEvents="event21";
	s.linkTrackVars = "eVar6,prop17,events";
}

// TRACK ECARDS
function trackEcards(pEvent, pName) {
	s.events = pEvent;
	s.eVar36 = s.prop26 = "kisses:ecards:" + pName;
	s.linkTrackEvents = pEvent;
	s.linkTrackVars = "eVar36,prop26,events";
}

// Track internal campaign clicks
function trackOmniClick(pTrackCode) {
	clearAllOmnitureVars();
	var s = s_gi(s_account);
    s.templtv = s.linkTrackVars;          //Saving the original state of the variables
    s.templte = s.linkTrackEvents;         //Saving the original state of the variables
    s.linkTrackVars = "events,eVar2";      //Defining the list of variables that will be sent in the server call
	s.eVar2 = pTrackCode + ":" + s.pageName // Tracking code will be provided by Kendra
    s.linkTrackEvents = "event9";
    s.events = "event9";              //Passing in the value into s.events
    
	s.tl(true,'o','internal campaign clicks');   //Sever call
    
	if(s.templtv) {
		s.linkTrackVars=s.templtv;    //reassigning the variables to the original state
	}
	
	if(s.templte) {
		s.linkTrackEvents=s.templte;   //reassigning the variables to the original state
    }
	clearAllOmnitureVars();
}

function clearAllOmnitureVars() {
	var tempVar;
	for (var i = 1; i < 51; i++) {
		tempVar = "eVar";
		tempVar = tempVar + i.toString();
		s[tempVar] = "";
	}
	for (i = 1; i < 51; i++) {
		tempVar = "prop";
		tempVar = tempVar + i.toString();
		s[tempVar] = "";
	}
	s.events = "";
	s.products = "";
	s.linkDownloadFileTypes = "";
	s.linkTrackVars = "";
	s.linkTrackEvents = "";
}

function loadFlash() {
	// HERSHEY BRAND FOOTER
	if (document.getElementById('hershey-canada-brandbar') != null) {
		var so3 = new SWFObject("/en/general/brandbar.swf", "so3", "731", "75", "9", "#FFFFFF"); so3.addVariable("bgColor", "0xFFFFFF"); so3.addVariable("xmlPath", "/en/general/xml/brandbar.xml");	so3.addVariable("logoPath", "/en/general/swf/logos/"); so3.addVariable("inputLiveAccount", "hersheyintcanadadd,hersheycanadaglobal"); so3.addVariable("inputDevAccount", "hersheyintcanadadev"); so3.write("hershey-canada-brandbar"); 
	}
}


buildPgName();