/*
	  Google Analytics wrapper
	----------------------------
	  -mway, 26 mar 2009


	  Features:
	----------------------------
	  - Automatic Google Analytics loading and tracking
	  - Automatically writes GA scripts into the DOM
	  - Can be used in the <head>
	  - Can be manually overridden to use similar GA footer placement
	  - Custom link tracking
	  - Alerts/notifications for misconfiguration


	  Requirements:
	----------------------------
	  - jQuery
	  - GA tracker ID
	  
	  
	  Usage
	----------------------------
	  In the document <head>:
	  <script type="text/javascript" src="/javascripts/tracker.js"></script>
	  <script type="text/javascript"> Tracker.init({ tracker_id: "UA-XXXXXXX-X" }); </script>
	  
	  
	  Optional Params
	----------------------------
	  tracker_id (string): google analytics	tracker ID
	  track_links (boolean, default = false): allow special link tracking
	  track_all_links (boolean, default = false): track all links via their href attribute (generally unnecessary)
	  link_selector (jQuery DOM path string): the selector for links to track if track_links is true but track_all_links is false
	  		eg, ".someClass" will track all links that have the "someClass" class attribute.
	  inline (boolean, default = false): initialize the Tracker object but wait for manual instantiation in the footer (see below)
	  
	  
	  Secondary Usage
	----------------------------
	  The tracker may also be used similarly to GA in that it can be called manually.  The only advantage of this
	  method would be to wait for an event to fire purposefully before loading analytics (eg, wait until a javascript
	  function that redirects mobile browsers to fire before tracking a hit to the page so the statistics aren't
	  inflated).  The tracker can be initialized anywhere - in the head (as above) or along with the secondary call to
	  instantiate it, but the inline parameter must be set to true.  After the init call:
	  
	  Tracker.instantiate();
	  
	  wherever you want the code to be placed.  It will physically write the javascript into document, so this circumvents
	  the $(document).ready() event when inline is false.  There is no difference otherwise; it can still be passed
	  selectors to track individual links.
*/

var Tracker = new function TrackerObject() {
	this.track_links;
	this.track_all_links;
	this.link_selector;
	this.is_active;
	this.id;
	this.object;
	this.inline;
	this.complete = false;
	
	this.init = function(options) {	
		if(typeof options == "object") {		
			Tracker.id = options.tracker_id;
			Tracker.track_links = (options.track_links != undefined ? options.track_links : false);
			Tracker.all_links = (options.track_all_links != undefined ? options.track_all_links : false);
			Tracker.link_selector = options.link_selector;
			Tracker.inline = (options.inline == undefined ? false : (options.inline == false ? false : true ));
			
			if(Tracker.inline == false) {
				$(document).ready(function() {
					Tracker.instantiate();
				});
			}
		} else {
			alert("You must specify init options.");
		}
	};
	
	this.instantiate = function(options) {
		if(Tracker.inline) {
			document.write(unescape("%3Cscript src='http://www.google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
		} else {
			$("body").append("<script src='http://www.google-analytics.com/ga.js' type='text/javascript'></script>");
		}
		Tracker.start();
	};
	
	this.start = function() {
		Tracker.validate();
	};
	
	this.validate = function() {
		if(typeof Tracker.object != "function") {
			if(typeof _gat != "object") {
				setTimeout("Tracker.validate()", 500);
				return false;
			} else {
				if(Tracker.id != undefined) {
					Tracker.object = _gat._getTracker(Tracker.id);
				} else {
					alert("No valid tracker ID.");
					return false;
				}
			}
		}
		
		if(Tracker.complete == false) {
			Tracker.pageSetup();
		}
		return true;
	};
	
	this.pageSetup = function() {
		if(Tracker.complete == false) {
			Tracker.object._trackPageview();
			if(Tracker.track_links) {
				Tracker.bindLinkEvents();
			}
			Tracker.complete = true;
		}
	};

	this.bindLinkEvents = function() {
		var selector;
		if(Tracker.track_links) {
			if(typeof jQuery == "function") {
				selector = (Tracker.track_all_links ? "a" : Tracker.link_selector);
				if(selector) {
					$(selector).click(function() {
						Tracker.track(this.href);
					});
				} else {
					alert("You must either track all links or specify a link selector.");
				}
			}
		}
	};
	
	this.track = function(uri) {
		try {
			if(Tracker.validate()) {
				Tracker.object._trackPageview(uri);
			}
		} catch(err) { return false; }
		return false;
	};
}