if (!Kwo) var Kwo = {};

Kwo.Student = {

  onProfileSubmit: function(args) {
    Kwo.exec("student.store", args, 
             {disable:true, callback:true})
  },

  onSearch: function(args, offset) {
    offset = offset || 0;
    Kwo.exec("student.list", [args, {"offset": offset}], 
             {container:$("results")});
  }

}

Kwo.School = {

  onSuggest: function() {
    new Kwo.Dialog("/school.suggest", null, 
                   {height: 350});
  },

  onSubmission: function(args) { 
    Kwo.exec("/yearbook/school.send", args,
             {callback:Kwo.School.onSubmissionCallback});
  },

  onSubmissionCallback: function(res) {
    if (Kwo.hasError(res)) {
      return Kwo.error(res);
    }
    $("school-form").addClassName("notification").update(res["result"]["callback_msg"]);
  },

  onAdd: function(elt) {
    elt = $(elt);
    if ($F(elt) == "0") {
      $("city-select").update().disable();
      $("school-select").update().disable();
      return ;
    }
    if ($(elt).id == "department-select") {
      $("school-select").update($H({"0": "-"})).disable();
      Kwo.exec("school.select.cities", elt.form,
               {callback: Kwo.School.onCityCallback.curry($("city-select"))});
      return ;
    }
    if ($(elt).id == "city-select") {
      $("school-select").update($H({"0": "-"})).disable();
      Kwo.exec("school.select.schools", elt.form,
               {callback: Kwo.School.onCityCallback.curry($("school-select"))});
      return ;
    }
  },

  onCityCallback: function(elt, res) {
    var options = "";
    $H(res["result"]["datas"]).each(function (pair) {
      options += '<option value="' + pair.key + '">' + pair.value + '</option>';
    })
    elt.enable().update(options);
  }

};

Kwo.Education = {

  onDelete: function(school_id) {
    Kwo.exec("education.delete", {"school_id":school_id},
             {disable:true, callback:true, confirm:true});
  },

  onEdit: function(school_id) {
    new Kwo.Dialog("education.edit", {"school_id":school_id}, 
                   {"height": 350});
  },

  onSubmit: function(args) {
    var opts = {callback: true};
    if (args.id != "education-new") { 
      opts["disable"] = true;
    }
    Kwo.exec("education.store", args, opts);
  }

};

