/*
 * Initiate Jquery Functions
 *
 */

//Kick-off / Page Load Functions
$(document).ready(function(){
		
	//New tab popup
	$('a[rel="external"]').attr("target", "_blank");
	
	//AutoPopulate
	$(".populate").each( function(){
		if ( $(this).val() == "" ) {
			$(this).val( $(this).attr("title") );
		}
	});
	$(".populate").focus(function(){
		if ( $(this).val() == $(this).attr("title") ) {
			$(this).val("");
		}
	});
	$(".populate").blur(function(){
		if ( $(this).val() == "" ) {
			$(this).val( $(this).attr("title") );
		}
	});
	
	//Email Encrypt
	$(".mask").each(function(){
		var ats, dots, address, i;
		ats = [ ' at ', ' (at) ', ' [at] ' ];
		dots = [ ' dot ', ' (dot) ', ' [dot] ' ];
		address = $(this).html();
		for ( i = 0; i < ats.length; i++ ) {
			address = address.replace(ats[i], '@');
		}
		for ( i = 0; i < dots.length; i++ ) {
			address = address.replace(dots[i], '.');
		}
		$(this).html('<a href="mailto:' + address + '">' + address + '</a>');
	});
	
	//Fancybox popup
	$("a.fancybox").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, titlePosition: 'inside'}); 

	//Current Nav State
	var path = location.pathname.substring(1);
	path = path.split("/");
	path = path[path.length - 1];
	if ( path == '' ) path = 'index.php';
	if ( path == 'grants-apply-online.php' ) {
		path = 'grants-request-submission.php';
	}
	parent_path = path;
	if ( parent_path.indexOf('-') != -1 ) {
		parent_path = parent_path.split('-');
		parent_path = parent_path[0];
	}
	if ( path ) {
		$('.navigation ul li a[href^="' + parent_path + '"]').parent().addClass("current");
		$('.sub_navigation ul li a[href^="' + path + '"]').parent().addClass("current");
	}

});
