// Config
var site_id = 99; /* www.pegasustheatre.org.uk*/
var image_height = 1;
var image_width = 1;
var url_root = "http://agora-eye.net/";

// Thanks to Bill Dortsch for his cookie recipes
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) {
			break;
		}
	}
	return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}


// Get/create the session ID
var session_id = GetCookie("tracker_session_id");

if (session_id) {
	
	// Cookie is set so this is an existing session
	var new_session = 0;
	
} else {
	
	// No cookie set - so this is a new session (or cookies are disabled)
	var new_session = 1;
	
	// Create the session_id used to identify path of user
	var session_id = site_id + "-" + ((Math.random() * 1)*1000000000000000000); 
	
	// TODO: Check if it's actually set - if not then don't try to store path/session data?
	SetCookie("tracker_session_id", session_id, "", "/", "", "");
	
}

// Build the URL to the tracker
var url_string = url_root;
url_string += "?site_id=" + site_id;
url_string += "&new_session=" + new_session;
url_string += "&session_id=" + escape(session_id);
url_string += "&remote_address=" + escape(remote_address);

if (download_file != "" && path2download != "") {
	
	// Download data
	url_string += "&download=" + escape(download_file);
	url_string += "&path2download=" + escape(path2download);
	
	// Link to be tracked
	document.write("<a href='" + url_string + "' target='_blank'");
	if (text_class != "") {
		document.write(" class='" + text_class + "' ");
	}
	document.write(">" + linkcontent + "</a>");
	
} else {
	
	// These are only required for page views, not downloads
	url_string += "&app_name=" + escape(navigator.appName);
	url_string += "&app_version=" + escape(navigator.appVersion);
	url_string += "&browser_language=" + escape(navigator.language);
	url_string += "&system_language=" + escape(navigator.systemLanguage);
	url_string += "&platform=" + escape(navigator.platform);
	url_string += "&colour_depth=" + escape(screen.colorDepth);
	url_string += "&pixel_depth=" + escape(screen.pixelDepth);
	url_string += "&screen_width=" + escape(screen.width);
	url_string += "&screen_height=" + escape(screen.height);
	url_string += "&available_width=" + escape(screen.availWidth);
	url_string += "&available_height=" + escape(screen.availHeight);
	url_string += "&protocol=" + escape(location.protocol);
	url_string += "&host_name=" + escape(location.hostname);
	url_string += "&port=" + escape(location.port);
	url_string += "&path_name=" + escape(location.pathname);
	url_string += "&file_name=" + escape(location.pathname.substring(location.pathname.lastIndexOf("/") + 1, location.pathname.length));
	url_string += "&query_string=" + escape(location.search);
	url_string += "&document_title=" + escape(document.title);
	url_string += "&last_modified=" + (Date.parse(document.lastModified) / 1000); // Unix timestamp
	url_string += "&referrer=" + escape(document.referrer);
	url_string += "&sales=" + escape(sales);
	url_string += "&activity=" + escape(activity);
	
	// Check for Java support
	url_string += "&java_enabled=";
	if (
		typeof navigator != 'undefined' &&
		typeof navigator.javaEnabled != 'undefined' &&
		navigator.javaEnabled()
	) {
		url_string += "1";
	} else {
		url_string += "0";
	}
	
	// Page view to be tracked - output an image
	document.write("<img border='0' hspace='0' vspace='0' width='" + image_width + "' height='" + image_height + "' src='" + url_string + "' />");
	
}

function track_lightbox(filename) {
	
	// Remove http://domain.name from the start if necessary
	filename = filename.replace(/^http:\/\/[^\/]+\//i, '/');
	
	// Build the URL to the tracker
	var url_string = url_root;
	url_string += "?site_id=" + site_id;
	url_string += "&new_session=" + new_session;
	url_string += "&session_id=" + escape(session_id);
	url_string += "&remote_address=" + escape(remote_address);
	url_string += "&app_name=" + escape(navigator.appName);
	url_string += "&app_version=" + escape(navigator.appVersion);
	url_string += "&browser_language=" + escape(navigator.language);
	url_string += "&system_language=" + escape(navigator.systemLanguage);
	url_string += "&platform=" + escape(navigator.platform);
	url_string += "&colour_depth=" + escape(screen.colorDepth);
	url_string += "&pixel_depth=" + escape(screen.pixelDepth);
	url_string += "&screen_width=" + escape(screen.width);
	url_string += "&screen_height=" + escape(screen.height);
	url_string += "&available_width=" + escape(screen.availWidth);
	url_string += "&available_height=" + escape(screen.availHeight);
	url_string += "&protocol=" + escape(location.protocol);
	url_string += "&host_name=" + escape(location.hostname);
	url_string += "&port=" + escape(location.port);
	url_string += "&path_name=" + escape(filename);
	url_string += "&file_name=" + escape(filename.substring(filename.lastIndexOf("/") + 1, filename.length));
	url_string += "&query_string=" + escape(location.search);
	url_string += "&document_title=" + escape(document.title);
	url_string += "&last_modified=" + (Date.parse(document.lastModified) / 1000); // Unix timestamp
	url_string += "&referrer=" + escape(document.referrer);
	url_string += "&sales=" + escape(sales);
	url_string += "&activity=" + escape(activity);
	
	// Check for Java support
	url_string += "&java_enabled=";
	if (
		typeof navigator != 'undefined' &&
		typeof navigator.javaEnabled != 'undefined' &&
		navigator.javaEnabled()
	) {
		url_string += "1";
	} else {
		url_string += "0";
	}
	
	// Load the "image" directly using JS instead of outputting it
	var trackerImage = new Image();
	trackerImage.src = url_string;
	
}

