/*
 * Console logger plugin
 *  %s	String
 *  %d, %i	Integer (numeric formatting is not yet supported)
 *  %f	Floating point number (numeric formatting is not yet supported)
 *  %o	Object hyperlink
 * 
 *  only logs if globalVars.log = true
 */

	$.log = function() {
	  	if(window.console) {
			if($.browser.safari){
				// Safari console
				var args = "";
				$.each(arguments,function(i,val){
					args += " " + val;
				});
				window.console.log(args); // fix to show args, Safari doesn't like .apply
			}
			if ($.browser.mozilla) {
				// Firefox with firebug
				window.console.log.apply(this, arguments);
			}
	  	} else {
	  		// no Firebug
	    	//alert(message);
		}
	};
	
/*
 *  Uncloak masked email addresses plugin
 *   replaces email address @ in "mailto" links and link text
 *   e.g.  <a href="mailto:abc--@--example.com">abc--@--example.com</a>
 *         becomes <a href="mailto:abc@example.com">abc@example.com</a>
 */

	jQuery.fn.emailUncloak = function(options){
		var settings = jQuery.extend({
		     at: "--@--",
			 log: false
		}, options);
		jQuery(this).each(function(i,selector){
			// get links with mailto
			var links = jQuery(selector).find('a[href^="mailto:"]');
			// function for replacing string
			var strReplace = function(s){
				if (typeof s != "undefined") {
					return s.replace(settings.at, "@");
				} else {
					return false;
				}
			};
			// step through each link
			jQuery.each(links, function(i,el){
				// replace label
				jQuery(this).html(strReplace(jQuery(this).html()));
				// replace href
				jQuery(this).attr("href",strReplace(jQuery(this).attr("href")));
				if (settings.log) {
					jQuery.log(jQuery(this));
				}
			});
		});
	};


/* External link tagger plugin
 *   adds class="external"
 *   adds target="_blank"
 *   binds Google Analytics track page view to links onClick
 *   
 *   Add later: 
 *   	allow https://
 *   	don't include URLs that match current domain in case they start with http://
 */


	jQuery.fn.externalLinks = function(options){
		var settings = jQuery.extend({
		     cssClass: "external",
			 target: "_blank",
		     gaPrefix: "/linkout/",
		     gaTracking: true,
			 log: false
		 }, options);
		 var links = jQuery(this).find('[href^="http://"]');
		// bind click for GA tracking code
		if(settings.gaTracking){
			jQuery(links).each(function(i,el){
				if(settings.log){
					jQuery.log(jQuery(this));
				}
				jQuery(this).click(function(){
					var href = jQuery(this).attr("href");
					href = href.substr(7,9999);	// trim off http://
					var i = href.indexOf("www.");	// trim off www.
					if (i===0){
						href = href.substr(4,9999);
					}
					url = settings.gaPrefix + encodeURI(href);		// encode URL
					//jQuery.log(pre + href);			// log href to console
					//urchinTracker(url)	// old GA tracker code
					//jQuery.log(url)
					pageTrackerMain._trackPageview(url)	// GA tracker code
					//return false;				// cancel link click action
				});
			});			
		}
		// add CSS class for styling
		if(settings.cssClass){
			links.addClass(settings.cssClass);
		}
		// add target to load in popup window
		if(settings.target){
			links.attr('target', settings.target);
		}
	};
