function taxonomy_search_directory(config) {
  this.config = config;
  this.selects = [];
  this.field = function() {
    this.content = document.createElement('DIV');
    this.get_first();
    return this.content;
  }
  this.get_first = function() {
    var options = [];
    for (var n in this.config.settings.vocabulary) {
      if (this.config.settings.vocabulary[n].depth == 0) options.push(this.config.settings.vocabulary[n]);
    }
    this.add_select(options, -1);
  }
  this.update = function(config) {
    this.config = config;
    var values = [];
    for (var n in this.selects) {
      values[n] = this.selects[n].options[this.selects[n].selectedIndex].value;
    }
    this.selects[0].remove();
    this.get_first();
    for (var n in values) {
      if (this.selects[n]) this.selects[n].select(values[n]);
    }
  }
  this.add_select = function(options, parent) {
    var select = document.createElement('SELECT');
    select.field = this;
    select.parent = parent;
    select.onchange = function() {
      this.field.set(this.options[this.selectedIndex].value);
    }
    select.get_child = function() {
      if (this.child) this.child.remove();
      var options = [];
      for (var n in this.field.config.settings.vocabulary) {
        for (var i in this.field.config.settings.vocabulary[n].parents) {
          if (this.field.config.settings.vocabulary[n].parents == this.options[this.selectedIndex].value) options.push(this.field.config.settings.vocabulary[n]);
        }
      }
      if (options.length > 0) this.child = this.field.add_select(options, this.options[this.selectedIndex].value);
      else this.child = null;
    }
    select.select = function(value) {
      if (this.parent == value) return;
/*      for (var n in this.options) {
    		alert(this.options[n].value);
        if (this.options[n].value == value) {
	    		alert(this.options[this.selectedIndex].value);
          this.options[n].selected = true;
          this.get_child();
        }
      }
*/

    	for(i = 0; i < this.options.length; i++){
        if (this.options[i].value == value) {
          this.options[i].selected = true;
          // this.get_child();
        }
    	}
    }
	  select.remove = function() {
      if (this.child) this.child.remove();
      this.field.selects.pop();
      this.parentNode.removeChild(this);
    }
    if (parent) select.parent = parent;
    options.unshift({'tid': parent, 'name': 'select'});
    for (var n in options) {
      var option = document.createElement('OPTION');
      option.value = options[n].tid;
      option.tid = options[n].tid;
      option.innerHTML = options[n].name;
      select.appendChild(option);
    }
    this.content.appendChild(select);
    this.selects.push(select);
    return select;
  }
  this.set = function(tid) {
    this.tid = tid;
    directory_update();
  }
  this.value = function() {
    if (this.tid) return {'tid': this.tid};
  }
}
