// Document Ready
jQuery(document).ready(function() { 
        
		// open a link in a new window
		jQuery("a[href$='.pdf'], ._blank").click(function(){
			var newwindow = window.open(this.href, '_blank');
			newwindow.focus();
			return false;
		});
		
		// search bar hint text
		var $searchInput = jQuery('#header #block-search-0 input.form-text');
		var $searchLabel = jQuery('#header #block-search-0 label');
		
		if ($searchInput.val()) {
			$searchLabel.hide();
		}
		
		$searchInput
		.focus(function() {
			$searchLabel.hide();
		})
		.blur(function() {
			if ($searchInput.val() == '')
				$searchLabel.show();
		});
		
		$searchLabel.click(function() {
			$searchInput.trigger('focus');
		});
		
		/* --------------------------------------------------------
			MagicLine top-nav animation
		-------------------------------------------------------- */
		
		// Hide existing CSS rules created to support nonJS users
		jQuery("#top-navigation ul#nice-menu-1 li a").not("#top-navigation ul#nice-menu-1 li ul li a").css("background", "none");
		jQuery("#top-navigation ul#nice-menu-1 ul").css("opacity", "0");
		
		var $el, leftPos, newWidth,
        $mainNav = jQuery("#top-navigation ul#nice-menu-1");
        
		// Add new li to navigation (styling set up in screen.css)
		$mainNav.append("<li id='magic-line'></li>");
		
		var $magicLine = jQuery("#magic-line");
		
		// Set appearance of the magicline based on the current menu item (active page)
		// The active item may be in a submenu so we need to test for that.
		var $activeItem = jQuery("#top-navigation ul#nice-menu-1 li a.active");
		var $parentItem = jQuery($activeItem).closest("li.menuparent");
		
		if (jQuery($parentItem).length) {
			// The current active navigation element is not part of the 1st level
			$magicLine
				.width(jQuery($parentItem).width())
				.css("left", jQuery($parentItem).position().left)
				.data("origLeft", $magicLine.position().left)
				.data("origWidth", $magicLine.width());
		} else {
			// The current active navigation element is in the 1st level
			$magicLine
				.width(jQuery($activeItem).parent().width())
				.css("left", jQuery($activeItem).parent().position().left)
				.data("origLeft", $magicLine.position().left)
				.data("origWidth", $magicLine.width());
		}
		
		// Set up animations for hovers (PART 1: Handles magicline over the 1st level)
		// Note: the navigation item being hovered over may not be in the 1st level. We'll need to check for this.
		jQuery("#top-navigation ul li").find("a").hover(
			function(event) {
				$el = jQuery(this);
				$elparent = jQuery($el).closest("li.menuparent");
				
				if (jQuery($elparent).length) {
					// The hover item is not on the 1st level
					leftPos = $elparent.position().left;
					newWidth = $elparent.width();
					
										
				} else {
					// The hover item is on the 1st level
					leftPos = $el.parent().position().left;
					newWidth = $el.parent().width();
					
				}
				
				// Animate based on the new position calculated above
				$magicLine.stop().animate({
					left: leftPos,
					width: newWidth
				});
				
			}, function() {
				$magicLine.stop().animate({
					left: $magicLine.data("origLeft"),
					width: $magicLine.data("origWidth")
			});
			
		});
		
		//Handle displaying the subnav on hover
		jQuery("#top-navigation ul li.menuparent").hoverIntent(
		function () {
			//Fade in the subnav
			$el = jQuery(this);
			$subNav = jQuery($el).children("ul");
			
			jQuery($subNav).stop().css("display", "block").animate({
			opacity: 1
			}, "slow");
			
		}, 
		function () {
			//Fade out the subnav
			$el = jQuery(this);
			$subNav = jQuery($el).children("ul");
			
			//jQuery($subNav).stop().delay(1000).animate({
			jQuery($subNav).stop().delay(1000).animate({
			opacity: 0
			}, "slow", function () {jQuery(this).css("display", "none")});
			
		});
			
		/*jQuery("#top-navigation ul li.menuparent ul li").hover(
		function() {
			jQuery(this).css("background-color","#b3c1ce");
		},
		function() {
			jQuery(this).css("background","none");
		});*/
		
		/* --------------------------------------------------------
			Various fade animations for the site
		-------------------------------------------------------- */ 
		jQuery(".fade span").css("opacity","0");
		jQuery(".fadehalf").css("opacity","0.5");
				
		jQuery("a.fade span").hover(
		function () {
			//Fade in the span
			jQuery(this).stop().animate({
			opacity: 1
			}, "slow");

		}, 
		function () {
			//Fade out the span
			jQuery(this).stop().animate({
			opacity: 0
			}, "slow");

		});
		
		jQuery(".fadehalf").hover(
		function () {
			//Fade in the span
			jQuery(this).stop().animate({
			opacity: 1
			}, "slow");

		}, 
		function () {
			//Fade out the span
			jQuery(this).stop().animate({
			opacity: .5
			}, "slow");

		});
		
		/* --------------------------------------------------------
			Font size adjustments
		-------------------------------------------------------- */ 
	    // Reset Font Size
	    var originalFontSize = jQuery('#right-column p').css('font-size');
		
	    jQuery(".resetFont").click(function(){
			jQuery('#right-column p').css('font-size', originalFontSize);
			return false;
	    });
	  
	    // Increase Font Size
	    jQuery(".increaseFont").click(function(){
			var currentFontSize = jQuery('#right-column p').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.1;
			jQuery('#right-column p').css('font-size', newFontSize);
			return false;
	    });
	  
	    // Decrease Font Size
	    jQuery(".decreaseFont").click(function(){
			var currentFontSize = jQuery('#right-column p').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*0.9;
			jQuery('#right-column p').css('font-size', newFontSize);
			return false;
	    });


		
});

