
/*
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);
c4prop5Verticals = ["Bigbrother","4car","Film","Music","4talent"];
if(c4result) {
	var c4Url = c4result[0];
	var c4Protocol = c4result[1];
	var c4Host = (c4result[2].indexOf("//")!=-1)?c4result[2].replace(/\/\//g,"/"):c4result[2];
	var c4Path = (c4result[3].indexOf("//")!=-1)?c4result[3].replace(/\/\//g,"/"):c4result[3];
	var c4Dirs = c4Path.indexOf("/")!=-1?c4Path.split("/"):null;
	var c4Filename = (c4Dirs&&c4Dirs.length&&c4Dirs[c4Dirs.length-1].indexOf('.')!=-1)?c4Dirs[c4Dirs.length-1]:'';
	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.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;
				if (v.indexOf("index.")!=-1 && c4Dirs==null){

					if (c4Host.indexOf("www.channel4.com")!=-1){
						v = "Home Page";
					} else {
						v = "Home Page - " + c4Host;
					}

				}
				//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 (c4Url.indexOf(c4Host+"/?")!=-1){

					if (c4Host.indexOf("www.channel4.com")!=-1){
						v = "Home Page ";
					} else {
						v = "Home Page - " + c4Host;
					}

				}

				//if error Pages
				if(document.errorPage != null) {
				v = document.errorPage==""?v:document.errorPage+" : URL "+c4Path;
				}
				v = (v.indexOf("/index.html")!=-1)?v.substring(0,v.indexOf("/index.html")):v;

			} else {
				// Added to give identification to Home Pages that are not the C4 root
				// such as 'picturethis.channel4.com'
				if (c4Host.indexOf("www.channel4.com")!=-1){
					v = "Home Page";
				} else {
					v = "Home Page - " + c4Host;
				}
			}
		} 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 (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());
		}
		if (v&&v=="Blogs") {
			v = (document.c4Vertical)?document.c4Vertical.replace(document.c4Vertical.charAt(0),document.c4Vertical.charAt(0).toUpperCase()):v;
		}
		if (v&&v=="Lifestyle") v="Life";
	}
	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());
	}
	if (v&&v=="Blogs") {
		v = (document.c4Vertical)?document.c4Vertical.replace(document.c4Vertical.charAt(0),document.c4Vertical.charAt(0).toUpperCase()):v;
	}
	if (v&&v=="Lifestyle") v="Life";
	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;}
			// Blogs override
			if (c4Dirs[1]=="blogs"||c4Dirs[0]=="blogs") {
				v = "Blogs - ";
				blognameloc = (c4Dirs[0]=="blogs")?2:3;
				blogname = (c4Dirs[blognameloc]!=null&&c4Dirs[blognameloc]!="")?c4Dirs[blognameloc]:"Unknown";
				blogname = blogname.match(/([\d\w]+)(?:\?|$)/)?blogname.match(/([\d\w]+)(?:\?|$)/)[1]:blogname;
				v += blogname;
			}
		}
	} else {
		v = document.c4SubDirectory;
	}
	break;
	case 4:
	//catch microsites
	//current LOGIC -- retrieves

	if(!document.c4microsite || document.c4microsite=="") {
	 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 {
			if(c4Path.indexOf('ontv')!=-1) {
				//other mirosites added 31/01/07
				if(c4Url.match(/(ontv)\/(\S+[^\/])/)!=null) {
				v = c4Url.match(/(ontv)\/(\S+[^\/])/)[2];
				v = v.substring(0,v.indexOf("/")!=-1?v.indexOf("/"):v.length);
				}
			} else if(c4Path.indexOf('on-tv')!=-1) {
				if(c4Url.match(/(on-tv)\/(\S+[^\/])/)!=null) {
				v = c4Url.match(/(on-tv)\/(\S+[^\/])/)[2];
				v = v.substring(0,v.indexOf("/")!=-1?v.indexOf("/"):v.length);
				}
			} else {
				v="";

			}
		}
	 }
	} else {
	 v = document.c4microsite;
	}
	break;
	case 5:
		var validProp5 = false;
		var currentVertical = c4props(2);
		for(i=0;i<c4prop5Verticals.length;i++) {
			if (c4prop5Verticals[i]==currentVertical) validProp5 = true;
		}
		v = (validProp5)?c4props(7):"";
	break;
	case 6:
	// Time of day reporting
	v = s.getTimeParting('h','0','2008');
	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( window.googleResult ) !== "undefined" ) {
		if (
				typeof( googleResult.internal ) !== "undefined" ||
				typeof( googleResult.external ) !== "undefined"
		) { /* Google Search */
			site	= ( googleResult.internal === "true" ) ? googleResult.site || "Unknown" : ( googleResult.external === "true" ) ? "websearch" : googleResult.site || "Unknown" ;
			query	= ( googleResult.query || "" ).replace( "|", "" ).toLowerCase();
			v 		= ( site + ": " + query ).toLowerCase();
		} else { /* previous Search */
			site	= googleResult.site || "Unknown" ;
			query	= ( googleResult.query || "" ).replace( "|", "" ).toLowerCase();
			v 		= ( site + ": " + query ).toLowerCase();
		}
	} else if ( c4Host=="search.channel4.com" ) {
		site	= "search_" + ( c4Params["site"] || "all" );
		query	= c4Params["q"] || "" ;
		v		= ( site + ": " + query ).toLowerCase();
	}
	break;
	case 9:
	//Search Results - uses the googleResult object if present
	if (typeof(googleResult) != 'undefined') {
		v = (googleResult.size || 0).toString();
	}
	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:
	if (document.c4SubDirectory==null||document.c4SubDirectory=="") {
		if(c4Path && (!document.errorPage ||document.errorPage=="") && c4Dirs!=null) {
			//v = c4Path.split("/").length>2?c4Path.split("/")[1]:"";
			v = c4Dirs[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;}
			v = (v==c4Filename||v.indexOf('?')!=-1)?"":v; // Empty if it is the filename part or query string
			// Blogs override
			if (c4Dirs[1]=="blogs"||c4Dirs[0]=="blogs") {
				v = "Blogs - ";
				blognameloc = (c4Dirs[0]=="blogs")?2:3;
				blogname = (c4Dirs[blognameloc]!=null&&c4Dirs[blognameloc]!="")?c4Dirs[blognameloc]:"Unknown";
				blogname = blogname.match(/([\d\w]+)(?:\?|$)/)?blogname.match(/([\d\w]+)(?:\?|$)/)[1]:blogname;
				v += blogname;
			}
		}
	} else {
		v = document.c4SubDirectory;
	}
	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;
	case 19:// full URL without domain infor
		if(c4Path && c4Dirs!=null) {
			v = c4Path;
		}
	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";
	}
}



