
if (!Kwo) var Kwo = {};

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

  onSignUp: function(args) {
    if ($("terms_of_use") && 
        $(args).down("#terms_of_use") && 
        !$("terms_of_use").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 ("_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)) return Kwo.error(res); 
    if ($("forgot-password-box")) {
      $("forgot-password-box").hide();
    }
    Kwo.warn(res); 
  },
  
  onPasswordRequest: function(args) {
    Kwo.exec("/community/password.send", args, 
             {callback:Kwo.Auth.onPasswordRequestCallback, disable:true});
  }

};

Kwo.Account = {

  timeout: null,

  view: function(page) {
    Kwo.go("/community/account");
  },
  
  refresh: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res); 
    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;
    }
    var notice = $$(".account-notice")[0];
    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.store", args, 
             {callback:true, disable:true});
  },
  
  onPasswordSubmit: function(args) {
    Kwo.exec("password.store", args, 
             {callback:true, disable:true, reset:true});
  },
  
  onProfileSubmit: function(args) {
    Kwo.exec("profile.store", args, 
             {callback:true, disable:true});
  }

};

Kwo.Message = {
  
  onCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res); 
    $$("#message-compose-dialog .callback-message")[0].show();
  },
  
  onCompose: function(item_key) {
    new Kwo.Dialog("/community/message.compose", {"item_key": item_key}, 
                   {height: 350});
  },
  
  onSubmit: function(args) { 
    Kwo.exec("/community/message.send", args, 
             {callback: Kwo.Message.onCallback, disable:true, reset:true});
  }

}

Kwo.Bookmark = {

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

  onCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res); 
    Kwo.getDialog().close();
  }

};

Kwo.Notice = {

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

};

