/*
	JavaScript Tracking Tool.  Version 2.0
	Written by Michael Langford for CARE, http://www.care.org/
	This script is (c) Copyright CARE USA.  Use by expressed permission only.

	Built on:
		jQuery 1.3.2: http://www.jquery.com/
		jQuery Query Plugin 2.1.3: http://plugins.jquery.com/project/query-object
		jQuery Class Plugin: http://github.com/taylanpince/jquery-class


		Cookie Helper Class: http://www.care.org/includes/js/utilities/cookies.js
*/

jQuery.namespace("care.trk");

jQuery(document).ready(function($){
	 var trk = new care.trk({
		"source_override_value":"",
		"subsource_override_value":"",
	 	"source_override_class":"trkosrc",
	 	"subsource_override_class":"trkossubsrc",
	 	"disabler":"disabletrk",
	 	"exclusion_class":"trkx",
	 	"cookie_expiration_length":0, //Length in DAYS.  Session-only cookie: 0
	 	"cookie_domain":"care.org", //Use TLD to facilitate cross-system tracking
	 	"source_cookie_name":"trksource",
	 	"subsource_cookie_name":"trksubsource",
	 	"source_parameter":"s_src",
	 	"subsource_parameter":"s_subsrc"
	 });
});


care.trk = jQuery.Class.extend({
	v:"2.0.1",

	process: function(){
		/* instantiate the cookie helper as object global */
		this.cookies = new care.utilities.cookies();

		/* Add the 'trk' class to all HREF elements on the page except
			 for the ones that already contain the exclusion class. */
		jQuery('a:not(".'+this.config.exclusion_class+'")').addClass("trk");

		this.source = this.getValue("source");
		this.subsource = this.getValue("subsource");
		jQuery.each(jQuery("a"),this.append.bind(this));
	},

	append: function(){
		jQuery.query.EMPTY();
		if(this.source){
			jQuery.query.SET(this.config.source_parameter, this.source);
		}
		if(this.subsource){
			jQuery.query.SET(this.config.subsource_parameter, this.subsource);
		}
		var linknode = jQuery(arguments[1]);

		if(jQuery.query.toString() != "" && linknode.attr("href") && (!linknode.attr("href").match("mailto:") && !linknode.attr("href").match("javascript:") && !linknode.attr("href").match("ftp:"))){
			if(linknode.hasClass(this.config.exclusion_class) == false){
				if(linknode.attr("href").indexOf("?") > -1){
					// This means there is an existing querystring -- move it to the end and replace it with an ampersand.
					var hrefarr = linknode.attr("href").split("?");
					var URI = hrefarr[0];
					var exquery = hrefarr[1];
					linknode.attr("href",URI + jQuery.query.toString() + "&" + exquery);

				}
				else{
					linknode.attr("href",linknode.attr("href") + jQuery.query);
				}
			}
		}

	},

	getValue: function(param){
		var cookieName = "";
		var parameterName = "";

		if(param == "source"){
			cookieName = this.config.source_cookie_name;
			parameterName = this.config.source_parameter;
		}else if(param == "subsource"){
			cookieName = this.config.subsource_cookie_name;
			parameterName = this.config.subsource_parameter;
		}

		if(jQuery.query.get(parameterName) != "" && jQuery.query.get(parameterName) != true){
			this.cookies.create(cookieName,jQuery.query.get(parameterName),this.config.cookie_expiration_length,this.config.cookie_domain)
			return jQuery.query.get(parameterName);
		}
		else{ // Parameter isn't in qstring, try pulling from Cookie.
			if(this.cookies.read(cookieName)){
				return this.cookies.read(cookieName);
			}
		}
		return null;
	},

	init: function(config){
		this.config = config; // Add config to object globals.
		/*	Check for global disable element.
				If found, remove it from the document then cancel processing. */
		if(jQuery(this.config.disabler).length > 0){
			jQuery('disabletrk').remove();
			return false;
		}else{
			/* load the Cookies helper class. */
			jQuery.getScript("/includes/js/utilities/cookies.js", this.process.bind(this));
		}
	}
});