var isOldIE = /MSIE [1-6]/.test(navigator.userAgent);
jQuery.noConflict();

//Supports autofocus attribute
if(jQuery('<input autofocus="autofocus" />')[0].autofocus !== true){
	jQuery(function($){
		$(':input[autofocus]').focus();
	});
}


jQuery(function($){
				
	
	
$('#shipping\\:region_id option') 
/*
    .livequery(function(){ 
    // use the helper function hover to bind a mouseover and mouseout event 
        $(this) 
            .each(function () {
							if($(this).attr('value') == '1' || 
			$(this).attr('value') == '5' || 
			$(this).attr('value') == '31' || 
			$(this).attr('value') == '32' || 
			$(this).attr('value') == '51' || 
			$(this).attr('value') == '58') {
								$(this).attr('disabled', true);
							}
							
							//alert(this.val());
							//return false;
				// alert('index: ' + i + ' object id: ' + o.id);
				// alert(i.name + '=' + i.value);
                // $(this).addClass('hover'); 
            }); 
    }, function() { 
        // unbind the mouseover and mouseout events 
        //$(this) 
        //    .unbind('mouseover') 
        //    .unbind('mouseout'); 
    }); 

  */  
    
	$('#billing\\:use_for_shipping_yes').click(function() {
		setAllowedBillingStates(true)											
	  $('#billing\\:region_id').val("");
	});
	
	$('#billing\\:use_for_shipping_no').click(function() {
		setAllowedBillingStates(false);													   
	});
	
	$('#shipping\\:same_as_billing').click(function() {
	  setAllowedShippingStates(true);
	  $('#shipping\\:region_id').val("");
	});
	
	
	$('#billing-buttons-container button').each(function(){
	 
	  var oldOnClick = this.onclick;
	  this.onclick = null;
	  
    $(this).click(function (e) {
      setAllowedShippingStates(true);

      //alert($('#shipping\\:region_id').val());
      //alert($('#shipping\\:region_id option[value=' + $('#shipping\\:region_id').val() + ']').attr("disabled"));
      
      if ($('#shipping\\:region_id option[value=' + $('#shipping\\:region_id').val() + ']').attr("disabled") == true)
      {
        //alert("shipping here");
        $('#shipping\\:region_id').val("");
      }
      
      if ($('#billing\\:region_id option[value=' + $('#billing\\:region_id').val() + ']').attr("disabled") == true)
      {
        //alert("billing here");
        $('#billing\\:region_id').val("");
      }
      
      if(oldOnClick)
      {
        oldOnClick.apply(this, [e]);
      }
    });
	});
	    
	
	setAllowedBillingStates(true);
	setAllowedShippingStates(true);
	
	//alert("asdf");
	//Insert home page SWF animation
	if(window.homeAnimationSWF){

		swfobject.embedSWF(
			homeAnimationSWF,
			"homePageAnimation",
			686,
			382,
			"9.0.0",
			"/wp-content/themes/chineseRiceWine/expressInstall.swf",
			window.homeAnimationFlashvars,
			{ //params
				wmode:'transparent'/*,
				AllowScriptAccess:'always',
				scale:'noScale',
				salign:'TL'*/
			}
		);
	}

	
	//Add hover class name to menu so that functional CSS rules can be written for IE6
	if(isOldIE){
		$('#nav > ul > li').hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
	}
	
	//Handle recipes
	$('#recipe-category-toc a').click(function(e){
		var $this = $(this);
		$('#recipe-category-toc li').removeClass('target');
		$this.closest('li').addClass('target');
		
		$('#recipes-list div.target').removeClass('target');
		$(this.href.substr(this.href.indexOf('#'))).addClass('target');
		e.preventDefault();
	});
	crwUpdateSteppedHeight();
	
	
	$('#crwNewsletterSignup').submit(function(e){
		$('#newsletterSignupEmail').removeClass('invalid');
		if(!$('#newsletterSignupEmail').val()){
			
			$('#newsletterSignupEmail').addClass('invalid').focus();
			e.preventDefault();
		}
	});
	$('#newsletterSignupEmail').focus(function(){
		if(this.value == this.defaultValue)
			this.value = '';
	}).blur(function(){
		if(!this.value)
			this.value = this.defaultValue;
	});
	
	$(document.documentElement).addClass('dom-loaded');
});

//jQuery('.tout.rightBorder > p:first-child + p').addClass('steppedHeight');

function setAllowedBillingStates(allowed){
	jQuery('#billing\\:region_id option').each(function() {
		var $this = jQuery(this);												
		if(
			$this.attr('value') == '1' || 
			$this.attr('value') == '5' || 
			$this.attr('value') == '31' || 
			$this.attr('value') == '32' || 
			$this.attr('value') == '35' || 
			$this.attr('value') == '51' || 
			$this.attr('value') == '58')
		{
		  //alert("disabling: " + $this.text());
			$this.attr('disabled', allowed);	
		}
	});
}


function setAllowedShippingStates(allowed){
	jQuery('#shipping\\:region_id option').each(function() {
		var $this = jQuery(this);												
		if(
			$this.attr('value') == '1' || 
			$this.attr('value') == '5' || 
			$this.attr('value') == '31' || 
			$this.attr('value') == '32' || 
			$this.attr('value') == '35' || 
			$this.attr('value') == '51' || 
			$this.attr('value') == '58')
		{
		  //alert("disabling: " + $this.text());
			$this.attr('disabled', allowed);	
		}
	});
}


/**
 * Ensure that the height of a certain container is always in a multiple of 99
 */
function crwUpdateSteppedHeight(){
	var backgroundHeight = 99;
	jQuery('.steppedHeight').each(function(){
		var $this = jQuery(this);
		$this.css('minHeight', 0); //remove min-height so that content height can be recalculated
		$this.css('minHeight', Math.ceil($this.height() / backgroundHeight) * backgroundHeight);
	});
}

