/****************************************************************
 * Converts obfuscated email addresses into normal, working email addresses.
 * Enter email address in plain text with @ symbol obfuscated: <span class="eml">foo('at' sign here)bar.com</span>
 * Run the defuscate function on the container: $(".eml").defuscate();
 ****************************************************************/

jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true
    }, settings);
    var regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            // If it's an <a> element, defuscate the href attribute
            $(this).attr('href', $(this).attr('href').replace(regex, '$1@$2'));
            // Make sure that the element's contents is not made into a link
            var is_link = true;
        }
        // Defuscate the element's contents
        $(this).html($(this).html().replace(regex, (settings.link && !is_link ? '<a href="mailto:$1@$2">$1@$2</a>' : '$1@$2')));
  });
}

// roll overs
$(document).ready(function() { 
						   
	$("#top-nav .rollover").each(function() { 
		
		$(this).mouseover(function(){
			var src = $(this).attr("src").replace("_normal", "_over");
			$(this).attr("src", src);
		}).mouseout(function(){
			var src = $(this).attr("src").replace("_over", "_normal");
			$(this).attr("src", src);
		});
	});	
	

	$("#btnForm").click(function() {
		if ($("#optFormMenu").val() != '') {
			window.location = $("#optFormMenu").val();
		}
	
	});
	
		/* Add phoneUS stuff */
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
		}, 
		"Please specify a valid phone number"
	);
	
	$("input[@type=text], input[@type=password], input[@type=checkbox], input[@type=radio], select, textarea").each(function(){
		$(this).addClass("form-inactive");
		});
	$("input[@type=text], input[@type=password], input[@type=checkbox], input[@type=radio], select:not(#optFormMenu), textarea").focus(function(){
		$(this).addClass("form-active");
		$(this).parent().parent().addClass("highlight");
	});
	$("input[@type=text], input[@type=password], input[@type=checkbox], input[@type=radio], select:not(#optFormMenu), textarea").blur(function(){
		$(this).removeClass("form-active");
		$(this).parent().parent().removeClass("highlight");
	});
	// convert the plaintext email addresses in the employee directory
	$("#directory, .eml").defuscate();
});

