$(function() {
	Checkout.init();
	Catalogue.init();
});

var Checkout = {
	init: function() {
		$("#billing_as_delivery").click(function() {
			if (this.checked) {
				$("input[name^='billing_']").each(function(i) {
					var n = $(this).attr("name");
					n = n.substr(8);
					if ($("input[name='"+n+"']").length) {
						$(this).attr("value", $("input[name='"+n+"']").attr("value"));
					}
				}).attr('readonly', true);
				
				$("#billing_title_drop").val($("#title_drop").val());
				$("#billing_country").val($("#country").val());
				$("#billing_state").val($("#state").val());
				if ($("#billing_country").val() == "UNITED STATES") {
					$("#billing_country").parent().next("p:hidden").show();
				} else {
					$("#billing_country").parent().next("p:visible").hide();
				}
					
			} else {
				$("input[name^='billing_']").attr('readonly', false);
			}
		});
		
		$(".country").change(function() {
			if ($(this).val() == "UNITED STATES") {
				$(this).parent().next("p:hidden").show();
			} else {
				$(this).parent().next("p:visible").hide();
			}
		});
	}
};

var Catalogue = {
	init: function() {
		$(".prodthumb").click(function() {
			$("#primary_image").attr("src", $(this).children("img").attr("src"));
		});
	}
};
