Kwo.Sponsorship = {

  onSend: function(elt) {
    elt = $(elt);
    Kwo.exec("/account/share/sponsorship.send", elt,
             {callback:Kwo.Sponsorship.onSendCallback, disable:elt});
  },
  onSendCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    Kwo.warn(res['result']['msg']);
    Kwo.go('/account/share/sponsorship.list');
  }

}

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

  initialize: function($super, elt) {
    elt = $(elt);
    this.name = "share";
    this.width = 500;
    this.height = 550;
    this.className = "layout-hbox";
    this.args = {"item_key":elt.readAttribute("data-item-key"),
                 "mode":elt.readAttribute("data-mode"),
                 "offer":(elt.readAttribute("is_offer") ? elt.readAttribute("is_offer") : 0)};
    $super(this.onDisplay, this.args);
  },
  
  onRedirect: function(service_id) {
    this.args["service_id"] = service_id;
    Kwo.go("/share/notification.redirect", this.args,
           {target:"blank"});
    return false;
  },

  onSend: function(elt) { 
    elt = $(elt);
    if ($("recipients").value.empty()) {
      $("recipients").addClassName("error");
      return ;
    }
    Kwo.exec("/share/notification.send", [elt, this.args], 
             {disable:elt, reset:elt, callback:this.onCallback});
  },

  onDisplay: function() {
    Kwo.exec("/share/notification.prompt", this.args, 
             {container:this.support});
  },
  
  onCallback: function(res) {
     if (Kwo.hasError(res)) return Kwo.error(res);
     Kwo.warn(res['result']['callback_msg']);
     Kwo.getDialog().close();
  }

});

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

  initialize: function($super, elt) {
    this.name = "importer";
    this.width = 400;
    this.height = 550;
    this.className = "layout-hbox";
    $super(this.refresh, this.args);
  },
  
  onSend: function(elt) { 
    elt = $(elt);
    if ($("recipients").value.empty()) {
      $("recipients").addClassName("error");
      return ;
    }
    Kwo.exec("/share/notification.send", [elt, this.args], 
             {disable:elt, reset:elt, callback:elt.down("DIV")});
  },

  onAuth: function(elt) {
    $("import-contact-login").removeClassName("error");   
    $("import-contact-pass").removeClassName("error");
    if ($("import-contact-login").value == "") {
      $("import-contact-login").addClassName("error"); 
    }
    if ($("import-contact-pass").value == "") {
      $("import-contact-pass").addClassName("error"); 
    }
    elt = $(elt);
    Kwo.exec("/share/import.contacts", elt, 
             {container: $("contact-import-result")});
  },
  
  onSelect: function(elt) { 
    elt = $(elt);
    var n = 0;
    elt.select("INPUT[type=checkbox]").each(function (e) {
      if (e.checked) n++;
    });
    if (n < 1) {
      alert("Veuillez sélectionner ou plusieurs contacts.");
      return ;
    }
    Kwo.go("/account/share/sponsorship.new", elt, 
             {method: "post"});
  },
  
  refresh: function() {
    Kwo.exec("/share/import", null, 
             {container: this.support});
  }

});
