
/* BB
Channel 4 Omniture
Version 1.2
Date of version 19/12/2006
*/
function c4getParams(qs) {
	splitItems = qs.split("&");
	finalObj = {};
	for (var i=0; i<splitItems.length; i++) {
		thisItem=splitItems[i].split("=");
		finalObj[thisItem[0]] = thisItem[1];
	}
	return finalObj;
}
function c4GetId(teststring) {

	idmatch=/(?:^|&|\?)id=(\d+)/;
	endidmatch=/(?:^|&|\?)[^=&]+id=(\d+)/;
	partidmatch=/(?:^|&|\?)[^=&]+id[^=&]+=(\d+)/;
	anydigitsmatch=/(?:^|&|\?)[^=&]+=(\d+)(?:&|$)/;
	directorymatch=/(?:^|\/)(\d+)(?:\/|$)/;

	if(teststring.match(idmatch)) {
		idno = teststring.match(idmatch)[1];
	} else if(teststring.match(endidmatch)) {
		idno = teststring.match(endidmatch)[1];
	} else if(teststring.match(partidmatch)) {
		idno = teststring.match(partidmatch)[1];
	} else if(teststring.match(anydigitsmatch)) {
		idno = teststring.match(anydigitsmatch)[1];
	} else if(teststring.match(directorymatch)) {
		idno = teststring.match(directorymatch)[1];
	} else {
		idno = "";
	}
		
	return idno;
}
c4urlmatch = /(\w+):\/\/([\w.]+)\/?(\S*)/;
c4domainmatch = /^https?:\/\/(?:\w+\.)?(\w+)\./;
c4result = document.location.href.match(c4urlmatch);
if(c4result) {
	var c4Url = c4result[0];
	var c4Protocol = c4result[1];
	var c4Host = c4result[2];
	var c4Path = c4result[3];
	var c4Dirs = c4Path.indexOf("/")!=-1?c4Path.split("/"):null;
	var c4QueryString = c4Path.indexOf("?")!=-1?c4Path.split("?")[1]:null;
	var c4Params = c4QueryString!=null?c4getParams(c4QueryString):null;
}
function c4pageType() {
 v = document.errorPage != null?document.errorPage:"";
	return v;
}
function c4props(w) {
	v="";
	if(c4Host.indexOf("cms.")!=-1 || c4Host.indexOf("dev.")!=-1 || c4Host.match(/\d+.\d+.\d+.\d+/)!=null ) {
	
	return "";
	}
	switch(w) {
	case "pagename":
	if(document.c4pname==null || document.c4pname=="") {
			if(c4Path) {
				v = c4Path;
				if(v.indexOf("microsites/")!=-1) {
					v = c4Url.replace(c4Url.match(/(microsites\/([A-Z]|[0-9\-?]+))/)[0],"...");	
				}
				// strip domain
				v = v.replace(c4Protocol+"://"+c4Host+"/","");
				//strip query
				v = v.indexOf("?")!=-1?v.split("?")[0]:v;
				//check community
				if (c4Host.indexOf("community")!=-1) {
					if (document.title.match(/^(.+) -/)) {
						v = "Forum Page - ";
						appendTitle = (document.title.match(/^(.+) -/).length>1)?document.title.match(/^(.+) -/)[1]:document.title;
						v += appendTitle;
					} else {
						v = "Forum Page - " + document.title;
					}
				}
				//check external domain
				v = c4Host.indexOf("channel4")!=-1?v:c4Host+":"+v;
				v = c4Path.indexOf("search")!=-1 && c4Path.indexOf("index")!=-1?v.substring(0,c4Path.indexOf("/index")):v;
				
				//check for index//check for trailing slash 
				v = v.lastIndexOf("/")==v.length-1?v.substring(0,v.length-1):v;  // used to be append HOME 
				
				v = v.indexOf("index.")!=-1 && c4Dirs==null?"Home Page":v;
				//v = v.indexOf(".")==-1 && v.indexOf(": Home")==-1 && v.indexOf("seacrh")!=-1?v+": Home":v;
				
			    // check index for query
				v = c4Url.indexOf(c4Host+"/?")!=-1?"Home Page":v;
				//if error Pages
				if(document.errorPage != null) {
				v = document.errorPage==""?v:document.errorPage+" : URL "+c4Path;
				}

			
			} else {
				v="Home Page"
			}
		} else {
			v=document.c4pname;
	}
		
	break;
	case "domain":
	v = c4Url.match(c4domainmatch)[1]||"";
	break;
	case "server":
	v = c4Url.match(c4domainmatch)[1]||"";
	break;
	case "vertical":
	//external domain

	if(c4props(2).indexOf("external domain")!=-1) {
	 v = (document.c4Vertical)?document.c4Vertical.replace(document.c4Vertical.charAt(0),document.c4Vertical.charAt(0).toUpperCase()):"External site, vertical not set"
	} else {
	  if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null) {
	  v = c4Path.match(/(\w+)/)[0].replace((c4Path.match(/(\w+)/)[0].charAt(0)),(c4Path.match(/(\w+)/)[0].charAt(0).toUpperCase()));
	  }
	  // extract community pages
	  v = c4Host.indexOf("community")!=-1?"Community":v;	
	  // verify is external domain
	  v = c4Host.indexOf("channel4")==-1?"external domain":v;
	  //check for query on root 
	  v = c4Url.indexOf(c4Host+"/?")!=-1?"":v;
	}
	break;
	case 1: 
	//retrieves domain
	 v = c4Url.match(c4domainmatch)[1]||"";
	break;
	case 2:
	//retrieves vertical - contains override for variable document.c4Vertical
	if (document.c4Vertical==null||document.c4Vertical=="") {
		if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null) {
			v = c4Path.match(/(\w+)/)[0].replace((c4Path.match(/(\w+)/)[0].charAt(0)),(c4Path.match(/(\w+)/)[0].charAt(0).toUpperCase()));
			// extract community pages
			v = c4Host.indexOf("community")!=-1?"Community":v;	
			// verify is external domain
			v = c4Host.indexOf("channel4")==-1?"external domain":v;
			//check for query on root 
			v = c4Url.indexOf(c4Host+"/?")!=-1?"":v;
			//check for sitewide apps, skip to second level if 'appXX' is at first level
			try {
				v = c4Path.match(/^apps\d+\//)?c4Path.match(/(\w+)/g)[1].replace((c4Path.match(/(\w+)/g)[1].charAt(0)),(c4Path.match(/(\w+)/g)[1].charAt(0).toUpperCase())):v;
			} catch(e) {v=v;}
		}
	} else {
		v = document.c4Vertical.replace(document.c4Vertical.charAt(0),document.c4Vertical.charAt(0).toUpperCase());
	}
	break;
	case 3:
	//catch sub directory if there is one - contains override for document.c4subdirectory
	if (document.c4SubDirectory==null||document.c4SubDirectory=="") {
		if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null) {
			v = c4Path.split("/").length>2?c4Path.split("/")[1]:"";
			if (c4Host.indexOf('community')!=-1) v = c4GetId(c4Path);
			//check for sitewide apps, skip to third level if 'appXX' is at first level
			try {
				v =  c4Path.match(/^apps\d+\//)?c4Path.match(/(\w+)/g)[2]:v; 
			} catch(e) {v=v;}
		}
	} else {
		v = document.c4SubDirectory;
	}
	break;
	case 4:
	//catch microsites
	 if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null ) {
	 	if(c4Path.indexOf("microsites")!=-1) {
			v = c4Url.match(/(microsites\/([A-Z]|[0-9\-?]+))\/(\S+[^\/])/)[3];
			v = v.substring(0,v.indexOf("/")!=-1?v.indexOf("/"):v.length);
		} else {
			v="";
		}
	 }
	break;
	case 5:
	//Channel
	if(!document.errorPage || document.errorPage=="") {
	v = c4Path.indexOf("more4")!=-1?"More4":c4Path.indexOf("e4")!=-1?"E4":c4Path.indexOf("film")!=-1?"Film4":"Channel 4";
	}
	break;
	case 6:
	//Vertical Bigbrother
	if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null) {
	v = c4Path.indexOf("bigbrother")!=-1?"Big Brother":"";
	}
	break;
	case 7:
	if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null) {
	 	//page detail ID and title or blank
		teststring=c4Path;
		idno = c4GetId(teststring);
		if (idno!="") v = "ID:" + idno + ":" + document.title;
	}
	break;
	case 8:
	//Search Keywords - uses the googleResult object if present
	if (typeof(googleResult) != 'undefined') { 
		site = googleResult.site || 'Unknown';
		query = googleResult.query || '';
		query = query.replace('|', '');
		v = site + ": " + query;
	} else if (c4Host=="search.channel4.com") {
		site = "search_" + (c4Params["site"] || "all");
		query = c4Params["q"] || '';
		v = site + ": " + query;
	}
	break;
	case 9:
	//Search Results - uses the googleResult object if present
	if (typeof(googleResult) != 'undefined') {
		v = googleResult.size || 0;
	}
	break;
	// 10,11,12 managed by plug in
	
	case 13:
	// possibble page number - uses the googleResult object if present
	if (typeof(googleResult) != 'undefined') {
		gStart = googleResult.start || 0;
		gNum = googleResult.num || 0;
		if (gStart < 0) gStart = 0; 
		if (typeof(googleResult.page) != 'undefined') {
			v = googleResult.page;
		} else {
			v = (gNum == 0) ? 0 : Math.floor(gStart / gNum) + 1;
		}
	} else if (c4Host=="search.channel4.com") { // else if we are in generic search results
		gStart = c4Params["start"] || 0;
		if (gStart < 0) gStart = 0;
		v = Math.floor(gStart/10) + 1;
	} else { // make a best guess
		if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null) {
		
		 if(c4Url.indexOf("search")!=-1) {
		 v = c4Path.indexOf("&")!=-1?c4Path.split("&")[1]:v;
		 
		 } else {
		 if(c4Path.indexOf(".jsp")!=-1) {
		 v = c4Path.indexOf("?")!=-1?c4Path.split("?")[1]:v;
		 v = v.indexOf("&page=")!=-1?v.split("&page=")[1]:"1";
		 }
		 }
		}
	}	
	break;
	case 15:
	//userID
	break;
	case 16:
	if(c4Path) {
		v = c4Path.indexOf("?")!=-1?c4Path.split("?")[1]:""
		
	}
	break;
	case 18:
	// Full URL for debugging within Omniture
	v = c4Url;
	break;
	case "hier":
	if((!document.errorPage ||document.errorPage=="")) {
	ppsyn ="channel4";
	ppsyn+=c4props(2)!=""?ppsyn="|"+c4props(2):"";
	ppsyn+=c4props(3)!=""?ppsyn="|"+c4props(3):"";
	ppsyn+=c4props(4)!=""?ppsyn="|"+c4props(4):"";
	ppsyn+=c4props("pagename")!=""?ppsyn="|"+c4props("pagename"):"";
	ppsyn+=c4props(7)!=""?ppsyn="|"+c4props(7):"";
	ppsyn+=c4props(8)!=""?ppsyn="|"+c4props(8):"";
	v=ppsyn;
	}
	break;
	}
	
	
	return v;
}

function c4pageInfluenceEvent() {
	s.events=(s.events)?s.events+",event7":"event7";
}

function c4searchResultsEvent() {
	if(!s.products&&s.prop9&&!isNaN(parseInt(s.prop9,10))) {
		s.products = ";;;;event8="+parseInt(s.prop9,10);
		s.events=(s.events)?s.events+",event8":"event8";
	}
}
