function get_delivery_details(nat, res)
{
  res.load('/ordine/spedizione/?delivery_nation=' + nat[0].value);
}

$(document).ready(function()
{
  $(".HACCP_content").css("display", "none");
  $(".HACCP_description").click(function()
  {
    if ($(this).children(".HACCP_content").css("display") == "block")
    {
      $(".HACCP_content").css("display", "none");
    } else {
      $(".HACCP_content").css("display", "none");
      $(this).children(".HACCP_content").css("display", "block");
    }
  });


  $("a[href^='/carrello/aggiungi/']").bind('click', function(ev)
  {
    if (ev.detail == 1 || ev.detail == undefined) {
      $('#item_in_cart').innerHTML = "Attendere <img src=\"/media/icons/wait_16.gif\" />";
      var id = $(this).attr("id").replace('add', '');
      $('#item_in_cart').load('/carrello/aggiungi/' + id + '/');
      $('#cart').load('/carrello/box/');
    }
  });
  $("a[href^='/carrello/aggiungi/']").removeAttr('href');

  if ($("#google_address").length)
  {
    $("#google_address").html("<p>Se non siete certi di come arrivare provate a scrivere qui sotto l'indirizzo del vostro punto di partenza, otterrete delle indicazioni dettagliate.<br /><form class=\"blue_box round_box\" onsubmit=\"window.open('http://maps.google.it/maps?f=d&amp;saddr=' + this['saddr'].value + '&amp;daddr=via+giusti+90%2FD,+calenzano\','',''); return false;\"><fieldset><legend>Raggiungere la PBF</legend><input type=\"text\" value=\"\" id=\"saddr\" /> <input type=\"submit\" value=\"Crea percorso\" /><br /><label for=\"saddr\">ad esempio &quot;<em>Prato</em>&quot; oppure &quot;<em>via Buozzi 25, Empoli</em>&quot;</label></fieldset>(apre una nuova finestra in Google Maps)</form></p>");
  }

  if ($("#payement_form").length)
  {
    var eur = $("#delivery_price");
    if (eur.length)
    {
      eur.show();
      var nat = $("#id_delivery_nation");
      get_delivery_details(nat, eur);
      nat.change(function() { get_delivery_details(nat, eur); });
    }

    if ($("#id_fattura").attr('checked') == false)
    {
      $($($("#id_fattura_name").parent().get(0)).parent().get(0)).hide();
      $($($("#id_fattura_address").parent().get(0)).parent().get(0)).hide();
      $($($("#id_fattura_partita_IVA").parent().get(0)).parent().get(0)).hide();
    }
    $("#id_fattura").change(function()
    {
      if ($(this).attr('checked'))
      {
	$($($("#id_fattura_name").parent().get(0)).parent().get(0)).show();
	$($($("#id_fattura_address").parent().get(0)).parent().get(0)).show();
	$($($("#id_fattura_partita_IVA").parent().get(0)).parent().get(0)).show();
      } else {
	$($($("#id_fattura_name").parent().get(0)).parent().get(0)).fadeOut();
	$($($("#id_fattura_address").parent().get(0)).parent().get(0)).fadeOut();
	$($($("#id_fattura_partita_IVA").parent().get(0)).parent().get(0)).fadeOut();
      }
    });
  }
});
