/**
* jQuery Alchemy United EZ Event Tracking (for Google Analytics)
* Version: 0.5.3
* URL: http://AlchemyUnited.com
* Description: Implement and manage Google Analytics Event Tracking settings with this EZ to use jQuery plugin. Event Tracking parm values can be set via user defined default settings and custom settings objects. Parm values can also be set inline via CSS. see README.txt for futher details
* Requires: jQuery 1.7.x+
* Author: Mark "Chief Alchemist" Simchock ( http://ChiefAlchemist.com)
* Copyright: Copyright 2012 Alchemy United
* License: 
*
* Copyright 2011 Alchemy United (ez.et@alchemyunited.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as 
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

/**
0.5.3 - Sat 28 Jan 2012
- Fixed bug with etrk_non. String of true / false are now converted to boolean 1 / 0.
- Fixed bug with etrk_val. String is now converted to integer during validation process.

0.5.2 - Tue 3 Jan 2012
- Tweaking and tightening per feedback from Andy Killen (CTO, SpeakyBoy.com). Thanks Andy.

0.5.1 - 22 Dec 2011 
- Moved switch/case for validation of user setting into a function. 
- Anything coming from the user options is now valiadated (using said function). 

0.5.0 - 15 Dec 2011
- The game begins.
*/

// Plugin closure wrapper
(function($) {
	// Main plugin function
	$.fn.ezeventtracking = function(options) {

	// start with a clean object and send options.etrk_def off to be validated
	var objValidatedUserDefaults = {};
	if ( options.etrk_def ){
		objValidatedUserDefaults = etrkValidateUserSettings ( options.etrk_def, 'userdefineddefaults' );
	}
   
    // Extend the two objects that define the defaults. That is merge them into etrk_def. 
	// Extend works from left to right. For example, in this case it will give precedent to objValidatedUserDefaults over the plugins defaults.etrk_def
    var etrk_def = {};
	etrk_def = $.extend({}, $.fn.ezeventtracking.defaults.etrk_def, objValidatedUserDefaults);

	// Now we can begin...
	
	// Disable right click.
	// Not recommended but none the less possible. Thanks to: http://www.catswhocode.com/blog/8-awesome-jquery-tips-and-tricks
	if ( etrk_def['etrk_rig']  == 'disable' ) {
		$(document).on("contextmenu",function(e){
			return false;
		});
	}
	
    // Iterate through DOM elements and work the EZ ET magic
    return this.each(function() {
		// Assign the current object to a variable for easier use
		var $this = $(this);

        $this.click(function() {
		// get all the classes for the clicked element 
		var strAllClass =  $(this).attr('class'),
		// and split them into an arry using the space as a delimiter
		arrAllClass = strAllClass.split(' '),
		// Declare the object objInlineParms. Values passed via the CSS class of the clicked element will eventually populate this object
		objInlineParms = {};
		// Declare the inlineDef variable and equal it to 'etrk_def'. If this value isn't overwritten then use the options etrk_def object for defaults 
		var inlineDef = 'etrk_def';	
		// .each loop thru the array of the classes
		$.each( arrAllClass, function(key,val){ 
			// if the class begins with etrk_ then it's a parm and we'll process it, else ignore it
			if ( 'etrk_'  ) {
				// split the parm key and the value
				// "hyphen hypen" is used as a delimiter in order to be user friendly and allow single hypen within a parm value
				var arrParms = val.split('--'); 
				// is the key specifying a user defined custom parm object
				objInlineParms[arrParms[0]] = arrParms[1];
			    if ( arrParms[0] == 'etrk_def' ) {
					if ( arrParms[1] )  {
						// check the (user) options, does the user defined custom parm object actually exist
						if ( options[arrParms[1]] ) {
							inlineDef = arrParms[1];
						}
					}
				} 
			}		
		});
		
		// start with a clean object and send objInlineParms off to be validated
		var objValidatedInlineSettings = {};
		if ( objInlineParms ){
			objValidatedInlineSettings  = etrkValidateUserSettings ( objInlineParms, 'other' );
		}
	
		// start with a clean object and send options[inlineDef] off to be validated
		var objValidatedCustomObject = {};
		if ( options[inlineDef] ){
			objValidatedCustomObject = etrkValidateUserSettings ( options[inlineDef], 'other' );
		}
		// ok, lets bring it all together in etrkDef
		var etrkDef = $.extend({}, etrk_def, objValidatedCustomObject, objValidatedInlineSettings );

		// if the label value is useCSSid then grab the ID of the element and update the value of etrk_lab
		if ( etrkDef['etrk_lab']  == 'useCSSid' ) {
			var strAttrID =  $(this).attr('id');
			if ( strAttrID ) {
				etrkDef['etrk_lab'] = strAttrID;
			}
		}
		// If the target is blank then make that magic happen
		// Thanks to: http://www.catswhocode.com/blog/8-awesome-jquery-tips-and-tricks
		if ( etrkDef['etrk_tar']  == 'blank' ) {
			this.target = "_blank";
		} 
			
		//alert ( 'SETTINGS: _rig = ' + etrkDef['etrk_rig'] + '   _tar = '+ etrkDef['etrk_tar'] + '   _cat = '+ etrkDef['etrk_cat'] + '   _act = '+ etrkDef['etrk_act'] + '   _lab = '+ etrkDef['etrk_lab'] + '   _val = '+ etrkDef['etrk_val'] + '   _non = '+ etrkDef['etrk_non']);
		_gaq.push( ['_trackEvent', etrkDef['etrk_cat'], etrkDef['etrk_act'] , etrkDef['etrk_lab'], etrkDef['etrk_val'] ,etrkDef['etrk_non'], etrkDef['etrk_non'] ] ); 
		//  ,etrkDef['etrk_non'] - worked with this removed
        });

    }); // end return this.each

  }; // end $.fn.ezeventtracking

  // Default settings for the plugin
  // No matter what happens we have this to fall back on
  // note: since this object is *not* run through the validation function the values for etrk_val and etrk_non are in post-validation format.
  // that is, etrk_val is an int and not a string, and etrk_non is 0 (zero) for boolean false 
  $.fn.ezeventtracking.defaults = {
  etrk_def : { etrk_rig : 'able', etrk_tar : 'blank', etrk_cat : 'etrk', etrk_act : 'click', etrk_lab : 'useCSSid', etrk_val : 0, etrk_non : 0 }
  };

// Private function that is used within the plugin
function etrkValidateUserSettings ( parmObjectSettings, parmObjectType ) {

	// set up an new empty object, only fill it with valid keys and valid values. 
	// we'll return this object as a scrubbed version of the parmObjectSettings
	var parmValidatedObject = {};
	$.each( parmObjectSettings, function(key,val){ 	

		switch( key ) {
			// right click setting is only valid for the user defined default object (as of this release/version)
			// and unless the value is disable just skip it and use the plugin hardcoded default - which is to leave the right click menu working.
			case 'etrk_rig' : 
				if ( ( parmObjectType  == 'userdefineddefaults'  ) && ( val == 'disable'  )){
					parmValidatedObject[key] = val;
				}	 
			break;
			case 'etrk_tar' : 
				// note: this is the string value blank, not the lack of a string blank
				parmValidatedObject[key] = 'other';
				if ( ( val == 'blank'  ) || ( val == '_blank' ) ){
					parmValidatedObject[key] =  'blank' ;
				} 
			break;
			case 'etrk_cat' : 
				if ( val ) {
					parmValidatedObject[key] =  val;
				}
			break;
			case 'etrk_act': 
				if ( val ) {
					parmValidatedObject[key] =  val;
				}
			break;
			case 'etrk_lab': 
				if ( val ) {
					parmValidatedObject[key] =  val;
				}
			break;
			case 'etrk_val': 
				//  must be an integer
				if ( val % 1 === 0 ) {
					parmValidatedObject[key] =  parseInt(val);
				}	 
			break;
			case 'etrk_non': 
				// must be true or false, else just skip over it and count on a fallback having it right. worst case the hardcoded defaults will. 
				if ( ( val  == 'true' ) || ( val  == 'false') ) {
					parmValidatedObject[key] =  0;
					if  ( val  == 'true' ) {
						parmValidatedObject[key] =  1;
					}
				} 
			break;
			default: 
				// default TBD
		}	
	});
	return parmValidatedObject;
}

})(jQuery); // end closure wrapper
