$(function() {
	Checkout.init();
	Catalogue.init();
	
	product_message();
	
	toggles();

});


function slideit(nameofid){
	$('#'+nameofid).toggle('slow');
}

function toggles(){

	$(".toggle").each(function(){

		$(this).click(function(){

			var nameofid = $(this).attr('id').replace("toggle_", "");
			slideit(nameofid);

			return false;

		});

	});

}

function product_message(){
	
	if($('#product_message')){
		window.setTimeout(function() {
		 $('#product_message').slideUp('slow');
		}, 5000);
	}

}


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"));
						$("input[name='"+n+"']").attr("value", $(this).attr("value"));//.attr('readonly', true);
					}
				});
				
				$("#title_drop").val($("#billing_title_drop").val());
				$("#country").val($("#billing_country").val());
				$("#state").val($("#billing_state").val());
				if ($("#country").val() == "UNITED STATES") {
					$("#country").parent().next("p:hidden").show();
				} else {
					$("#country").parent().next("p:visible").hide();
				}
				
//				$("#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();
//				}
					
				
				
				// update the delivery box too now.
				// LINE 113
				var myData = $("#country").val();
				$.ajax({
				  type: "POST",
				  url: '/shop/getDelivery/',
				  data: 'deliveryArea='+myData,
				  success: function(data) {
					  
					  $('#delivery_option').children().remove().end().append(data) ;
					  
					//$('#selectBox').html(data);
				  }
				});
				
				
			} else {
				$("input:not([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();
			}			
			
		});
		
		$("#billing_as_delivery").click(function(){
			if($(this).is(":checked")){
				$("#billing_country").change(function(){});
			}
			else
			{
				$("#billing_country").change(function() {
					// update the delivery box too now.
	
					$('#deliveryChoice').show();
	
					var myData = $(this).val();
					$.ajax({
					  type: "POST",
					  url: '/shop/getDelivery/',
					  data: 'deliveryArea='+myData,
					  success: function(data) {
						  $('#delivery_option').children().remove().end().append(data) ;
					  }
					});
				});
			}
		});
		
		if(!$("#billing_as_delivery").is(':checked')){
			$("#billing_country").change(function() {
				// update the delivery box too now.

				$('#deliveryChoice').show();

				var myData = $(this).val();
				$.ajax({
				  type: "POST",
				  url: '/shop/getDelivery/',
				  data: 'deliveryArea='+myData,
				  success: function(data) {
					  $('#delivery_option').children().remove().end().append(data) ;
				  }
				});
			});
		}

		$("#country").change(function() {
			// update the delivery box too now.
			// LINE 79
			
			$('#deliveryChoice').show();
			
			var myData = $(this).val();
			$.ajax({
			  type: "POST",
			  url: '/shop/getDelivery/',
			  data: 'deliveryArea='+myData,
			  success: function(data) {
				  
				  $('#delivery_option').children().remove().end().append(data) ;
				  
				//$('#selectBox').html(data);
			  }
			});
		});
		
		
		$("#payWithCard").click(function() {

			if($('#cardDetails').css('display') != 'block'){
				$('#cardDetails').css('display', 'block');
			}

		});

		$("#payWithPaypal").click(function() {

			if($('#cardDetails').css('display') == 'block'){
				$('#cardDetails').css('display', 'none');
			}

		});

	}
};

var Catalogue = {
	init: function() {
		$(".prodthumb").click(function() {
			$("#primary_image").attr("src", $(this).children("img").attr("src"));
		});
	}
};

