
$(document).ready(function() {
   // put all your jQuery goodness in here.
   // Do not instantiate elements until the document has finished loading


   	var cookieState = $.cookie('state');
   	//alert(cookieState);
   	var redirectOnce;
   	//alert(cookieState);
   	   	   	
   	if (cookieState == null) {
   		tb_show("Shipping State Verification","TB_inline?height=150&width=300&inlineId=hiddenModalContent");
   		$(".flash-container").html("<img src='http://alpha.wsjwine.com/images/flashpic.jpg'>");
   	}
   	
   	else if (cookieState != null) {
   		$(".ship-to-state").html(cookieState + "&nbsp;<a class='state-change' href='#'>Change state</a>");
   		$("#state option[value='"+ cookieState +"']").attr('selected', 'selected');
   		
   	}
   	
   	else if (cookieState == 'MA' || cookieState == 'CA'|| cookieState == 'NY' && redirectOnce == 'null' ) {
   		location.href = "/ComplianceRedirect.aspx"
   		redirectOnce = '1';
   	}
   	
   	
   	$(".state-change").click(function () { 
    	tb_show("Shipping State Verification","TB_inline?height=150&width=300&inlineId=hiddenModalContent");
    	$(".flash-container").html("<img src='http://alpha.wsjwine.com/images/flashpic.jpg'>");
    	return false;
    });
   	

		$('#GiftMessage').each(function(){
		var limit = 60;
		var oldLength = $(this).val().length;
		// update characters
		$(this).parent().find('.counter').html(limit - oldLength + ' characters left');
		// bind on key up event
		$(this).keyup(function(){
		// get new length of characters
		var newLength = $(this).val().length;
		// update
		$(this).parent().find('.counter').html(limit - newLength + ' characters left');
		
		if (newLength > limit) {
			$('.counter').html('You cannot write more then '+limit+' characters!');
			$(this).val($(this).val().substr(0,limit));
			return false;
		}
		else {
			$(this).parent().find('.counter').html( 60 - newLength + ' characters left');
		}
		});
	});


});
	
	
