$(document).ready(function() {
	jQuery.url.setUrl(location.href);
	var location_current = jQuery.url.attr("path");
	
	//a user clicked a link to an external domain, or a document, log it before they leave
	$('a').click(function() {
		if ($(this).parents('#navigation').size() == 0) {
			var link = $(this).attr("href");
			//the user clicked a link to a different domain, record this
			jQuery.url.setUrl(link);
			
			//lets see if the file is a link to a document
			if(jQuery.url.attr("file") != null) {
				$.post("histories/ping", "data[History][url]="+link+"&data[History][referrer]="+location_current, function() {
					//the next page load will show as original referrer, since its referred from another site
					$.cookie("url", null); 
					window.location.replace(link);
				});
				return false;
			}
			
			//check if the link goes to a different domain
			if(document.domain != jQuery.url.attr("host") && jQuery.url.attr("host") != null) {
				$.post("histories/ping", "data[History][url]="+link+"&data[History][referrer]="+location_current, function() {
					//the next page load will show as original referrer, since its referred from another site
					$.cookie("url", null); 
					window.location.replace(link);
				});
				return false;
			}
		}
	})
	
	//a user loaded this page, now report which page was loaded
	var referrer = "original";
	if($.cookie("url") !== null) {
		referrer = $.cookie("url");
	}
	$.post("histories/ping", "data[History][url]="+location_current+"&data[History][referrer]="+referrer);
	$.cookie("url", location_current);
})
