// A helper class used to generisize and wrap GA functionality
// so that the rest of the script is unaware of the GA specific details
AnalyticsHelper = {
	init: function(){
		// decide which analytics account to use by examining the url
		if(window.location.toString().match('dev.') || window.location.toString().match('qa.'))
			this.pageTracker = _gat._getTracker("UA-5068817-2");
		else
			this.pageTracker = _gat._getTracker("UA-5068817-1");
			
		// configure GA options
		this.pageTracker._setDetectTitle(true);
		this.pageTracker._initData();
		
		// track each page view
		this.pageTracker._trackPageview();
	},
	// track outgoing links in /outgoing/
	trackOutgoing: function(href){
		var index = href.indexOf('://');
		if(index != -1)
			href = href.substring(index + 3, href.length);
		this.pageTracker._trackPageview('/outgoing/' + href);
	},
	// tracks store locator clicks by store name in event
	trackStore: function(name){
		// look for required event tracker if not created create it
		if(!$defined(this.storeTracker))
			this.storeTracker = this.pageTracker._createEventTracker('Store Locator');
		this.storeTracker._trackEvent(name);
	},
	// track product views in event
	trackProduct: function(name){
		if(!$defined(this.productTracker))
			this.productTracker = this.pageTracker._createEventTracker('Product');
		this.productTracker._trackEvent(name);
	},
	// track category views in event
	trackCategory: function(name){
		if(!$defined(this.categoryTracker))
			this.categoryTracker = this.pageTracker._createEventTracker('Category');
		this.categoryTracker._trackEvent(name);
	},
	// track recipe print and views in event
	trackRecipe: function(name, action){
		if(!$defined(this.recipeTracker))
			this.recipeTracker = this.pageTracker._createEventTracker('Recipe');
		this.recipeTracker._trackEvent(action, name);
	},
	// track coupon
	trackCoupon: function(location){
		if(!$defined(this.couponTracker))
			this.couponTracker = this.pageTracker._createEventTracker('Coupon');
		this.couponTracker._trackEvent(location);
	}
};
AnalyticsHelper.init();

window.addEvent('domready', function(){
	// add click event to track all outgoing links globally
	$$('a.link_outgoing').addEvent('click', function(){
		AnalyticsHelper.trackOutgoing(this.get('href'));
	});
	
	// store locator event tracking
	if($('store_locator_brands')){
		$('store_locator_brands').addEvent('click', function(e){
			var target = $(e.target);
			if(target.get('tag') == 'img')
				AnalyticsHelper.trackStore(target.get('alt'));
		});
	}
	
	// recipe viewing
	if($('recipe')){
		var title = document.title.split('>')[1].trim();
		AnalyticsHelper.trackRecipe(title, 'View');
	}
	// recipe printing
	if($('print_recipe')){
		$('print_recipe').addEvent('click', function(){
			var title = document.title.split('>')[1].trim();
			AnalyticsHelper.trackRecipe(title, 'Print');
		});
	}
	
	// track category and product viewing
	if($('product_vanity')){
		var name = $('product_vanity').getElement('h3').get('text');
		AnalyticsHelper.trackProduct(name);
	}
	else{
		// if not on the product page it is safe to track the category
		// we don't want to accidently track categories 2 times
		if($('category_header')){
			var name = $('category_header').get('text');
			AnalyticsHelper.trackCategory(name);
		}
	}
	
	// coupon tracking
	if($('coupon_print')){
		$('coupon_print').addEvent('click', function(){
			var pageLocation = window.location.href;
			AnalyticsHelper.trackCoupon(pageLocation);
		});
	}
});
