/*
 * Easy Google OnClick Track jQuery Plugin (http://www.userfirstinteractive.com/)
 * @author Scott D. Brooks 
 * @created by UserFirst Interactive (creations@userfirstinteractive.com)
 * 
 * @version 0.1
 * 
 * @changelog
 * v 0.1 	->	Starting release [Nov. 26, 2008]
 * 
 * future development: track back button click
 */
(function($){
	$.fn.easygoogleonclick = function(options) {		
		var defaults = {
			analyticsaccount:	"",			// should never be ""
			onlyTrackClass:		"",			// if you only wish to track links with a certain class name, use this
			referenceBy:		"href",		// acceptable values:  href, html, title, name, id or combinations location|href, location|title, location|html (what the link says)
			trackprefix:		"/click/",			// prefix text added prior to the tracking link reference.  i.e.  "click-"
			trackpostfix:		"",			// postfix text added after to the tracking link reference.  i.e.  "-outbound"
			addGoogleTracking:	false		// if the user wants us to add the tracking code for them, then set true
		};
		var options = $.extend(defaults, options);
		
		if (options.analyticsaccount == "") {
			alert("Easy Google OnClick Track has been misconfigured.  Please add your analytics account number for variable 'analyticsaccount'.");
		}
		
		if (options.addGoogleTracking) {
			// add google code
			var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
			document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
			var pageTracker = _gat._getTracker(options.analyticsaccount);
			pageTracker._trackPageview();
		}
		
		return this.each(function() {
			var obj = $(this); 
			var newOnClickCode = "";
			var linkReference = "";
			
			if ((options.onlyTrackClass != "") && (obj.hasClass(options.onlyTrackClass) == false)) {
				return;  // does not have the correct class, so don't apply tracking
			}
			
			switch (options.referenceBy)
			{
				case "href":  
					linkReference = jQuery.url.attr("path");
					break;
				case "location|href":  
					pageLocation = jQuery.url.attr("path");
					linkReference = "LOCATION" + pageLocation + "HREF/" + obj.attr("href").replace('http://','');
					break;
				case "location|title":  
					pageLocation = jQuery.url.attr("path");
					linkReference = "LOCATION" + pageLocation + "TITLE/" + obj.attr("title");
					break;
				case "location|html":  
					pageLocation = jQuery.url.attr("path");
					linkReference = "LOCATION" + pageLocation + "HTML/" + obj.attr("html").replace('http://','');
					break;
				default:  
					linkReference = obj.attr(options.referenceBy);
					break;
			}
			
			// if an onClick already exists for this link, then concat the pageTracker
			if (obj.attr("onClick") !== undefined) {
				newOnClickCode = obj.attr("onClick") + " pageTracker._trackPageview('" + options.trackprefix + linkReference + options.trackpostfix + "');";  // append code, if onClick exists
			} else {
				newOnClickCode = "pageTracker._trackPageview('" + options.trackprefix + linkReference + options.trackpostfix + "');";
			}
			obj.attr("onClick", newOnClickCode );
		});
	};
})(jQuery);

