$(document).ready(function(){
  $(".header_gray_list").hover(
    function() {
      if (!$(this).hasClass("nohover")) {
        $(this).addClass("header_blue_list");
        $(this).find(".btn_apple").addClass("btn_apple_on");
        $(this).find(".btn_clipboard").addClass("btn_clipboard_on");
      }
    },
    function() {
      if (!$(this).hasClass("nohover")) {
        $(this).removeClass("header_blue_list");
        $(this).find(".btn_apple").removeClass("btn_apple_on");
        $(this).find(".btn_clipboard").removeClass("btn_clipboard_on");
      }
    }
  );

  $(".toggler").click(function(e) {
    $(".val").each(function() {
      $(this).toggleClass("hidden");
    });
    $(".field").each(function() {
      $(this).toggleClass("hidden");
    })
    $(".buttons").each(function() {
      $(this).toggleClass("hidden");
    })
    e.preventDefault();
  });

  // $("#toggle_1").click(function(e) {
  //   $("#block_1").slideToggle(200, 'easeInOutCubic');
  //   $("#plus_minus_1").toggleClass("plus_minus_on");
  //   e.preventDefault();
  // });
  // $("#toggle_2").click(function(e) {
  //   $("#block_2").slideToggle(200, 'easeInOutCubic');
  //   $("#plus_minus_2").toggleClass("plus_minus_on");
  //   e.preventDefault();
  // });
  // $("#toggle_3").click(function(e) {
  //   $("#block_3").slideToggle(200, 'easeInOutCubic');
  //   $("#plus_minus_3").toggleClass("plus_minus_on");
  //   e.preventDefault();
  // });

  $(".toggle").click(function(e) {
    $("div#div_show").toggle();
    $("div#form_hide").toggle();
    e.preventDefault();
  });

  $(".meal_toggler").click(function(e) {
    // the $(".meal_blocker")
    var mb = $(this).parent().parent().next();
    mb.slideToggle(200, 'easeInOutCubic');
    $(this).children(".meal_plus_minuser").toggleClass("plus_minus_on");
    e.preventDefault();
  });

  $("a.toggle_block").click(function(e) {
    var block = $(this).parent().parent().next();
    var pm = $(this).find('.plus_minus');
    block.slideToggle(200, 'easeInOutCubic');
    pm.toggleClass("plus_minus_on");
    e.preventDefault();
  });

  // Cardio program /////////////////////////////////
  // When the interval/continuous option changes, close that section and open the next one.
  $(".ci").click(function(e) {
    var p = $(this).parent().parent().parent().parent();
    var pm = p.parent().find(".meal_plus_minuser");
    var v = $(this).val();
    p.slideToggle(200, 'easeInOutCubic');
    pm.toggleClass("plus_minus_on");
    // Show the next area
    if (v == 'continuous') {
      $("#continuous").slideDown(200, 'easeInOutCubic');
      $("#interval").slideUp(200, 'easeInOutCubic');
    } else if (v == 'interval') {
      $("#continuous").slideUp(200, 'easeInOutCubic');
      $("#interval").slideDown(200, 'easeInOutCubic');
    }
  });

  $("#cardio-continuous-cancel").click(function(e) {
    $("#continuous").slideToggle(200, 'easeInOutCubic');
    $("#continuous").prev().slideToggle(200, 'easeInOutCubic');
    $(".interval_continuous input:checked").attr("checked", false);
    e.preventDefault();
  });

  $("#cardio-interval-cancel").click(function(e) {
    $("#interval").slideToggle(200, 'easeInOutCubic');
    $("#interval").prev().slideToggle(200, 'easeInOutCubic');
    $(".interval_continuous input:checked").attr("checked", false);
    e.preventDefault();
  });

  $(".add-mode").click(function(e) {
    var i = $(".c_mode").children().size();
    var str = '<div class="input text"><input type="text" value="" class="text" id="c_mode_'+i+'" name="data[ClientCardio][suggestion][mode]['+i+']"/></div>';
    $(".c_mode").append(str);
    e.preventDefault();
  });

  $(".del-mode").click(function(e) {
    $(".c_mode div.input:last").remove();
    e.preventDefault();
  });
  
  // Add Client ////////////////////////////////////////////
  // Last Name
  $("input#input-last-name").focus(function() {
    if ($(this).attr("value") == "Last Name") {
      $(this).attr("value", "");
    }
  });
  $("input#input-last-name").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Last Name");
    }
  });
  // First Name
  $("#input-first-name").focus(function() {
    if ($(this).attr("value") == "First Name") {
      $(this).attr("value", "");
    }
  });
  $("#input-first-name").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "First Name");
    }
  });
  // Age
  $("#input-age").focus(function() {
    if ($(this).attr("value") == "Age") {
      $(this).attr("value", "");
    }
  });
  $("#input-age").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Age");
    }
  });
  // Gender
  $("#input-gender").focus(function() {
    if ($(this).attr("value") == "Gender") {
      $(this).attr("value", "");
    }
  });
  $("#input-gender").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Gender");
    }
  });
  // Email Address
  $("#input-email").focus(function() {
    if ($(this).attr("value") == "Email Address") {
      $(this).attr("value", "");
    }
  });
  $("#input-email").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Email Address");
    }
  });
  // Home Phone
  $("#input-home").focus(function() {
    if ($(this).attr("value") == "Home Phone") {
      $(this).attr("value", "");
    }
  });
  $("#input-home").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Home Phone");
    }
  });
  // Work Phone
  $("#input-work").focus(function() {
    if ($(this).attr("value") == "Work Phone") {
      $(this).attr("value", "");
    }
  });
  $("#input-work").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Work Phone");
    }
  });
  // Cell Phone
  $("#input-cell").focus(function() {
    if ($(this).attr("value") == "Cell Phone") {
      $(this).attr("value", "");
    }
  });
  $("#input-cell").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Cell Phone");
    }
  });

  // Edit Client ////////////////////////////////////////////
  // Toggle the editable form
  $("#edit-client").click(function(e) {
    // Edit photo link
    $('.edit_photo').toggle();
    // Name
    $('.name').toggle();
    $('.edit-name').toggle();
    // Age
    $('.age').toggle();
    $('.edit-age').toggle();
    // Gender
    $('.gender').toggle();
    $('.edit-gender').toggle();
    // Email Address
    $('.email').toggle();
    $('.edit-email').toggle();
    // Home Phone
    $('.phone-home').toggle();
    $('.edit-phone-home').toggle();
    // Work Phone
    $('.phone-work').toggle();
    $('.edit-phone-work').toggle();
    // Cell Phone
    $('.phone-cell').toggle();
    $('.edit-phone-cell').toggle();
    // Password
    $('.edit-password').toggle();
    // Submit button
    if ($("#edit-client").hasClass("hidden")) {
      $("#edit-client").removeClass("hidden");
      $("#edit-client").show();
      $("#edit-client-submit").hide();
    } else {
      $("#edit-client-submit").removeClass("hidden");
      $("#edit-client-submit").show();
      $("#edit-client").hide();
    }
    // $("#edit-client").toggle();
    // $("#edit-client-submit").toggle();

    e.preventDefault();
  });
  
  $("#edit-client-submit").click(function(e) {
    // Switch values to nothing if the values are the defaults
    if ($('#input-last-name').attr("value") == "Last Name") { $("#input-last-name").attr("value", ""); }
    if ($('#input-first-name').attr("value") == "First Name") { $("#input-first-name").attr("value", ""); }
    if ($('#input-email').attr("value") == "Email Address") { $("#input-email").attr("value", ""); }
    if ($('#input-age').attr("value") == "Age") { $("#input-age").attr("value", ""); }
    if ($('#input-home').attr("value") == "Home Phone") { $("#input-age").attr("value", ""); }
    if ($('#input-work').attr("value") == "Work Phone") { $("#input-age").attr("value", ""); }
    if ($('#input-cell').attr("value") == "Cell Phone") { $("#input-age").attr("value", ""); }
    // Submit the form
    $("#client-profile-form").submit();
    e.preventDefault();
  });

  // Get the url.
  var url = String(self.document.location);
  // If we're on the meal log history page and we have a # in the url, we should open up the date UI
  if (url.indexOf("/clients/meal_log_history") !== 0 || url.indexOf("/clients/meal-log/history") !== 0) {
    var hash = String(self.document.location.hash.substring(1));
    if (hash.length > 0) {
      // Open up the date UI
      $("#"+hash).show();
      $("#meal_plus_minus_"+hash).addClass("plus_minus_on");
    }
  }

});

// Avatar Functionality ////////////////////////////////////////////
function avatar_remove() {
  $("#avatar").attr("src", "/img/default_mug.jpg");
}

function avatar_refresh() {
  var i = $("#client-id").html();
  $("#avatar").attr("src", "/files/avatars/"+i+".jpg");
}