if (typeof(Ficia) == 'undefined') Ficia = {};
if (typeof(Ficia.Google) == 'undefined') Ficia.Google = {};

Ficia.Google.Analytics = function(args){
	this.trackerId = args.trackerId;
	if (args.domainName) this.domainName = args.domainName;
};

Ficia.Google.Analytics.prototype = {
	trackerId: null,
	domainName: null,
	pageTracker: null,

	load: function(cb){
		var self = this;

		var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
		var script = document.createElement('SCRIPT');
		script.type = 'text/javascript';
		script.src  = gaJsHost + 'google-analytics.com/ga.js';
		document.getElementsByTagName('head')[0].appendChild(script);

		var waiting;
		waiting = function(){
			if (typeof _gat != 'undefined') {
				try {
					self.pageTracker = _gat._getTracker(self.trackerId);
					if (self.domainName) self.pageTracker._setDomainName(self.domainName);
					self.pageTracker._trackPageview();
				} catch(err) {
					this.pageTracker = null;
					return;
				}
				if (cb) cb(self.pageTracker);
			} else {
				setTimeout(waiting, 200);
			}
		};
		setTimeout(waiting, 200);
	},
	track: function(category, action, label, value){
		if (this.pageTracker === null) return;
		this.pageTracker._trackEvent(category, action, label, value);
	},
	track_pageview: function(path){
		if (this.pageTracker === null) return;
		this.pageTracker._trackPageview(path);
	},
	set_custom_var: function(index, name, value, opt_scope){
		if (this.pageTracker === null) return;
		this.pageTracker._setCustomVar(index, name, value, opt_scope);
	},
	_: ''
};
