function updateShipping() {
	var hidden_state = $('#hidden-state').val(),
		hidden_shipping_state = $('#hidden-shipping-state').val();
		
	$('#hidden-state').val($('#billing-state').val());
	$('#hidden-shipping-state').val($('#shipping-state').val());

	if(hidden_state != $('#hidden-state').val() || hidden_shipping_state != $('#hidden-shipping-state').val()) {
		
		$("#submit_order #submit").attr("disabled","disabled").fadeTo(250,.5);
		
		$('#save_customer_info_form').ajaxSubmit({
			target: '#lc_left',
			iframe: false,
			success: function() {
				$("#submit_order #submit").removeAttr("disabled").fadeTo(250,1);
			}
		});
	}
}

$(document).ready(function(){
	$('body').addClass('has-js');
	
	// Homepage
	var numRand = Math.floor(Math.random()*5);
	numRand += 1;
	
	$('#home_header').removeClass().addClass('home_grad' + numRand);


	// Field Labels
	$("label.fadelabel").inFieldLabels();
	
	
	// Fancybox
	$(".video_play").fancybox({
		'width'				: '75%',
		'height'			: '75%',
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	
	// Signup Form
	$('#green_footer_bar #download1 input').focus(function(){
		if ($.browser.msie) {
			$('#green_footer_bar #download2').show();			
		} else {
			$('#green_footer_bar #download2').fadeIn();
		}
		
		$('#email_address2').focus();
	});
	
	$(document).click(function(){
		if ($.browser.msie) {
			$('#green_footer_bar #download2').hide();			
		} else {
			$("label.fadelabel").css('opacity','1');
			$('#green_footer_bar #download2').fadeOut();
		}
	});
	
	$('#green_footer_bar #download2').click(function(e){	
		e.preventDefault();
		return false;
	});
	
	$('#green_footer_bar #download2 button').click(function(){	
		$('#signup-form').submit();
	});
	
	var returnVal = $("#signup-form input[name='return']").val();
	$("#signup-form input[name='return']").val(returnVal + '/ajax');
	
	$("#signup-form").validate({
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted above.'
					: 'You missed ' + errors + ' fields.  They have been highlighted above.';
				$("em.error-msg").html(message);
				$("em.error-msg").fadeIn('fast').css('display','block');
			} else {
				$("em.error-msg").hide();
			}
		},
		submitHandler: function(form) {
		   	$(form).ajaxSubmit({
				target: '#download3', 
				beforeSubmit: function() {
					$('#download2, #download1').fadeOut('fast',function(){
						$('#download3').fadeIn('slow'); 
					});
				},
				iframe: true
			});
		}
	});
	
//	$("#signup-form").validate({
//		onkeyup: false,
//		invalidHandler: function(e, validator) {
//			var errors = validator.numberOfInvalids();
//			if (errors) {
//				var message = errors == 1
//					? 'You missed 1 field. It has been highlighted above.'
//					: 'You missed ' + errors + ' fields.  They have been highlighted above.';
//				$("em.error-msg").html(message);
//				$("em.error-msg").fadeIn('fast').css('display','block');
//			} else {
//				$("em.error-msg").hide();
//			}
//		},
//		submitHandler: function(form) {
//		   	$(form).ajaxSubmit({
//				target: '#download3', 
//				success: function() {
//					$('#download2, #download1').fadeOut('fast',function(){
//						$('#download3').fadeIn('slow'); 
//					});
//				}
//			});
//		}
//	});
	
	
	// Testimonial Videos
	clickCount = 0;
	
	$('.video_reload').click(function(e){
		e.preventDefault();
		videoCount = $(this).parent().parent().children('.ss_video_wrap').length;
		clickCount += 1;
		$(this).parent().removeClass('active').hide();
		
		if(clickCount < videoCount){
			$(this).parent().next().addClass('active').fadeIn();
		} else {
			clickCount = 0;
			$('.ss_small_video div:nth-child(2)').addClass('active').fadeIn();
		}
	});
	
	
	// Seminars Back Button
	$('#seminars-back').click(function(){
		thisSite = window.location.host;
		prevPage = document.referrer;
		if(prevPage.indexOf(thisSite) != -1){
			history.go(-1);
			return false;
		}
	});
	
	
	// Product Images
	$("#product_large_image img:first-child").addClass('active');
	
	$('.individual_product_thumb').click(function(e){
		e.preventDefault();
		var thumbClicked = $(this).index() + 1;
		$('#product_large_image img.active').removeClass('active').hide();
		$("#product_large_image img:nth-child("+thumbClicked+")").fadeIn().addClass('active');
	});
	
	$('#ip_location').change(function(){
		contentTest = $('#ip_location').val();
		newHeadline = $('#ip_location option:selected').attr('data-headline');
		newContent = $('#ip_location option:selected').attr('data-description');
		
		if(contentTest){
			$('.product-content').html(newHeadline + newContent);
			$('#location_field').val(contentTest);
		}
	});
	
	
	// Star Ratings
	$('#write_review').click(function(e){
		e.preventDefault();
		$('#rating-form').slideDown();
	});
	
	$('.star_rating_system a').click(function(e){
		e.preventDefault();
		stars = $(this).attr('data-rating');
		$('.star_rating_system a.active').removeClass('active');
		$(this).addClass('active');
		$(".star_rating_system select option[value="+stars+"]").attr('selected','selected');
	});
	
	$("#rating-form").validate({
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below.'
					: 'You missed ' + errors + ' fields.  They have been highlighted below.';
				$("em.error-msg").html(message);
				$("em.error-msg").fadeIn('fast').css('display','block');
			} else {
				$("em.error-msg").hide();
			}
		}
	});
	
	$("#add-to-cart-form").validate({
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below.'
					: 'You missed ' + errors + ' fields.  They have been highlighted below.';
				alert(message);
				$("em.error-msg").html(message);
				$("em.error-msg").fadeIn('fast').css('display','block');
			} else {
				$("em.error-msg").hide();
			}
		}
	});
	
	$("#checkout_form").validate({
		rules: {
			state: {required: true},
			shipping_state: {required: true}
		},
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below.'
					: 'You missed ' + errors + ' fields.  They have been highlighted below.';
				$("em.error-msg").html(message);
				$("em.error-msg").fadeIn('fast').css('display','block');
			} else {
				$("em.error-msg").hide();
			}
		}
	});
	
	
	// Shipping Info
	$('input.same-info').change(function(){
		billing_fname = $('#billing-fname').val();
		billing_lname = $('#billing-lname').val();
		billing_email = $('#billing-email').val();
		billing_phone = $('#billing-phone').val();
		billing_adr = $('#billing-address').val();
		billing_city = $('#billing-city').val();
		billing_state = $('#billing-state').val();
		billing_stateName = $('#selectstate').text();
		billing_zip = $('#billing-zip').val();
		billing_country = $('#billing-country').val();
	
		if($(this).is(':checked')){
			if(billing_fname != ''){
				$('#shipping-fname').val(billing_fname).prev().addClass('active');
			}
			if(billing_lname != ''){
				$('#shipping-lname').val(billing_lname).prev().addClass('active');
			}
			if(billing_email != ''){
				$('#shipping-email').val(billing_email).prev().addClass('active');
			}
			if(billing_phone != ''){
				$('#shipping-phone').val(billing_phone).prev().addClass('active');
			}
			if(billing_adr != ''){
				$('#shipping-address').val(billing_adr).prev().addClass('active');
			}
			if(billing_city != ''){
				$('#shipping-city').val(billing_city).prev().addClass('active');
			}
			if(billing_state != ''){
				$('#shipping-state').val(billing_state);
				$('#selectshipping_state').text(billing_stateName);
				updateShipping();
			}
			if(billing_zip != ''){
				$('#shipping-zip').val(billing_zip).prev().addClass('active');
			}
			if(billing_country != ''){
				$('#shipping-country').val(billing_country).prev().addClass('active');
			}
		} else {
			$('#shipping-fname').val('').prev().removeClass('active');
			$('#shipping-lname').val('').prev().removeClass('active');
			$('#shipping-email').val('').prev().removeClass('active');
			$('#shipping-phone').val('').prev().removeClass('active');
			$('#shipping-address').val('').prev().removeClass('active');
			$('#shipping-apt').val('').prev().removeClass('active');
			$('#shipping-city').val('').prev().removeClass('active');
			$('#shipping-state').val('').prev().removeClass('active');
			$('#shipping-zip').val('').prev().removeClass('active');
			$('#shipping-country').val('').prev().removeClass('active');
		}
	});
	
	$("#shipping-state, #billing-state").change(function(){
		updateShipping();
	});
	
	$('#ee-error-wrap a').click(function(e){
		history.go(-1);	
	});
});
