
if (!Kwo) var Kwo = {};

Kwo.Auth = {
 
  onSignIn: function(args) {
    Kwo.exec("/community/user.signin", args, 
             {callback: Kwo.Auth.onAuthCallback, disable: true});
  },

  onSignUp: function(args) {
    var input = $(args).down(".terms_of_use");
    if (!Object.isUndefined(input) && !input.checked) {
      return Kwo.warn(msg_accept_terms_of_user.ucfirst());
    }
    Kwo.exec("/community/user.signup", args, 
             {callback: Kwo.Auth.onAuthCallback, disable: true});
  },

  onAuthCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    window["_user_id"] = res["result"]["user_id"];
    if (Kwo.getDialog("auth")) {
      Kwo.getDialog("auth").onCallback();
    }
    else if ("_auth_callback" in window) {
      window["_auth_callback"].call();
    } 
    else if (window.location.href.indexOf("sign") != -1) {
      Kwo.home()
    }
    else {
      Kwo.reload();
    }
  },

  onPasswordRequestCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res); 
    this.hide().previous().show();
    Kwo.warn(res); 
  },
  
  onPasswordRequest: function(args) {
    Kwo.exec("/community/password.send", args, 
             {callback: Kwo.Auth.onPasswordRequestCallback.bind($(args)), 
              disable: true});
  }

};

Kwo.Class.Auth = Class.create(Kwo.Dialog, {

  initialize: function($super) {
    this.name = "auth";
    this.width = 800;
    this.height = 500;
    $super("/community/signup");
  },

  onCallback: function() {
    this.close();
  }

});

Kwo.Account = {

  timeout: null,

  view: function(page) {
    Kwo.go("/community/account");
  },
  
  refresh: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res); 
    if ("result" in res && "callback_url" in res["result"]) {
      return Kwo.go(res["result"]["callback_url"]);
    }
    Kwo.reload();
  },
  
  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;
    }
    if (!$("account-notice")) return ;
    var notice = $("account-notice");
    notice.show(); 
    notice.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 () { notice.hide() }, 5000);
  },
  
  initAuthBox: function() {
    Kwo.exec("/community/widget.auth", null, 
             {container:"kwo-auth-box"});
  },
  
  logout: function(args) {
    Kwo.exec("/community/user.signout", null, 
             {callback:Kwo.home});
  },
  
  signup: function() {
    Kwo.go("/community/account");
  }
  
};

Kwo.User = {
  
  onAvatarSet: function(file) {
    Kwo.exec("/account/community/avatar.set", {"image": file},
             {callback: Kwo.User.onAvatarCallback});
  },

  onAvatarCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    Kwo.exec("/account/community/avatar.edit", null, 
             {container: $("avatar-box")});
  },

  onEmailSubmit: function(args) {
    Kwo.exec("email.save", args, 
             {callback:true, disable:true});
  },

  onEmailConfirm: function(elt) {
    Kwo.exec("/community/email.confirm.request", null,
             {callback: $(elt).up("div")});
  },
  
  onPasswordSubmit: function(args) {
    Kwo.exec("password.save", args, 
             {callback:true, disable:true, reset:true});
    return true;
  },
  
  onProfileSubmit: function(args) {
    Kwo.exec("profile.save", args, 
             {callback:true, disable:true});
  }

};

Kwo.Composer.Message = Class.create(Kwo.Dialog, {

  initialize: function($super, elt) {
    this.name = "abuse";
    this.layout = "hbox";
    this.width = 600;
    this.height = 400;
    this.args = {"item_key": $(elt).readAttribute("data")};
    $super("/community/message.compose");
  },

  onSubmit: function(elt) {
    Kwo.exec("/community/message.send", $(elt),
             {callback: $(elt), disable:true});
  }

});

Kwo.Composer.Bookmark = Class.create(Kwo.Dialog, {

  initialize: function($super, elt) {
    this.name = "bookmark";
    this.layout = "hbox";
    this.args = {"item_key": $(elt).readAttribute("data")};
    this.width = 500;
    this.height = 300;
    $super("/community/bookmark.compose");
  },

  onSubmit: function(elt) {
    Kwo.exec("/community/bookmark.save", $(elt), 
             {callback: $(elt), disable:true});
  }

});


Kwo.Notice = {

  onSubmit: function(args) {
    Kwo.exec("notices.set", args, 
             {callback:true, disable:true});
  }

};

