$(document).ready(function() {

  function move_desc(target_id, desc_id) {
    var height = $(target_id).height();
    var current_top = $(target_id).css('top');
    var x = $(".ingredient-wrapper ." + desc_id + " .ingredient-field .option").width();
    $(target_id).css('top', '-' + (height + 10) + 'px').css('left', x+'px').css('z-index', '100');
  }
  
  function get_id(classes) {
    var temp = classes.split(" ");
    return temp[temp.length - 1];
  }
  
  $(".ingredient-sensor").hover(function() {
    var raw_id = get_id($(this).attr('class'));
    var target_id = "#description-" + raw_id;
    if ($(target_id).hasClass('ingredient-balloon') == false) {
      target_id = "#thumb-" + raw_id;
    }

    move_desc(target_id, desc_id = raw_id);
    $(target_id).css("display", "block");
  }, function() {
    var raw_id = get_id($(this).attr('class'));
    var target_id = "#description-" + raw_id;
    
    if ($(target_id).hasClass('ingredient-balloon') == false) {
      target_id = "#thumb-" + raw_id;
    }
    
    move_desc(target_id, desc_id = raw_id);
    $(target_id).css("display", "none");
  });
  
  $('#edit-field-extra-cheese-value-No-Cheese,#edit-field-extra-cheese-0-value-None,#edit-field-extra-dressing-value-No-Dressing').click(function() {
    var no_cheese = $(this);
    if (no_cheese.attr('checked') == true) {
      $('.group-step-4 .form-checkbox').removeAttr('checked');
      $('.group-extras .form-checkbox').removeAttr('checked');
      no_cheese.attr('checked', 'checked');
    }
  });
  
  $('#edit-field-free-sides-value-No-Side').click(function() {
    var no_side = $(this);
    if (no_side.attr('checked') == true) {
      $('#pid-create-salad .group-free-sides .form-checkbox').removeAttr('checked');
      no_side.attr('checked', 'checked');
    }
  });
  $('#pid-create-salad .group-free-sides .form-checkbox').click(function() {
    var free_side = $(this);
    if ($(this).attr("id") != 'edit-field-free-sides-value-No-Side' && !$("#pid-create-salad .group-free-sides #edit-field-free-sides-value-No-Side").attr("checked")) {
      $('#pid-create-salad .group-free-sides .form-checkbox').removeAttr('checked');
      free_side.attr("checked", "checked");
    }
  });
  
  
  $('.group-step-4 .form-checkbox').click(function() {
    var cheese = $(this);
    var no_cheese_pizza = $('#edit-field-extra-cheese-value-No-Cheese');
    var no_cheese_pasta = $('#edit-field-extra-cheese-0-value-None');
    var no_dressing = $('#edit-field-extra-dressing-value-No-Dressing');
    if (no_cheese_pizza.attr('checked') == true || no_cheese_pasta.attr('checked') == true || no_dressing.attr('checked') == true) {
      cheese.removeAttr('checked');
    }
  });
  
  $('#pid-create-salad .group-free-sides .form-checkbox').click(function() {
    var side = $(this);
    var node_side_salad = $('#edit-field-free-sides-value-No-Side');
    if (node_side_salad.attr('checked') == true && side.attr('id') != 'edit-field-free-sides-value-No-Side') {
      side.removeAttr('checked');
    }
  });
  
  $('#pid-create-panini #edit-field-panini-veggies-value-No-Veggies').click(function() {
    var no_veggies = $(this);
    if (no_veggies.attr('checked') == true) {
      $("#pid-create-panini .group-step-3 .form-checkbox").removeAttr('checked');
      no_veggies.attr('checked', 'checked');
    }
  });
  
  $("#pid-create-panini .group-step-3 .form-checkbox").click(function() {
    var veggie = $(this);
    var no_veggies_panini = $('#edit-field-panini-veggies-value-No-Veggies');
    if (no_veggies_panini.attr('checked') == true && veggie.attr('id') != 'edit-field-panini-veggies-value-No-Veggies') {
      veggie.removeAttr('checked');
    }
  });
  
  $('.group-extras .form-checkbox').click(function() {
    var cheese = $(this);
    var no_cheese_pizza = $('#edit-field-extra-cheese-value-No-Cheese');
    var no_cheese_pasta = $('#edit-field-extra-cheese-0-value-None');
    var no_dressing = $('#edit-field-extra-dressing-value-No-Dressing');
    if (no_cheese_pizza.attr('checked') == true && cheese.attr('id') != 'edit-field-extra-cheese-value-No-Cheese') {
      cheese.removeAttr('checked');
    }
    else if (no_cheese_pasta.attr('checked') == true && cheese.attr('id') != 'edit-field-extra-cheese-0-value-None') {
      cheese.removeAttr('checked');
    }
    else if (no_dressing.attr('checked') == true && cheese.attr('id') != 'edit-field-extra-dressing-value-No-Dressing') {
      cheese.removeAttr('checked');
    }
  });
  
  var checked_soup = 0;
  $(".group-step-5 #edit-field-sides-value-Make-it-a-Bread-Bowl").attr("disabled","disabled");
  $(".group-step-5 #edit-field-sides-value-Soup-of-the-Day").click(function() {
    if ($(this).attr("checked")) {
      checked_soup += 1;
      $(".group-step-5 #edit-field-sides-value-Make-it-a-Bread-Bowl").removeAttr("disabled");
    } else {
      checked_soup -= 1;
      if (checked_soup == 0) {
        $(".group-step-5 #edit-field-sides-value-Make-it-a-Bread-Bowl").removeAttr("checked").attr("disabled","disabled");
      }
    }
  });
  $(".group-step-5 #edit-field-sides-value-Tomato-Basil-Soup").click(function() {
    if ($(this).attr("checked")) {
      checked_soup += 1;
      $(".group-step-5 #edit-field-sides-value-Make-it-a-Bread-Bowl").removeAttr("disabled");
    } else {
      checked_soup -= 1;
      if (checked_soup == 0) {
        $(".group-step-5 #edit-field-sides-value-Make-it-a-Bread-Bowl").removeAttr("checked").attr("disabled","disabled");
      }
    }
  });
  $(".group-step-5 #edit-field-sides-value-Broccoli-Cheddar-Soup").click(function() {
    if ($(this).attr("checked")) {
      checked_soup += 1;
      $(".group-step-5 #edit-field-sides-value-Make-it-a-Bread-Bowl").removeAttr("disabled");
    } else {
      checked_soup -= 1;
      if (checked_soup == 0) {
        $(".group-step-5 #edit-field-sides-value-Make-it-a-Bread-Bowl").removeAttr("checked").attr("disabled","disabled");
      }
    }
  });
  
  var checked_lasagna = 0;
  $("#pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-Lasagna---Red-Sauce").click(function() {
    if ($(this).attr("checked")) {
      checked_lasagna = 1;
      $("#pid-create-pasta .group-step-1 .form-checkbox").removeAttr("checked").attr("disabled", "disabled");
      $("#pid-create-pasta .group-step-6 .form-checkbox").removeAttr("checked").attr("disabled", "disabled");
    } else {
      checked_lasagna -= 1;
      if (checked_lasagna == 0) {
        $("#pid-create-pasta .group-step-1 .form-checkbox").removeAttr("disabled");
        $("#pid-create-pasta .group-step-6 .form-checkbox").removeAttr("disabled");
      }
    }
  });
  $("#pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-Lasagna---Bolognese-Sauce").click(function() {
    if ($(this).attr("checked")) {
      checked_lasagna = 1;
      $("#pid-create-pasta .group-step-1 .form-checkbox").removeAttr("checked").attr("disabled", "disabled");
      $("#pid-create-pasta .group-step-6 .form-checkbox").removeAttr("checked").attr("disabled", "disabled");
    } else {
      checked_lasagna -= 1;
      if (checked_lasagna == 0) {
        $("#pid-create-pasta .group-step-1 .form-checkbox").removeAttr("disabled");
        $("#pid-create-pasta .group-step-6 .form-checkbox").removeAttr("disabled");
      }
    }
  });
  $("#pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-Lasagna---Creamy-Marinara-Sauce").click(function() {
    if ($(this).attr("checked")) {
      checked_lasagna = 1;
      $("#pid-create-pasta .group-step-1 .form-checkbox").removeAttr("checked").attr("disabled", "disabled");
      $("#pid-create-pasta .group-step-6 .form-checkbox").removeAttr("checked").attr("disabled", "disabled");
    } else {
      checked_lasagna -= 1;
      if (checked_lasagna == 0) {
        $("#pid-create-pasta .group-step-1 .form-checkbox").removeAttr("disabled");
        $("#pid-create-pasta .group-step-6 .form-checkbox").removeAttr("disabled");
      }
    }
  });
  $("#pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-Fettuccini, #pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-Spaghetti, #pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-8-grain-Rotini, #pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-8-grain-Penne, #pid-create-pasta .group-step-2 #edit-field-pasta-noodles-value-Cheese-Tortellini").click(function() {
    if ($(this).attr("checked")) {
      $("#pid-create-pasta .group-step-1 .form-checkbox").removeAttr("disabled");
      $("#pid-create-pasta .group-step-6 .form-checkbox").removeAttr("disabled");
    }
  });
  
  $("#pid-create-salad .group-extras .field-lite-less-dressing .form-checkbox").click(function() {
    if ($(this).attr("checked")) {
      $("#pid-create-salad .group-extras .field-dressing-on-side .form-checkbox").removeAttr("checked");
    }
  });
  $("#pid-create-salad .group-extras .field-dressing-on-side .form-checkbox").click(function() {
    if ($(this).attr("checked")) {
      $("#pid-create-salad .group-extras .field-lite-less-dressing .form-checkbox").removeAttr("checked");
    }
  });
  
});
