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());
		var product_amount = parseInt($("#product_amount").val());
	//check cookies for actions
		var is_action = false;
			  
		 if (document.cookie && document.cookie != '') {
			 if ($.cookie('slovart_counter') == '3') { 
			      var cookies = document.cookie.split(';');
			      var cookies_slovart = new Array();
			      for (var i = 0; i < cookies.length; i++) {
			          var cookie = jQuery.trim(cookies[i]);
			          // Does this cookie string begin with the name we want?
			          if (cookie.substring(0, 16) == ('slovart_product_')) {
		        	  //split to ID and amount
			        	product = cookie.split('=');
		        	  //get product ID
			            productid = product['0'].split('_product_');
			            console.log(productid['1']);
			            console.log(product['1']);
		    			$.getJSON(eshop_add_product_to_cart, { product_id: productid['1'], product_amount: product['1'] }, function(data){
		    				if (data.result == "ok")
		    				{// The product was added
		    					// Set amount of products
		    					if (i == 2) {
			    					$(".eshop-product-amount").html(data.products_amount);
			    					$(".eshop-product-amount-text").html(data.products_amount_text);
		    					}
		    				}
		    			});		                
		                is_action = true;
			          }
			      }
			      //console.log('redirect');
			      setTimeout('window.location = $("input[name=\'eshop_shopping_cart\']").val()', 2000);
			 }
		 }

		if (product_id > 0 && !is_action)
		{// Add product to the cart
			$.getJSON(eshop_add_product_to_cart, { product_id: product_id, product_amount: product_amount }, 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(){
		$("#eshop_cart").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());
		$('#eshop_cart').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();
	});
});

