jQuery(document).ready(function($){
	
	/* --------------------------------------- MAIN NAVIGATION --------------------------------------- */
	
	// HOVER
	$("#nav a, .aux-arrow, .aux-balance, .teaser-arrow, .btn, #subnav a,").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(/\.(\w\w\w)$/, "-over.$1") );
			}
			
		},
		
		// out
		function(){
			var theImg = $(this).find('img:first');
			// replace the original src
			if (theImg.data("originalsrc") != null ) {
				theImg.attr("src", theImg.data("originalsrc"));
			}
		}
	);

	// SUBMIT HOVER
	$(".submitHover").hover( 
		// over
		function(){
			var btn = $(this);
			// store the original image src
			btn.data( "originalsrc" , btn.attr("src") );
			// add -over to the image unless it is already -over
			if (!btn.attr("src").match(/-over/) ) {
				btn.attr( "src", btn.attr("src").replace(/\./, "-over.") );
			}
			
		},
		
		// out
		function(){
			var btn = $(this);
			// replace the original src
			if (btn.data("originalsrc") != null ) {
				btn.attr("src", btn.data("originalsrc"));
			}
		}
	);
	
	

	
	/*$("ul#aux-nav li .aux-sub").css({'opacity':'0'});*/
	$("#aux-nav li").hoverIntent({    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 250, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	});
	
	
	/* --------------------------------------- CAROUSEL - FOR MAIN PAGES --------------------------------------- */
	
		$('#carousel').infiniteCarousel({
			displayTime: 6000,
			transitionSpeed: 1200,
			easeLeft: 'easeInOutExpo',
			easeRight: 'easeInOutExpo',
			showCaptions: true,
			onSlideEnd: checkPromo
		});
		
		checkPromo();
		
		function checkPromo() {
			// find the position of the promo
			var index = $('#carousel li').index($('.hero-promo'));
			// if it is the in second position of the array then it is the current slide
			if (index == 1) {
				$('.carousel-headline').fadeOut();
			} else {
				$('.carousel-headline').fadeIn();
			}
		}
	
	/* --------------------------------------- CAROUSEL - FOR SUB PAGES --------------------------------------- */
	
	/*$(function(){
		$('#carousel-small').infiniteCarousel({
			elementHeight: 200
		});
	});*/
	
	/*send last image to far left*/
		$('#carousel-small ul li:last').css('position','absolute');
		$('#carousel-small ul li:last').css('left','-906px');
	
	/* --------------------------------------- TABS --------------------------------------- */
	
	$('.tabs a').click(function(e){
		e.preventDefault;
		var $this = $(this);
		$('.tabs a img').removeClass('active');
		$('.tabs a img').each(function()
			{
				$(this).attr( "src", $(this).attr("src").replace(/-active\./, ".") );
			}
		);
		
		var theImg = $this.find('img');
		
		if (!theImg.attr("src").match(/-active/) ) {
			theImg.attr( "src", theImg.attr("src").replace(/\./, "-active.") );
		}
		theImg.addClass('active');
		
		$('.panel').hide();
		$( $this.attr('href') ).show();
		return false;
	});

	$('input:text[placeholder]').placeholderLabel();


	// Participation vs Demonstration
	$(".sliderLink").click(function(e){
		e.preventDefault;
		$( $(this).attr('href') ).slideToggle();
		return false;
	});
	
});


window.onload = function() {
	// PRELOAD

	$("#nav a, #subnav a, #aux-nav li > a, #aux-nav .aux-arrow, #aux-nav .aux-balance").each(
		function() {
			var theImg = $(this).find('img:first');
			if (!theImg.attr("src").match(/-over/) ) {
				$("<img>").attr( "src", theImg.attr("src").replace(/\.(\w\w\w)$/, "-over.$1") );
			}
		}
	);
	
}



	// MEGA AUX NAV
	function megaHoverOver(){
		
		if ( $.browser.msie ) {
			$(this).find(".aux-sub").show();
			/*$(this).find(".aux-sub").stop().slideDown('fast');*/

        } else {
			$(this).find(".aux-sub").stop().fadeTo('fast', 1).show();
		}
		
		var theImg = $(this).find('img:first');
		theImg.data( "originalsrc" , theImg.attr("src") );
		if (!theImg.attr("src").match(/-over/) ) {
			theImg.attr( "src", theImg.attr("src").replace(/\.(\w\w\w)$/, "-over.$1") );
		}
			
		
	}
	
	function megaHoverOut(){ 
	
		if ( $.browser.msie ) {
			$(this).find(".aux-sub").hide();
			/*$(this).find(".aux-sub").stop().slideUp('fast');*/
		} else {
			$(this).find(".aux-sub").stop().fadeTo('fast', 0, function() { $(this).hide(); });
		}
		
		var theImg = $(this).find('img:first');
		if (theImg.data("originalsrc") != null ) {
			theImg.attr("src", theImg.data("originalsrc"));
		}
		
		
	}


	function submitPrivateDining() {
		var $this = $("#contact-form");

	
		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=first_name]");
		if (!$lastNameField.attr('value'))
			{
				alert('Please enter your First Name.');
				$lastNameField.focus();
				return false;
			}
	
		var $emailField = $this.find("input[name=email_real]");
		if (!$emailField.attr('value'))
			{
				alert('Please enter your Email.');
				$emailField.focus();
				return false;
			}	
		
		var location = $this.find("input[name=location]").attr('value');
		_gaq.push(['_trackPageview', '/action/pd_contact_form/' + location]);
		
		 var params = $this.serialize();
		$.post("/cgi-bin/private_dining_contact.cgi", params,
		   	function(data){
			$this.find(".btnSubmit").fadeOut('slow', function(){
				$this.find("input[type=text], textarea").attr('value', '');
				$this.find("select").find(":first-child").attr('selected', 'selected');
				$this.find(".btnSubmit").after(data);
				$("#response").fadeIn('slow');
			});
			
			setTimeout(function(){
					$("#response").fadeOut('slow',function(){
						$("#response").remove();
						$this.find(".btnSubmit").fadeIn('slow');
					});
					}, 5000);
	
		});
		
		return false;
		
	}
