// JavaScript Document

function autoCompleter(anId,anUrl) {
  this.id=anId;
  this.url=anUrl;
  this.wait='/_pur_sangs_/themes/arqana/img/ajax_loader.gif';
  this.container=$(anId+'Container');
  this.listContainer=null;
  this.waitingImg=null;
  this.toolTip=null;
  this.tooltipText=null;
  //this.afterUpdate=function(this.element, selectedElement) { alert(aLi.id); };
  this.requestParams=new Array();
  this.requestParams.push('htmlid='+anId);
  this.minChars=3;
}

function autoCompleterToolTipShow(event) {
  if ($('tooltip_'+Event.element(event).id))
    new Effect.Appear('tooltip_'+Event.element(event).id, { duration: 0.4 });
}
function autoCompleterKeyup(event) {
  if ($('tooltip_'+Event.element(event).id)) {
    if (Event.element(event).value.length>2) {
      new Effect.Fade('tooltip_'+Event.element(event).id, { duration: 0.4 });
    } else if (!$('tooltip_'+Event.element(event).id).visible()) {
      new Effect.Appear('tooltip_'+Event.element(event).id, { duration: 0.4 });
    }
  }
}

autoCompleter.prototype.build=function() {
  if (!this.container) return false;
  
  var pos=this.container.cumulativeOffset();
  var d=document.getElementsByTagName('body');
  var b=d[0];

  d=Builder.node('div', { id: 'autocomplete_'+this.id, className: 'autocomplete' });
  b.appendChild(d);
  this.listContainer=$(d);
  this.listContainer.setStyle({
	display: 'none',
	position: 'absolute',
	left: (pos[0]+1)+'px',
	top: (pos[1]+20)+'px',
	width: '220px',
	height: 'auto',
	zindex: '1000'
  });

  d=Builder.node('img', { id: 'waiter_'+this.id, src: this.wait });
  b.appendChild(d);
  this.waitingImg=$(d);
  this.waitingImg.setStyle({
	display: 'none',
	position: 'absolute',
	left: (pos[0]+parseInt(this.container.getWidth())+2)+'px',
	top: (pos[1]+1)+'px',
	zindex: '1000'
  });

  if (this.tooltipText) {
    d=Builder.node('div', { id: 'tooltip_'+this.id, className: 'autocompletetooltip' },this.tooltipText);
    b.appendChild(d);
    this.toolTip = $(d);
    this.toolTip.setStyle({
	display: 'none',
	position: 'absolute',
	left: (pos[0]+0)+'px',
	top: (pos[1]-40)+'px',
	margin: '0px',
	padding: '8px 0px 0px 0px',
	zindex: '1000'
    });
    
    $(this.id).observe("click", autoCompleterToolTipShow);
    $(this.id).observe("keyup", autoCompleterKeyup);
  }
  

  var reqOptions={
    paramName: "value",
    minChars: this.minChars,
    indicator: this.waitingImg.id,
    afterUpdateElement : this.afterUpdate
  };
  if (this.requestParams.length>0) reqOptions.parameters=this.requestParams.join('&');
  
  //alert('[autoCompleter.prototype.build] '+this.url);
  
  new Ajax.Autocompleter(this.id, this.listContainer.id, this.url, reqOptions);

  return true;
}

