/*
 * Peer1 tracking code.
 * Version:2011121001
 */
var peer1tracking = {
	valid_vars:{"mm_campaign":null
		,"keyword":null
		,"v":null
		,"utm_source":null
		,"utm_medium":null
		,"utm_term":null
		,"utm_campaign":null
		,"utm_ad_group":null
		,"utm_content":null
		,"PSE":null
		,"PAG":null
		,"PKW":null
		,"gclid":null
		,"mkwid":null
		,"pcrid":null}
	,COOKIE_LONG:null
	,COOKIE_SHORT:null
	,COOKIE_1HOUR:null
	,cookies:{}
	,newcookie:"" //to be deleted
	,keyword:null
	,referrerHostName:null
	,referrerURL:null
	,URLMinusQueryString:document.location.protocol+"//"
		+document.location.hostname
		+document.location.pathname
};

peer1tracking.DecodeQueryStringIntoMap = function (querystring) {
	var vars = querystring.split("&");
	for (var i = 0; i < vars.length; i++) {
	    var pair = vars[i].split("=");
	    if(typeof this.valid_vars[pair[0]] != 'undefined')
	    	this.valid_vars[pair[0]]=unescape(pair[1]);
	}
};

peer1tracking.DecodeQueryString = function (querystring) {
	var output = {};
	if(typeof querystring != 'undefined') {
		var vars = querystring.split("&");
		for (var i = 0; i < vars.length; i++) {
		    var pair = vars[i].split("=");
		    output[pair[0].toLowerCase()]=unescape(pair[1]); //lowercasing the key just because external urls can do anything
		}
	}
	return output;
};

peer1tracking.ParseCookies = function () {
	if(document.cookie.length<1)
		return false;
	var vars = document.cookie.split(";");
	for (var i=0;i<vars.length;i++) {
		this.cookies[vars[i].substring(0,vars[i].indexOf("=")).replace(/^\s+|\s+$/g,"")]=
			vars[i].substring(vars[i].indexOf("=")+1);
	}
	return true;
};

peer1tracking.SetCookie = function(name, value, expiryUTCDate) {
	if(value==null || value=="null")
		return false;
	var expiry=expiryUTCDate==null?"":" expires="+expiryUTCDate+";";
	var path=" path=/;"
	document.cookie=name+"="+escape(value)+";"+expiry+path;
	this.newcookie+=name+"="+escape(value)+";"+expiry+path;
	return true;
};

peer1tracking.GetKeywordByProvider = function(providerName) {
//this.referrerQueryVars is expected to be initialized
	var key = 'q';
	switch(providerName) {
		case 'yahoo':
			key = 'p';
			break;
		case 'aol':
			key = 'query';
			break;
		default: //we already set default before so nothing to do
	}
	return typeof this.referrerQueryVars[key] != 'undefined'?this.referrerQueryVars[key]:null;
};

peer1tracking.ExecuteTracking = function () {
	//setup dates
	var temp_date = new Date();
	temp_date.setDate(temp_date.getDate() + 365);
	this.COOKIE_LONG = temp_date.toUTCString();
	temp_date = new Date();
	temp_date.setDate(temp_date.getDate() + 30);
	this.COOKIE_SHORT = temp_date.toUTCString();
	temp_date = new Date();
	temp_date.setTime(temp_date.getTime()+1000*60*60*1);
	this.COOKIE_1HOUR = temp_date.toUTCString();

	this.DecodeQueryStringIntoMap(document.location.search.substring(1));
	this.ParseCookies();
	
	//parse vendor info
	if(typeof this.cookies["fc_v"] == 'undefined') {//not a repeat vendor visitor
		for(var key in this.valid_vars) {
			if(this.valid_vars[key]!=null) {
				this.SetCookie("fc_"+key, this.valid_vars[key], this.COOKIE_LONG);
			}
		}
		if(this.valid_vars["v"]!=null) {
			this.SetCookie("p1_fc_pageurl", this.URLMinusQueryString , this.COOKIE_LONG);
		}
	}
	if(this.valid_vars["v"]!=null) {//this is a vendor visitor
		for(var key in this.valid_vars) {
			if(this.valid_vars[key]!=null) {
				this.SetCookie("lc_"+key, this.valid_vars[key], this.COOKIE_SHORT);
			}
		}
		this.SetCookie("p1_lc_pageurl", this.URLMinusQueryString, this.COOKIE_SHORT);
	}
	
	//set landing page
	if(typeof this.cookies["p1_fc_pageurl"] == 'undefined')
		this.SetCookie("p1_fc_pageurl", this.URLMinusQueryString, this.COOKIE_LONG);
	if(typeof this.cookies["p1_lc_pageurl"] == 'undefined')
		this.SetCookie("p1_lc_pageurl", this.URLMinusQueryString, this.COOKIE_SHORT);
	
	
	if(document.referrer.length!=0) {//this hit comes from an referring location. tag some cookies
		this.referrerURLParts = document.referrer.split("?");
		//line below could potentially math [google.local] against http://somebody.local/google.local/morepathname/
		this.localrequest = this.referrerURLParts[0].toLowerCase().indexOf(document.location.hostname)>0;
		
		if(!this.localrequest) {//external link to this page

			//setup referrer domain name
			this.referrerTLDParts = this.referrerURLParts[0].toLowerCase();
			this.referrerTLDParts = this.referrerTLDParts.substring( //drop http
					this.referrerTLDParts.indexOf("http://")+7);
			this.referrerTLDParts = this.referrerTLDParts.split("/")[0]; //drop everything after /
			this.referrerHostName = this.referrerTLDParts;
			this.referrerTLDParts = this.referrerTLDParts.split(".");

			//setup referrer query variables
			this.referrerQueryVars = this.DecodeQueryString(this.referrerURLParts[1]);
			
			//grab keywords for domains with one suffix. i.e. peer1.com
			this.keyword = this.GetKeywordByProvider(this.referrerTLDParts[this.referrerTLDParts.length-1]);

			//failing to find a keyword let's try again in case it's peer1.co.uk (2 suffix case)
			if(this.referrerTLDParts.length>2 && this.keyword==null)
				this.keyword = this.GetKeywordByProvider(this.referrerTLDParts[this.referrerTLDParts.length-2]);
			//if keyword is still null then let's use key q otherwise it's still null
			if(this.keyword==null &&  typeof this.referrerQueryVars['q'] != 'undefined')
				this.keyword = this.referrerQueryVars['q'];

			this.referrerURL = document.referrer;
			if(typeof this.cookies["p1_fc_sekw"] == 'undefined') { //first time visitor (not local)
				this.SetCookie("p1_fc_se",this.referrerHostName, this.COOKIE_LONG);
				this.SetCookie("p1_fc_sekw",this.keyword, this.COOKIE_LONG);
				this.SetCookie("p1_fc_serefurl",this.referrerURL, this.COOKIE_LONG);

				this.SetCookie("p1_lc_se",this.referrerHostName, this.COOKIE_SHORT);
				this.SetCookie("p1_lc_sekw",this.keyword, this.COOKIE_SHORT);
				this.SetCookie("p1_lc_serefurl",this.referrerURL, this.COOKIE_SHORT);
			}
		}		
	}
	else if(typeof this.cookies["p1_reftype"] == 'undefined') {//this is a direct
		this.SetCookie("p1_reftype", "Direct", this.COOKIE_1HOUR);
	}
};

peer1tracking.ExecuteTracking();

