var selectFix = {
  autoWidth: function(event) {
    myE = event || window.event;
    if(!this.isOpen) this.selectedIndex = Math.max( 0, this.selectedIndex );
    this.style.width = "auto";
    if(myE.type=="mousedown" && !this.isOpen) this.focus();
    this.isOpen = true;
  },
  
  staticWidth: function(event) {
    myE = event || window.event;
    if(this.selectedIndex==this.myIndex && myE.type=="change") {
      this.selectedIndex = Math.max( 0, this.selectedIndex );
    }else{
      this.style.width = this.w;
      this.isOpen = false;
    }
    this.myIndex = this.selectedIndex;
  },
  
  initSelectFix: function() {
    var mySelects = document.getElementsByTagName("select");
    for(var x=0, mySelect; mySelect = mySelects[x]; x++) {
      mySelect.w = mySelect.offsetWidth;
      mySelect.style.width = "auto";
      afterW = mySelect.offsetWidth;
      mySelect.style.width = mySelect.w;
      if (mySelect.w < afterW) {
        mySelect.onmousedown = selectFix.autoWidth;
        mySelect.onfocus = selectFix.autoWidth;
        mySelect.onblur = selectFix.staticWidth;
        mySelect.onchange = selectFix.staticWidth;
        
        mySelect.isOpen = false;
        mySelect.myIndex = -1;
        
        mySelect.style.position = "absolute";
        mySelect.parentNode.style.position = "relative";
      }
    }
  },
  
  init: function () {
    /* IE only -- so just attachEvent */
    if (window.attachEvent && !window.opera) window.attachEvent("onload", this.initSelectFix);
  }
}

selectFix.init();
