


// *****************************       <=== howdy
// ajax code goes in:
//
// hakuna_server_add_user()
// hakuna_server_remove_user()
//
// *****************************


$(document).ready(function() {

  $('.hakuna_user_control').click(function() {
    hakuna_clicked();
    return false;
  });

  $('.hakuna-box-close').click(function() {
    hakuna_box_close($(this));
    return false;
  });

});



function hakuna_clicked() {

  // ORDER of what happens:
  //   (1) both boxes are hidden quickly
  //   (2) the correct box slowly opens using a slidedown animation
  //   (3) the other box does a background quick hide (in case of fast user clicking)
  // this is all complexity due to the sliding animation

  // (1)
  hakuna_quick_hide_box_added();
  hakuna_quick_hide_box_removed();

  if (hakuna_is_add_button()) {
    hakuna_slow_show_box_added();
    hakuna_switch_button_to_remove();
    hakuna_server_add_user();
    return;
  }

  if (hakuna_is_remove_button()) {
    hakuna_slow_show_box_removed();
    hakuna_switch_button_to_add();
    hakuna_server_remove_user();
    return;
  }

}


function hakuna_slow_show_box_added() {
  // (2) & (3)
  $('#hakuna-box-added').slideDown('slow', hakuna_quick_hide_box_removed);
}
function hakuna_slow_show_box_removed() {
  // (2) & (3)
  $('#hakuna-box-removed').slideDown('slow', hakuna_quick_hide_box_added);
}

function hakuna_quick_hide_box_removed() {
  $('#hakuna-box-removed').hide();
}
function hakuna_quick_hide_box_added() {
  $('#hakuna-box-added').hide();
}

function hakuna_box_close(x) {
  x.parents('.hakuna-box').hide();
}

function hakuna_is_add_button() {
  return $('.hakuna_user_control').hasClass('hakuna-add');
}
function hakuna_is_remove_button() {
  return $('.hakuna_user_control').hasClass('hakuna-remove');
}

function hakuna_switch_button_to_remove() {
  $('.hakuna_user_control').removeClass('hakuna-add');
  $('.hakuna_user_control').addClass('hakuna-remove');
}
function hakuna_switch_button_to_add() {
  $('.hakuna_user_control').removeClass('hakuna-remove');
  $('.hakuna_user_control').addClass('hakuna-add');
}

function hakuna_server_add_user() {
    AddUserToContacts(CurOwnerID);
}

function hakuna_server_remove_user() {
    RemoveUserFromContacts(CurOwnerID);
}


