/*
  +----------- [[[ --- pIscIs.lt --- ]]] --------------+
  |                                                    |
  | js select box improvements:                        |
  |   - added first item to the options list           |
  |   - added closeSel() method actions                |
  |   - added check (check for css class 'to_replace') |
  |     if select box shuold be changed                |
  |                                                    |
  | http://code.piscis.lt                              |
  | By David aka pIscIs                                |
  | 2008-08-05                                         |
  +----------------------------------------------------+
  
  +--------- [[[ --- original source --- ]]] ----------+
  |                                                    |
  | http://v2.easy-designs.net/articles/replaceSelect/ |
  | By Aaron Gustafson                                 |
  +----------------------------------------------------+ 
*/

    function selectReplacement(obj) {
      // append a class to the select
      obj.className += ' replaced';
      // create list for styling
      var ul = document.createElement('ul');
      ul.className = 'selectReplacement';
      var opts = obj.options;
      for (var i=0; i<opts.length; i++) {
        var selectedOpt;
        if (opts[i].selected) {
          selectedOpt = i;
          break;
        } else {
          selectedOpt = 0;
        }
      }
      for (var i=0; i<opts.length; i++) {
        if(i == 0) {
          var li = document.createElement('li'); 
          var txt = document.createTextNode(opts[i].text);
          li.appendChild(txt);
          li.className="first"
          li.selIndex = opts[i].index;
          li.selectID = obj.id;
          li.onclick = function() {
            closeSel(li);
          }
          ul.appendChild(li);
          var li_first = li;
        }
        
        var li = document.createElement('li');
        var txt = document.createTextNode(opts[i].text);
        li.appendChild(txt);
        li.selIndex = opts[i].index;
        li.selectID = obj.id;
        li.onclick = function() {
          selectMe(this);
        }
        
        if (i == selectedOpt) {
          li_first.innerHTML = li.innerHTML;
          li.className = 'selected';
          li.onclick = function() {
            this.parentNode.className += ' selectOpen';
            this.onclick = function() {
              selectMe(this);
            }
          }
        }
        if (window.attachEvent) {
          li.onmouseover = function() {
            this.className += ' hover';
          }
          li.onmouseout = function() {
            this.className = 
              this.className.replace(new RegExp(" hover\\b"), '');
          }
        }
        ul.appendChild(li);
        ul.onclick = function () {
          document.body.onclick = function () {
              //closeSel(li);
          }
        }
      }
      // add the input and the ul
      obj.parentNode.appendChild(ul);
    }
    function selectMe(obj) {
      var lis = obj.parentNode.getElementsByTagName('li');
      for (var i=0; i<lis.length; i++) {
        if (lis[i] != obj && lis[i].className != 'first') { // not the selected list item
          lis[i].className='';
          lis[i].onclick = function() {
            selectMe(this);
          }
          
       } else {
        if(lis[i].className != 'first') {
            setVal(obj.selectID, (obj.selIndex));
            lis[0].innerHTML = obj.innerHTML;
            obj.className='selected';
            obj.parentNode.className = 
              obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
            obj.onclick = function() {
              obj.parentNode.className += ' selectOpen';
              this.onclick = function() {
                selectMe(this);
              }
            }
          }
        }
        //alert(obj.selectID);
      }
    }
    
    function closeSel(obj) {
       var lis = obj.parentNode.getElementsByTagName('li');
       lis[0].parentNode.className = 'selectReplacement';
    }
    
    function setVal(objID, selIndex) {
      //alert(objID);
      var obj = document.getElementById(objID);
      obj.selectedIndex = selIndex;
    }
    function setForm() {
      var s = document.getElementsByTagName('select');
      for (var i=0; i<s.length; i++) {
        if(s[i].className == 'to_replace') {
          selectReplacement(s[i]);
        }
      }
    }
    
    
  
