/*
 * Common Javascript functions
 * V 1.3
 */

/*
 * 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",
			 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);
		// 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);
		}
	}


/*
 * Google Analytics outbound link tracking code
 * triggers trackPageview(options.prefix + link href) 
 */
	
	jQuery.fn.gaTrackOutbound = function(options){
		var settings = jQuery.extend({
		     prefix: "/linkout/",
			 log: false
		 }, options);
		var links = jQuery(this);
		jQuery(links).each(function(i,el){
		});			
			jQuery(this).click(function(){
				// create clean href
				var href = jQuery(this).attr("href");
				if (href.substr(0, 7) == "http://") {
					href = href.substr(7, 9999); // trim off http://
				}
				if (href.substr(0, 8) == "https://") {
					href = href.substr(8, 9999); // trim off https://
				}
				var i = href.indexOf("www.");	// trim off www.
				if (i===0){
					//href = href.substr(4,9999);
				}
				url = settings.prefix + encodeURI(href);		// encode URL
				if (settings.log) {
					jQuery.log(url);
				}
				// GA event tracking
				try {
				    //var myTracker=_gat._getTrackerByName();
				    //_gaq.push(['myTracker._trackEvent', ' + settings.gaCategory + ', ' + href + ']);
					_gaq.push(['_trackPageview', url]);
				}catch(err){
					if (settings.log) {
						$.log(err);
					}
				}
				//urchinTracker(url)	// old GA tracker code
				//pageTracker._trackPageview(url) // old GA tracker code
				//return false;				// cancel link click action
			});
	};

/*
 * Google Analytics event tracking code
 * triggers trackEvent(link, category, event) 
 */
	
	gaTrackEvent = function(options){
		var settings = jQuery.extend({
		     category: "", 	// e.g. Videos
			 action: "", 	// e.g. Play
			 label: "",  	// e.g. Video Title
			 value: 0,  	// e.g. ??
			 page: "",   	// e.g. url-to-log
			 log: false
		 }, options);
		// GA event tracking
		try {
		    //var myTracker=_gat._getTrackerByName();
			if (settings.action) {
				_gaq.push(['_trackEvent', settings.category, settings.action, settings.label, settings.value]);
				//_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday'])
				if(settings.log){jQuery.log("event %s, %s, %s, %s",settings.category, settings.action, settings.label, settings.value);}
			}
			if (settings.page) {
				_gaq.push(['_trackPageview', settings.page]);
				if(settings.log){jQuery.log("pageview %s", settings.page);}
			}
		}catch(err){
			if (settings.log) {
				$.log(err);
			}
		}
	};
	

/*
 * make equal height plugin
 */
	jQuery.fn.equalHeight = function(options){
		var settings = jQuery.extend({
			verticalCenter: false,
			log: true
		}, options);
		
		tallest = 0;
		lastTop = 0;
		row = 0;
		cols = [];
		
		group = $(this);
		//$.log("number of items %s",group.size());	
		group.each(function(i, el){
			// set lastTop for first item
			if(i == 0){
				lastTop = $(this).offset().top;
			}
			//$.log("item %s",i);
			// if last item
			if(i == group.size() - 1){
				//$.log("last item");
				// check if this is the tallest column in the row
				thisHeight = $(this).height();
				//$.log("height %s",thisHeight);
				// update tallest height if this one is tallest
				if (thisHeight > tallest) {
					tallest = thisHeight;
				}				
				// add column to row list
				cols[cols.length + 1] = $(this);
			}
			// set to tallest height if last one in row
			if ($(this).offset().top > lastTop || i == group.size() - 1) {
				//$.log("setting height to %s",tallest);
				// set height of columns in last row list
				$.each(cols, function(i, el){
					// set height
					$(this).height(tallest);
				// set line height to vertically center
				//$(this).css("line-height",tallest+"px");
				});
				// reset columns
				row += 1;
				lastTop = $(this).offset().top;
				tallest = 0;
				cols = [];
			}
			// check if this is the tallest column in the row
			thisHeight = $(this).height();
			//$.log("height %s",thisHeight);
			// update tallest height if this one is tallest
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
			// add column to row list
			cols[cols.length + 1] = $(this);
		});
	}

		

$(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>');
		});
		

		
	/* zebra striping */
	
		$("table.zebra").each(function(){
			$(this).find("tr:even").addClass("even");
			$(this).find("tr:odd").addClass("odd");
			$(this).find("tr:first").removeClass("even").addClass("header");
		});
		$("ul.zebra").each(function(){
			$(this).find("li:even").addClass("odd");
			$(this).find("li:odd").addClass("even");
		});
		
	/* add body class for IE */
	
		if($.browser.msie){
			$("body").addClass("ie");
		}

	/* content reveals */
	
		// hide reveal content blocks
		$("div.reveal").hide();
		// reveal trigger event
		$("a.reveal").each(function(){
			// get id
			var id = $(this).attr("rel");
			//$.log(id);
			// replace content with image
			if($(this).text()=="+"){
				$(this).text("");
			}
			$(this).attr("title","Click to show content").append(" <img src=\"/images/icon-plus.png\" alt=\"\">");
			// preload minus image
			var i = new Image;
			i.src = "/images/icon-minus.png";
			$(this).toggle(
				function(){
					// set image src to minus
					$(this).find("img").attr("src","/images/icon-minus.png");
					// slide down content
					$("#"+id).slideDown('fast',function(){
						// set hash
						//window.location.hash = "#"+id;
					});
					return false;
				},
				function(){
					// set image src to plus
					$(this).find("img").attr("src","/images/icon-plus.png");
					// slide up content
					$("#"+id).slideUp('fast');
					return false;
				}
			);
		});


});
$(window).load(function(){
		$(".columns-equalheight").children().equalHeight();
		$(".equalheight").children().equalHeight();
});
