jQuery(function() {

	// IMAGE BACKGROUND SWAP
	$('.highlight').hover(
		function () {
		
			$(this).css('backgroundPosition','0 -' + $(this).height() + 'px'); 
		},
		function () {
		
			$(this).css('backgroundPosition','0 0'); 
		}
	);
	
	// IMAGE CYCLE
	$('#roller_cats').cycle({ fx: 'fade' });
	$('#scratcher_cats').cycle({ fx: 'fade' });
	
});

function placeOrder (location) {
	
	pageTracker._trackPageview("/order/click/" + pageName + "/" + location); 	
	window.location.href = 'http://store.amazingtreatmachine.com/products/Amazing-Treat-Machine.html';
	//if (location != 'headerButton') $('#orderform').submit();
}

function wholesale_contact_submit () {

	var error = false;
	
	if (!validate_email($('#email').val())) error = form_error('#email');
	
	if (!$('#firstname').val()) error = form_error('#firstname');
	if (!$('#lastname').val()) 	error = form_error('#lastname');
	if (!$('#phone').val()) 	error = form_error('#phone');
	if (!$('#address1').val()) 	error = form_error('#address1');
	if (!$('#city').val()) 		error = form_error('#city');
	if (!$('#state').val()) 	error = form_error('#state');
	if (!$('#zip').val()) 		error = form_error('#zip');
	if (!$('#country').val()) 	error = form_error('#country');
			
	if (error) {

		// SHOW ERROR MESSAGE
		$('#wholesale_error').css('display','block');
	} else {
	
		// SUBMIT DATA
		$.ajax({
				
			type: 'post',
			url: 'http://www.amazingtreatmachine.com/wp-content/themes/Amazing_Treat_Machine/data_wholesale_submit.php',
			data: $('#wholesale_contact').serialize(),	
			dataType: 'json',
			cache: false,
			success: function (json) { 
			
				if (json.response == 'success') {
				
					$('#wholesale_contact').html('<h2>Thank You</h2><p>Your information has been received. We will be in contact soon.</p>');
				} else {
				
					$('#wholesale_contact').html('<h2>Submission Error</h2><p>Apologies. There was a problem processing your information.</p><p>Please try again later or contact us directly at <a href="mailto:retail@amazingtreatmachine.com">retail@amazingtreatmachine.com</a>.</p>');
				}
			}
		});
		
	}
}

function form_error (id) {

	$(id).parent().addClass('required');
	$(id).change(function() { $(id).parent().removeClass('required'); });
	
	return true;
}

function validate_email (email) {
	
	if ( !email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) ) return false;
	return true;
}
