
if (!Kwo) var Kwo = {};

Kwo.Auth = {
 
  "button": null,

  "onLogin": function(args) {
    if ($(args).down("#terms_of_use") && !$("terms_of_use").checked) {
      Kwo.warn(msg_accept_terms_of_user.ucfirst());
      return false;
    }
    Kwo.Auth.button = $(args).select("input[type=submit]")[0];
    if (Kwo.Auth.button) Kwo.Auth.button.disable();
    Kwo.exec("/community/login", args, {"callback": Kwo.Auth.onLoginCallback});
    return false;
  },

  "onLoginCallback": function(res) {
    if (Kwo.hasError(res)) {
      Kwo.error(res); 
      if (Kwo.Auth.button) Kwo.Auth.button.enable();
      return ;
    }
    if ("_auth_callback" in window) {
      window["_auth_callback"].call();
    } 
    else {
      if (window.location.href.indexOf("signup") != -1) {
        Kwo.home()
      }
      else {
        Kwo.reload();
      }
    }
  },

  "onPasswordRequestCallback": function(res) {
    if (Kwo.hasError(res)) {
      Kwo.error(res); 
      return ;
    }
    Kwo.warn(res); 
    if (Kwo.Auth.button) Kwo.Auth.button.up("DIV").toggle();
  },
  
  "onPasswordRequest": function(args) {
    args = $(args).match('form') ? $(args) : $(args).up('form');
    Kwo.Auth.button = $(args).select("input[type=submit]")[0];
    Kwo.exec("/community/password.send", args, 
             {"callback": Kwo.Auth.onPasswordRequestCallback});
  }

};

Kwo.Account = {

  "timeout": null,

  "clean": function() {
    if ($("kwo-account-status")) {
      $("kwo-account-status").hide();
    }
  },
  
  "view": function(page) {
    var url = "/community/account?";
    if (page !== undefined) {
      url = url + "#" + page;
    }
    Kwo.go(url);
  },
  
  "refresh": function(msg) {
    msg = msg || "";
    Kwo.Account.setMessage(msg);
    var elt = $("kwo-account").select(".page_link")[0];
    Kwo.exec(elt.getAttribute("action"), {"page": elt.id}, 
             {"container": "kwo-account-content"});
  },

  "autoSelect": function () {
    if (window.location.hash.substr(1).length > 2) {
      var tmp = window.location.hash.substr(1).split(":");
      if (tmp.length == 1 && $(tmp[0])) {
        Kwo.Account.selectExtension(tmp[0],true);
        return;
      }
      if (tmp.length == 2 && $(tmp[0]) && $(tmp[0] + ":" + tmp[1])) {
        Kwo.Account.selectExtension(tmp[0],false);
        Kwo.Account.selectAction($(tmp[0] + ":" + tmp[1]));
        return;
      }
    }
    var ext = $$(".page_link")[0];
    Kwo.Account.selectExtension(ext,true);
    return;
  },

  "deselectExtension": function () {
    $$("#kwo-account-menu .page_link").each(function (e) { $(e).removeClassName("selected"); });
    $$("div.kwo-account-submenu").each(function (e) { $(e).hide(); });
  },
  
  "selectExtension": function (el, flag) {
    Kwo.Account.deselectExtension();
    var id = el.id;
    el = $(el);
    el.addClassName("selected");
    if (el.getAttribute("action")) {
      Kwo.Account.exec(el);
    } else if (el.id) {
      $("kwo-account-submenu-" + el.id).show();
      if (flag) {
        Kwo.Account.selectAction($$("#kwo-account-submenu-" + el.id + " .page_link")[0]);
      }
    }
  },
  
  "selectAction": function (el) {
    $$(".kwo-account-submenu .page_link").each(function (e) { $(e).removeClassName("selected"); });
    $(el).addClassName("selected");
    Kwo.Account.exec(el);
  },
  
  "exec": function (name) {
    Kwo.Account.clean();
    $("kwo-account-content").update('<img src="/app/sys/pix/throbber.gif" />');
    var elt = $(name);
    if (window.location.hash.length >= 2) {
      window.location.href = window.location.href.replace(window.location.hash, "#" + elt.id); 
    } else {
      window.location.href += "#" + elt.id;
    }
    Kwo.exec(elt.getAttribute("action"), {"page": elt.id}, 
             {"container": "kwo-account-content"});
  },
  
  "setMessage": function(msg, error) {
    error = false;
    if (typeof msg == "object" && "error" in msg) {
      if (msg["error"] >= 1) {
        error = "Oops! " + msg["result"]["msg"].join(",");
      }
      else {
        msg = msg["result"]["callback_msg"];
      }
    }
    else {
      error = error || false;
    }
    var pix = "ok.png";
    if (error != false) {
      pix = "ko.png";
      msg = error;
    }
    else {
      msg = Object.isUndefined(msg) ||  msg.empty() ? "ok" : msg;
    }
    $("kwo-account-status").show();
    $("kwo-account-status").update(msg.ucfirst() + '<img src="/app/sys/pix/bullets/' + pix + '" />'
                                  +'<div style="clear:both;"></div>');

    window.clearTimeout(Kwo.Account.timeout);
    Kwo.Account.timeout = window.setTimeout(function() { $("kwo-account-status").hide(); }, 5000);
  },
  
  "initAuthBox": function() {
    Kwo.exec("/community/widget.auth", null, 
             {"container":"kwo-auth-box"});
    return false;
  },
  
  "logout": function(args) {
    Kwo.exec("/community/logout", null, 
             {"confirm": args, "callback": function () { Kwo.home(); }});
  },
  
  "signup": function () {
    Kwo.go("/community/signup");
  }
  
};

Kwo.User = {
  
  "removeFile": function(name, msg) {
    msg = msg || "Sure ?";
    Kwo.exec("/community/user.remove_file", {"name": name}, 
             {"confirm": msg});
  },

  "storeEmail": function(args) {
    Kwo.exec("/account/community/email.store", args, 
             {"callback": Kwo.Account.setMessage});
    return false;
  },
    
  "storePassword": function(args) {
    Kwo.exec("/account/community/password.store", args, 
             {"callback": Kwo.Account.setMessage, "reset": true});
    return false;
  },

  "storeProfile": function(args) {
    Kwo.exec("/account/community/profile.store", args, 
             {"callback": Kwo.Account.setMessage});
    return false;
  },

  "composeMessage": function(recipient_id) {
    new Kwo.Dialog("/community/message.dialog", {"recipient_id": recipient_id}, {"height": 350});
  },

  "onMessageCallback": function(res) {
    if (Kwo.hasError(res)) {
      Kwo.error(res); 
      Kwo.User.button.disabled = false;
      return ;
    }
    $(Kwo.User.button.form).hide();
    $(Kwo.User.button.form).next(".kwo-message").show();
  },

  "onMessageSubmit": function(args) {
    Kwo.User.button = $(args).select("input[type=submit]")[0];
    Kwo.User.button.disabled = true;
    Kwo.dlg = Kwo.exec("/community/message.send", args, 
                       {"callback": Kwo.User.onMessageCallback});
  }

};

Kwo.Bookmark = {

  "button": null,

  "onSubmit": function(args) {
    Kwo.Bookmark.button = args;
    Kwo.Bookmark.button.disable();
    Kwo.exec("/community/bookmark.store", args, 
             {"callback": Kwo.Bookmark.onCallback});
  },

  "onCallback": function(res) {
    if (Kwo.hasError(res)) {
      Kwo.error(res); 
    }
    else {
      Kwo.getDialog().close();
    }
    Kwo.Bookmark.button.enable();
  }

};

Kwo.Inbox = {
  
  "form": null,

  "Message": { 

    "onComment": function(elt) {
      Kwo.Inbox.form = $(elt);
      Kwo.exec("/sys/item.comment", elt, 
               {"callback": Kwo.Inbox.Message.view.curry(message_id), "disable": true});
      return false;
    },

    "onDelete": function(elt) {
      var args = {"offset": window.inbox_offset};
      if (elt.tagName.toUpperCase() == "FORM") {
        args = [elt, args];
      }
      else {
        args["id"] = elt.name;
      }
      Kwo.exec("/account/community/message.delete", args, 
               {"confirm": elt, "callback": Kwo.Inbox.display});
    },

    "view": function(id, res) {
      if (!Object.isUndefined(res) && Kwo.hasError(res)) {
        Kwo.error(res);
        Kwo.Inbox.form.enable();
        return false;
      }
      Kwo.exec("/account/community/message", {"id": id}, 
               {"container": "kwo-account-content"});
    }
    
  },
  
  "display": function(res) {
    Kwo.exec("/account/community/messages", {"offset": window.inbox_offset}, 
             {"container": "kwo-account-content"});
  }

};
