// JavaScript Document
jQuery(document).ready(function($) { 	

	$("#lgr-cycle").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 5000);
	$("#lgr-tabs").tabs({ fx: { opacity: 'toggle' } })
	$("table.stripe tr:even").addClass("oddrow");
	
	// Messages
	//$('.message').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
	$('.message .close').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
	$('.message .close').click(function() {
		$(this).parent().fadeOut('slow', function() { $(this).remove(); });
	});
	
	// forms
	$("#validate").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.prev("label").children("span").text('') );
			element.prev("label").children("span").css("color","red");
		},
		   errorElement: "em",
		highlight: function(element, errorClass) {
			$(element).addClass("error");
		},
		invalidHandler: function(form, validator) {
			 var errors = validator.numberOfInvalids();
			 if (errors) {
			   var message = errors == 1
				? 'You\'ve missed 1 required field. It has been highlighted below.'
				: 'You\'ve missed ' + errors + ' required fields. They have been highlighted below.';
			   $("div.form-error").html(message);
			   $("div.form-error").hide().addClass("message").addClass("error").fadeIn("fast");
			 } else {
			   $("div.form-error").hide();
			 }
		    },			
		validClass: "success",
		submitHandler: function(form) {
			//form.submit();
			document.form['validate'].submit();
		}
	});
	
	//checkout
	$(".ship-address").hide();
	$(".ship-address-option").show();
	$(".ship-address-option a").click(function(){
		$(".ship-address").fadeIn("slow");
		$(".ship-address-option").hide();
			return false;
		});
	$("select optgroup[label=Canada]").hide();
	$(".billing-country, .shipping-country").click(function(){
		var showCountry = (this.value.toUpperCase());
		$(this).parent().parent().children().children().children("optgroup:not(.select-country-"+showCountry+")").hide()
		$(this).parent().parent().children().children().children(".select-country-"+showCountry).show().parent().children().children().eq(0).attr('selected', 'selected');
		if (showCountry=="CN") {
			$(this).parent().parent().children().children().children(".select-country-"+showCountry).parent().parent().children("label").hide().html("City, Providence, Postal Code: <span>Required</span>").fadeIn();
		} else {
			$(this).parent().parent().children().children().children(".select-country-"+showCountry).parent().parent().children("label").hide().html("City, State, Zip Code: <span>Required</span>").fadeIn();
		}
	});
	
	
	$('#contact-form').submit(function() {

		// Disable the submit button
		$('#contact-form input[type=submit]')
			.attr('value', 'Sending...')
			.attr('disabled', 'disabled');

		// AJAX POST request
		$.post(
			$(this).attr('action'),
			{
				name:$('#Your-Name').val(),
				email:$('#Your-Email').val(),
				message:$('#Your-Message').val()
			},
			function(errors) {
				// No errors
				if (errors == null) {
					$('#contact-form').hide().html('<h2>Thank you.</h2><p>Your message has been sent.</p>').show();
					//Cufon.replace('h1, h2, h3, h4, h5, h6');
					
					$('.alert').removeClass('alert');
				}

				// Errors
				else {
					// Re-enable the submit button
					$('#contact-form input[type=submit]').removeAttr('disabled').attr('value', 'Send message');
					$('#contact-form label span').css("color","#666666").html("Required");

					// Technical server problem, the email could not be sent
					if (errors.server != null) {
						$('.alert').removeClass('alert');
						alert(errors.server);
						return false;
					}

					// Empty the errorbox and reset the error alerts
					$('.alert').removeClass('alert');

					// Loop over the errors, mark the corresponding input fields,
					// and add the error messages to the errorbox.
					for (field in errors) {
						if (errors[field] != null) {
							$('#' + field).addClass('alert');
							$('#' + field).prev("label").children("span").css("color","red").html(errors[field]);

						}
					}
				}
			},
			'json'
		);

		// Prevent non-AJAX form submission
		return false;
	});
		

});  // end jQuery

