
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];
    Kwo.Auth.button.disable();
    Kwo.exec("/community/login", args, {"callback": Kwo.Auth.onLoginCallback});
    return false;
  },

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

  "onPasswordCallback": function(h) {
    Kwo.error(h["result"]["msg"]); 
    if (h["error"] >= 1) {
      return ;
    }
    Kwo.Auth.button.up("DIV").toggle();
  },

  "onPasswordRequest": function(args) {
    Kwo.Auth.button = $(args).select("input[type=submit]")[0];
    Kwo.exec("/community/password.send", args, {"callback": Kwo.Auth.onPasswordCallback});
    return false;
  }

};

Kwo.Account = {

  "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) {
    Kwo.Account.setMessage(msg);
    var elt = $("kwo-account").select(".page_link")[0];
    Kwo.exec(elt.getAttribute("action"), {"page": elt.id}, {"container": "kwo-account-content"});
    setTimeout(function() { $("kwo-account-status").hide(); }, 30000);
  },

  "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 = error || false;
    msg = (msg === undefined) ? "ok" : msg;
    $("kwo-account-status").show();
    $("kwo-account-status").update('<img src="/app/sys/pix/ok.gif" id="emblem" />'+msg.ucfirst()
                                +'<div style="clear:both;"></div>');
    setTimeout(function() { $("kwo-account-status").hide(); }, 30000);
  },

  
  "initAuthBox": function() {
    Kwo.exec("/community/widget.auth", null, {"container":"kwo-auth-box"});
    return false;
  },

  
  "logout": function(args) {
    Kwo.exec("/community/logout", null, {"confirm": args});
  },



  
  "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, {"reset": true});
  },
    
  "storePassword": function(args) {
    Kwo.exec("/account/community/password.store", args, {"reset": true});
  },

  "storeProfile": function(args) {
    Kwo.exec("/account/community/profile.store", args);
  },

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

  "onMessageCallback": function(h) {
    if (h["error"] >= 1) {
      Kwo.error(h["result"]["msg"]); 
      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.disabled = true;
    Kwo.exec("/community/bookmark.store", 
             args, 
             {"callback": Kwo.Bookmark.onCallback});
  },

  "onCallback": function(h) {
    if (h["error"] >= 1) {
      Kwo.error(h["result"]["msg"]); 
    }
    else {
      Kwo.go(h["result"]["url"]);
    }
    Kwo.Bookmark.button.disabled = false;
  }

};
