 // PixelTrack.js
 // written by Raphael Spindell <rspindell@webspin.biz>  
 // last updated: August, 06, 2009
 // requires: pixeltrack.php
 // requires: jquery, jquery plugins: jquery.cookie, jquery.base64, jquery.query

$(document).ready(function() {  
	var refdebug = false;  // set to false for production use
	
	// get PHPSESSID from query string parameter
	var PHPSESSID = $.query.get('PHPSESSID');
	
	// function to simulate the php session.use_trans_sid  setting, whereby the PHPSESSID is appended to href's in the output stream
	function js_trans_sid(data) {
				var phpsessid = data.phpsessid;
				if (refdebug) alert("This is the phpsessid retrieved is: " + phpsessid);
				
				// munge all matching href links on page 
				// $("a[href^='http://fantaseayachts.localhost']").each(function()  { 
					$("a[href]").each(function()  { 					
					var href = this.href;
					//alert("munging: " + href);
					// if the hrefs dont already contain the PHPSESSID query parameter, then add it
					if  (href.indexOf('PHPSESSID=') == -1 ) {
						// check if only param and assign correct prefix as ? or & for param seperator
						var queryprefix = (href.indexOf('?')  ==  -1) ? '?' : '&amp;';    
						this.href = href  +  queryprefix + 'PHPSESSID='  +  phpsessid; 
					}
			   });					
	};	
	
	if (PHPSESSID) {
		// if we have a PHPSESSID on the querystring, then cookies must be off (of course, js could be the reason, but we wouldn't be HERE then either now would we?
		if (refdebug) alert("This PHPSESSID in querystring is: " + PHPSESSID);
		// munge links
		js_trans_sid({'phpsessid' : PHPSESSID});
	} else {
		
		var referrer  = document.referrer;  // Get referrer
		
		// referrer may be blank or null if browser is not sending value because of anonymity settings, or calling from a bookmark perhaps
		referrer = (referrer == "" || referrer == null) ? "No Referrer Detected." : referrer;			
		var referrer64 = $.base64Encode(referrer);   // Encode referrer in Base64 to ensure accurate transport
		
		if (refdebug) alert("This referrer is: " + referrer);
		
		// get cookie and set cookie
		var refcookie = $.cookie('fantasearef64');
		if (refcookie == null || refcookie == "") {
			// cookie does not exist, try to set a persistant cookie so user returning to site within a months time will be tracked as having come  via this referrer
			$.cookie('fantasearef64', referrer64, {path: '/', expires: 30}); // expiry date may be ignored by some browsers
			if (refdebug) alert("cookie was nul or blank, so cookie set to: " + referrer + "(" + referrer64 + ")");
		} else {
			if (refdebug) alert("existing refcookie:" + refcookie + " (" + $.base64Decode(refcookie) + ")");
		}
		
		// EXPERIMENTAL
		//  check cookie again, if still not existing, then cookies not accepted by browser. try to start a php trans_sid session via ajax or tracking pixel
		refcookie = $.cookie('fantasearef64');
		if (refcookie == null || refcookie == "") {		
			// Cookies are not enabled, set tracker by loading a tracking pixel into the html with the referrer as a param
			if (refdebug) alert('cookies not enabled, creating tracking pixel');
			$("#pixeltracker").append("<img src='/_common/_scripts/pixeltrack.php?PHPSESSID=" + PHPSESSID + "&amp;action=getpixel&amp;fantasearef64=" + referrer64 + " width='1' height='1'/>");
			

			// Create a php session and retrieve phpsessid in json object via ajax, then munge all links on page to add phpsessid query parameter for cooieless sessions
			$.getJSON("/_common/_scripts/pixeltrack.php?action=getphpsessid&format=json&fantasearef64=" + referrer64,  js_trans_sid);
		}
	} 
});