/******
** MLIB Slicer v1.0/BS
** copyright Label Technologies 2008
** instanciation : new MlibSlicer(idTargetDiv);
** set content : g_mlibSlicers[idTargetDiv].setContent(htmlString);
** move : g_mlibSlicers[idTargetDiv].up() | g_mlibSliders[idTargetDiv].down() | g_mlibSliders[idTargetDiv].right() | g_mlibSliders[idTargetDiv].left();
** change loop interval : g_mlibSlicers[idTargetDiv].interval = intMicroseconds;
** change step : g_mlibSlicers[idTargetDiv].step = intNbPixels;
** show content : g_mlibSlicers[idTargetDiv].show();
** hide content : g_mlibSlicers[idTargetDiv].hide();
** force content position : g_mlibSlicers[idTargetDiv].forcePosition(intLeftPos, intTopPos);
**
** NB : target should be a div and should have style.height and style.width set
*******/

var g_mlibSlicers = new Array();
function MlibSlicer(id, w, h) {
  this.id = id;
  this.div = document.getElementById(id);
  if(this.div && document.getElementById(id).tagName.toLowerCase() == 'div')
    this.init(w, h);
}

MlibSlicer.prototype = {
  step: 10,
  interval: 10,
  onmove: false,
  target: null,
  init: function(w, h) {
    g_mlibSlicers[this.id] = this;
    this.div.style.overflow = "hidden";
    this.w = parseInt(this.div.style.width);
    this.h = parseInt(this.div.style.height);
    this.contentDiv = document.createElement('DIV');
    this.contentDiv.style.width = (w ? w : this.w) + 'px';
    if(h)
      this.contentDiv.style.height = h + 'px';
    this.contentDiv.style.position = 'relative';
    this.contentDiv.style.top = '0px';
    this.contentDiv.style.left = '0px';
    this.div.appendChild(this.contentDiv);
    this.div.style.position = "relative";

  },
  setContent: function(html, bolHidden) {
    if(bolHidden)
      this.hide();
    else
      this.show();
    this.contentDiv.innerHTML = html;
  },
  getContentW: function() {
    return this.contentDiv.offsetWidth;
  },
  getContentH: function() {
    return this.contentDiv.offsetHeight;
  },
  canSliceDown: function() {
    return parseInt(this.contentDiv.style.top) > -this.getContentH() + this.h;
  },
  down: function(callback, auto) {
    if(!this.canSliceDown())
      return;
    if(!auto && this.onmove)
      return;
    var top = parseInt(this.contentDiv.style.top);
    if(!this.onmove) {
      this.onmove = true;
      this.target = Math.max(-this.getContentH() + this.h, top - this.h);
    }
    top -= this.step;
    if(top <= this.target) {
      top = this.target;
      this.onmove = false;
    }
    this.contentDiv.style.top = top + "px";
    if(this.onmove)
      setTimeout("g_mlibSlicers['" + this.id + "'].down('" + (callback ? callback : '') + "', true);", this.interval);
    else if(callback)
      eval(callback + "('" + this.id + "');");
  },
  canSliceUp: function() {
    return parseInt(this.contentDiv.style.top) < 0;
  },
  up: function(callback, auto) {
    if(!this.canSliceUp())
      return;
    if(!auto && this.onmove)
      return;
    var top = parseInt(this.contentDiv.style.top);
    if(!this.onmove) {
      this.onmove = true;
      this.target = Math.min(0, top + this.h);
    }
    top += this.step;
    if(top >= this.target) {
      top = this.target;
      this.onmove = false;
    }
    this.contentDiv.style.top = top + "px";
    if(this.onmove)
      setTimeout("g_mlibSlicers['" + this.id + "'].up('" + (callback ? callback : '') + "', true);", this.interval);
    else if(callback)
      eval(callback + "('" + this.id + "');");
  },
  canSliceLeft: function() {
    return parseInt(this.contentDiv.style.left) > -this.getContentW() + this.w;
  },
  left: function(callback, auto) {
    if(!this.canSliceLeft())
      return;
    if(!auto && this.onmove)
      return;
    var left = parseInt(this.contentDiv.style.left);
    if(!this.onmove) {
      this.onmove = true;
      this.target = Math.max(-this.getContentW() + this.w, left - this.w);
    }
    left -= this.step;
    if(left <= this.target) {
      left = this.target;
      this.onmove = false;
    }
    this.contentDiv.style.left = left + "px";
    if(this.onmove)
      setTimeout("g_mlibSlicers['" + this.id + "'].left('" + (callback ? callback : '') + "', true);", this.interval);
    else if(callback)
      eval(callback + "('" + this.id + "');");
  },
  canSliceRight: function() {
    return parseInt(this.contentDiv.style.left) < 0;
  },
  right: function(callback, auto) {
    if(!this.canSliceRight())
      return;
    if(!auto && this.onmove)
      return;
    var left = parseInt(this.contentDiv.style.left);
    if(!this.onmove) {
      this.onmove = true;
      this.target = Math.min(0, left + this.w);
    }
    left += this.step;
    if(left >= this.target) {
      left = this.target;
      this.onmove = false;
    }
    this.contentDiv.style.left = left + "px";
    if(this.onmove)
      setTimeout("g_mlibSlicers['" + this.id + "'].right('" + (callback ? callback : '') + "', true);", this.interval);
     else if(callback)
      eval(callback + "('" + this.id + "');");
 },
  show: function() {
    this.contentDiv.style.display = "block";
  },
  hide: function() {
    this.contentDiv.style.display = "none";
  },
  forcePosition: function(left, top) {
    this.contentDiv.style.left = left + 'px';
    this.contentDiv.style.top = top + 'px';
  }
  
}