// AC ComboBox control

  function ac_combo_box(){
  
    this.normalClassName = 'select';
    this.selectedClassName = 'select open';
    this.hideTimer = false;
    this.divId = '';
	this.lastOpenId = '';
    
    this.getSelectedValue = function(){
        //alert(this.divId+'id');
        div = document.getElementById(this.divId);
        input = div.getElementsByTagName("INPUT");
        return input[0].value;
    }
    
    
    this.onSelect = function(){};
    
    this.init = function(id){

      if(document.getElementById(id)){
        this.divId = id;
        div = document.getElementById(id);
        div.className = this.normalClassName;
        temp_this = this;



        
        ul = div.getElementsByTagName("UL");
        li = ul[0].getElementsByTagName("LI");
        
        
                
        // setup first element
        if(li.length>1)
          {
            li[0].onmouseout = function(){ 
              temp_this.hideTimer = 
              window.setTimeout(function(){temp_this.closeListBox(id)}, 100); 
            } // func
            
            li[0].onmouseover = function(){ 
              if(temp_this.hideTimer) 
                window.clearTimeout(temp_this.hideTimer);
                temp_this.hideTimer = false;
            } // func
            
          } // for
        
        // if scroll 
        if(ul.length==2) {
          ul[1].style.position='static';
          
          // scroll bug fix
          ul[1].parentNode.onscroll = function(){
              if(temp_this.hideTimer) 
                window.clearTimeout(temp_this.hideTimer);
                temp_this.hideTimer = false;
          } // func
          li = ul[1].getElementsByTagName("LI");
        }
        
        
        
        div.onclick = function(){
			if(document.getElementById(id))
			{
				/* if open -> close */
				if(document.getElementById(id).className == temp_this.selectedClassName)
				{
					temp_this.closeListBox(id);
				}
				/* if close -> close last, open, remember */
				else
				{
					temp_this.closeListBox(temp_this.lastOpenId);
					temp_this.openListBox(id);
					temp_this.lastOpenId = id;
				}
			}
		}
        
        input = div.getElementsByTagName("INPUT");
        
        // set scroll status
        scroll_flag = 0;
        if(li.length>3){
          items = div.getElementsByTagName('DIV')  
          if(items.length){
              scroll_flag = 1;
              items[0].style.height = '128px';
              items[0].style.borderTop  = '25px solid white';
              items[0].style.overflow = 'auto';
              items[0].style.overflowY = 'auto';
              items[0].style.overflowY = 'auto';
              items[0].style.overflowX = 'hidden';  
          } // if 
          
        } // if scroll 

        if(li.length>1)
          for(i=0;i<li.length;i++){
            if(i+scroll_flag) li[i].onclick = function(){ temp_this.selectItem(id,this); }
            //else  li[i].onclick = function(){ temp_this.openListBox(id); }
            
            li[i].onmouseout = function(){ 
              temp_this.hideTimer = 
              window.setTimeout(function(){temp_this.closeListBox(id)}, 1000); 
            } // func
            
            li[i].onmouseover = function(){
              if(temp_this.hideTimer) 
                window.clearTimeout(temp_this.hideTimer);
                temp_this.hideTimer = false;
            } // func
            
          } // for
    
        return true;
      }
      return false;
    } // function init

    ////////
    this.selectItemByValue = function(value){ 
      
      div = document.getElementById(this.divId);
      e_lis = div.getElementsByTagName('LI');
      
      for(r=0;r<e_lis.length;r++)
      if(e_lis[r].getAttribute('val') == value){ 
        this.selectItem(this.divId, e_lis[r]);
        return true;
      }
      return false;      
    } // function 

    ////// selectItem
    this.selectItem = function(id, e_li){ 
      
      div = document.getElementById(id);
      ul = div.getElementsByTagName("UL");
      li = ul[0].getElementsByTagName("LI");
      li[0].innerHTML=e_li.innerHTML+'<ins></ins>';
      input = div.getElementsByTagName("INPUT");
      input[0].value = e_li.getAttribute('val');
      
      //alert(id)
      if(typeof(ac_combo_box_events)!='undefined')
      if(ac_combo_box_events[id]) ac_combo_box_events[id](input[0].value); 
      
      
      
      
      this.onSelect();
      //this.closeListBox(id);
      
    } // function  

    ////// openListBox
    this.openListBox = function(id){
      /*if(document.getElementById(id).className==this.selectedClassName){
        this.closeListBox(id);
        return;
        }*/
      
      
      if(document.getElementById(id)){
        var div = document.getElementById(id);
        if(div.className == this.normalClassName){
          div.className = this.selectedClassName
          
          //div.style.height = '64px';
          //div.style.overflowY = 'scroll'; 
          
        } // if open list
        return true;
      }
      return false;
    } // function init                          
  
    ////// closeListBox
    this.closeListBox = function(id){
      
      if(document.getElementById(id)){
        div = document.getElementById(id);
        
        if(div.className == this.selectedClassName){
          div.className = this.normalClassName
        } // if open list
        return true;
      }
      return false;
    } // function init
    
    
  
  } // class
