// OMNITURE Common functions

// 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;
}

// Track internal campaign clicks
function trackOmniClick(pTrackCode) {
	
	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
    }
}

buildPgName();