/**
 * Google Analytics Plugin for jQuery
 * Usage: 
 * $.gaTracker.trackPageView():			Track page view immediately  
 * $.gaTracker.trackPageViewOnReady()	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)
 * @author mdobel
 * @see ismodule <isgapageview>
 * @todo ismodule <isgatracktrans attribute="basket" attribute="affiliateid" ...?>
 * @todo externalize tracker id
 */
(function($) {
	
	$.extend({
			
		gaTracker: {
			_tracker: null, 
			_trackerID: "UA-11100218-1",
			
			init: function() {
				if (this._tracker == null) {
					try {
						this._tracker = _gat._getTracker(this._trackerID);
						this._tracker._initData();
					} catch (e) {
						return false;
					}
				}
				return this._tracker != null;
			}, 
			
			trackPageView: function() {
				if (this.init()) {
					try { 
						this._tracker._trackPageview();
					}
					catch (e) {;} 				
				}
			},
			
			/**
			* trackEvent is an addon function to Track Events on the site
			* @author oJaeger
			* @addon
			*/
			trackEvent: function(category, action, optional_label, optional_value) {
				if (this.init()) {
					try { 
						this._tracker._trackEvent(category, action, optional_label, optional_value);
						//  alert(" category: "+category+"\n action: "+action+"\n optional_label: "+optional_label+"\n optional_value: "+optional_value);
					}
					catch (e) {;} 				
				}
			},
			
			trackPageViewOnReady: function() {
				$(document).ready(function() {
					$.gaTracker.trackPageView();
				});
			},
			
			addTrans: function(orderId, affiliation, total, tax, shipping, city, state, country) {
				if (this.init()) {
					try { 
						this._tracker._addTrans(orderId, affiliation, total, tax, shipping, city, state, country);
					}
					catch (e) {;} 				
				}
			},
			
			addItem: function(orderId, sku, name, category, price, quantity) {
				if (this.init()) {
					try { 
						this._tracker._addItem(orderId, sku, name, category, price, quantity);
					}
					catch (e) {;} 				
				}
			},
			
			trackTrans: function() {
				if (this.init()) {
					try { 
						this._tracker._trackTrans();
					}
					catch (e) {;} 				
				}
			}
			
		}
	
	});
	
	$.getScript((('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.') + 'google-analytics.com/ga.js', 
		function() {;}
	);
		
})(jQuery);
