/**
* requires omniture s object to be defined prior to being called.

* examples:
	1.find 1 specific link and add custom link tracking
		no options:  $("#myLink").omniture_link_tracking();
		with options: $("#myLink").omniture_link_tracking({linkName:"the_marketplace_link",linkType:"e"});
	2.find multiple links and add custom link tracking
		no options: $("#csw_marketplace a").omniture_link_tracking();
		with options: $("#csw_marketplace a").omniture_link_tracking({linkName:"csw footer links"});
	3.find 1 form and add custom link tracking
		no options: $("#mktsearchform").omniture_link_tracking();
		with options:		
			$("#mktsearchform").omniture_link_tracking({
				linkName:"csw marketplace search",
				getValue:function(){return $("#csw_marketplace_input").val();}
			});
			
default link names:  
[passed value/or objType+eventType]:[innerHTML of anchor/serialized action on form/alt tag of image link/passed value]


* options: linkType,eventType,linkName,linkValue,getValue,deliminator
	linkType = o,e,d - see omniture custom link tracking
	eventType = event type to fire custom link tracking, defaults to click for anchor elements and submit on forms
	linkName = defaults to object type (anchor/form/div/etc.) and event type (click/submit/onchange) pass it in for custom naming
	linkValue = defaults to anchor tag inner text and form serialized action if not passed, getValue overrides this value - meant to be a static value whereas getValue is used to get a specific value from another element i.e keyword search input
	getValue = pass function that returns value,used to get a value to append to customLinkName, i.e. if you want to pass form keyword value, appends to end of customLinkName, called on event (submit for forms, click on anchors)
*/	

(function ($) {
	$.fn.omniture_link_tracking = function(options) {  
            var defaults = {      
                linkType: "o",
				eventType:"click",
				deliminator:": ",
debug:false
            };    
            var options = $.extend(defaults, options); 
            this.each(function(){
				var linkObj = $(this);
				var omnitureThisObj = this;
				var objType = linkObj.attr('tagName');
				if(objType == "FORM"){
					var isForm = true;
					options.eventType = "submit";
				}
 				linkObj.bind(options.eventType,function(e){
					if(options.linkName != null){
						var customLinkName = options.linkName;	
					}else{
						var customLinkName = objType + "_" + options.eventType;
					}								
					if(options.getValue != null){
						var text = options.getValue();								
					}else if(options.linkValue != null){
						var text = options.linkValue;
					}else{
						if(isForm){
							var text = linkObj.serialize();
						}else{
							var text = linkObj.attr("title");
							if(text == ""){
                            	text = linkObj.text();
                            	if(text == ""){
									text = linkObj.find("img").attr("alt");
                                }
							}
						}	
					}
					customLinkName = customLinkName + options.deliminator + text;
					if(options.debug){
						e.preventDefault();
						alert(customLinkName);
					}else{
						s.tl(omnitureThisObj,options.linkType,customLinkName);
					}
				});
            });  
	};
})(jQuery);

