﻿(function(){

  var PomocSearch = new Class({

    tween: null,
    input: null,
    container: null,
    search_hint: null, 
  
    uri:         '/pomoc/search',
    method:      'GET',
    form:        'form1',
    input:       'form1_userquery',
    search_hint: 'search_hint',
  
    texts: {
      empty: 'Wprowadź słowo kluczowe, albo frazę.',
      no_result:'Nie znaleziono odpowiedzi.',
      default_input_value: 'Wpisz szukaną frazę'
    },
  
    onSuccess: function(json)
    {
      if(this.input.get('value').length === 0) {
        json = {'0': [this.texts.empty]};
      }
      else if(json.length === 0) {
        json = {'0': [this.texts.no_result]};
      }
      
      json = new Hash(json);
  
      if( !$('results') ) {
        $(this.form).appendChild(this.container);
      }
        
      if(this.container.get('text').length !== 0) {
        this.tween.addEvent('complete', this.fill.pass(json,this));
        this.tween.start('opacity', 1, 0);
      } else {
        if(this.search_hint) {
          this.search_hint.morph({opacity: 0, display: 'none'});
        }
        this.fill(json);
      }
    },
    
    initialize: function()
    {
      this.input = $(this.input);
      this.input.set('value', this.texts.default_input_value);
      this.form  = $(this.form);
      this.form.addEvent('submit', this.search.bind(this));
      this.container = new Element('div', {id: 'results', style: "height:0"}).set({opacity: 0});
      this.tween = new Fx.Tween(this.container, {link: 'chain', duration: 'short'});
      this.search_hint = $(this.search_hint);
      this.input.addEvents({
        'focus': this.toggle_value.pass([this.input, 'focus'], this),
        'blur':  this.toggle_value.pass([this.input, 'blur'], this)
      })
    },

    toggle_value: function (el, type)
    {
      if( type === 'focus' && el.value === this.texts.default_input_value ) {
        el.value = '';
        return true;
      }
      if ( type === 'blur' && el.value.trim() === '' ) {
        el.value = this.texts.default_input_value;
        return true;
      }
    },
    
    search: function (e)
    {
      e.stop();
      
      var value = this.input.get('value');
      
      if(value.length === 0) {
        this.onSuccess.call(this, []);
        return false;
      }
      
      new Request.NK({
        url:    this.uri,
        method: this.method,
        data: { userquery: this.input.get('value')},
        onSuccess: this.onSuccess.bind(this)
      }).send();
    },
     
    trimline: function(str)
    {
      if(window.location.pathname.substr(1).split('/').length > 1 && str.length > 36){
        return str.substr(0, 36)+' …';
      } else if(str.length > 67) {
        return str.substr(0, 67)+' …';
      }
      return str;
    },
    
    fill: function(json)
    {
      var ul, li;
      
      this.tween.removeEvents('complete');
      this.container.set('text', '');
      this.container.appendChild(ul = new Element('ul'));
      
      json.each(function(result, id) {
        ul.appendChild(li = new Element('li'));
        
        if (result[2] == 0) {
          if (id != 0) {
            li.appendChild(new Element('a', {
              title: result[0],
              href: '/pomoc/pytanie/' + id,
              text: this.trimline(result[0])
            }));
          }
          else {
            li.appendChild(new Element('span', {
              text: this.trimline(result[0])
            }));
          }
        }
        else {
          li.appendChild(new Element('a', {
            title: result[0],
            text: this.trimline(result[0])
          }));
        }
      }.bind(this));
      
      this.tween.start('height', this.container.getSize().y, ul.getSize().y).start('opacity',0,1);
    }
  });
  
  window.addEvent('domready_nk', function() {
    new PomocSearch();
  });

})();
