var lastDiv;

$(function() {
	// Scrolling
	$('.top-nav li > a').click(function() {
		// Capture which link
		var navId = $(this).attr('id');
		var newId = '#' + navId.replace('L','D');
		// Scroll
		$.scrollTo(newId, {duration: 2500, easing: 'swing'});
		return false;
	});
	
	$(window).scroll(function() {
		var scrollPosition = $(window).scrollTop()
		var contentBlocks = $('.content-wrapper > div');
		
		var cumulativePosition = 0
		for (var i=0; i < contentBlocks.length; i++) {
			var block = contentBlocks[i];
			cumulativePosition += $(block).height();
			
			if(scrollPosition < cumulativePosition) {
				var navId = $(block).attr('id');
				var newId = '#' + navId.replace('D','L');
				
				$('.top-nav-link').removeClass('active');
				$(newId).addClass('active');
				if (navId == 'attractionsD' && lastDiv != navId){
					$('#nickel-arcade.attraction-box').click();
				}
				lastDiv = navId;
				break;
			}
		}

	});
	
	// Scroll for header logo
	$('.logo').click(function() {
		$.scrollTo('#welcomeD', {duration:2000, easing: 'swing'});
		return false;
	});
	
	// Scroll for link on birthday section
	$('.bday-cl').click(function() {
		$.scrollTo('#contactD', {duration:2000, easing: 'swing'});
		return false;
	});
			
	/*Show & Hide for attractions content
	$('.attraction-box').click(function() {
		//Capture which link and create selector for box to toggle
		var boxId = '#' + $(this).attr('id') + ' ' + '.attraction-content';
		var laser = '#laser-tag .attraction-content';
		var pizzeria = '#city-pizzeria .attraction-content';
        //add class to make #laser-tag box float accordingly
		if ( boxId == laser ){
			$('#laser-tag').addClass('fr');	
		}
		else {
			$('#laser-tag').removeClass('fr');
		}
		// change width of #city-pizzeria accordingly
		if ( boxId == pizzeria ){
			$('#city-pizzeria').removeClass('grid_6').addClass('grid_12');
		}
		else {
			$('#city-pizzeria').removeClass('grid_12').addClass('grid_6');
		}
		// Show box only if it's hidden
		if ( $(boxId).css('display') == 'none') {
			$('.attraction-content').hide(500);
			$(boxId).fadeIn(1000);
		}
	});*/
	
	// Fancy box for image thumbs
	$('.image-group').fancybox({
        'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false	
	});
	
	//Fancybox for comparison chart
	$('.compare-button').fancybox({
        'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false	
	});
				
	// Form content select & focus styles
    $('.contact-info').focus(function() {
    	$(this).removeClass('idle').addClass('active');
    	if (this.value == this.defaultValue) {
    		this.value = '';
    	}
    	if(this.value != this.defaultValue) {
    		this.select();
    	}
    });
    
    // Form content blur style
    $('.contact-info').blur(function() {
    	$(this).removeClass('active').addClass('idle');
    	if (this.value == ''){
    		this.value = (this.defaultValue);
    	}
    });
    
    // Form validation and Ajax biz
    $('.error').hide();

    $('#submit').click(function() {
		// validate and process form
		// first hide any error messages
   		$('.error').hide();
		
		var isError = false;
	  	var name = $('input#name').val();
	  	if (name == 'Enter your name') {
      		$("label#name_error").fadeIn(300);
      		isError = true;
     	}
		var email = $('input#email').val();
		if (email == 'Enter your email address') {
      		$("label#email_error").fadeIn(300);
      		isError = true;
    	}
		var message = $('textarea#comments').val();
		if (message == 'Ask a question, or leave a comment or suggestion here') {
	    	$("label#comments_error").fadeIn(300);
      		isError = true;
	    }
		if (!isError) {
			var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
			
			$.ajax({
			    type: "POST",
			    url: "site_media/bin/process.php",
			    data: dataString,
			    success: function() {
			    	$('#contact-container').hide();
			    	$('#success-container').fadeIn(800);
			    }
		    });
	    }
	});
	
	// Reset form in case user wants to use it again
	$('.form-reset').click(function() {
		$('#success-container').hide();
		$('#contact-container').fadeIn(800);
		$("input#name").val('Enter your email address');
		$("input#email").val('Enter your email address');
		$("textarea#comments").val('Ask a question, or leave a comment or suggestion here');
		
	});

});
