var Searcher = Class.create({
  regios: [],
  button: null,
  input: null,
  autocompleter: null,
  populating_div: null,
  websites: [],

  initialize: function(){
    this.button = $('region_button');
    this.input = $('region');
    this.populating_div = $('autocomplete');
    this.websites = $$('.nieuws li a').invoke('hide'); 
  
    this.regios = $$('.cities').collect(function(elm){
        //return (elm.innerHTML || elm.contentText).split(", ").invoke("strip");
        return (elm.innerHTML || elm.contentText).split(", ").invoke("strip");
    }).flatten().uniq().sort();
   
    this.autocompleter = new Autocompleter.Local(this.input, this.populating_div, this.regios, {
      fullSearch: true,
      afterUpdateElement: this.updateList.bind(this)
    });
      
    this.input.observe("keypress", this.on_input_keydown.bind(this));
    this.button.observe("click", this.on_input_click.bind(this));
    this.websites.each(function(website){
      website.observe("click", this.on_website_click.bind(this));
    }, this);
  },

  on_input_keydown: function(event){
    e = event || window.event;
    if (event.keyCode == 13) {
      this.updateList();
      e.stop();
      //this.on_input_click().bind(this);
    }
  },
  
  on_website_click: function(event){
    var link = event.element().tagName == "A" ? event.element() : event.element().up("a");
    window.location = link.href;
    event.stop();
  },

  on_input_click: function(event){
    this.updateList();
    event.stop();
  },

  updateList: function(event){ 
    var search = this.input.value;

    var result = $$('.cities').select(function(elm){
      if (elm.innerHTML.toLowerCase().match(search.toLowerCase())) {
        return elm;
      }
    }).collect(function(elm){ return elm.up("a"); });

    result.invoke("appear");

    this.websites.each(function(elm){ 
      if(!result.include(elm)){
        elm.hide();
      }
    });

    if(result.length == 0) this.websites.last().appear();
  },

  buttonize: function(){
    this.button.up().observe("mouseenter", function(){ this.button.up().addClassName("hover") }.bind(this));
    this.button.up().observe("mouseleave", function(){ this.button.up().removeClassName("hover").removeClassName("click") }.bind(this));
    this.button.up().observe("mousedown", function(){ this.button.up().addClassName("click") }.bind(this));
    this.button.up().observe("mouseup", function(){ this.button.up().removeClassName("click") }.bind(this));
  },

  init_regios: function(){
    $$('.nieuws li a').each(function(elm){
      cities = elm.select('.cities').first();

      if(elm != undefined){
        elm.select('.cities').first().innerHTML.split(", ").each(function(city){
          this.regios.push(city.strip());
        }, this);
      }
    }, this);
    
    this.regios = this.regios.uniq().sort();
  },

  bind_website_click_events: function(){
    //this.input.observe('change', this.selecteer_editie_click_event.bind(this));
    $$('.websites li a').each(function(elm){
      elm.observe("click", this.website_link_click_event.bind(this));
    }, this);

  },

  selecteer_editie_click_event: function(event){
    $$('.nieuws li a').invoke('hide');

    var class_name = this.value.toLowerCase();
    $$("." + class_name).invoke("appear");
  },

  website_link_click_event: function(event){
    event.element().up('a').down(".website_image img").puff();
    event.stop();
    var href = event.element().up('a').href;

    setTimeout(function(){
      location.href = href;
    }, 500);
  }

});

document.observe("dom:loaded", function(){
  searcher = new Searcher();
});

/*
var REGIOS = [];

$$('.nieuws li a').each(function(elm){
  cities = elm.select('.cities').first();
  if(elm != undefined){
    elm.select('.cities').first().innerHTML.split(", ").each(function(city){
      REGIOS.push(city.strip());
    });
  }
});

REGIOS = REGIOS.uniq().sort();

var autocompleter = new S2.UI.Autocompleter('autocompleter1', {
  choices: REGIOS 
});
*/

