/**
 * Google Analytics Plugin for jQuery
 * Usage: 
 * $.gaTracker.trackPageView(opt_pageURL):			Track page view immediately  
 * $.gaTracker.trackPageViewOnReady(opt_pageURL)	Track page view on document.ready
 * $.gaTracker.addTrans(orderId, affiliation, total, tax, shipping, city, state, country)
 * $.gaTracker.addItem(orderId, sku, name, category, price, quantity)
 * $.gaTracker.trackTrans() 
 * $.gaTracker.trackEvent(category, action, optional_label, optional_value)
 * $.gaTracker.trackPageLoadtime(opt_pageURL):			Track page view immediately  
 * @author mdobel
 * @see ismodule <isgaprepare> Mit diesem Modul wird die ShopID aus der entsprechenden
 * 									shopProperties.xml des derzeit aktiven Shops ermittelt und für das weitere
 * 									Tracking aufbereitet.
 * 									z.B. <setting name="GoogleAnalyticsId" value="UA-123456789-1" />
 * @see ismodule <isgapageview>
 * @see ismodule <isgatracktrans>
 * @see ismodule <isgatrackevent>
 * @see ismodule <isgatrackevent_js>
 */
var _gaq = _gaq || [];

(function($) {
	
	$.extend({
			
		gaTracker: {
			_tracker: null,
			_gaID: null,
			
			initGaID: function(gaID) {
				if (this._gaID == null) {
//alert("<-- initGaID --> gaID: " + gaID);
					try {
						this._gaID = gaID;
					} catch (e) {
						return false;
					}
				}
				return this._gaID != null;
			}, 
			
			init: function() {
				if (this._tracker == null) {
//alert("<-- init --> gaID: " + this._gaID);
					try {
						this._tracker = 1; // _gat._createTracker(this._gaID);
						_gaq.push(['_setAccount', this._gaID]);
						_gaq.push(['_gat._anonymizeIp']);
						// deprecated this._tracker._initData();
					} catch (e) {
						return false;
					}
				}
				return this._tracker != null;
			}, 
			
			trackPageView: function(opt_pageURL) {
				if (this.init()) {
					try {
						_gaq.push(['_trackPageview', opt_pageURL])
					}
					catch (e) {;}
				}
			},
			
			/**
			* trackPageLoadtime is a function to Track the Loadtime
			* * @author oJaeger
			* @addon
			*/
			trackPageLoadTime: function(opt_pageURL) {
				if (this.init()) {
					try { 
						_gaq.push(['_trackPageLoadTime', opt_pageURL]);
					}
					catch (e) {;}
				}
			},
			
			/**
			* trackEvent is an addon function to Track Events on the site
			* @author oJaeger
			* @addon
			*/
			trackEvent: function(category, action, optional_label, optional_value, optional_noninteraction) {
				if (this.init()) {
					try { 
						_gaq.push(['_trackEvent', category, action, optional_label, optional_value, optional_noninteraction]);
					}
					catch (e) {;}
				}
			},
			
			trackPageViewOnReady: function(opt_pageURL) {
				$(document).ready(function() {
					$.gaTracker.trackPageView(opt_pageURL);
					//alert("<-- trackPageViewOnReady --> gaID: " + this._gaID + " opt_pageURL: " + opt_pageURL);
				});
			},
			
			addTrans: function(orderId, affiliation, total, tax, shipping, city, state, country) {
				if (this.init()) {
					try { 
						_gaq.push(['_addTrans', orderId, affiliation, total, tax, shipping, city, state, country]);
					}
					catch (e) {;}
				}
			},
			
			addItem: function(orderId, sku, name, category, price, quantity) {
				if (this.init()) {
					try { 
						_gaq.push(['_addItem', orderId, sku, name, category, price, quantity]);
					}
					catch (e) {;}
				}
			},
			
			trackTrans: function() {
				if (this.init()) {
					try {
						_gaq.push(['_trackTrans']);
					}
					catch (e) {;}
				}
			}
			
		}
	
	});
	
	var scriptName = 'ga.js';
	if ('dev.' == window.location.hostname.substr(0,4)) {
		scriptName = 'u/ga_debug.js';
	}
	/* do not use $.getScript(), because it disables caching (by adding a random parameter to the url) */
	$.ajax({
		url: (('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.') + 'google-analytics.com/' + scriptName,
		dataType: 'script',
		cache: true
	});
		
})(jQuery);

