
var customSearch = Class.create();
customSearch.prototype = {
  cats: null,
  cat1: null,
  cat2: null,
  initialize: function(cat1, cat2){
    this.cat1 = $(cat1);
    this.cat2 = $(cat2);
  },
  setCats: function(cats){
    this.cats = cats;
  },
  activate: function(cat1, cat2){
    if(this.cats!=null){
      var open = null;
      for(var i = 0; i<this.cats.length; i++){
        this.cat1.options[i] = new Element('option');
        this.cat1.options[i].innerHTML = this.cats[i].title;
        this.cat1.options[i].value = this.cats[i].id;
        if(cat1 == this.cats[i].id){
          this.cat1.options[i].selected = true;
          open = i;
        }
      }
      if(open!=null){
        this.select1cat(open, cat2);
      }

      this.cat1.observe('change', function(){
        this.select1cat(this.cat1.selectedIndex, 0);
      }.bind(this));
      
      this.cat1.disabled = false;
    }
  },
  select1cat: function(id, id2){
    while(this.cat2.options.length>0){
      this.cat2.options[0].remove();
    }
    if (this.cats[id].inner && this.cats[id].inner.length > 0) {
      for(var i = 0; i<this.cats[id].inner.length; i++){
        this.cat2.options[i] = new Element('option');
        this.cat2.options[i].innerHTML = this.cats[id].inner[i].title;
        this.cat2.options[i].value = this.cats[id].inner[i].id;
        if(id2 == this.cats[id].inner[i].id){
          this.cat2.options[i].selected = true;
        }
      }
      this.cat2.disabled = false;
    }else{
      this.cat2.disabled = true;
    }
  }
};