/**
 * GoogleAnalytics
 * This is an analytics package for Google Analytics website tracking
 *
 * @author Toby Miller <tmiller@tobymiller.com>
 * @copyright Copyright (C) 2008, Toby Miller
 * @license MIT
 */

var GoogleAnalytics = new Class({
	Implements: Options,

	/**
	 * default options
	 */
	defaultOptions: {
		'profiles': []
	},

	/**
	 * initialize class
	 *
	 * @param object optional options
	 * @return void
	 */
	initialize: function(options){
		this.setOptions(this.defaultOptions, options);
		this.profileId = null;
		this.pageTracker = null;
		this.queued = [];
		for (var i = 0; i < this.options.profiles.length; i++){
			if (document.location.host.match(this.options.profiles[i].uri)){
				this.profileId = this.options.profiles[i].profile;
				i = this.options.profiles.length;
			}
		}
		if (this.profileId != null){
			new Asset.javascript((("https:" == document.location.protocol) ? "https://ssl." : "http://www.") + "google-analytics.com/ga.js", {
				onload: function() {
					this.pageTracker = _gat._getTracker(this.profileId);
					this.pageTracker._initData();
					this.pageTracker._trackPageview('');
					if (this.queued.length > 0) for (var i = 0; i < this.queued.length; i++) this.pageTracker._trackPageview(this.queued[i]);
				}.bind(this)
			});
		}
	},

	/**
	 * record analytics
	 *
	 * @param string tag
	 * @return void
	 */
	record: function(tag){
		if (this.pageTracker != null) this.pageTracker._trackPageview(tag);
		else this.queued[this.queued.length] = tag;
	}
});
var ga = new GoogleAnalytics({
	'profiles': [
		{'uri': /^sand/, 'profile': 'UA-6179742-2'},
		{'uri': /^dev\./, 'profile': 'UA-6179742-3'},
		{'uri': /^stage\./, 'profile': 'UA-6179742-4'},
		{'uri': /.*/, 'profile': 'UA-6179742-1'}
	]
});
function record(tag){ ga.record(tag) }

