//
// 22/12/2011 12:00
//
$(document).ready(function() {
	$('a').mouseup(function(e) {
		var href = $(this).attr('href');
		var orderValue = $(this).attr('charset'); // Change this to whichever link attribute you want to use as the value
		
		var title 		= $(this).html();
		var fileName	= href.substring(href.lastIndexOf('/') + 1);
		
		// Tracking these download links like
		// href = http://econtent.tauranga.govt.nz/data/sustainable_living/files/forestdiscovery.pdf
		if (href.indexOf('econtent.tauranga.govt.nz') > -1) 
		{
			var date = new Date;
			var orderId = parseInt(date.getTime() / 1000);

			// Default value if defined value is bad or not present
			if(orderValue.length < 1 || isNaN(parseFloat(orderValue)))
				orderValue = 1.0;
				
			// For debuging
			//console.log('Title: ' + title + ', File: ' + fileName + ', Value: ' + orderValue);
	  
			// Begin tracking this transaction			
			_gaq.push(['_addTrans', orderId, 'Tauranga City Council', orderValue, '0.00', '0.00', 'Tauranga', 'Bay of Plenty', 'NZ']);
			 
			// Add transactions items / products to the tracker
			_gaq.push(['_addItem', orderId, fileName, fileName, 'Downloads', orderValue, '1']);
		}
	});
	
	// GA doesn't reset after every transaction, so don't submit them until we leave the page
	window.onbeforeunload = function() {
		_gaq.push(['_trackTrans']);
	}
	
	$(window).unload(function() {
		_gaq.push(['_trackTrans']);
	});
});


