
var __autofill_objects;
var statustr ='';
function __autofill_trigger(nr) {
  return __autofill_objects[nr].main_loop();
}

function autofill(name,url,expandBy,minChars) {
  this.color_h="#ffffc0";
  this.color  ="#ffffff";

  this.setup(name,url,expandBy,minChars);
  this.init();
  this.input_user_inhalt=this.input.value;
  this.init_completion_span();
  this.next();
}

autofill.prototype.setup = function(name,url,expandBy,minChars) {
  this.url   = url;
  this.expandBy=expandBy;
  this.minChars = minChars;

  this.end_suggestions=false;
  var oThis = this;
  this.cache = new Object();
  this.input = ( "object"==typeof name )? name:$(name);
  if ( !this.input ) return false;

  if ( "object" != typeof __autofill_objects )
    __autofill_objects = new Array();
  this.id = __autofill_objects.length;
  __autofill_objects.push(this);

  Event.observe(this.input,"keyup",function(oEvent) { return oThis.handle_keys(oEvent); } );
  Event.observe(this.input,"keydown",function(oEvent) { return oThis.handle_keys_down(oEvent); } );
  //Event.observe(this.input,"blur",function(oEvent) { return oThis.handle_blur(oEvent); } );
}


autofill.prototype.init = function() {
  this.last_key=-1;
  this.off=true;
  this.last_key_press_time=false;
  this.xmlrequestsndx=null;
  this.input_user_inhalt="";
}

autofill.prototype.next = function() {
  this.timeout=window.setTimeout("__autofill_trigger("+this.id+")",300);
}

autofill.prototype.handle_keys_down = function(oEvent){
  var tc;
  if (oEvent.which){
		tc=oEvent.which;
  }else if(oEvent.keyCode){
		tc=oEvent.keyCode;	
  }	

  this.last_key_press_time=(new Date()).getTime();
  if (tc==Event.KEY_RETURN || tc==Event.KEY_TAB){
    this.hide_span(true);
    this.last_key=tc;
    return true;
  }
  this.last_key = tc;
}

autofill.prototype.handle_blur = function(oEvent){
  this.hide_span(true);
  this.last_key = Event.KEY_TAB;
  return true;
}
      

autofill.prototype.handle_keys = function(oEvent){
  if (this.last_key==Event.KEY_RETURN) return false;
  var tc;
  if (oEvent.which){
		tc=oEvent.which;
  }else if(oEvent.keyCode){
		tc=oEvent.keyCode;	
  }	
  if ((tc==Event.KEY_ESC) && this.is_span_displayed(this.input_user_inhalt)){
    this.input.value=this.input_user_inhalt;
    this.hide_span(false);
    this.last_key=tc;
    return false;
  }
  this.last_key=tc;
  if (tc==Event.KEY_DOWN){ this.move_down(); return; }
  if (tc==Event.KEY_UP)  { this.move_up(); return; }
  if (this.input_user_inhalt!=this.input.value){
    this.input_user_inhalt=this.input.value;
  }
}

autofill.prototype.init_completion_span = function(){
  this.span=document.createElement("SPAN");
  this.span.style.borderRight="gray 1px solid";
  this.span.style.borderLeft="gray 1px solid";
  this.span.style.borderTop="gray 1px solid";
  this.span.style.borderBottom="gray 1px solid";
  this.span.style.paddingRight="0";
  this.span.style.paddingLeft="0";
  this.span.style.paddingTop="0";
  this.span.style.paddingBottom="0";
  this.span.style.position="absolute";
  this.span.style.backgroundColor="#FFFFFF";
  this.span.style.overflow="hidden";
  this.span.style.zIndex="1";
  this.span.style.visibility="hidden";
  document.body.appendChild(this.span);	
  this.calculate_span_position();
}

autofill.prototype.calculate_span_position = function(){
  var pos = Position.cumulativeOffset(this.input);
  this.span.style.top=(this.input.offsetHeight+pos[1]-1)+"px";
  this.span.style.left=pos[0]+"px";
  this.span.style.width=(this.input.offsetWidth+this.expandBy-2)+"px";
}

autofill.prototype.fill_span = function(term){
  var oThis = this;
  var inhalte=this.cache[term];
  var div;
  var spanlinks;
  var spanrechts;
  this.span.style.visibility="hidden";
  this.span.id=term;
  
  while(this.span.childNodes.length>0) {
    this.span.removeChild(this.span.childNodes[0]);
  }
  for (var i=0; i<inhalte.length; i++){
    div=document.createElement("DIV");
    div.className="complete";
    if (i==0 && !this.off){
      div.style.backgroundColor=this.color_h;
    }else{
      div.style.backgroundColor=this.color;
    }		
    div.style.width=this.span.offsetWidth-3;
    div.style.whiteSpace="nowrap";
    div.style.cursor="default";
    Event.observe(div,"mouseover",function(oEvent) { return oThis.highlight(oEvent); });
    Event.observe(div,"click",function(oEvent) { return oThis.set_input_and_submit(oEvent); });
    this.span.appendChild(div);
    spanlinks=document.createElement("SPAN");
    spanlinks.style.position="relative";
    spanlinks.style.left="2px";
    spanlinks.style.cursor="default";
    spanlinks.innerHTML=inhalte[i][0];
    div.appendChild(spanlinks);
		
    spanrechts=document.createElement("SPAN");
    spanrechts.style.position="absolute";
    spanrechts.style.right="2px";
    spanrechts.style.color="#FF0000";
    spanrechts.style.cursor="default";
    spanrechts.innerHTML=inhalte[i][1];
    div.appendChild(spanrechts);
  }
  if ((inhalte.length > 0)){
    this.show_span();
    if (!this.off)
      this.setselection();		
  }
}

autofill.prototype.hide_span = function(end_suggestions_){
  this.end_suggestions=end_suggestions_;
  if ( this.end_suggestions ) {
    this.init();
  }
  this.span.style.visibility="hidden";
}

autofill.prototype.show_span = function(){
  if (!this.end_suggestions){
    this.span.style.visibility="visible";
  }
}

autofill.prototype.highlight = function(oEvent){
  var elm = Event.findElement(oEvent,"div");
  var divs=this.span.childNodes;
  for (var i=0; i < divs.length; i++){
    divs[i].style.backgroundColor=this.color;
  }
  elm.style.backgroundColor=this.color_h;
}

autofill.prototype.move_down = function(oEvent){
  var divs=this.span.childNodes;
  this.off=false;
  for (var i=0; i < divs.length; i++){
    if (translate_rgb(divs[i].style.backgroundColor)==this.color_h ){
      var inext = (i==divs.length-1)? 0:i+1;
      divs[inext].style.backgroundColor=this.color_h;			
      divs[i].style.backgroundColor=this.color;
      this.input.value=divs[inext].childNodes[0].innerHTML;
      return;
    }		
  }
  divs[0].style.backgroundColor=this.color_h;
  this.input.value=divs[0].childNodes[0].innerHTML;
  return;	
}

autofill.prototype.move_up = function(oEvent){
  var divs=this.span.childNodes;
  for (var i=0; i < divs.length; i++){
    if (translate_rgb(divs[i].style.backgroundColor)==this.color_h){
      if (i==0){
        divs[i].style.backgroundColor=this.color;		
        this.off=true;
        this.input.value=this.input_user_inhalt;
        return;							
      }else{
        divs[i-1].style.backgroundColor=this.color_h;
        this.input.value=divs[i-1].childNodes[0].innerHTML;
        divs[i].style.backgroundColor=this.color;
        this.off=false;		  
        return;			
      }
    }
  }	
}

autofill.prototype.setselection = function(){
  if ( ((new Date()).getTime() - this.last_key_press_time) < 100) return;
  if (this.last_key==Event.KEY_BACKSPACE){ return; }
  if (document.all){
    var range=this.input.createTextRange();
    this.input.value=this.span.childNodes[0].childNodes[0].innerHTML;
    range.moveStart("character",this.input_user_inhalt.length);
    range.select();
  }else{
    this.input.value=this.span.childNodes[0].childNodes[0].innerHTML;
    this.input.setSelectionRange(this.input_user_inhalt.length,this.input.value.length);		
  }
}


autofill.prototype.set_input_and_submit = function(oEvent){	
  var elm = Event.findElement(oEvent,"div");
  this.input.value=entity_decode(elm.childNodes[0].innerHTML);
  this.hide_span(true);
  Event.stop(oEvent);
  // HERE IS MORE WORK TO DO !! document.forms[0].submit();
}

autofill.prototype.is_span_displayed = function(x){
  return ((this.span.style.visibility!="hidden") && this.span.id==x)? true:false;
}

autofill.prototype.main_loop = function(){
  //$("status").innerHTML = (new Date()).getTime()+" "+this.input_user_inhalt+" "+statustr;
  if (this.input_user_inhalt.length > this.minChars-1){
    if (!this.cache[this.input_user_inhalt]){
      if ((this.xmlrequestsndx==null) || (this.xmlrequestsndx!=this.input_user_inhalt)){
        this.get_data(this.input_user_inhalt);
        this.hide_span(false);
      }			
    }else{
      if (!this.is_span_displayed(this.input_user_inhalt) && (this.last_key!=Event.KEY_ESC) && (this.last_key!=-1) && (!this.end_suggestions))
        this.fill_span(this.input_user_inhalt);
    }
  }else{
    this.hide_span(false);
  }
  this.next();
}

autofill.prototype.add_data_to_cache = function(answer){
  var zeilen = answer.responseText.split('\n');
  var term=zeilen[0];
  var suggestions=new Array();
  for (var i=1; i < zeilen.length-1; i++){
    zeile=zeilen[i].split('\x1B');
    suggestions[i-1]=new Array(zeile[0],zeile[1]);
  }
  this.cache[term]=suggestions;
  this.xmlrequestsndx=null;
}

autofill.prototype.get_data = function(term) {
  var oThis = this;
  var pars = "term="+encodeURIComponent(term);

  var url = this.url;
  var url_a = url.split("?");
  if ( url_a.length > 1 ) {
    pars += "&"+url_a[1];
    url = url_a[0];
  }

  var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: function(answer){ return oThis.add_data_to_cache(answer); }
		});
  this.xmlrequestsndx=term;	
}

function entity_decode(term){
        term=term.replace(/&auml;/g,'ä');
        term=term.replace(/&ouml;/g,'ö');
        term=term.replace(/&uuml;/g,'ü');
        term=term.replace(/&Auml;/g,'Ä');
        term=term.replace(/&Ouml;/g,'Ö');
        term=term.replace(/&Uuml;/g,'Ü');
        term=term.replace(/&szlig;/g,'ß');
        term=term.replace(/&quot;/g,'"');
        term=term.replace(/&nbsp;/g,' ');
        term=term.replace(/&deg;/g,'°');
        term=term.replace(/&amp;/g,'&');
        return term;
}

function translate_rgb(wert){
        if (wert.charAt(0)=='#'){
                return wert;
        }else{

                r=wert.slice(4,7);
                g=wert.slice(9,12);
                b=wert.slice(14,17);
                wert='#'+toHex(r)+toHex(g)+toHex(b);
                return wert;
        }
}

function toHex(dec) {
        // create list of hex characters
        var hexCharacters = "0123456789abcdef"
        // if number is out of range return limit
        if (dec < 0)
                return "00"
        if (dec > 255)
                return "FF"
        // decimal equivalent of first hex character in converted number
        var i = Math.floor(dec / 16)
        // decimal equivalent of second hex character in converted number
        var j = dec % 16
        // return hexadecimal equivalent
        return hexCharacters.charAt(i) + hexCharacters.charAt(j)
}

function getm() {
   var m3_u = (location.protocol=='https:'?'https://sslsites.de/www.raucher-freunde.de/ads/www/delivery/ajs.php':'http://www.raucher-freunde.de/ads/www/delivery/ajs.php');
   var m3_r = Math.floor(Math.random()*99999999999);
   if (!document.MAX_used) document.MAX_used = ',';
   document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
   document.write ("?campaignid=0");
   document.write ('&amp;cb=' + m3_r);
   if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
   document.write ("&amp;loc=" + escape(window.location));
   if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
   if (document.context) document.write ("&context=" + escape(document.context));
   if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
   document.write ("'><\/scr"+"ipt>");
}

