/**
 * Autocompleter.Request
 *
 * http://digitarald.de/project/autocompleter/
 *
 * @version		1.1.2
 *
 * @license		MIT-style license
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @copyright	Author
 */


Autocompleter.Request.Landing = new Class({

	Extends: Autocompleter.Request.JSON,
  
  onCommand: function(e) {
    if (!e && this.focussed) return this.prefetch();
    if (e && e.key && !e.shift) {
      switch (e.key) {
        case 'enter':
          if (this.element.value != this.opted) return true;
          if (this.selected && this.visible) {
            //this.choiceSelect(this.selected);
            
            
            var src = this.selected.getChildren('a')[0].getProperty('href');
            window.location = src; 
            return false;
            
            return !!(this.options.autoSubmit);
          }
          break;
        case 'up': case 'down':
          if (!this.prefetch() && this.queryValue !== null) {
            var up = (e.key == 'up');
            this.choiceOver((this.selected || this.choices)[
              (this.selected) ? ((up) ? 'getPrevious' : 'getNext') : ((up) ? 'getLast' : 'getFirst')
            ](this.options.choicesMatch), true);
          }
          return false;
        case 'esc': case 'tab':
          this.hideChoices(true);
          break;
      }
    }
    return true;
  },
  
  
  
  choiceSelect: function(choice) {
    if (choice) this.choiceOver(choice);
    this.setSelection(true);
    this.queryValue = false;
  },
  
  options: {
    'injectChoice': function(token) {
      
      var link = new Element('a', {'href': token.url, 'html': this.markQueryValue(token.title)});

      var choice = new Element('li');
      link.inject(choice);
      choice.inputValue = token.title;
      
      choice.addEvents({
          'click': function(el){
            window.location = token.url;
          }
        });
      
      this.addChoiceEvents(choice).inject(this.choices);
    }
  }
  
  
  
});

