// 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,telstrabpmoviesdev';
	VBM.reportsuites = 'telstraglobalprd,telstrabpbigpondprd,telstrabpmoviesprd';
	
	// Define first levels of hierarchy
	VBM.hierarchy = 'BP|Entertainment|Movies|Movies';
	
	// Method to strip any funny stuff out of the pageName
	VBM.sanitisePageName = function(string){
		// replace | with hyphen.
		string = string.replace(/[|]/g, '-');
		return string;
	};
	
	VBM.getQueryString = function(){
		var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
		
		while (m = re.exec(queryString)) {
			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
		if (window.location.pathname === '/') {
			VBM.hierarchySuffix = '|home';
		}
		else {
			VBM.pathArray = window.location.pathname.split('/');
			VBM.hierarchySuffix = '|' + VBM.pathArray[1] + '|' + VBM.sanitisePageName(document.title); 
		}
		VBM.hierarchySuffix = VBM.hierarchySuffix.toLowerCase();
		VBM.hierarchy = VBM.hierarchy + VBM.hierarchySuffix;
		
		// ****************************************************
		// * 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")
		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.split('|');
		var i;
		for (i = 0; i < VBM.hierarchy.length; i += 1) {
			
			VBM.hierarchy[i]= VBM.hierarchy[i].replace(' - bigpond movies','');
			
			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) {
					s.pageName = s.pageName + ':';
				}
				s.pageName = s.pageName + VBM.hierarchy[i];
				
			}
			
      if (VBM.doEvents === 'true'){
			    s.events= VBM.event;
          s.products='\;BP:Movies-'+ VBM.productName+'\;1\;'+VBM.productPrice;
          if (VBM.event === 'purchase'){
            s.purchaseID = VBM.purchaseID;
          }
        
      }
		}
		
		var s_code = s.t();
		if (s_code) {
			document.write(s_code);
		}
		s.manageVars('clearVars');
	}
}

function reportingEvent(actionEvent, productName, productPrice, purchaseID){
  if (actionEvent == undefined){
    VBM.doEvents ='false';
  }else{
    VBM.doEvents ='true';
    switch(actionEvent){
      case('title'):
        VBM.event = 'prodView';
        VBM.productName = productName;
        VBM.productPrice = '';
        break;
        
      case('checkout'):
        VBM.event = 'scAdd,scOpen,scCheckout';
        VBM.productName = productName;
        VBM.productPrice = '';
        break;
      
      case('purchase'):
        VBM.event = 'purchase';
        VBM.productName = productName;
        VBM.productPrice = productPrice;
        VBM.purchaseID = purchaseID;
        break;

    }
  }
}

function omnEvent(prodName) { 	
    s.un = VBM.reportsuites;
 
  	s.linkTrackVars = 'products,events';
  	s.linkTrackEvents = 'event39';
  	s.events='event39';
  	s.products='\;BP:Movies-'+ prodName;
  	
  	var s_code = s.tl(this,'o');
		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();}");

