// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// Disable the enter key from submitting a form on a specific element.
// Used on autocomplete fields
function noEnter(e) {
  var key;
  if(window.event)
    key = window.event.keyCode;
  else
    key = e.which;
  return key != 13;
}



// Social controls: Sharing/Liking

function likeObject(object_type, object_id) {
  url = '/like/' + object_type + '/' + object_id;
  success = "replaceWithCheckBox('like_" + object_type + "_" + object_id + "');";
  fail = "";
  sendAjax(url, success, fail);
  return false;
}

function shareObject(object_type, object_id) {
  url = '/share/' + object_type + '/' + object_id;
  success = "";
  fail = "";
  sendAjax(url, success, fail);
  return false;
}

function replaceWithCheckBox(elem_id) { 
  $(elem_id).innerHTML = "<img src='/images/icons/liked_action_icon.gif' alt='OK' />";
}

function sendAjax(url, on_success, on_fail) {
  new Ajax.Request(url, {
    method: 'get',
    parameters: { authenticity_token: AUTH_TOKEN },
    onSuccess: function(transport) {
      if (transport.responseText == "ok") {
        eval(on_success);
      } else {
        eval(on_fail);
      }
    }
  });
}