
if (!Kwo) var Kwo = {};

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

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

  onAuthCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    window["_user_id"] = res["result"]["user_id"];
    if (Kwo.getDialog("auth")) {
      var dialog = Kwo.getDialog("auth");
      dialog.onCallback();
      dialog.close();
    }
    else if ("onAuthCallback" in window) {
      window.onAuthCallback();
    }
    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('form').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, opts) {
   this.name = "auth";
   this.width = 850;
   this.height = 480;
   this.opts = opts || {};
   if ("callback" in this.opts) {
     this.onCallback = function () {
       if (Object.isString(this.opts["callback"])) {
         Kwo.go(this.opts["callback"]);
       }
       else {
         this.opts["callback"].call();
//         this.close();
       }
     }
   }
   else {
     this.onCallback = function () { 
       if ("onAuthCallback" in window) {
         window.onAuthCallback();
       }
//       this.close(); 
     }
   }
   $super("/community/signup");
 }

});

Kwo.Account = {

  timeout: null,

  onEnter: function(page) {
    if (!Kwo.isAuth()) {
      new Kwo.Class.Auth({callback: "/community/account"});
      return ;
    }
    Kwo.go("/community/account");
  },
    
  onLeave: function(elt) {
    Kwo.exec("/community/user.signout", null,
             {callback: Kwo.home,
              confirm: elt});
  }, 

  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 = "Erreur ! " + 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(notice.hide.bind(notice), 5000);
  },
  
  initAuthBox: function() {
    Kwo.exec("/community/widget.auth", null, 
             {container: "kwo-auth-box"});
  }
  
};

Kwo.User = {
  
  onAvatarSet: function(file) {
    Kwo.exec("/account/community/avatar.save", {"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-item-key")};
    $super("/community/message.compose");
  },

  onSubmit: function(elt) {
    elt = $(elt);
    Kwo.exec("/community/message.send", [this.args, elt],
             {callback: elt, disable: elt});
  }

});

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

  initialize: function($super, elt) {
    this.name = "favorite";
    this.layout = "hbox";
    this.args = {item_key: $(elt).readAttribute("data-item-key"),
                 url: $(elt).readAttribute("data-url")};
    this.width = 400;
    this.height = 320;
    $super("/community/favorite.prompt");
  },

  onSave: function(elt) {
    if (!Kwo.isAuth()) {
      var auth = new Kwo.Class.Auth();
      auth.onCallback = this.onSave.bind(this).curry(elt);
      return ;
    }
    elt = $(elt);
    Kwo.exec("/community/favorite.save", [this.args, elt], 
             {callback: this.close(), disable: elt});
  }

});

Kwo.Favorite = {

  onAlertUnset: function(elt) {
    elt = $(elt);
    Kwo.exec("/community/favorite.alert.unset", 
             {id: elt.up("TR").readAttribute("data-id")},
             {confirm: elt, callback: Kwo.Favorite.onCallback});
  },

  onCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    Kwo.reload();
  },

  onDelete: function(elt) {
    elt = $(elt);
    Kwo.exec("/community/favorite.delete", 
             {id: elt.up("TR").readAttribute("data-id")},
             {confirm: elt, callback: Kwo.Favorite.onCallback});
  }

}

Kwo.Notice = {

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

};


