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());
		var product_amount = parseInt($(this).parent().find("input[name='product_amount']").val());
		var redirect_to_cart = parseInt($(this).parent().find("input[name='redirect_to_cart']").val());
		
		if (product_id > 0)
		{// 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);
					
					if (redirect_to_cart == true)
					{
						// 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();
						}
					}
				}
			});
		}
	});
	
	// Add product to the cart by url parameter
	var product_id = $.getUrlVar('to_cart');
	var eshop_add_product_to_cart = $("input[name='eshop_add_product_to_cart']").val();
	var product_amount = 1;
	
	if (product_id > 0)
	{// 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(){
		$(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;
	});
	
	// Click on the link to submit form
	$(".recount").click(function(){
		$(".eshop_shopping_cart_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").css("display", "none");
		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").css("display", "table-row");
			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").css("display", "table-row");
			}
		}
		else
		{// The transport type is not selected
			$("label[for='payment_type']").parents("tr").css("display", "none");
			$(".eshop-payment-type-errors").parents("tr").css("display", "none");
		}
	}).change();
	
	// orders list
	$('.order_detail').click(function() {
		$(this).parent().next().toggle();
		return false;
	}).next().hide();
	
	$('.cennik-dopravy').boxy({title: "Cenník dopravy"});
	
	$("input[name='transport_type']").change(function(){
		if ($("input:checked[name='transport_type']").val() == 'osobne')
		{
			$('#shops').attr('disabled', false);
		}
		else
		{
			$('#shops').attr('disabled', true);
		}
	}).change();
});

// Read a page's GET URL variables and return them as an associative array.
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

