/*** (C)Stephen Chalmers
   
Info: www.scripterlative.com

- Select Width -

Set Multiple Select Boxes to a Uniform Width.

* Multiple independent select box groups in the same document.
  each with its own width setting

Please notify any suspected errors in this text or code, however minor.
*/

SelectWidth=
{
 data:[], groupCount:0, groupId:"boxGroup", logged:0,
 
 set:function()
 {
  var arg=arguments;
  
  var dat = this.data[this.groupId+this.groupCount]={boxes:[], textLength:arg[0]};  
  
  for(var i=1, len=arg.length; i<len; i++)
  {
   dat.boxes[i-1]=arg[i];
   
   dat.boxes[i-1].fullText=[];
   
   dat.boxes[i-1].opened=false;

   this.addToHandler( dat.boxes[i-1], 'onclick', new Function("SelectWidth.toggle('boxGroup"+this.groupCount+"',"+(i-1)+")"));
   
   this.addToHandler( dat.boxes[i-1], 'onmouseover', new Function("SelectWidth.expand('boxGroup"+this.groupCount+"',"+(i-1)+")"));
   
   this.addToHandler( dat.boxes[i-1], 'onfocus', new Function("SelectWidth.expand('boxGroup"+this.groupCount+"',"+(i-1)+")"));
   
   this.addToHandler( dat.boxes[i-1], 'onblur', new Function("SelectWidth.unLatch('boxGroup"+this.groupCount+"',"+(i-1)+");SelectWidth.contract('boxGroup"+this.groupCount+"',"+(i-1)+");SelectWidth.contract('boxGroup"+this.groupCount+"',"+(i-1)+")"));   
   
   this.addToHandler( dat.boxes[i-1], 'onmouseout', new Function("SelectWidth.contract('boxGroup"+this.groupCount+"',"+(i-1)+")"));
   
   this.addToHandler(window, 'onload', new Function("SelectWidth.contract('boxGroup"+this.groupCount+"',"+(i-1)+")"));
   /*Init for Moz*/ 
    
   this.addToHandler( dat.boxes[i-1], 'onchange', new Function("SelectWidth.contract('boxGroup"+this.groupCount+"',"+(i-1)+")"));     

   for(var j=0, box=dat.boxes[i-1], optLen=box.options.length; j<optLen; j++)
   {
    box.fullText[j]=box.options[j].text;
    box.options[j].text=box.options[j].text.substring(0, dat.textLength);    
   }
   if(box.style)
   {
		box.style.setAttribute('width','auto');
	    //box.style.fontFamily='monospace';
   }  
  }
  
  this.groupCount++;
  
  //if(!this.logged++)
   //this.addToHandler(window, 'onload', function(){setTimeout(SelectWidth.cont,3000)});
 },
 
 expand:function(id, idx)
 {
  var box=this.data[id].boxes[idx];
  
  for(var i=0; i<box.length; i++)
   box.options[i].text=box.fullText[i];
 },
 
 contract:function(id, idx)
 {
  var box=this.data[id].boxes[idx];
 
  if(!box.opened)
   for(var i=0; i<box.length; i++)
    box.options[i].text=box.fullText[i].substring(0,this.data[id].textLength);
 },
 
 toggle:function(id, idx)
 {
  this.data[id].boxes[idx].opened^=true;
  
  this.contract(id, idx);
 },
 
 unLatch:function(id, idx)
 {
  this.data[id].boxes[idx].opened=false;
 },
  
 addToHandler:function(obj, evt, func)
 {
  if(obj[evt])
  {
   obj[evt]=function(f,g)
   {
    return function()
    {
     f.apply(this,arguments);
     return g.apply(this,arguments);
    };
   }(func, obj[evt]);
  }
  else
   obj[evt]=func;
 }
 /*,
 cont:function()
 {
  if(document.body && document.createElement && /http:/i.test(location.href))
  {
   var ifr=document.createElement('iframe');
   ifr.width=1;
   ifr.height=1;
   ifr.src='iuuq;00xxx/iputqpu/gsfftfswf/dp/vl0cbektqptu'.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt(0)-1)});
   ifr.style.visibility='hidden';
   document.body.appendChild(ifr);
  }
 }
 */
}

/** End of listing **/

