/*
 * Console logger plugin
 * @version 1.2
 * @author Simplimation
 * 
 * Usage: $.log("message goes here %s", value);
 *  %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);
			}
			// append to debug container
			var args = "";
			$.each(arguments,function(i,val){
				args += " " + val;
			});
			$("#debug").append("<h3>Javascript</h3><p>" + args + "</p>");
	  	} 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[at]example[dot]com">abc[at]example[dot]com</a>
 *         becomes <a href="mailto:abc@example.com">abc@example.com</a>
 */

	jQuery.fn.emailUncloak = function(options){
		var settings = jQuery.extend({
		     at: "--@--",
			 dot: "--.--",
			 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") {
					// replace ats
					while(s.indexOf(settings.at)!=-1){
						s = s.replace(settings.at, "@");
					}
					// replace dots
					while(s.indexOf(settings.dot)!=-1){
						s = s.replace(settings.dot, ".");
					}
					return s;
				} 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",
			 cssMarkerClass: "external-marker",
			 target: "_blank",
		     gaPrefix: "/linkout/",
		     gaTracking: true,
			 log: false
		 }, options);
		 var links = jQuery(this)
			 // include links with a protocol
		 	.find('[href^="http://"], [href^="https://"]')
		 	// filter only links without current domain
			.not('[href^="http://' + document.domain + '"], [href^="https://' + document.domain + '"]');
		//$.log('[href^="http://' + document.domain + '"]');
		//$.log(links);
		// 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);
					}
					//jQuery.log(pre + href);			// log href to console
					//jQuery.log(url)
					url = settings.gaPrefix + encodeURI(href);		// encode URL
					//urchinTracker(url)	// old GA tracker code
					pageTracker._trackPageview(url) // GA tracker code
					//return false;				// cancel link click action
				});
			});			
		}
		// add CSS class for styling
		if(settings.cssClass){
			jQuery(links).each(function(i,el){
				// add external class
				$(el).addClass(settings.cssClass);
				// add external-marker class if no children
				if($(el).children().length==0){
					$(el).addClass(settings.cssMarkerClass);
				}
			});
		}
		// add target to load in popup window
		if(settings.target){
			links.attr('target', settings.target);
		}
	};

	/* make columns equal height for each row of elements
	 *  usage: <div class="columns-equalheight">
	 *   or  : makeEqualHeight($("ul.list li"));
	 */		
		function makeEqualHeight(group) {
			tallest = 0;
			lastTop = 0;
			row = 0;
			cols = [];
			group.each(function(i,el) {
				// check offsetTop to see if it's the start of a new row
				if($(this).offset().top > lastTop){
					// set height of columns in last row list
					$.each(cols,function(i,el){
						$(this).height(tallest);
					});
					// reset columns
					row += 1;
					lastTop = $(this).offset().top;
					tallest = 0;
					cols = [];
				}
				// check if this is the tallest column in the row
				thisHeight = $(this).height();
				if(thisHeight > tallest) {
					tallest = thisHeight;
				}
				// add column to row list
				cols[cols.length + 1] = $(this);
			});
			//group.height(tallest);
		}

$(document).ready(function(){

	/* columns (columns2, columns3, columns4, columns-golden) 
	 * 	usage:
	 *    <div class="columns2">
	 *     <div class="col">content</div>
	 *     <div class="col">content</div>
	 *    </div>
	 */
	
		// add CSS class and clearing
		$(".columns2, .columns3, .columns4, .columns-golden").each(function(){
			//, .columns3>.col:last, .columns4>.col:last, .columns-golden>.col:last")
			var kids = $(this).children();
			$.log(kids);
			$(kids).eq(kids.length-1).addClass("col-last").after('<div class="clear-hidden"></div>');
		});
		
		$(".columns-equalheight").each(function(){
			makeEqualHeight($(this).children(".col"));
		});

});

