///////
// linkTracking.js
//
// Changelog:
//  v3.0 - Nov. 2011 - Updating it to use new front-end tracking techniques and moving more frontend elements witin (jgp)
//  v2.3 - Apr. 2011 - Updated to use asyncronous _gaq tracker, fixed download link resolution for IE6.
//  v2.2 - Feb. 2009 - Adapted by TeamDDM for our CMS (change fileTypes regex; check domain of outbound links; parse url vars, set label for downloads; create exceptions for links that contain js functions)
//  v2.1 - Jan. 22nd 2009 - Set bUseEventForOutbout and bUserEventForDownload to toggle use of Events or Page Views
// 	v2.0 - Jan. 2009 - Use Google Analytics Events to track downloads and external links
//  v1.0 - Inspired by the work of Justin Cutroni - Google Analytics Short Cut - http://gashortcut.com/
///////

jQuery(document).ready(function() {
	
	// Creating custom :external selector
	$.expr[':'].external = function(obj){
	    return !obj.href.match(/^mailto\:/)
	    		&& !obj.href.match('reveal_email')
	    		&& !obj.href.match('showHide')
	    		&& !obj.href.match('/^cdn./')
	    		&& !obj.href.match('/cloudfront\.net/')
	            && (obj.hostname != location.hostname);
	};

	// Add 'external' CSS class to all external links
	$('a:external').addClass('external');
	
	/* Track External Links (external), Confirm Popups (ok) and Zoomable Images (zoomable) */
	$('a.external, a.zoomable, a.ok').live('mousedown', function() {
		if(typeof(_gaq) != 'object') return;
		
		var category = 'outbound';
		var action = 'click';
		var label = $(this).attr('href');
		
		if($(this).hasClass('external') || $(this).hasClass('ok')) {
			category = 'outbound';
			action = 'click';
			/* Search to see if it's a confirm pop up external link */
			regex = new RegExp(/(show_extlink_popup|confirm_extlink_popup)\('([^']*)'/i);
			match = regex.exec(label);
			if(match) {
				label = decodeURIComponent(match[2]) + '::' + match[1];
			}			
		} else if($(this).hasClass('zoomable')) {
			category = 'zoom';
			action = 'view';
			label = $(this).attr('title');
		}

		_gaq.push(['_trackEvent', category, action, label]); 
	});
	
	/* Track old download.php scripts */
	$.expr[':'].download = function(obj) {
		return obj.href.match(/(download\.php|doAction\(.*download)/i);
	};
	
	// Add 'download' CSS class to all download.php links
	$('a:download').addClass('download-track');
	
	$('a.download-track').live('mousedown', function() {
		if(typeof(_gaq) != 'object') return;
		var label = $(this).attr('name');
		_gaq.push(['_trackEvent', 'download', 'click', label]);
	});
	
	$.fn.trackMediaEvent = function(event, object) {
		if(typeof(_gaq) != 'object') return;
		var category = object.tagName.toLowerCase();
		var label = object.title;
		if(event.type == 'play' && event.target.currentTime < 1 ) {
			// Initial play
			_gaq.push(['_trackEvent', category, 'play', label]);
		} else if (event.type == 'ended') {
			// Finished viewing
			_gaq.push(['_trackEvent', category, 'ended', label]);
		} // else, don't track
	}
});

/* Track Tipdowns and Accordians */
function pod_accordion_click(headerText,podID){
	if(typeof(_gaq) != "undefined"){
		var labelText = headerText + " - [" + podID + "]";
		_gaq.push(['_trackEvent', 'Pod-Accordion', 'view', labelText]);
	}
}

function pod_tipdown_click(headerText, podID) {
	if(typeof(_gaq) != "undefined"){
		var labelText = headerText + " - [" + podID + "]";
		_gaq.push(['_trackEvent', 'Pod-Tipdown', 'view', labelText]);
	}
}

/// EOF ///
