addEvent(window,'load',init_myselect);
var myselect_id=0;
var waitSelect;

function init_myselect()
{
var rgx1 = /myselect/;
var rgx2 = /recherche/;
var labels = document.getElementsByTagName('label');
  for (var j=0; j<labels.length; j++){
    var label = labels[j];
    var input = null;
    //~ if (label.className!='myselect')  continue;
    if (!rgx1.test(label.className))  continue;
    if (rgx2.test(label.className))  
      {
      input=document.createElement('input');
      input.className=label.className;
      input.htmlFor=label.htmlFor;
      input.value=label.innerHTML;
      input.title=label.title;
      input.name='rech_'+label.title;
      input.id='rech_'+label.title;
      input.style.width=(label.clientWidth-5)+'px';
      input.style.height=label.clientHeight+'px';
      label.parentNode.replaceChild(input,label);
      label=input;
      j--;
      }
    myselect(label);
    if (input)
      {
      jQuery(function() 
        {
        jQuery("#rech_"+label.title).suggest('Ask/'+label.title,{
        onSelect: function() {
        this.parentNode.choix(this.textContent);
         window.location.href=this.firstChild.href;
          }
        ,resdiv:jQuery('#'+label.title)
        ,defaultClass: 'labelOUT'
        ,selectClass: 'labelOVER'
        ,minchars: 3
	,ParentControl:true
        });
        });
      }
      
    
  }
}
function myselect(label)
{
  elements=jQuery(label.htmlFor);
  selectbox=elem('ul');
  selectbox.className='myselect';
  selectbox.id=label.title;
  
  selectbox.sort=function(a, b) {
    var x = a.firstChild.data.toLowerCase();
    var y = b.firstChild.data.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  }
  
  jQuery("#body")[0].appendChild(selectbox);
  
  links=new Array();
  for (var e=0; e<elements.length; e++)
    {
    links.push(elements[e]);
    }
  links.sort(selectbox.sort)
  for (var e=0; e<links.length; e++)
    {
    li=elem('li');
    li.appendChild(links[e].cloneNode(true));
    selectbox.appendChild(li);
    }


  
  selectbox.style.left=GetOffset(label,'offsetLeft')+"px";
  selectbox.style.top=GetOffset(label,'offsetTop')+label.offsetHeight-1+"px";
  selectbox.label=label;
  selectbox.over=function()
    {
    this.style.background="red";
    }
  selectbox.out=function()
    {
    this.style.background="blue";
    }
  selectbox.init_options=function()
    {
    options=this.getElementsByTagName('li');
    for (var i=0; i<options.length; i++)
      {
      var option = options[i];
      option.className='labelOUT';
      if (option.firstChild.href==document.location)
		{
		if (this.label.tagName=='INPUT')
			{
			if (ns6)
				this.label.value=option.textContent;
			else
				this.label.value=option.firstChild.innerHTML;
			}
		else
		    this.label.innerHTML=option.firstChild.innerHTML;
		}
          addEvent(option,'mouseover',function () {
		  if (!ns6)
			{
			e=event.srcElement;
			if (e.tagName!='LI') e=e.parentNode;
			}
		  else e=this;
          jQuery('#'+e.parentNode.id).children('li.labelOVER').removeClass('labelOVER').addClass('labelOUT');
          e.className='labelOVER';
          });
        addEvent(option,'click',function () {
          e=ns6?this:event.srcElement
		  if (e.tagName!='LI') e=e.parentNode;
          e.parentNode.choix(e.firstChild.textContent);
		  document.location=e.firstChild.href;
          });
      }
    }
  selectbox.init_options();
  selectbox.choix=function(value)
  {
	if (this.label.tagName=='INPUT')
		this.label.value=value;
    else
		this.label.innerHTML=value
    this.hideoptions();
  }
  selectbox.select=function(value)
  {
  options=this.getElementsByTagName('li');
  for (var i=0; i<options.length; i++)
    {
    var option = options[i];
    if (option.firstChild.innerHTML==value)
      {
      this.scollto(option);
      break;
      }
    }
  }
  selectbox.scollto=function(item)
  {
    if (this.style.display=='none') this.style.display='block';
    if ((this.scrollTop+this.offsetHeight)<item.offsetTop)
      this.scrollTop = item.offsetTop;
    //~ this.choix(item.firstChild.innerHTML);
  }
  selectbox.showoptions= function()
  {
    this.style.display='block';
    this.style.height='';
    if (this.offsetHeight>280) this.style.height='280px';
    myselect_id=this.id;
    this.select(this.label.innerHTML);
  }
  selectbox.hideoptions= function()
  {
    clearTimeout(waitSelect);
    this.style.display='none';
    myselect_id=0;
  }
  selectbox.hideoptions();
  selectbox.style.display='none'
  addEvent(label,'click',function () {
    e=ns6?this:event.srcElement
    selectbox=jQuery('#'+e.title)[0];
    if (myselect_id) 
      {
      if (myselect_id==selectbox.id) 
        {
        selectbox.hideoptions();
        return;
        }
      jQuery('#'+myselect_id)[0].hideoptions();
      }
    selectbox.showoptions()
    });
  addEvent(selectbox,'mouseover',function () { clearTimeout(waitSelect);});
  addEvent(selectbox,'mouseout',function () { clearTimeout(waitSelect); waitSelect = setTimeout("if (myselect_id) jQuery('#'+myselect_id)[0].hideoptions()",300);});
}

var ns6=document.getElementById&&!document.all;

function GetOffset(obj,Offset)
{
   if (!obj) return;
   var val = obj[Offset];
   if (obj.offsetParent!=null)
      val +=  GetOffset(obj.offsetParent,Offset);
   return val;
}

/*
function #() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}
*/
function elem(name, attrs, style, text) {
    var e = document.createElement(name);
    if (attrs)
        for (key in attrs) 
                e.setAttribute(key, attrs[key]);
    if (style)
        for (key in style)
            e.style[key] = style[key];
    if (text) 
        e.innerHTML=text;
    return e;
}
function addEvent( o, e, h ) { o.addEventListener ? o.addEventListener( e, h, false ) : o.attachEvent ? o.attachEvent( 'on' + e, h ) : o[ 'on' + e ] = h; };
