/****************************************************************************
  analytics block:  
  this block is used on all sites that have their own profile and *do not*
  roll up to a main profile.   
  
  the event tracking helper function
		handleEventTracker(link, category, action, label, value)
  is used in place of the normal  
		_gaq.push(['_trackEvent', category, action, label, value]);
  for example:
		<a href="..." onclick="handleEventTracker(this, 'ExternalLinks', 'WisSiblingSites', 'Experts', 1); return false">Video</a>
  note the use of *return false;*
****************************************************************************/

//note that by setting _SetDomainName to 'none',
//_setAllowHash = false is not required.  
var _gaq = _gaq || [];
_gaq.push(['_setAccount', main_property_id]);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);

//customVariables
if(customVariable) {
	for(i = 0; i != customVariable.length; ++i) {
		_gaq.push(
			[	'_setCustomVar', 
				customVariable[i][0],
				customVariable[i][1],
				customVariable[i][2],
				customVariable[i][3]
			]
		)	
	}
}

//url_override
override_defined = typeof(url_override) != "undefined";
override_null = !override_defined ? null : url_override == null;

//track page view, allowing for overridden url
if(override_defined && !override_null) {
	_gaq.push(['_trackPageview', url_override]);
} else {
	_gaq.push(['_trackPageview']);
}

(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);
})();

//a remedial function to ensure that clicks processing events don't get
//processed before the gatc is loaded  
function handleEventTracker(link, category, action, label, value) {
	try {		
		_gaq.push(['b._trackEvent', category, action, label, value]);
		hasTarget = link.target != "undefined";
		if(hasTarget & link.target == "_blank") {
			return;
		}
		setTimeout('document.location = "' + link.href + '"', event_track_lag_milliseconds)
	} catch(err){}
}

/****************************************************************************
eCommerce block.  If eCommerce is used then set eCommPage = true;
This is used on the *Thank You* or *confirmation* page of an actual 
eCommerce order, **not** on the order page itself.  
if eCommPage = true, then you're obligated to fetch the details of the 
transaction from the server and populate the two parameter arrays.
The function eCommInit() runs on page load.  
****************************************************************************/


function trackECommerce() {
	if(eCommercePage) {
		//push and track.  
		transactionOK = addTransaction(transaction);
		if(transactionOK) {
			itemsOK = addItems(items);
			if(itemsOK) {
				_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
			}
		}
	}
}

/******************************************************************************
*  	transaction parameters
*
*	parameter name				optional/required				array position
*	orderID 					:required  						transaction[0]
*	storeName 					:optional 			 			transaction[1]
*	total						:required						transaction[2]
*	tax							:optional  						transaction[3]
*	shipping					:optional 						transaction[4]
*	city						:optional  						transaction[5]
*	state						:optional 						transaction[6]
*	country						:optional 						transaction[7]
*
*	NB:  	*All* parameters are *strings*.  
*	NB:	 	Parameters are named here for reference.  They're passed 
*			as array members.
*	NB:  	Since parameters are matched by order, 
			null parameters *must* be passed as empty strings.
	E.g.:
			transaction = [
				'orderID value',
				'storeName',
				'total',
				'tax',
				'shipping',
				'city',
				'state',
				'country'
			]			
********************************************************************************/

/******************************************************************************
*  	addItem parameters
*
*	name						optional/required				array position
*	orderID 					:required  						item[0]
*	sku 						:required 			 			item[1]
*	productName					:optional, but always supply 	item[2]
*	category					:optional 				 		item[3]
*	unitPrice					:optional						item[4]
*	quantity					:optional  						item[5]
*
*	NB:  	*All* parameters are *strings*.  
*	NB:	 	Parameters are named here for reference.  They're passed 
*			as array members.
*	NB:  	Since parameters are matched by order, 
*			null parameters *must* be passed as empty strings.
*	NB:		Even if only one item is passed, 
*			this array should be two-dimensional.  e.g.:
*			items = [
*				[
*					'orderId value', 
*					'sku value', 
*					'productName value', 
*					'category value',
*					'unitPrice value',
*					'quantity value'
*				]
*			];
********************************************************************************/

function addTransaction(transaction) {
	//e-commerce-specific code.  
	//Note that this begins after the call to _trackPageview
	hasTransaction = true;
	try {
		_gaq.push(['_addTrans',
			transaction[0],         // order ID - required
			transaction[1],  		// affiliation or store name
			transaction[2],         // total - required
			transaction[3],         // tax
			transaction[4],         // shipping
			transaction[5],       	// city
			transaction[6],     	// state or province
			transaction[7]          // country
		]);
	} catch(err) {
		hasTransaction = false
	}
	return hasTransaction;
}
function addItems(itemArray) {
	hasItems = true;
	try {		
		for(i = 0; i != itemArray.length; ++i) {
			_gaq.push(
				[
					'_addItem', 
					itemArray[i][0], 
					itemArray[i][1], 
					itemArray[i][2], 
					itemArray[i][3], 
					itemArray[i][4], 
					itemArray[i][5]
				]
			);
		}
	} catch(err)  {
		hasItems = false;
	}
	return hasItems;
}

//run eComm after page load.
if (window.addEventListener) // W3C standard
{
  window.addEventListener('load', trackECommerce, false); // NB **not** 'onload'
} 
else if (window.attachEvent) // Microsoft
{
  window.attachEvent('onload', trackECommerce);
}
/****************************************************************************
  end eCommerce block
****************************************************************************/
