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"
	$("a.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);
					
					// 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(){
		$(".eshop-person-fields").css("display", ($("input:checked[name='customer_type']").val() == "person" ? "table-row" : "none"));
		$(".eshop-body-corporate-fields").css("display", ($("input:checked[name='customer_type']").val() != "person" ? "table-row" : "none"));
	}).change();
	
	// Hide delivery address fields
	$("input[name='delivery_address_choice']").change(function(){
		$(".eshop-delivery-address").css("display", ($("input:checked[name='delivery_address_choice']").val() != "identical" ? "table-row" : "none"));
	}).change();
	//$(".eshop-delivery-address").css("display", ($("input[name='delivery_address_choice'][checked]").val() == "identical" ? "none" : "table-row"));
	
	// 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();
	});

	
	$('#searchInput').blur(function(){
		
		if ($('#searchInput').val() == '')
		{
			$('#empty').val('1');
			$('#searchInput').val('sem napíšte autora alebo názov knihy, prípadne ISBN');
			$('#searchInput').css({'color':'#D5D5D5'});
		};
	});
			
	$('#searchInput').click(function(){
		if ($('#empty').val() == 1)
		{
			$('#searchInput').val('');
			$('#empty').val('0');
			$('#searchInput').css({'color':'black'});
		}
	}).change();
	
	$('#addComment').click(function(){
		$('#addCommentForm').show('slow');
	}).change();
});

jQuery(document).ready(function(){
	$('.accordion .detail').click(function() {
		$(this).parent().next().toggle();
		return false;
	}).next().hide();
});


