
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");
    $('error-captcha').hide();
    if (!Object.isUndefined(input) && !input.checked) {
      //return Kwo.warn(msg_accept_terms_of_user.ucfirst());
      $('error-tos').show();
      return ;
    }
    Kwo.exec("/community/user.signup", args,
             {callback: Kwo.Auth.onAuthCallback, disable: true});
  },

  onAuthCallback: function(res) {
    if (Kwo.hasError(res)) {
      if (res["error"] == 1215) {
        $('error-email-inv').show();
        return ;
      }
      if (res["error"] == 1213) {
        $('error-email-dup').show();
        return ;
      }
      if (res["error"] == 1212) {
        $('error-captcha').show();
        return ;
      }
      if (res["error"] == 1214) {
        $('error-pwd-diff').show();
        return ;
      }
      $('auth-failed').show();
      return ;
    }
    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 {
        $('signup-panel').hide();
        $('signup-validation').innerHTML = '<img src="http://nodes.reactivpub.fr/scripts/tracking.php?params=1250|4&track='+$('email-login').value+'" width="1" height="1" marginwidth="0" marginheight="0" />';
        $('signup-congratulations').show();
        //setTimeout("Kwo.reload()",2000);
      }
    },
    
    closeSignup: function() {
  	  Kwo.reload();
    },

  onPasswordRequestCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    //this.hide().previous().show();
    //Kwo.warn(res);
    $('pwd-request-ok').show();
  },

  onPasswordRequest: function(args) {
    if($('pwd-request').value.blank() || UAD.Form.checkElt($('pwd-request'))) {
      $('pwd-request').next('div.form-error', 0).show();
      $('pwd-request').addClassName('error');
      return false;
    }
    Kwo.exec("/community/password.send", args,
             {callback: Kwo.Auth.onPasswordRequestCallback.bind($(args)),
              disable: true});
  },

  onSwitchPage: function() {
    var elt = $('email-check');
    var email = elt.value;

    if (UAD.Form.checkElt(elt) || email.blank()) {
      elt.next('div.form-error', 0).update("Email manquant ou incorrect").show();
      elt.addClassName('error');
      return false;
    }
    else {
      elt.next('div.form-error', 0).hide();
      elt.removeClassName('error');
    }
    Kwo.exec("/community/user.check", {"email": email},
             {callback: function(res) {
               if (Kwo.hasError(res)) {
                 elt.next('div.form-error', 0).update("Vous êtes déjà inscrit, merci de vous identifier.").show();
                 elt.addClassName('error');
                 return ;
               }
               $('step1').hide();
               $('signup-form').show();
               $("email-login").value = email;
             }})
  }
};

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

 initialize: function($super, opts) {
   this.name = "auth";
   this.width = 520;
   this.height = 560;
   opts = opts || {};
   if ("callback" in opts) {
     this.onCallback = function () {
       if (Object.isString()) {
         Kwo.go(opts["callback"]);
       }
       else {
         opts["callback"].call();
         this.close();
       }
     }
   }
   else {
     this.onCallback = function () {
       UAD.Panier.updateWidget();
       this.close();
       if ($("bookmark-form")) {
         Kwo.getDialog("bookmark").onSubmit($("bookmark-form"));
       }
     }
   }
   $super("/community/signup");
 }

});

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.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")};
    $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-item-key"),
                 "url": $(elt).readAttribute("data-url")};
    this.width = 500;
    this.height = 300;
    $super("/community/bookmark.compose");
  },

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

});

Kwo.Bookmark = {

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

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

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

}


Kwo.Notice = {

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

};

