/*********************************
Coremetrics Tagging Plugin

Requires: 
jQuery and Coremetrics libraries

Methods:
init = binds click events to anchors and mapped areas
custom = binds to custom element click event
send = executes the coremetrics function(s) pulling necessary parameters from html and settings 
	-Determines if element is navigational, executing appropriate functions 
debug = allows you to enable debuging in console to see parameters being passed to coremetrics functions on click

Attributes:
Settings = Array includes defaults:
	pageID: false,
	elementCategory: false,
	debug: false,
	boundElements: a, area,
	navClass: nav
*********************************/
(function(jQuery){
		
	var settings = {
		debug: false
	};	
		
	var methods = {
		init: function(options){
			
			settings = {
				pageID: false,
				elementCategory: false,
				debug: false,
				boundElements: 'a, area',
				navClass: 'nav'		
			};

			jQuery.extend(settings, options);
			
			return this.each(function(){

				//BIND TO ELEMENTS
				jQuery(this).find(settings.boundElements).click(function(){	
					jQuery(this).tagIt('send', settings);
					if(settings.debug) return false;
				});
			});
		},
		custom: function(options){
			
			settings = {
				pageID: false,
				elementCategory: false,
				debug: false,
				navClass: 'nav'
			};

			jQuery.extend(settings, options);
			
			return this.each(function(){
				
				//BIND TO ELEMENT
				jQuery(this).click(function(){	
					jQuery(this).tagIt('send', settings);
					if(settings.debug) return false;
				});
			});
		},
		send: function(settings){
			
			/*RETREIVE URL AND MANUALCLICKNAME FROM CLICKABLE ITEM
				AND CHECK TO SEE IF THE ITEM IS NAV ELEMENT*/
			var url = jQuery(this).attr('href');
			var manualLinkClickName = jQuery(this).attr('data-tag');
			var tagNav = jQuery(this).attr('data-tag-nav');
			
			//CHECK TO SEE IF CLICKABLE ITEM IS A NAV ELEMENT
			var nav = false;
			//if(tagNav.search(settings.navClass) != -1) nav = true;
			if(tagNav != undefined){ nav = true;}
			
			if(settings.debug){
				console.log('manualLinkClickName: '+manualLinkClickName);
				console.log('url: '+url);
				console.log('pageID: '+settings.pageID);
				console.log('elementCategory: '+settings.elementCategory);
				console.log('Nav element: '+nav);
				return false;
			}
			
			//FIRE COREMETRICS TAGGING FUNCTIONS
			if(!nav) cmCreateManualLinkClickTag(url, manualLinkClickName, settings.pageID);
			cmCreateElementTag(manualLinkClickName, settings.elementCategory);
		},
		debug: function(options){

			return this.each(function(){
				settings.debug = true;
				console.log('Debugging On');
			});
		}
	};
	
	jQuery.fn.tagIt = function(method){
		if ( methods[method] ) {
		      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		    } else if ( typeof method === 'object' || ! method ) {
		      return methods.init.apply( this, arguments );
		    } else {
		      jQuery.error( 'Method ' +  method + ' does not exist on jQuery.tagIt' );
		    }
	}// end tagIT
})(jQuery)
