	
$(document).ready(function(){
	
	// HOVER
	$("#siteNav a, #auxNav a, .hover").hover( 
		// over
		function(){
			var theImg = $(this).find('img:first');
			// store the original image src
			theImg.data( "originalsrc" , theImg.attr("src") );
			
			// add -over to the image unless it is already -over
			if (!theImg.attr("src").match(/-over/) ) {
				theImg.attr( "src", theImg.attr("src").replace(/\.png/, "-over.png") );
				theImg.attr( "src", theImg.attr("src").replace(/\.jpg/, "-over.jpg") );
			}
			
		},
		
		// out
		function(){
			var theImg = $(this).find('img:first');
			// replace the original src
			if (theImg.data("originalsrc") != null ) {
				theImg.attr("src", theImg.data("originalsrc"));
			}
		}
	);
	
	
	// HEADER MAP TOGGLE
	$("#headerMapTab").toggle(
	  function () {
		$("#headerMap").slideDown('slow');
	  },
	  function () {
		$("#headerMap").slideUp('slow');
	  }
	);
	
		
	// PLACEHOLDERS
	$('[placeholder]').placeholderLabel();
	
	
	// STICKY HEADER
	var headerHeight = $("#siteHeader").height();
	var siteSubnavHeight = $("#siteSubnavContainer").height();
	
	var cutOff =  headerHeight + 88;
	$(window).scroll(function(){
		if($(window).scrollTop() > cutOff){
		  $('#siteSubnavContainer').addClass('fixed');
		} else {
		  $('#siteSubnavContainer').removeClass('fixed');
		}
	});

	// SMOOTH SCROLL
	$('.jumpScroll a, a[rel=jump], .sectionHeader a').bind('click', function(){
		$('html,body').animate({
				scrollTop: $( $(this).attr('href') ).offset().top - headerHeight - siteSubnavHeight
			},'slow');
		return false;
	});
	
	// MAP MARKERS
	$("#headerMap .marker").click(function(e){
		e.preventDefault;

		$this = $(this);
		var $callout = $this.next('a');

		var wasVisible = false;
		if ($callout.is(":visible")) {
			wasVisible = true;
		}
		
		$("#headerMap .marker").css('zIndex', '1');
		$this.css('zIndex', '4');		

		$("#headerMap .callout").fadeOut();

		if (!wasVisible) {		
			$callout.fadeIn();
		}
		
		return false;
	});	
	
	
	// MERIT BADGES
	$("#meritBadges a").click(function(e){
		e.preventDefault;
		$(".meritBadgeDetail").hide();
		$($(this).attr('href')).show();
		return false;
	});
	
	// INQUIRY FORM
	$(".inquiryForm form").submit(function(){
		var $this = $(this);
		
		var $firstNameField = $this.find("input[name=first_name]");
		if (!$firstNameField.attr('value'))
			{
				alert('Please enter your First Name.');
				$firstNameField.focus();
				return false;
			}
			
		var $lastNameField = $this.find("input[name=last_name]");
		if (!$lastNameField.attr('value'))
			{
				alert('Please enter your Last Name.');
				$lastNameField.focus();
				return false;
			}
			
		var $emailField = $this.find("input[name=real_email]");
		if (!$emailField.attr('value'))
			{
				alert('Please enter your Email.');
				$emailField.focus();
				return false;
			}
		

		
			
		 var params = $this.serialize();
		$.post("/cgi-bin/inquiry.cgi", params + '&random=' + Math.random(),
		   	function(data){
			$this.find(".submitButton").fadeOut('slow', function(){
				$this.find("input[type=text]").attr('value', '');
				$this.find(".submitButton").after(data);
				$("#response").fadeIn('slow');
			});
			
			setTimeout(function(){
					
					var title = $this.prev('h1').html();
					_gaq.push(['_trackEvent', 'Contact Request', section, title]);
					
			
					$("#response").fadeOut('slow',function(){
						$("#response").remove();
						$this.find(".submitButton").fadeIn('slow');
					});
				}, 5000);
	
		});		
		return false;
	});
	
	// LIGHTBOX 
	$(".photoGallery a").lightBox({
		imageLoading: '/images/lightbox/ico-loading.gif',
		imageBtnClose: '/images/lightbox/btn-close.gif',
		imageBtnPrev: '/images/lightbox/btn-prev.gif',
		imageBtnNext: '/images/lightbox/btn-next.gif',
		imageBlank: '/images/lightbox/blank.gif'
	 });
	 
	 $(".photoGallery a").click(function(){
	 	var photo = $(this).attr('href');
		 _gaq.push(['_trackEvent', 'View photo gallery', section, photo]);
	 });
	 
	 
	 // TRACK EMAILS
	 $("a[href^=mailto]").click(function(){
	 	var email = $(this).attr('href').replace(/mailto:/, '');
		_gaq.push(['_trackEvent', 'Click Email Address', section, email]);
		
	 });
	 
	 	
});


window.onload = function() {
	// PRELOAD

	$("#siteNav a, #auxNav a, .hover").each(
		function() {
			var theImg = $(this).find('img:first');
			if (!theImg.attr("src").match(/-over/) ) {
				$("<img>").attr( "src", theImg.attr("src").replace(/\./, "-over.") );
			}
		}
	);

}
