//Asynchronous Google Page Tracking Script
if (typeof (GSNTrackingItems) == 'undefined' || GSNTrackingItems == null) {
    var GSNTrackingItems = new Object();
}

GSNTrackingItems.PageTracking = {
	Items : [
		{ TrackingKey: '', Prefix: '', TrackingHost: document.location.hostname},
		{ TrackingKey: '', Prefix: 'gsnChainTracking', TrackingHost: 'gsn.' + document.location.hostname},
		{ TrackingKey: '', Prefix: 'gsnGlobalTracking', TrackingHost: 'gsnrecipes.com'}
	]
}

var _gaq = _gaq || [];

function submitPageTracking(){
	GSNTrackingItems.PageTracking.Items.each(function(item){
		if(item.Prefix == ''){
			_gaq.push([item.Prefix + '_setAccount', item.TrackingKey]);
			_gaq.push([item.Prefix + '_setDomainName', item.TrackingHost]);
			_gaq.push([item.Prefix + '_trackPageview']);		
		}
		else{
			var multipleTracking = "_gaq.push(function(){_gat._createTracker('"+ item.TrackingKey +"', '"+ item.Prefix+ "');});";	
			eval(multipleTracking);
			_gaq.push([item.Prefix + '._setDomainName', item.TrackingHost]);
			_gaq.push([item.Prefix + '._trackPageview']);		
		}
	});
	
	// google implements their script
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
}	

/* Original script before adding _gat._createTracker code

	GSNTrackingItems.PageTracking.Items.each(function(item){
		if(item.Prefix == ''){
			_gaq.push([item.Prefix + '_setAccount', item.TrackingKey]);
		}
		else{
			_gaq.push(function() {
			    _gat._createTracker(item.TrackingKey, item.Prefix);
			});
		}
		_gaq.push([item.Prefix + '_setDomainName', item.TrackingHost]);
		_gaq.push([item.Prefix + '_trackPageview']);		
	});
	
	// google implements their script
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
*/

function submitTransactionTracking(){
	if (GSNOrderItems.Order.TotalItems > 0){
		GSNTrackingItems.PageTracking.Items.each(function(item){
			if(item.Prefix == ''){
				// build transaction info
				_gaq.push([item.Prefix + '_addTrans',
					GSNOrderItems.Order.OrderID,  // order ID - required
					GSNOrderItems.Order.StoreName,  // affiliation or store name
					GSNOrderItems.Order.Total,  // total - required
					GSNOrderItems.Order.Tax,  // tax
					GSNOrderItems.Order.DeliveryFee,  // shipping
					GSNOrderItems.Order.City,  // city
					GSNOrderItems.Order.State,  // state or province
					GSNOrderItems.Order.Country  // country
				]);	
		
				// loop through each item in the cart
				for (var j=0; j < GSNOrderItems.Order.TotalItems; j++){
					_gaq.push([item.Prefix + '_addItem',
						GSNOrderItems.Order.OrderID,  // order ID - required
						GSNOrderItems.Order.SubSelectOptions[j].UPC,  // SKU/code - required
						GSNOrderItems.Order.SubSelectOptions[j].ProductName,  // product name
						GSNOrderItems.Order.SubSelectOptions[j].Category,  // category or variation
						GSNOrderItems.Order.SubSelectOptions[j].UnitPrice,  // unit price - required
						GSNOrderItems.Order.SubSelectOptions[j].Quantity  // quantity - required
					]);
				}
		
				// submit transaction to the Analytics servers
				_gaq.push([item.Prefix + '_trackTrans']);
			}
			else{
				// build transaction info
				_gaq.push([item.Prefix + '._addTrans',
					GSNOrderItems.Order.OrderID,  // order ID - required
					GSNOrderItems.Order.StoreName,  // affiliation or store name
					GSNOrderItems.Order.Total,  // total - required
					GSNOrderItems.Order.Tax,  // tax
					GSNOrderItems.Order.DeliveryFee,  // shipping
					GSNOrderItems.Order.City,  // city
					GSNOrderItems.Order.State,  // state or province
					GSNOrderItems.Order.Country  // country
				]);	
		
				// loop through each item in the cart
				for (var j=0; j < GSNOrderItems.Order.TotalItems; j++){
					_gaq.push([item.Prefix + '._addItem',
						GSNOrderItems.Order.OrderID,  // order ID - required
						GSNOrderItems.Order.SubSelectOptions[j].UPC,  // SKU/code - required
						GSNOrderItems.Order.SubSelectOptions[j].ProductName,  // product name
						GSNOrderItems.Order.SubSelectOptions[j].Category,  // category or variation
						GSNOrderItems.Order.SubSelectOptions[j].UnitPrice,  // unit price - required
						GSNOrderItems.Order.SubSelectOptions[j].Quantity  // quantity - required
					]);
				}
		
				// submit transaction to the Analytics servers
				_gaq.push([item.Prefix + '._trackTrans']);
			}
		});
	}
}

