var ESHOP_COOKIE_NAME = 'eshop_show_alert';

$(document).ready(function(){
	// Change flag of showing alert
	$("input[name='eshop_show_alert']").change(function(){
		// Set status
		var eshop_show_alert = ($("input[name='eshop_show_alert']").attr("checked") ? "false" : "true");
		$.cookie(ESHOP_COOKIE_NAME, eshop_show_alert);
	});
	
	// Click on the button "Add to cart"
	$("input.basket_button").click(function(){
		var eshop_add_product_to_cart = $("input[name='eshop_add_product_to_cart']").val();
		var product_id = parseInt($(this).parent().find("input[name='product_id']").val());
		
		if (product_id > 0)
		{// Add product to the cart
			$.getJSON(eshop_add_product_to_cart, { product_id: product_id }, function(data){
				if (data.result == "ok")
				{// The product was added
					// Set amount of products
					$(".eshop-product-amount").html(data.products_amount);
					$(".eshop-product-amount-text").html(data.products_amount_text);
					
					// Show alert
					if ((null === $.cookie(ESHOP_COOKIE_NAME)) || ($.cookie(ESHOP_COOKIE_NAME) == "true"))
					{// The alert should by shown
						tb_show(null, "#TB_inline?height=200&amp;width=240&amp;inlineId=basket_nadler&amp;modal=true", false);
					}
					else
					{// The alert should not by shown
						window.location = $("input[name='eshop_shopping_cart']").val();
					}
				}
			});
		}
	});
	
	// Click on the link to submit form
	$(".eshop-submit-form").click(function(){
		$(this).parents("form").submit();
		return false;
	});
	
	// Click on the link to go to next step in shopping cart
	$(".eshop-shoping-cart-next").click(function(){
		$("input[name='redirect']").val($("input[name='eshop_next_step_url']").val());
		$(this).parents("form").submit();
		return false;
	});
	
	// Hide company fields
	$("input[name='customer_type']").change(function(){
		if ($("input:checked[name='customer_type']").val() == "person")
		{ // It is a person
			$(".eshop-person-fields").show();
			$(".eshop-body-corporate-fields").hide();
		}
		else
		{ // It is a company
			$(".eshop-person-fields").hide();
			$(".eshop-body-corporate-fields").show();
		}
	}).change();
	$("input[name='customer_type']").click(function(){
		$("input[name='customer_type']").change();
	});
	
	// Hide delivery address fields
	$("input[name='delivery_address_choice']").change(function(){
		if ($("input:checked[name='delivery_address_choice']").val() == "identical")
		{ // The addresses are identical 
			$(".eshop-delivery-address").hide();
		}
		else
		{
			$(".eshop-delivery-address").show();
		}
	}).change();
	$("input[name='delivery_address_choice']").click(function(){
		$("input[name='delivery_address_choice']").change();
	});
	
	// Confirm order
	$(".eshop-confirm-order").click(function(){
		$(".eshop-confirm-order-form").submit();
		return false;
	});
	
	// Select delivery type
	$("input[name='transport_type']").change(function(){
		$("input[name='payment_type']").parents("tr").hide();
		var $selected_payment_types = $(".transport_type_payments-" + $("input:checked[name='transport_type']").val());
		if ($selected_payment_types.length > 0)
		{// The transport type is selected
			$("label[for='payment_type']").parents("tr").show();
			var selected_payment_types = $selected_payment_types.val().split(";");
			for(var i = 0; i < selected_payment_types.length; i++)
			{
				$("input[name='payment_type'][value='" + selected_payment_types[i] + "']").parents("tr").show();
			}
		}
		else
		{// The transport type is not selected
			$("label[for='payment_type']").parents("tr").hide();
			$(".eshop-payment-type-errors").parents("tr").hide();
		}
	}).change();
	$("input[name='transport_type']").click(function(){
		$("input[name='transport_type']").change();
	});
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
});

