var loadTime = 0;
var pageId = 0;
var slotId = 0;
var ppfs = 1;
var params = '';
var actions = '';

//if there is not a ppfs_traffic_id yet and the user has cookies enabled log a traffic_id which will determine if a master_traffic_id needs recorded
if (getCookie("ppfs_traffic_id") == null) {
	if (typeof navigator.cookieEnabled == "undefined" && !navigator.cookieEnabled)
		document.cookie = "ppfs_traffic_id";
	if(navigator.cookieEnabled || getCookie("ppfs_traffic_id") != null) {
		//not asynchronous so that the cookie can be set before the next if statement is executed
		new Ajax.Request('/logging/recordPpfsTraffic', 
			{asynchronous:false,  
			parameters:'ppfs=' + ppfs +
				'&request_uri=' + escape(document.location.href) }
		); 
	}
}

//if the previous cookie took or they already had one
if(getCookie("ppfs_traffic_id") != null) {
	//determine if the user is coming back to the site.  If so log a traffic_id for the new visit
	if(getCookie("traffic_id") == null) {
		new Ajax.Request('/logging/recordTraffic', 
			{asynchronous:true, 
			parameters:'referrer=' + escape(document.referrer) + 
				'&request_uri=' + escape(document.location.href) + 
				'&user_agent=' + escape(navigator.userAgent)}
		);
	}
	
	//init slots and actions so they can be tracked
	window.onload = function () { 
		loadTime = new Date().getTime();
		new Ajax.Request('/logging/initActions', {
		  method:'get',
		  onSuccess: function(transport){		  
		    eval(transport.responseText);
		  }
		});	
		
		new Ajax.Request('/logging/initSlots', {
		  method:'get',
		  onSuccess: function(transport){
		    eval(transport.responseText);
		  }
		});		
	};
	
	window.onbeforeunload = function () { 		
		if(document.location.href.indexOf('?') >= 0)
			params = document.location.href.substr(document.location.href.indexOf('?') + 1) + params;
		
		var unloadTime = new Date().getTime();
		if(loadTime == 0)
			loadTime = unloadTime;

		if(actions.length > 0) {			
			actions = '[' + actions.substr(0, actions.length - 1) + ']';
			eval('actions = ' + actions);
			for(var i = 0; i < actions.length; i++)
				params += '&act_' + actions[i].action_id + '_' + actions[i].time_diff + '=' + actions[i].element_name;
		}

		//this is not asynchronous because when it is it sometimes does not make the call when using the browser navigational features
		new Ajax.Request('/logging/recordTrafficLog', 
			{asynchronous:false, 
			parameters:'time_diff=' + (unloadTime - loadTime) + 
				'&page_id=' + pageId + 
				'&slot_id=' + slotId +
				'&' + params}
		); 
	};
}
//if you update this function verify the same function in the logging action doesnt need updated
function getRootDomain(domain) {
	if (domain.indexOf(":") > 0)
		domain = domain.substr(domain.indexOf(":") + 1);
	
	var directories = domain.split("/");
	for(var i = 0; i < directories.length; i++)
		if(directories[i].length > 0) {
			var tokens = directories[i].split(".");
			if(tokens.length > 2)
				return directories[i].substr(directories[i].indexOf(".") + 1);
			else
				return directories[i];  						
		}
	return "";
}

function getCookie(name)
{
  var aCookie = document.cookie.split("; ");
  for (var i = 0; i < aCookie.length; i++) {
    var aCrumb = aCookie[i].split("=");
    if (name == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return null;
}
