/* Adapted by Sean Carlos http://antezeta.com/news/google-analytics-tracking-scripts from http://ronaldheft.com/code/analyticator/ (donate: http://ronaldheft.com/code/donate/)

	You may freely use this code as long as you maintain the above credits.  Links to antezeta.com welecome.

	 v 1.0 beta 2010-08-30 changes from Ronald Heft's Analyticator 6.1.1 version
	
	1) Use onmouse up instead of onclick in order to track all mouse clicks (left, center & right buttons).  Side effect: keyboard enter won't be tracked.
	2) Allow separate configuration of page view vs. event tracking for outgoing links and downloads.  For downloads, allow for configuration based on file type.
	3) Allow for parameterization of Event category lables.

	Open issues: 

	a) should external downloads be tracked as downloads or external link clicks.  Currently tracked as downloads.
	b) would like to track onclick + oncontextmenu events in order to track left, right mouse buttons + enter key.  Currently tracking 3 mouse buttons.

*/
// Note: logic currently tracks external downloads as downloads rather than exit clicks.

// File extensions for non-document downloads
var analyticsFileTypes = ['exe','xpi','mp3','vcf','diff','zip','gz','tgz','rar','js','gif','png','jpeg','jpg','css','wma','mov','avi','wmv','mpeg','swf'];
// File extensions for document downloads
var analyticsDocFileTypes = ['docx','xlsx','pptx','pdf','odt','ods','odp','doc','xls','ppt','txt'];

/* --- end configuration --- */
// console.log('/js/ga-tracking.js - now loaded');

var JQ = jQuery.noConflict();
JQ(document).ready(function() {

	JQ('a').each(function() {
		var a = JQ(this);
		var href = a.attr('href');
		var classes = a.attr('class');
		classesArray = classes.split(' ');
		
		// Check if the a tag has a href, if not, stop for the current link
		if ( href == undefined )
			return;
		
		var url = href.replace('http://','').replace('https://','');
		var hrefArray = href.split('.').reverse();
		var extension = hrefArray[0].toLowerCase();
		var hrefArray = href.split('/').reverse();
		var domain = hrefArray[2];
		var downloadTracked = false;

	 	// WATERPIK SPECIFIC: If the link is a special case "Coupon" link
		if ( (JQ.inArray('ga-download-coupon', classesArray) != -1) && ( downloadTracked == false ) ) {
			// Mark the link as already tracked
			downloadTracked = true;

			//Get product info from custom attribute "data-product'
			var model = a.attr('data-product');
			var couponvalue = a.attr('data-couponvalue');

			// Add the tracking code
			a.mouseup(function() {
				_gaq.push(['_trackEvent', location.href, 'download-coupon', model ]);
			//	console.log(['_trackEvent', location.href, 'download-coupon', model, couponvalue]);
			});
		}
	 	// WATERPIK SPECIFIC: If the link is a special case "Buy Now" link
		if ( (JQ.inArray('ga-buy-now', classesArray) != -1) && ( downloadTracked == false ) ) {
			// Mark the link as already tracked
			downloadTracked = true;

			//Get product info from custom attribute "data-product'
			var model = a.attr('data-product');

			// Add the tracking code
			a.mouseup(function() {
				_gaq.push(['_trackEvent', location.href, 'click-buy-now', model]);
			//	console.log(['_trackEvent', location.href, 'click-buy-now', model]);
			});
		}
	 	// If the link is a non-document download (file extension list above in config)
		if ( (JQ.inArray(extension,analyticsFileTypes) != -1) && ( downloadTracked == false ) ) {
			// Mark the link as already tracked
			downloadTracked = true;
			
			// Add the tracking code
			a.mouseup(function() {
				_gaq.push(['_trackEvent', location.href, "download-non-document", href]);
			//	console.log(['_trackEvent', location.href,  "download-non-document", href]);
			});
		}
	 	// WATERPIK SPECIFIC: If the link is a special case "Buy Now" link
		if ( (JQ.inArray('ga-buy-now-drugstorecom', classesArray) != -1) && ( downloadTracked == false ) ) {
			// Mark the link as already tracked
			downloadTracked = true;

			//Get product info from custom attribute "data-product'
			var model = a.attr('data-product');

			// Add the tracking code
			a.mouseup(function() {
				_gaq.push(['_trackEvent', location.href, 'click-buy-now-drugstorecom', model]);
			//	console.log(['_trackEvent', location.href, 'click-buy-now', model]);
			});
		}
	 	// If the link is a non-document download (file extension list above in config)
		if ( (JQ.inArray(extension,analyticsFileTypes) != -1) && ( downloadTracked == false ) ) {
			// Mark the link as already tracked
			downloadTracked = true;
			
			// Add the tracking code
			a.mouseup(function() {
				_gaq.push(['_trackEvent', location.href, "download-non-document", href]);
			//	console.log(['_trackEvent', location.href,  "download-non-document", href]);
			});
		}

	 	// If the link is a document download (file extension list above in config). 
		// Include check to insure link not already tracked due to file extension specified in both 
		// document and non-document arrays by mistake
		if ( (JQ.inArray(extension,analyticsDocFileTypes) != -1)  && ( downloadTracked == false ) ) {
			// Mark the link as already tracked
			downloadTracked = true;
			
			// Add the tracking code
			a.mouseup(function() {
				_gaq.push(['_trackEvent', location.href, "download-document", href]);
			//	console.log(['_trackEvent', location.href,  "download-document", href]);
			});
		}
	
		// If the link is external
	 	if ( ( href.match(/^http/) ) && ( !href.match(document.domain) ) && ( downloadTracked == false ) ) {
	    	// Add the tracking code
			a.mouseup(function() {
				_gaq.push(['_trackEvent', location.href, "external-link", href]);
			//	console.log(['_trackEvent', location.href,  "external-link", href]);
			});
		}
	});

});



