/*
 * jQuery.urchin - A Google-analytics helper jQuery plugin
 * - ported from dojox.analytics.Urchin http://www.dojotoolkit.org/
 * Author: Jens Arps http://jensarps.de/
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 */

;(function($) {

$.urchin = { 
	// summary: A Google-analytics helper, for post-onLoad inclusion of the tracker, and 
	//		dynamic tracking during long-lived page cycles. 
	//
	//
	//	example:
	//	|	// plain tracking:
	//	|	$.urchin.load({ acct:"UA-123456-7" });
	//  |   // more options:
	//  |   $.urchin.load({
	//  |       acct: "UA-123456-7",
	//  |       timeout: "10000",
	//  |       waitForDOM: false
	//  |   });
	//
	//  You can later call $.urchin.trackPageView() to track dynamic URLs
	//
	//	example:
	//  |  $.urchin.trackPageView('/some-xhr-url');
	//
	//
	
	defaultArgs : {
		
		// acct: String
		//		your GA urchin tracker account number.
		acct: '',
	
		// loadInterval: Integer
		//		Time (in ms) to wait before checking for a ready Analytics API
		loadInterval: 42,
		
		// decay: Float
		// 		Multipler for the interval loadInterval to ensure timer does not
		//		run amok in the event our _gat object is never defined. This 
		//		is multiplied against the last `loadInterval` and added, causing
		//		the interval to continuosly become longer, until a `timeout` 
		//		limit is reached.
		decay: 0.5,
		
		// timeout: Integer
		//		Time (in ms) for the load interval to reach before giving up
		//		all together. Note: this isn't an overall time, this is the
		//		time of the interval being adjusted by the `decay` property.
		timeout: 4200,
		
		// waitForDOM: Boolean
		//		Defines wether loading will be wrapped in a $(document).ready()
		//      or called directly.
		waitForDOM: true
	
	},
	
	args: {},

	load: function(args){
		// summary: initialize tracker. Immediately starts the load
		//		sequence.
		this.tracker = null;
		this.args = $.extend(this.defaultArgs, args);
		
		if(this.args.waitForDOM) {
			var inst = this;
			$(document).ready(function() {
				inst._loadGA();
			});
		} else {
			this._loadGA();
		}
	},
	
	_loadGA: function(){
		// summary: load the ga.js file and begin initialization process
		var gaHost = ("https:" == document.location.protocol) ? "https://ssl." : "http://www.";
		
		$.ajax({
		   type: "GET",
		   url: gaHost + "google-analytics.com/ga.js",
		   success: function(){},
		   dataType: "script",
		   cache: true
		
		});
		
		var inst = this;
		setTimeout(function(){ inst._checkGA(); }, this.args.loadInterval);
	},

	_checkGA: function(){
		// summary: sniff the global _gat variable Google defines and either check again
		//		or fire onLoad if ready.
		console.log(this);
		
		if(this.args.loadInterval > this.args.timeout){ return; }
		
		var inst = this;
		setTimeout(function(){
			!window["_gat"] ? inst._checkGA() : inst._gotGA();
		}, this.args.loadInterval);
		this.args.loadInterval *= (this.args.decay + 1);
	},

	_gotGA: function(){
		// summary: initialize the tracker
		this.tracker = _gat._getTracker(this.args.acct);
		this.tracker._initData();
		this.GAonLoad.apply(this, arguments);
	},
	
	GAonLoad: function(){
		// summary: Stub function to fire when urchin is complete
		//	description:
		//		This function is executed when the tracker variable is 
		//		complete and initialized. The initial trackPageView (with
		//		no arguments) is called here as well, so remember to call 
		//		manually if overloading this method.
		
		this.trackPageView();
	},
	
	trackPageView: function(/* string */url){
		// summary: A public API attached to this widget instance, allowing you 
		//		Ajax-like notification of updates. 
		//
		//	url: String
		//		A location to tell the tracker to track, eg: "/my-ajaxy-endpoint"
		//
		
		this.tracker._trackPageview.apply(this, arguments);
	}
	
}

})(jQuery);
