/****************************************************
Author: Brian J Clifton, fix by Lary Stucker-http://freshclicks.net, June 2008
Url: http://www.advanced-web-metrics.com
This script is free to use as long as this info is left in

*** Script for tracking the first and last referrer of a visitor ***

All scripts presented have been tested and validated by the author and are believed to be correct
as of the date of publication or posting. The Google Analytics software on which they depend is
subject to change, however; and therefore no warranty is expressed or implied that they will
work as described in the future. Always check the most current Google Analytics documentation.

****************************************************/
// Grabbed an excerpt from Clifton's script ... and then sprinkling the checkNewVisitor() and grabReferrer() 
// calls anywhere in our code where we make our pageTracker calls. - Eggers, 1:30am May 13, 2009
// http://www.advanced-web-metrics.com/scripts/first-and-last-referrer.txt
// http://www.google.com/support/forum/p/Google+Analytics/thread?tid=0e672a6afb56558c&hl=en

function checkNewVisitor(){
   // check if this is a first time visitor
   newVisitor = 0;
   var myCookie = " " + document.cookie + ";";
   var searchName = "__utma=";
   var startOfCookie = myCookie.indexOf(searchName);
   if (startOfCookie == -1) {   // i.e. first time visitor
	  newVisitor = 1;
   }
}

function grabReferrer(){
// grab campaign and referrer info from the _utmz cookie
    
	if (newVisitor) {		  	
		var z = _uGC(document.cookie, "__utmz=", ";");
		urchin_source = _uGC(z,"utmcsr=", "|");
		urchin_medium = _uGC(z,"utmcmd=", "|");
		urchin_term = _uGC(z,"utmctr=", "|");
		urchin_content = _uGC(z,"utmcct=", "|");
		urchin_campaign = _uGC(z,"utmccn=", "|");
		var gclid = _uGC(z,"utmgclid=","|");
		if (gclid != "-") {
			urchin_source = "google";
			urchin_medium = "cpc";
		}
		var urchin_combined = urchin_source + " - " + urchin_medium + " - " + urchin_term;
		superSetVar(unescape(urchin_combined));
	}

}
//////////////////////////////////////////////////////////////////////////
// End of Clifton's code excerpt
//////////////////////////////////////////////////////////////////////////



	



//////////////////////////////////////////////////////////////////////////
// http://www.lunametrics.com/blog/2008/04/17/stuff-more-than-one-value-in-gas-user-defined-segment/
// "standard" read cookie function  (copied from Prusak's gwo_write.js)	
function read_cookie(cookie_name) {
	var my_cookie=""+document.cookie;
	var ind=my_cookie.indexOf(cookie_name);
	if (ind==-1 || cookie_name=="") return "";
	var ind1=my_cookie.indexOf(';',ind);
	if (ind1==-1) ind1=my_cookie.length; 
	return unescape(my_cookie.substring(ind+cookie_name.length+1,ind1));
}

function superSetVar(appendValue) {
	var gaCode = "UA-493040-1";
	var getVar = read_cookie('__utmv');								// read the __utmv cookie
	hasValue = getVar.indexOf(appendValue);							// does the cookie already have the value we want to append?
	if ( hasValue == -1 ) {											// if the new value is not already in the cookie
		removePrefix = /^.*\.(.*)/.exec(getVar);					// __utmv cookie has the format 12345678.cookieValue - remove the ## prefix
		if (removePrefix && removePrefix[1]) {
			newVar = removePrefix[1] + appendValue;					// append the value
		} else {
			newVar = appendValue;									// this will execute if the cookie was not already set.
		}
		if (gaCode == null) { alert('Please set "gaCode" variable in parent source.'); }
		var superSetVarTracker = _gat._getTracker(gaCode);			// set up a tracker to call _setVar from
		superSetVarTracker._initData();  
		superSetVarTracker._setVar(newVar);							// call _setVar with the new value
	}
}


///////////////////////////////////////////////////////////////////////////
// Pulled from ga.js ...
function _uGC(l,n,s) {
	 if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
	 var i,i2,i3,c="-";
	 i=l.indexOf(n);
	 i3=n.indexOf("=")+1;
	 if (i > -1) {
	  i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
	  c = l.substring((i+i3),i2);
	 }
	 return c;
}
