$(document).ready(function() {
	/* Use 'jQuery' here instead of '$' because BC's 
		Lightbox script will break it if you don't */
	jQuery(".tweet").tweet({
		join_text: "auto",
		username: "mywinecellarphx",
		avatar_size: 42,
		count: 1,
		auto_join_text_default: "", 
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
	
	/* Newsletter Validation */
	jQuery("#newsletterForm").validate({
		rules: {
			FullName: "required",
			EmailAddress: {
				required: true,
				email: true
			}
		},
		messages: {
			FullName: "",
			EmailAddress: ""
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit();
			alert('Your newsletter subscription request has been submitted. Please check your email for confirmation.');
			jQuery(form).resetForm();
			return false;
		},
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				alert('One or more fields were not properly filled in.');
			}
		},
		errorElement: "div"
	});
	
	/* Online Reservation Form Validation */
	jQuery("#reservationForm").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			}
		},
		messages: {
			FullName: "",
			EmailAddress: "",
			HomePhone: "",
			CellPhone: "",
			ValidateBox: ""
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit();
			alert('Your request has been submitted successfully. Please check your email for confirmation.');
			jQuery(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	/* Book Even Form Validation */
	jQuery("#bookEventForm").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			}
		},
		messages: {
			FullName: "",
			EmailAddress: "",
			HomePhone: "",
			CellPhone: "",
			CAT_Custom_184291: "",
			CAT_Custom_184295: "",
			CAT_Custom_184297: "",
			ValidateBox: ""
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit();
			alert('Your request has been submitted successfully. Please check your email for confirmation.');
			jQuery(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	/* Blog Comment Form Validation */
	jQuery("#blogCommentForm").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			}
		},
		messages: {
			FullName: "",
			EmailAddress: "",
			Feedback: "",
			ValidateBox: ""
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit();
			alert('Your comment has been submitted successfully.');
			jQuery(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	/* Online Reservation Form Validation */
	jQuery("#contactForm").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			}
		},
		messages: {
			FullName: "",
			EmailAddress: "",
			HomePhone: "",
			ValidateBox: ""
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit();
			alert('Your request has been submitted successfully. Please check your email for confirmation.');
			jQuery(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
});

// Custom jquery code
function formatText(index, panel) {
  return index + "";
}

function showResponse(responseText, statusText, xhr, $form)  { 
	alert('Your newsletter subscription request has been submitted. Please check your email for confirmation.');
}

// a custom method making the default value invalid
jQuery.validator.addMethod("defaultInvalid", function(value, element) {
	return value != element.defaultValue;
}, "");

jQuery.validator.setDefaults({
	highlight: function(input) {
		jQuery(input).addClass("textInputOn");
	},
	unhighlight: function(input) {
		jQuery(input).removeClass("textInputOn");
	}
});

(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	jQuery.preLoadImages = function() {
		var args_len = arguments.length;
	
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

jQuery.preLoadImages("/images/buttons/submit2on.png","/images/icons/bang.png","/images/buttons/submit3On.png","/images/buttons/bookNowOn.png");
