/**
 * SUPERVALU Analytics Core
 * Version 1.2.0 - 04/28/2008
 * @author Benjamin Truyman
 **/

/**
 * SUPERVALU namespace
 **/
var SVU = SVU || {};

// Helper namespace
SVU.Helpers = {};

// Analytics helper
SVU.Helpers.Analytics = {
	initialized: false,
	options: {
		account: []
	},
	trackers: [],
	initialize: function (options) {
		// Set options
		if (options.account === undefined) {
			throw new Error('Account number must be defined for Analytics helper.');
		} else {
			if (typeof options.account === 'string') {
				this.options.account.push(options.account);
			} else if (typeof options.account === 'object') {
				for (var i=0; i < options.account.length; i++) {
					this.options.account.push(options.account[i]);
				};
			}
		}
		if (this.initialized === false) {
			if (options.autoTrackPageView === false && options.autoTrackPageView !== undefined) {
				this.options.autoTrackPageView = options.autoTrackPageView;
			} else {
				this.options.autoTrackPageView = true;
			}
			if (options.propSalt === undefined) {
				this.options.propSalt = 'svu';
			} else {
				this.options.propSalt = options.propSalt;
			}
			if (options.props !== undefined) {
				this.props = options.props;
			};
		
			// Download analytics JavaScript core
			this.downloadScript();
		
			// Execute routines when analytics JavaScript has been downloaded
			$(this).bind('scriptReady', SVU.Utils.Delegate.create(this, function (e) {
				this.buildTracker();
				if (this.options.autoTrackPageView) this.trackPageView();
			}));
		}
		this.initialized = true;
	},
	downloadScript: function () {
		var that = this;
		var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
		$.getScript(gaJsHost + 'google-analytics.com/ga.js', function () {
			$(that).trigger('scriptReady');
		});
	},
	buildTracker: function () {
		// Try to create tracker
		try {
			if (typeof this.options.account == 'string') {
				this.trackers.push(_gat._getTracker(this.options.account));
			} else {
				for (var i=0; i < this.options.account.length; i++) {
					this.trackers.push(_gat._getTracker(this.options.account[i]));
				};
			}
		} catch(err) {}
	},
	buildURL: function (href) {
		// Define placeholder variables
		var path, tags = '';
		if (href) {
			path = href;
		} else {
			path = window.location.pathname + window.location.search;
		}
		
		// Add page properties to path
		if (this.props) {
			// Build prop query string
			for (prop in this.props) {
				tags += '&' + this.options.propSalt + '_' + prop + '=' + this.props[prop];
			}
			// Determine if a query string already exists in the path
			var regexp = new RegExp(/^.*?\?/);
			if (!path.match(regexp)) {
				tags = '?' + tags.substr(1, tags.length);
			}
		}
		
		return String(path + tags);
	},
	trackEvent: function (category, action, label, value) {
		// Check for arguments
		if (category === undefined || action === undefined) {
			throw new Error('A category and action must be provided in order to track an event with the Analytics helper.');
		} else {
			for (var i=0; i < this.trackers.length; i++) {
				this.trackers[i]._trackEvent(category, action, label, value);
			};
		}
	},
	trackPageView: function (href) {
		if (href) {
			for (var i=0; i < this.trackers.length; i++) {
				this.trackers[i]._trackPageview(this.buildURL(href));
			};
		} else {
			for (var i=0; i < this.trackers.length; i++) {
				this.trackers[i]._trackPageview(this.buildURL());
			}
		}
	}
};

// Utility namespace
SVU.Utils = {};

// Event Delegate
SVU.Utils.Delegate = {
	create: function (obj, func) {
		return function (e) {
			e = e || {};
			// Forces currentTarget property of the Event object for browsers that don't correctly support it
			e.currentTarget = this;
			// Call function within context of specified object
			func.apply(obj, [e]);
		};
	}
};