/*
 * Email Defuscator - jQuery plugin 1.0 alpha
 *
 * Copyright (c) 2007 Joakim Stai
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 *
 */

/**
 * Converts obfuscated email addresses into normal, working email addresses.
 *
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @descr Converts obfuscated email addresses into normal email addresses
 */
 
jQuery.fn.defuscate=function(b){b=jQuery.extend({link:true},b);var c=/\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;return this.each(function(){if($(this).is('a[@href]')){$(this).attr('href',$(this).attr('href').replace(c,'$1@$2'));var a=true}$(this).html($(this).html().replace(c,(b.link&&!a?'<a href="mailto:$1@$2">$1@$2</a>':'$1@$2')))})}

$(function() {
	
	$("q").prepend("&ldquo;").append("&rdquo;");
	$("q q").prepend("&lsquo;").append("&rsquo;");
	// Add classes to input pseudotypes
	$("input[type='text'], input[type='password']").addClass("text");
	$("input[type='button'], input[type='submit'], input[type='checkbox'], input[type='image'], input[type='radio'], input[type='reset'], button").addClass("nontext");
	$("input[type='button'], input[type='submit'], input[type='reset'], button").addClass("button");
	// Add classes to link pseudotypes
	$("a[href^='mailto:']").addClass("mailto");
	$("a[href$='.pdf']").addClass("docpdf");
	$("a[href$='.swf']", "a[href$='.fla']", "a[href$='.swd']").addClass("docflash");
	$("a[href$='.xls']", "a[href$='.csv']", "a[href$='.xlt']", "a[href$='.xlw']").addClass("docexcel");
	$("a[href$='.ppt']", "a[href$='.pps']").addClass("docppoint");
	$("a[href$='.doc']", "a[href$='.rtf']", "a[href$='.txt']", "a[href$='.wps']").addClass("docword");
	$("a[href$='.zip']", "a[href$='.gzip']", "a[href$='.rar']").addClass("doczip");
	// External link popper
	$('a[href^=http://]').not('[href*='+window.location.host+']').attr('target','_blank').addClass("external");
		/* 
		checks href against current domain. If not matching and href contains http://, target blank it and add class external
		*/
	
	// Remove classes from a link containing an image
	$("a img").parent().removeClass();
	
	// Add a class to first element in a group
	$("#nav li:first-child").addClass("first");
	
	// Add a class to last element in a group
	$("#nav li:last-child").addClass("last");
	
	// Add a class to a list containing a list
	$("#nav li ul").parent().addClass("parent");
	
	// Activate pngFix for fixing PNG transparency issues
	$('#branding').pngFix();
	
	// Activate Superfish Plugin
	$('#nav').superfish();
		
	// Wrap <hr> so bg-image doesn't have a border in IE6 and Opera
	$("hr").wrap('<div class="hr"></div>');
	
	/* Add class="obfuscated" and href="mailto:name(at)example.com" to an email address link */
	$('.obfuscated').defuscate();
	
	// Set equal heights
	$(".equalHeights").equalHeights();
	
	// Give Active Navigation Class of "current"
	$(".page-about .nav-about").addClass("current");
	
	// @printer
	// empower print
	$("#print-page a").click(function(){ window.print(); return false; });
	
	// Font-resizer
	$('#utility').prepend('<p id="resizer"><small>Text Size</small>&nbsp;<span class="small"><a class="decreaseFont" href="#abcd">A</a></span>&nbsp;<span class="medium"><a class="resetFont" href="#abcd">A</a></span>&nbsp;<span class="large"><a class="increaseFont" href="#abcd">A</a></span></p>');
	
	// Reset Font Size
	var originalFontSize = $('body').css('font-size');
	$(".resetFont").click(function(){
		$('body').css('font-size', originalFontSize);
	});
	
	// Increase Font Size
	$(".increaseFont").click(function(){
		var currentFontSize = $('body').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.12;
		$('body').css('font-size', newFontSize);
		return false;
	});
	
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		var currentFontSize = $('body').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.9;
		$('body').css('font-size', newFontSize);
		return false;
	});
	
	// Move the label over the input and clear it on mouse click
	$("#searchform input[type='text']").inputSetter();
	
	// Few setups for IE6
	if(document.all){
		//add class to drop downs and buttons 
	    $('#nav li, button').hover(
			function() { $(this).addClass('over'); },
			function() { $(this).removeClass('over'); }
	    );
	}// if document.all
	
});

// jQuery plugins

// clean console.log
function cl(logit){ if(window.console&&window.console.firebug) { console.log(logit) } };//cl()
// example: cl("If you use cl() instead of console.log(), it won't break IE when you forget to take it out.");


// IE6 fixes
// make sure IE has the abbr and acronym tag
if(document.all){
	document.createElement("abbr");
	document.createElement("acronym");
}
// prevent IE6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

/*
	
	@inputClear
	apply automatic input clearing to the search input - add as needed
	$("#searchform input").inputClear();

*/
jQuery.fn.inputClear = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

// end inputClear


/*

	12.19.08 - pdf
	
	@inputSetter
	Input Setter
	pull label and insert as field. clear with click.	
	optional lower param if == 1, string toLowerCase	

	ex: $("#sitesearch input").inputSetter(1); // makes default text lowercase
		$("#sitesearch input").inputSetter(); // no lowercasing

	see custom validation for jquery.validation.js below

*/

jQuery.fn.inputSetter = function(lower) {	
	return this.each(function() {
		var $input = $(this);
		var $label = $("label[for='"+$input.attr("id")+"']");
		var labeltext = lower && lower==1 ? $label.text().toLowerCase() : $label.text();
		$label.hide();	
		$input.val(labeltext);		
		$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
			  .blur(function() { if (!this.value.length) { this.value = labeltext; } });		
	});
};

// end inputSetter

// javascript method: "pxToEm"
eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(4($){k.9.7=5.9.7=4(a){3 b=p;2(b.j==5)b=d(b);2(!a)3 a=$(\'g\');2($(a).6()>0){3 c=b*(1/(d($(a).r("n-6"))))+\'m\';8 c}l 8\'i: o h q f s a e t\'}})(u);',31,31,'||if|var|function|String|size|pxToEm|return|prototype||||parseInt|DOM|is|body|scope|Error|constructor|Number|else|em|font|Provided|this|argument|css|not|element|jQuery'.split('|'),0,{}));

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:
   Scott Jehl (scott@filamentgroup.com) 
   Todd Parker (todd@filamentgroup.com)
   http://www.filamentgroup.com
 *
 * Copyright (c) 2007 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights of the first-children of a provided element 
 								  and sets their min-height to the tallest height. Sets in em units by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method								  
 * Usage Example: $(element).equalHeights();
   						      Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 1.0, 08.02.2007
 * Changelog:
 *  08.02.2007 initial Version 1.0
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
				if($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if(!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm($(this)); //use ems unless px is specified or 
		// for ie6, set height since min-height isn't supported
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
		if ($.browser.msie && (ie6)) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return $(this);
};

document.documentElement.className = "js";

// @flicker
// prevent ie6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}
