jQuery(function($){
	
	if ($('div.shippingStateDiv').length === 0){
		return;
	}

	//fetch ID for the Australia state
	var australiaID = $('#shippingState option:contains(Australia)').attr('value');
	
	var hideAustraliaState = function(trigger){
		var $shippingCountry = $('#shippingCountry');
		if ($shippingCountry.length === 0 || $shippingCountry.hasClass('cnprocessed')) {
			return;
		}
		
		$shippingCountry.addClass('cnprocessed');
		
		if ($shippingCountry.val() === 'AU') {
			$('div.shippingStateDiv').hide();
			$('#shippingState').val(australiaID);
			if (trigger === true) {
				$('#shippingState').triggerHandler('change');
			}
		}		
	};	
	
	//these functions will be overridden to apply the hiding function right after cart is rewritten 
	var funcs = ['RefreshCart','UpdateItemQuantity', 'SetShippingCountry', 'UpdateShipping', 'ApplyTaxRate', 'ValidateCart'];	
	$.each(funcs, function(index, func){
		if (typeof window[func] != 'function') {
			return;
		}
		var _wrapper = window[func];
		var _alert = window.alert;
		
		window[func] = function(){
			//alert has to be disabled for the UpdateShipping
			if (func === 'UpdateShipping') {
				window.alert = function(){};
			}
			
			var result = _wrapper.apply(this,arguments);
			
			if (func === 'UpdateShipping') {
				window.alert = _alert;
			}			
			
			hideAustraliaState(func !== 'ApplyTaxRate');
			return result;
		};
	});
	
	//run the function on page refresh
	hideAustraliaState(true);
});
