// Namespace declarations
if (mw == null || typeof(mw) != "object") {
  var mw = new Object();
}
if (mw.rest == null || typeof(mw.rest) != "object") {
  mw.rest = new Object();
}


mw.rest = {

  session_cookie: "v3_sesh",
  username_cookie: "em",

  get_username: function () {
    return mw.utils.get_cookie_var(mw.rest.username_cookie);
  },


  do_logout: function () {
    var data = "";
    var login_url = "/rest/v3/services/weblogout";
    $.ajax({
      url: login_url,
      type: "POST",
      async: false,
      data: "data=" + data,
      success: function (data, textStatus, XMLHttpRequest) {
        window.location = "http://grano.la";
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $(error_div).html("<p>Incorrect username or password</p>");
      }
    })
  },


  // TRY LOGIN
  // We deliberately do not use the remote_method function for this call because this is a special case
  // remote_method fail to auth triggers the login box 
  do_login: function (user, pass, error_div) {
    var data = '{"username":"' + user + '","password":"' + pass + '"}';
    var url = "/rest/v3/services/weblogin";
    $.ajax({
      url: "https://"+document.domain+url,
      type: "POST",
      async: false,
      data: "data=" + data,
      success: function (data, textStatus, XMLHttpRequest) {
        window.location.replace(encodeURI(window.location.pathname));
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $(error_div).html("<p>Incorrect username or password</p>");
      }
    })
  }




} // end namespace

