/**
 * 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){
			document.write('<scr' + 'ipt type="text/javascript" src="' + (('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.') + 'google-analytics.com/ga.js' + '"></scr' + 'ipt>');
			this.record();
		}
	},

	/**
	 * record analytics
	 *
	 * @param string tag
	 * @return void
	 */
	record: function(){
		var tag = (arguments.length > 0) ? arguments[0] : null;
		if (tag != null) this.queued[this.queued.length] = tag;
		if (this.pageTracker != null){
			if (this.queued.length > 0) for (var i = 0; i < this.queued.length; i++) this.pageTracker._trackPageview(this.queued[i]);
		} else {
			if ((typeof _gat) != 'undefined'){
				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]);
			} else this.record.delay(1000, this);
		}
	}
});
var ga = new GoogleAnalytics({
	'profiles': [
		{'uri': /^localhost/, 'profile': 'UA-2777587-6'},
		{'uri': /^2ua7381ktf/, 'profile': 'UA-2777587-6'},
		{'uri': /^local\./, 'profile': 'UA-2777587-6'},
		{'uri': /^dev\./, 'profile': 'UA-2777587-6'},
		{'uri': /^stage\./, 'profile': 'UA-2777587-6'},
		{'uri': /.*/, 'profile': 'UA-2777587-4'}
	]
});
function record(tag){ ga.record(tag) }

