$(document).ready(
	function()
	{
	
		// Add hover class to nav.
		$('#nav li.depth-1').mouseenter(
			function()
			{
				if ( !$(this).hasClass('active') ) {
					$(this).addClass('hover');
				}
			}
		).mouseleave(
			function()
			{
				$(this).removeClass('hover');
			}
		);
		// Add a print functionality to the print button.
		$('a.print').click(
			function()
			{
				window.print();
			}
		);
		
		var sitesAnimating = false;
		var sitesListOn = false;
		var siteClicked = false;
		
		$('html').click(
			function()
			{
				if ( sitesListOn == true && siteClicked == false ) {
					// Hide the sites list.
					$('#site-selector ul').fadeOut(100, function() { sitesAnimating = true; sitesListOn = false; } );
				}
			}
		);
	
		$('body').addClass('js');
		// Kill the site select box and replace it with something fancy.
		var d = $('<div/>').attr('id', 'site-selector-container').appendTo('#site-selector');
		var ul = $('<ul/>').css({'display':'none'}).appendTo(d);
		$('#site-selector option').each(
			function(i)
			{
				var title = $(this).text();
				var href = $(this).val();
				var ss = $(this).attr('selected');
				var l = $('<li/>').addClass($(this).attr('selected'));
				var a = $('<a/>').attr('href', '#null').attr('rel', href).html('<span>'+title+'</span>').click(
					function()
					{
						var s = $(this).attr('rel');
						if ( sitesListOn == true && sitesAnimating == false ) {
							siteClicked = true;
							$('#site-selector li').removeClass('selected');
							$(this).parent().addClass('selected');
							// Close the list.
							// Go to the contact page with the selected option as the get val.
							$('#site-selector ul').fadeOut(100, function() { sitesAnimating = true; sitesListOn = false; window.location.href = s; } );
						}
					}
				).appendTo(l);
				if ( ss === 'selected' ) {
					l.addClass('selected');
				}
				if ( i == 0 ) {
					l.addClass('first');
				}
				l.appendTo(ul);
			}
		);
		$('#site-selector form').remove();
		// Now add the fancy select button.
		var b = $('<a href="#null" id="site-selector-button"><span>Choose</span></a>').appendTo('#site-selector');
		$('#site-selector h4, #site-selector-button').click(
			function()
			{
				// Show the list of states.
				if ( sitesListOn == false ) {
					sitesAnimating = true;
					// Start animating.
					$('#site-selector ul').fadeIn(100, function() { sitesAnimating = false; sitesListOn = true; } );
				}
			}
		);
		
		//
		// Search
		//
			
		// Make the input text brighter if the value is searchable.
		if ( $(".search input").val() != '' && $(".search input").val() != 'Search Noetic Group' ) {
			$(".search input").addClass("active");
		}
		// Do stuff when selecting the input field.
		$(".search input").focus(
			function()
			{
				$(this).attr("rel", $(this).val())
						.val("")
						.addClass("active");
			}
		).blur(
			function()
			{
				if ( $(this).val() == '' ) {
					var relValue = $(this).attr("rel");
					$(this).val($(this).attr("rel")).removeAttr("rel");
				}
				if ( $(this).val() == '' || $(this).val() == 'Search Noetic Group' ) {
					$(this).removeClass("active");
				}
			}
		);
		
	}
);

function validateSearch( el )
{
	if ( el.find("input").val() == '' || el.find("input").val() == 'Search Noetic Group' ) {
		return false;
	}
	return true;
}
