/**
 *  Fader
 *
 *  o = object or id to fade 
 *  i = initial opacity
 *  s = fade speed
 *
 *  MH
 *
**/

function fader(o, s, i) {

  // x = tracks the opacity

  this.o      = typeof(o) == 'object' ? o : document.getElementById(o);
  this.x      = typeof(i) == 'number' ? (i > 10 ? 10 : (i < 0 ? 0 : i)) : 10;
  this.speed  = typeof(s) == 'number' ? s : 50;
  this.timer  = null;
  this.lock   = false;
  this.ie     = document.all ? true : false;
  this.ie_o   = null;

  this.fade = function(to, f) {

    var mode  = to >= this.x ? 'in' : 'out';

    if( mode != this.lock ) {
      clearTimeout(this.timer);

    }

    if( this.x != to  ) {

      this.lock = mode;
      this.x    = Math.round(this.x + (mode == 'in' ? 1 : -1));

      if( this.ie == false ) {
        this.o.style.opacity  = this.x == 0 ? 0 : (this.x / 10);

      } else {
        this.fade_ie(x);

      }

      this.timer = setTimeout(function() { this.fade(to, f); }, this.speed);

    } else {
      this.lock = false;
      this.fade_callback(f);
    }

  } // ! fade()


  // Internet Explorer
  this.fade_ie = function(ie_x) {

    if( this.ie_o && typeof(this.ie_o) == 'object' ) {

      this.ie_o.style.filter = 'alpha(opacity=' + (Math.abs(ie_x - 10) * 10) + ')';

      if( ie_x == 10 ) {
        var o_children = this.o.getElementsByTagName('div');
        for(var o_child in o_children) {
          if( o_children[o_child].className == 'ie-fade' ) {
            o_children[o_child].removeNode(true);

          }
        }
        this.ie_o = null;
      }

    } else {
      this.ie_o   = document.createElement('div');
      this.ie_o.className = 'ie-fade';

      this.ie_o   = this.o.appendChild(this.ie_o);
      this.fade_ie(ie_x);
    }

  }


  this.fade_callback = function(f) {

    if( f && typeof(f) == 'function' ) {

      setTimeout(f, 0);

    }

  }


  this.fade_set = function(i) {

    this.x  = typeof(i) == 'number' ? (i > 10 ? 10 : (i < 0 ? 0 : i)) : 10;

    if( this.ie == false ) {
      this.o.style.opacity    = this.x == 0 ? 0 : this.x / 10;

    } else {
      this.fade_ie(i);

    }

  }

  return this;

} // ! fade()

