
var logger = new Log(Log.DEBUG, Log.popupLogger );

// -------------
// class KeyCode
// 
function KeyCode (eve) {
  this.code = (eve)?eve.which:(window.event?window.event.keyCode:null);
}
KeyCode.prototype.UP = 38;
KeyCode.prototype.DOWN = 40;
KeyCode.prototype.ENTER = 13;
KeyCode.prototype.LEFT = 37;
KeyCode.prototype.RIGHT = 39;
KeyCode.prototype.TAB = 9;
KeyCode.prototype.SHIFT = 16;

// --- menu ---
//
//
function menu ( nodeID, formField ) {

  this.panelItv = 0;
  this.headerItv = 0;
  this.footerItv = 0;

  this.nodeID = nodeID;
  this.formField = document.getElementById( formField );
  var self = this;
  this.panel = document.getElementById( nodeID );

  this.panel.appendChild( this.header = document.createElement("div") ); 
  this.panel.appendChild( this.container = document.createElement("div") );
  this.panel.appendChild( this.footer = document.createElement("div") );

  // header
  var img = document.createElement("img");
  img.src = '/images/ajax/hoch.gif';
  img.width = '198';
  this.header.appendChild( img );

  this.header.onmousedown = function () {
    self.up();
    if ( ! self.headerItv ) self.headerItv = setInterval( function () { self.up(); }, 200 );
    self.header.style.cursor = 'pointer';
  }

  this.header.onmouseout = this.header.onmouseup = function () {
    if ( self.headerItv ) clearInterval( self.headerItv ) ;
    self.headerItv = 0;
    self.header.style.cursor = 'default';
  }

  // footer
  var img = document.createElement("img");
  img.src = '/images/ajax/runter.gif';
  img.width = '198';
  this.footer.appendChild( img );

  this.footer.onmousedown = function () {

    self.down();
    if ( ! self.footerItv) self.footerItv = setInterval( function () { self.down(); }, 200 );
    self.footer.style.cursor = 'pointer';
  }
  this.footer.onmouseout = this.footer.onmouseup = function () {
    if ( self.footerItv ) clearInterval( self.footerItv ) ;
    self.footerItv = 0;
    self.footer.style.cursor = 'default';
  }

  this.active = false;

  // register callbacks to 'this' using closures: 

  this.panel.onmouseout = function() { 
    self.panelItv = setInterval( function () { self.show(false); }, 250);
  }
  this.panel.onmouseover = function() {
    if ( self.panelItv ) clearInterval( self.panelItv ) ;
    self.show(true);
  }
  this.formField.onkeydown = function(e) { 
    var done = self.keyEvent(e);
    return done;
  }
  this.formField.onfocus = function(e) {
    self.doAjaxSearch();
  }
  this.formField.onblur = function(e) {
    //self.panelItv = setInterval( function () { self.show(false); }, 250);
  }

  // firefox buG
  this.formField.form.onsubmit = function (e) { 
     if ( e && (e.target == self.formField) ) return false;
     return true;
  }

}
menu.prototype.updateHeaderFooter = function () {
  //var hasHeader = this.header.childNodes[0].style.visibility = 'visible';
  this.header.style.visibility = (this.first <= 0) ? 'hidden' : 'visible';
  this.footer.style.visibility = (this.first + this.size >= this.data.length ) ? 'hidden' : 'visible';

}

menu.prototype.down = function () {
  if ( this.panel.style.visibility == 'hidden' ) return;

  var found = false;
    for ( var i=0;i<this.container.childNodes.length;i++) {
      var node = this.container.childNodes[i];
      if ( node.className == 'highlight') {

        // expandieren
        var last = this.first + this.size;
        if ( (i == this.container.childNodes.length-1) && (last < this.data.length)  ) {
            this.first++;
            this.container.removeChild( this.container.firstChild ) ;
            this.container.appendChild( this.newItem( this.data[last] ) );  
            i--;
        }

        if ( this.container.childNodes[i+1] ) {
          node.className ='';
          this.container.childNodes[i+1].className ='highlight';
        }
        found = true; 
        break;
      }
    }

    if ( (! found) && this.container.childNodes[0] ) this.container.childNodes[0].className = 'highlight';
  this.updateHeaderFooter();

}
menu.prototype.up = function () {
  if ( this.panel.style.visibility == 'hidden' ) return;

  var found = false;
    for ( var i=this.container.childNodes.length-1; i>=0;i--) {
      var node = this.container.childNodes[i];
      if ( node.className == 'highlight') {

        if ( i==0 && this.first > 0 ) {
          this.first--;

          this.container.removeChild( this.container.lastChild ) ;
          this.container.insertBefore( this.newItem( this.data[this.first] ), this.container.firstChild );  
          i++;
        }
        if ( this.container.childNodes[i-1] ) {
          node.className ='';
          this.container.childNodes[i-1].className ='highlight';
        }
        found = true;
        break;
      }
    }
    if ( (! found) && this.container.childNodes[this.container.childNodes.length-1] ) this.container.childNodes[this.container.childNodes.length-1].className = 'highlight';

  this.updateHeaderFooter();
}
menu.prototype.newItem = function ( label ) {
    var self = this;
    var item = document.createElement("h5");
    var link= document.createElement("a");
    link.appendChild( document.createTextNode( label ) ) ;
    //link.href='javascript:chooseValue("' + label + '");';
    link.href="#";
    link.onclick= function () {
       self.formField.value = label;
       self.show(false);
       return false;
    }
    item.appendChild(link);
  return item;
}

menu.prototype.show = function ( doShow ) {
  this.panel.style.visibility = doShow?'visible':'hidden';  
  if ( this.panelItv ) { clearInterval( this.panelItv ); this.panelItv = 0; }

  if ( ! doShow ) {
    if ( this.headerItv ) { clearInterval( this.headerItv ); this.headerItv = 0; }
    if ( this.footerItv ) { clearInterval( this.footerItv ); this.footerItv = 0; }
    this.header.style.visibility = 'hidden';
    this.footer.style.visibility = 'hidden';
  }
}

menu.prototype.wipe = function () {
  while ( this.container.firstChild ) this.container.removeChild( this.container.firstChild ) ;
}

menu.prototype.setData = function ( result, size ) {
  this.wipe();

  this.data = [];
  this.size = size;
  for ( d in result) if ( typeof(result[d]) == 'string' && result[d] != '' ) this.data.push( result[d] ) ;

  for ( d in this.data ) {
    if ( this.container.childNodes.length >= size ) break;
    this.container.appendChild( this.newItem( this.data[d] ) );
  }
  this.first = 0;
  this.updateHeaderFooter();

  this.show(this.data.length>0?true:false);

}

menu.prototype.keyEvent = function (eve) {
  var kc = new KeyCode(eve);

  var found = false;
  var self = this;

  if ( kc.code == kc.DOWN ) {
    this.down();

  } else if ( kc.code == kc.UP ) {
    this.up();

  } else if ( kc.code == kc.ENTER ) {
    if ( this.panel.style.visibility == 'visible' ) {
      for ( var i=this.container.childNodes.length-1; i>=0;i--) {
        var node = this.container.childNodes[i];
        if ( (node.className == 'highlight')) {
          this.formField.value = node.firstChild.firstChild.data;
          this.show( false );
          return false;
        }
      }
    }
    this.formField.form.submit();
  } else if ( kc.code == kc.LEFT || kc.code == kc.RIGHT ) {
    return true;
  } else if ( kc.code == kc.TAB ) {
    // leave input via tab
    this.show(false);
    return true;
  } else {
    if ( this.active ) return;
    this.active = true;
    setTimeout( function () { self.doAjaxSearch() },100);
    return true;
  }
  return false;
}

menu.prototype.doAjaxSearch = function () {

  var merkmal = this.formField.value;


  this.active = false;
  var self = this;

  if ( merkmal == '' ) {
    self.show(false);
    return;
  }

  req = new HTTP.Request({
    uri: '/generate/ajaxsearch',
    parameters: 'merkmal=' + merkmal,
    onError: function (trans) {
      alert(trans);
    },
    onSuccess: function (trans,that) {
      var data;
      try {
        var data = eval('('+trans.responseText+')'); // JSON "parsen"
      } catch(e) {
        alert('eval: Ungültiges JSON: ' + e);
        return;
      }
      self.setData( data.result, 5 );
          
    }
  });

}