var Checkout = Class.create({
	initialize: function(){
		this.form = $("CartAddForm"); 

		this.domesticCountry = "USA";

		this.defaultShippingMethodIds = {
			"domestic": "CartShipVia30",
			"international": "CartShipVia45"
		};

		if($("CartShipCountry") && $("CartShipCountry").selectedIndex != undefined){
			// Set which shipping methods are displayed initially
			this.setInternationalShippingVisibility();

			// Set observer to check international visibility
			$("CartShipCountry").setAttribute("onChange", "javascript: checkout.setInternationalShippingVisibility()");
		}

		this.observeBillingCheckbox();
		this.observePaymentMethodSelect();
	},
	
	setInternationalShippingVisibility: function(){

		var countrySelect = $("CartShipCountry");	
		var selectedCountry = countrySelect.options[countrySelect.selectedIndex].value;

		// Uncheck all the shipping methods
		$$(".CartShipViaInput").each(function(e, index){
			e.removeAttribute("checked");
		});

		// Hide the international method(s) if domestic is selected
		if(selectedCountry == this.domesticCountry){
			this.selectShippingRegion("domestic");

		// Hide the domestic methods if international is selected
		} else{
			this.selectShippingRegion("international");
		}
	},

	selectShippingRegion: function(region){
		var shipViaElements = $$(".ship-via");
		shipViaElements.each(function(e, index){
			if(e.className == "ship-via " + region){
				e.style.display = "";
			} else{
				e.style.display = "none";
			}
		});	

		// Check the appropriate input
		$(this.defaultShippingMethodIds[region]).setAttribute("checked", "checked");
	},

	showBilling: function(){
		this.observeBillingCheckbox("show");
	},

	hideBilling: function(){
		this.observeBillingCheckbox("hide");
	},

	observeBillingCheckbox: function(override){
		var checkbox = $("CartBillingSameAsShipping");	
		if(checkbox == undefined){
			return;
		}

		if(override != undefined){
			if(override == "hide"){
				$("billing-information-notice-hidden").style.display = "";
				$("billing-information-notice-shown").style.display = "none";
				$("checkout-billing-fields").style.display = "none";
				checkbox.checked = true;
			} else{
				$("billing-information-notice-hidden").style.display = "none";
				$("billing-information-notice-shown").style.display = "";
				$("checkout-billing-fields").style.display = "";
				checkbox.checked = false;
			}
		} else{
			if(checkbox.checked == true){
				$("billing-information-notice-hidden").style.display = "";
				$("billing-information-notice-shown").style.display = "none";
				$("checkout-billing-fields").style.display = "none";
			} else{
				$("billing-information-notice-hidden").style.display = "none";
				$("billing-information-notice-shown").style.display = "";
				$("checkout-billing-fields").style.display = "";
			}
		}
	},

	observePaymentMethodSelect: function(){
		if($("CartPaymentMethod")){
			var select = $("CartPaymentMethod");
			var selectedMethod= select.options[select.selectedIndex].value;
			if(selectedMethod == "paypal"){
				$("paypal-fields").style.display = "";
				$("credit-card-fields").style.display = "none";
			} else{
				$("paypal-fields").style.display = "none";
				$("credit-card-fields").style.display = "";
			}
		}
	}

	
});
