jQuery(document).ready(function(){
	// Navigation
	var config = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 40, // number = milliseconds for onMouseOver polling interval    
	     over: function(){jQuery(this).children('ul').fadeIn("medium")}, // function = onMouseOver callback (REQUIRED)    
	     timeout: 50, // number = milliseconds delay before onMouseOut    
	     out: function(){jQuery(this).children('ul').fadeOut("medium")} // function = onMouseOut callback (REQUIRED)    
	};
	var primary_navigation = jQuery("#Navigation").children();
	primary_navigation.hoverIntent( config );
	jQuery("#Navigation a").attr('title','');
	
	primary_navigation.hover(function(){
		jQuery(this).addClass('hovering');
	},function(){
		jQuery(this).removeClass('hovering');
	});
	
	// Switch
	jQuery('#Overlay').click(function(){ jQuery(this).fadeOut('fast');});
	jQuery('#Switch').toggle(function (){
		jQuery(this).blur();
		jQuery(this).css({'background-position':'0 -86px'});
		jQuery('#Overlay').fadeIn('medium');
	},function(){
		jQuery(this).blur();
		jQuery(this).css({'background-position':'0 0'});
		jQuery('#Overlay').fadeOut('medium');
	});

	// Search bar
	var default_search_string = 'Search Switch ...';
	jQuery("#HeaderSearch input.searchField").focus(function(){
		if (jQuery(this).attr('value') == default_search_string){
			jQuery(this).attr('value','');
		}
	});
	jQuery("#HeaderSearch input.searchField").blur(function(){
		if (jQuery(this).attr('value') == ''){
			jQuery(this).attr('value',default_search_string);
		}
	});
	
	// Purchase Information
	jQuery("#PurchaseInformation").fancybox({ 
    	'margin'        	: '50px 300px',
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000'
	});	
	
	jQuery('.entry').jScrollPane();
	
	// Appointment Request
	jQuery("#AppointmentRequest").fancybox({ 
    	'margin'        	: '50px 300px',
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000'
	});

	var response_container = jQuery('#ContactResponse');
	var error_animation = 500;
	var errors = new Array();

	function resetAppointmentRequestForm() {
		errors = new Array();
		response_container.slideUp("fast");	
		jQuery('#ContactName').removeClass('error');
		jQuery('#ContactEmail').removeClass('error');
	}
		
	jQuery( ".datepicker" ).datepicker();

	jQuery("#AppointmentRequestForm form").submit(function(){
		jQuery('#ContactSubmit').blur();
		resetAppointmentRequestForm();
		response_container.slideUp("slow");	

		jQuery('#AppointmentRequestForm').height( 'auto' ); 

		var input_field = jQuery("input#ContactName");
		if (input_field.val() == "") {
			if (errors.length == 0) input_field.focus();
			input_field.addClass('error');
			errors.push('You must enter a name.');
		}
		
		var input_field = jQuery("input#ContactEmail");
		if (input_field.val() == "") {
			if (errors.length == 0) input_field.focus();
			input_field.addClass('error');
			errors.push('You must enter a valid email address.');
		}
		
		if (errors.length > 0) {
			var error_string = '';
			for (x in errors){
				error_string += '<p class="error">' + errors[x] + '</p>';
			}
			response_container.html(error_string);
			response_container.slideDown("slow");	
			return false;
		}
		
		jQuery('#SecretCode').val('booya');
		resetAppointmentRequestForm();
		var submission_button = jQuery('#ContactSubmission').html('<div id="SubmitWait"><br /><img src="/wp-content/themes/switch/images/waiting.gif" /><br />Submitting</div>');

		$.ajax({
			type: "POST",
			url: "/wp-content/themes/switch/appointment_request.php",
			data: jQuery("#AppointmentRequestForm form").serialize(),
			success: function(response) {
				if (response == 'fail') {
					response_container.html('<p class="errorString">There was a problem with your submission. Please try again.</p>');
					response_container.slideDown("slow");	
				}else{
					jQuery("#AppointmentRequestForm form").slideUp();
					response_container.html('<p class="success">Thank you for requesting an appointment. A staff member will be in touch to confirm your request.</p>');
					response_container.slideDown("slow");	
					submission_button.hide();
				}
			}
		});
		return false;
	});
});

