// Global variable to hold our stuff
var VBM = {};

function reportingBeacon(override){

	// Define the report suites. Use development until approved to go live.
	//VBM.reportsuites = 'telstraglobaldev,telstrabpbigponddev,telstrabpafldev';
	//VBM.reportsuites = 'telstraglobalprd,telstrabpbigpondprd,telstrabpaflprd';
	
	// Define first levels of hierarchy
	var hostname = window.location.hostname.replace(/^www\./,'');

		SitesArray = new Object();
		SitesArray['media.telstra.com.au'] 	= 'Homepage';
			
		if(SitesArray[hostname]) {
			VBM.section = SitesArray[hostname];
			VBM.reportsuites = 'telstrabpbigpondprd';
		} else {
			VBM.reportsuites = 'telstrabpbigponddev'; //test only it will give one of the above random sites
			VBM.section = 'Homepage';
		}

	VBM.hierarchy = 'BP|BP|Portal|'+ VBM.section;
	VBM.QueryString = '';
	
	
	// Method to strip any funny stuff out of the pageName
	VBM.sanitisePageName = function(string){
		// replace | with hyphen.
		string = string.replace(/[|]/g, '-');
		// replace ? with blank
		string = string.replace(/[?]/g, '');
		return string;
	};
	
	VBM.getQueryString = function(){
		var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
		
		while (m = re.exec(queryString)) {
			//alert(m[1]);
			result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
		}
		
		return result;
	};
	
	// If any argument is passed into reportingBeacon(), do nothing
	if (!override) {
	
		// Put code here to pull out the page name and any additional levels of hierarchy, 
		// and pop it on the end of the hierarchy.
		// By default this grabs the document title, if it exists.
		
		// Home page
		var	myhash = {};
		myhash = VBM.getQueryString();
		var val;
		for (val in myhash) {
    		if (myhash.hasOwnProperty(val) && myhash[val].length < 16) {
	        	VBM.QueryString += val + myhash[val]+"|";
    		}
		}
		
		if ((window.location.pathname === '/home.html' || window.location.pathname === '/') && VBM.QueryString.length == 0 ) {
			VBM.hierarchySuffix = '|home';
			//alert(window.location.pathname);
		}
		else {
			VBM.pathArray = window.location.pathname.split('/');
			//alert(VBM.pathArray[1]);
			if (VBM.pathArray.length > 1) {
				 
				 var i;
				 VBM.hierarchySuffix ='';
				 for (i = 1; i < VBM.pathArray.length; i += 1) {if (VBM.pathArray[i].length > 0) {VBM.hierarchySuffix += '|' + VBM.pathArray[i] ;}}

				 	 VBM.hierarchySuffix =VBM.hierarchySuffix + '|'+ VBM.sanitisePageName(document.title)+ '|' + VBM.QueryString; 
			}
			else {
					VBM.hierarchySuffix = '|' + VBM.sanitisePageName(document.title) + '|' + VBM.QueryString;; }
			}
	
		VBM.hierarchySuffix = VBM.hierarchySuffix.toLowerCase();
		VBM.hierarchy = VBM.hierarchy + VBM.hierarchySuffix;
		
		//alert(VBM.hierarchy);
		
		// ****************************************************
		// * You shouldn't need to change anything below here *
		// ****************************************************
		
		// Use Omniture's s.manageVars() to clear all the variables we set here.
		// If using AJAX methods where the page isn't re-loaded, you should do this
		// for ALL Omniture variables using:
		s.manageVars("clearVars");
		
		// Report suites, if they exist
		if (VBM.reportsuites) {
			s.un = VBM.reportsuites;
		}
		
		// Split out pageHierarchy into the appropriate props and vars
		VBM.hierarchy=VBM.hierarchy.replace(/\|telstra media.*|/gi,'');
		VBM.hierarchy=VBM.hierarchy.replace(/\.html|/gi,'');
		
		VBM.hierarchy = VBM.hierarchy.split('|');
		
		var i;
		for (i = 0; i < VBM.hierarchy.length; i += 1) {
			 
			 if (VBM.hierarchy[i].length > 1) {

			switch (i) {
				case 0:
					s.prop1 = VBM.hierarchy[i];
					s.eVar1 = VBM.hierarchy[i];
					break;
				case 1:
					s.prop2 = VBM.hierarchy[i];
					s.eVar2 = VBM.hierarchy[i];
					break;
				case 2:
					s.prop3 = VBM.hierarchy[i];
					s.eVar3 = VBM.hierarchy[i];
					break;
				case 3:
					s.channel = VBM.hierarchy[i];
					s.eVar4 = VBM.hierarchy[i];
					break;
				case 4:
					s.prop4 = VBM.hierarchy[i];
					s.eVar5 = VBM.hierarchy[i];
					break;
				case 5:
					s.prop5 = VBM.hierarchy[i];
					s.eVar15 = VBM.hierarchy[i];
					break;
			}
			
			// Build up the hierarchy delimited by pipes
			if (i !== 0) {
				s.hier1 = s.hier1 + '|'
			}
			s.hier1 = s.hier1 + VBM.hierarchy[i];
			
			// s.pageName skips the second level and delimit by colon
			if (i !== 1) {
				if (i !== 0 && VBM.hierarchy[i].length > 1) {
					s.pageName = s.pageName + ':';
				}
				s.pageName = s.pageName + VBM.hierarchy[i];
				
			}
		}//if
			
		}
		
		var s_code = s.t();
		if (s_code) {
			document.write(s_code);
		}
		s.manageVars('clearVars');
	}
}

s.manageVars=new Function("c","l","f",""
+"var s=this,vl,la,vla;l=l?l:'';f=f?f:1 ;if(!s[c])return false;vl='pa"
+"geName,purchaseID,channel,server,pageType,campaign,state,zip,events"
+",products,transactionID';for(var n=1;n<51;n++){vl+=',prop'+n+',eVar"
+"'+n+',hier'+n;}if(l&&(f==1||f==2)){if(f==1){vl=l;}if(f==2){la=s.spl"
+"it(l,',');vla=s.split(vl,',');vl='';for(x in la){for(y in vla){if(l"
+"a[x]==vla[y]){vla[y]='';}}}for(y in vla){vl+=vla[y]?','+vla[y]:'';}"
+"}s.pt(vl,',',c,0);return true;}else if(l==''&&f==1){s.pt(vl,',',c,0"
+");return true;}else{return false;}");
s.clearVars=new Function("t","var s=this;s[t]='';");
s.lowercaseVars=new Function("t",""
+"var s=this;if(s[t]){s[t]=s[t].toString();s[t]=s[t].toLowerCase();}");


/**************report to omniture************************************/
reportingBeacon();

/** Google Analytics code **/
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-23566142-1', 'auto');
  ga('send', 'pageview');