function getObjectsoortTitle(strObjecttypeSafe) {
  var conv = $H({
bbdin: "Bosbungalow Dinkel",
bbijs: "Bosbungalow IJssel",
bvree: "Bosvilla Reest",
bvreg: "Bosvilla Regge",
bvvec: "Bosvilla Vecht",
bvvel: "Bosvilla Vecht Luxe",
bcswi: "Boschalet Swifter",
bchou: "Boschalet Houtlander",
bcsa6: "Boschalet Sallander (6p)",
bcsa4: "Boschalet Sallander Luxe (4p)",
bcsam: "Boschalet Sallander MIVA",
kam: "Kampeerplaats"
  });
  return conv.get(strObjecttypeSafe.toLowerCase()) || strObjecttypeSafe;
}


var LastMinutes = Class.create({
  initialize: function() {
    this.options = Object.extend({
      // defaults
      autoload: true,
      find: {}
    }, arguments[0] || {});

    this.containerElement = $(this.options.container);
    this.contentElement = this.containerElement.down(".lastminutesContent");
    this.boxElement = this.containerElement.down(".lastminutesBox");
    this.objectsoortElement = $(this.options.find.objectsoort);
    this.duurElement = $(this.options.find.duur);
    this.maandElement = $(this.options.find.maand);

    this.objectsoortElement.observe("change", this.updateWithEffect.bind(this));
    this.duurElement.observe("change", this.updateWithEffect.bind(this));
    this.maandElement.observe("change", this.updateWithEffect.bind(this));

    if (this.options.autoload) {
      this.getJSON();
    }
  },

  getJSON: function() {
    this.contentElement.update('')
    this.containerElement.addClassName("waiting");
    new Ajax.Request(this.options.src, {
      method: "post",
      parameters: {
        vbid: this.options.vbid
      },
      requestHeaders: { Accept: "application/json" },
      evalJSON: "force",
      onSuccess: function(transport) {
        var json = transport.responseJSON;
        //console.debug(this.options.src + ": ", json);
        this.records = $A(json);
        this.records.each(function(record) {
          this.copyTemplate(record);
        }.bind(this));
        this._bLoaded = true;
        this.update();
        this.containerElement.removeClassName("waiting");
      }.bind(this)
    });
  },

  copyTemplate: function(record) {
    var element = this.containerElement.down(".lastminuteItemTemplate").cloneNode(true);
    record.objectsoort_title = getObjectsoortTitle(record.objectsoort);
    element.update( element.innerHTML.interpolate(record) );
    if (record.objectsoort_safe == "kampeerplaats") {
      element.down(".lastminuteButtonInfo").remove();
    }
    this.contentElement.insert(element.down());
  },

  updateWithEffect: function() {
    var duration = 0.5;
    new Effect.Fade(this.boxElement, {
      duration: duration,
      afterFinish: function() {
        this.update();
        new Effect.Appear(this.boxElement, {
          duration: duration
        });
      }.bind(this)
    });
  },

  find: function() {
    var options = arguments[0] || {};
    return this.records.findAll(function(record) {
      if (options.objectsoort && !(record.objectsoort_safe == options.objectsoort)) return false;
      if (options.duur        && !(record.duur && record.duur.include(options.duur))) return false;
      if (options.maand       && !(record.maanden && record.maanden.include(options.maand))) return false;
      return true;
    });
  },

  update: function() {
    if (!this._bLoaded) {
      this.getJSON();
      return;
    }
  
    var results = this.find({
      objectsoort: this.objectsoortElement.getValue(),
      duur: this.duurElement.getValue(),
      maand: this.maandElement.getValue()
    });

    // Show/hide results
    this.records.each(function(record) {
      Element[ results.include(record) ? "show" : "hide" ]("lastminuteItem_"+record.id);
    });

    // Update selecte elements (show/hide options)
    this.updateSelectElements();

    // Update result text
    this.updateResultText(results.size());

    // Show content
    if (!this.containerElement.visible()) {
      new Effect.SlideDown(this.containerElement, { duration: 0.25 });
    }

    if (this.options.onupdate) {
      this.options.onupdate();
    }
  },

  updateSelectElements: function() {
    // Update 'objectsoort' pulldown
    var hObjectsoort = new Hash();
    this.find({
      objectsoort: null,
      duur: this.duurElement.getValue(),
      maand: this.maandElement.getValue()
    }).each(function(record) {
      var key = record.objectsoort_safe;
      hObjectsoort.set(key, (hObjectsoort.get(key) || 0) + 1);
    });
    this.updateOptions(this.objectsoortElement, hObjectsoort, false);

    // Update 'maanden' pulldown
    var hMaand = new Hash();
    this.find({
      objectsoort: this.objectsoortElement.getValue(),
      duur: this.duurElement.getValue(),
      maand: null
    }).each(function(record) {
      if (record.maanden) record.maanden.each(function(m) {
        hMaand.set(m, (hMaand.get(m) || 0) + 1);
      });
    });
    this.updateOptions(this.maandElement, hMaand, false);

    // Update 'duur' pulldown
    var hDuur = new Hash();
    this.find({
      objectsoort: this.objectsoortElement.getValue(),
      duur: null,
      maand: this.maandElement.getValue()
    }).each(function(record) {
      if (record.duur) record.duur.each(function(d) {;
        hDuur.set(d, (hDuur.get(d) || 0) + 1);
      });
    });
    this.updateOptions(this.duurElement, hDuur, false);
  },

  updateOptions: function(element, hash, bSetCount) {
    element = $(element);
    if (!element) return;
    hash = $H(hash);
    for (var i = 0, length = element.length; i < length; i++) {
      opt = element.options[i];
      if (opt.value) {
        if (bSetCount && $H(hash).get(opt.value)) {
          this.addCountText(opt, $H(hash).get(opt.value));
        }
        if ($H(hash).get(opt.value)) {
          //Element.show(opt);
          opt.style.color = "white";
        }else{
          //Element.hide(opt);
          opt.style.color = "#404040";
        }
      }
    }
  },

  getSelectElementText: function(element) {
    var text = element.options[element.selectedIndex].text;
    text = text.replace(/\(\d+\)$/, "");
    return text;
  },

  addCountText: function(opt, count) {
    if (opt.text.match(/\(\d+\)$/)) {
      opt.text = opt.text.replace(/\(\d+\)$/, "("+count+")");
    }else{
      opt.text = opt.text + " " + "("+count+")";
    }
  },

  updateResultText: function(results) {
    var resultElement = this.boxElement.down(".lastminuteItemResult p");
    if (!resultElement) return;

    //var objectsoortValue = this.objectsoortElement.getValue();
    //var maandValue = this.maandElement.getValue();
    var objectsoortText = this.objectsoortElement.getValue() ? this.getSelectElementText(this.objectsoortElement) : null;
    var maandText = this.maandElement.getValue() ? this.getSelectElementText(this.maandElement) : null;

    var text;
    if (!results) {
      text = "Er zijn geen last-minute aanbiedingen gevonden";
    }else if (results == 1) {
      text = "Er is één last-minute aanbieding gevonden";
    }else{
      text = "Er zijn " + results + " last-minute aanbiedingen gevonden";
    }
    if (objectsoortText) {
      text += " voor een " + objectsoortText;
    }
    if (maandText) {
      text += " in " + maandText;
    }
    text += ".";
    resultElement.update(text);
    resultElement.show();
  },

  nop: false
});



/* function addObjecttypes(selectElement) {
  var objectsoorten = $H();

  $$(".lastminuteItem").each(function(element) {
    var objectsoort = element.down(".lastminuteObjectsoortNaam");
    if (!objectsoort.title) return;
    if (!objectsoorten.get(objectsoort.title)) {
      objectsoorten.set(objectsoort.title, $H({count: 0}));
    }
    objectsoorten.get(objectsoort.title).set("count", objectsoorten.get(objectsoort.title).get("count") + 1 );
    objectsoorten.get(objectsoort.title).set("title", objectsoort.innerHTML);
  });

  objectsoorten.each(function(pair) {
    var option = '<option value="#{value}">#{text}</option>'.interpolate({
      value: pair.key,
      text: (pair.value).get("title") + " (" + (pair.value).get("count") + ")"
    });
    $(selectElement).insert(option);
  });
  $(selectElement).setValue("");
} */

