// Set the id of the adsense module
window.google_analytics_uacct = "UA-10810851-2";

(function($) {
	var pageTracker;

	/**
	* Track a pageview, e.g.:
	* $.gaTrackPage({accountId: 'UA-0000000-0'});
	*/
	$.gaTrackPage = function(callerSettings) {
		// Create settings and defaults here
		var settings = $.extend({
			accountId: "UA-10810851-2"
		}, callerSettings || {});

		//check whether to use an unsecured or a ssl connection:
		var host = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
		var src = host + 'google-analytics.com/ga.js';
		//load the Google Analytics javascript file:
		$.ajax(
			{
				type: 'GET', url: src,
				success: function() {
					//the ga.js file was loaded successfully, set the account id:
					pageTracker = _gat._getTracker(settings.accountId);
					//track the pageview:
					pageTracker._trackPageview();
					// The slashes are to ensure the period is in the url, the $ is to make sure it is the end of the url, the i is to make it case insensitive.
					filetypes = /\.exe$|\.zip$|\.msi$|\.air$|\.doc$|\.xml$|\.txt$/i;
					$("a").live("click", function(){
						// Track mailto links
						if ($(this).attr("href").match(/^mailto\:/i)) {
							var url = $(this).attr("href").replace(/^mailto\:/i, "")
							pageTracker._trackPageview("/mailto/" + url)
						}
						// Track external links
						else if (location.host != this.host.replace(/\:80$/i, "") && location.host != this.host.replace(/\:443$/i, "")) {
							var url = $(this).attr("href").replace(/^https?\:\/\/(www\.)*/i, "")
							pageTracker._trackPageview("/outgoing/" + url)
						}
						// Track downloads (links with a given extension)
						else if ($(this).attr("href").match(filetypes)) {
							var host = location.host.replace(/\./, "\\.")
							var pattern = new RegExp('^(https?\:\/\/)*(www\.)*('+host+')*\/','i')
							var url = $(this).attr("href").replace(pattern, "")
							pageTracker._trackPageview("/downloads/" + url)
						}
					});
				},
				error: function() {
					//the ga.js file wasn't loaded successfully:
					throw "Unable to load ga.js; _gat has not been defined.";
				},
				dataType: 'script', cache: true
			}
		);
	};
 
 
	/**
	* Track an event, e.g.:
	* 
	*   $('a.twitter').click(function() {
	*     $.gaTrackEvent('feed', 'click', 'Twitter', 'willemvzyl');
	*   });
	*/
	$.gaTrackPageHit = function(url) {
		if (typeof pageTracker != undefined) {
			//the pageTracker was defined, track the event:
			pageTracker._trackPageview(url);
		} else {
			//the pageTracker wasn't defined:
			throw "Unable to track event; pageTracker has not been defined";
		}
	};

	/**
	* Track an event, e.g.:
	* 
	*   $('a.twitter').click(function() {
	*     $.gaTrackEvent('feed', 'click', 'Twitter', 'willemvzyl');
	*   });
	*/
	$.gaTrackEvent = function(category, action, label, value) {
		if (typeof pageTracker != undefined) {
			//the pageTracker was defined, track the event:
			pageTracker._trackEvent(category, action, label, value);
		} else {
			//the pageTracker wasn't defined:
			throw "Unable to track event; pageTracker has not been defined";
		}
	};
})(jQuery);
