/******
** MLIB Slider v1.0/BS
** copyright Label Technologies 2008
** instanciation : new MlibSlider(idTargetDiv);
** call : g_mlibSliders[idTargetDiv].slide('horizontal' | 'vertical' | 'both' [, strCallbackFunction]):
** change loop interval : g_mlibSliders[idTargetDiv].setInterval(intMicroseconds);
** change step : g_mlibSliders[idTargetDiv].setStep(intNbPixels);
**
** NB : target should be a div and should have style.height and style.width set
*******/

var g_mlibSliders = new Array();
function MlibSlider(id, overflowMode) {
  this.id = id;
  this.overflowMode = this.overflowModes[overflowMode] ? this.overflowModes[overflowMode] : 'auto';
  if(document.getElementById(id) && document.getElementById(id).tagName.toLowerCase() == 'div')
    this.init();
}


MlibSlider.prototype = {
  overflowModes: {'hidden': 'hidden', 'auto': 'auto', 'scroll': 'scroll'},
  init: function() {
    g_mlibSliders[this.id] = this;
    this.div = document.getElementById(this.id);
    this.h = this.getH();
    this.w = this.getW();
    this.ratio = this.h / this.w;
    this.state = this.div.style.display == 'none' ? 'closed' : 'open';
    this.encapsulate();
    this.initialized = true;
    this.setStep(10);
    this.setInterval(10);
  },
  setStep: function(step) {
    this.step = step;
    this.stepX = this.w > this.h ? step : step / this.ratio;
    this.stepY = this.w > this.h ? step * this.ratio : step;
  },
  setInterval: function(interval) {
    this.interval = interval;
  },
  getH: function() {
    var h = parseInt(this.div.style.height);
    if(h && !isNaN(h))
      return h;
    return this.div.offsetHeight;
  },
  getW: function() {
    var w = parseInt(this.div.style.width);
    if(w && !isNaN(w))
      return w;
    return this.div.offsetWidth;
  },
  setH: function(h) {
    this.div.style.height = h + 'px';
  },
  setW: function(w) {
    this.div.style.width = w + 'px';
  },
  encapsulate: function() {
    var insideDiv = document.createElement('DIV');
    for(var l in this.div.style) {
      if(l.substr(0, 7).toLowerCase() == 'padding')
        continue;
      try{
        insideDiv.style[l] = this.div.style[l];
      } catch(e) {}
    }
    insideDiv.innerHTML = this.div.innerHTML;
    insideDiv.style.display = 'block';
    insideDiv.style.overflow = this.overflowMode;
    this.div.innerHTML = "";
    this.div.appendChild(insideDiv);
    this.div.style.overflow = "hidden";
  },
  slide: function(dir, callback, auto) {
    var action, w, h;
    if(!auto)
      clearTimeout(this.idTimeout);
    switch(this.state) {
      case 'closing':
        action = auto ? 'closing' : 'opening';
        break;
      case 'opening':
        action = auto ? 'opening' : 'closing';
        break;
      case 'closed':
        this.div.style.width = dir == 'vertical' ? this.w + 'px' : '1px';
        this.div.style.height = dir == 'horizontal' ? this.h + 'px' : '1px';
        this.div.style.display = 'block';
        action = 'opening';
        break;
      case 'open':
        this.div.style.width = this.w + 'px';
        this.div.style.height = this.h + 'px';
        this.div.style.display = 'block';
        action = 'closing';
        break;
    }
    this.state = action;
    switch(action) {
      case 'opening':
        switch(dir) {
          case 'both':
            w = this.getW() + this.stepX;
            h = this.getH() + this.stepY;
            if(w >= this.w || h >= this.h) {
              w = this.w;
              h = this.h;
              this.state = 'open';
            }
            this.setH(h);
            this.setW(w);
            break;
          case 'horizontal':
            w = this.getW() + this.step;
            if(w >= this.w) {
              w = this.w;
              this.state = 'open';
            }
            this.setW(w);
            break;
          default:
            h = this.getH() + this.step;
            if(h >= this.h) {
              h = this.h;
              this.state = 'open';
            }
            this.setH(h);
            break;
        }
        break;
      case 'closing':
        switch(dir) {
          case 'both':
            w = this.getW() - this.stepX;
            h = this.getH() - this.stepY;
            if(w <= 1 || h <= 1) {
              w = 1;
              h = 1;
              this.state = 'closed';
              this.div.style.display = 'none';
            }
            this.setH(h);
            this.setW(w);
            break;
          case 'horizontal':
            w = this.getW() - this.step;
            if(w <= 1) {
              w = 1;
              this.state = 'closed';
              this.div.style.display = 'none';
            }
            this.setW(w);
            break;
          default:
            h = this.getH() - this.step;
            if(h <= 1) {
              h = 1;
              this.state = 'closed';
              this.div.style.display = 'none';
            }
            this.setH(h);
            break;
        }
        break;
    }
    if(this.state != 'open' && this.state != 'closed')
      this.idTimeout = setTimeout('g_mlibSliders["' + this.id + '"].slide("' + dir + '", "' + (callback ? callback : '') + '", true);', this.interval);
    else if(callback)
      eval(callback + "('" + this.id + "');");
  }
};


