/**
 * Bibliothèque de fonctions pour l'animation de blocs de texte
 *
 * @author J-S.CONAN - Eskape (c) 2006
 * @version 1.0
 * @date dec 2006
 */

// table des objets créés
var _BOXES = new Array();

// constructeur
function FloatingBox(el)
{
  this.width = 0;
  this.height = 0;
  this.element = null;
  this.elementID = '';
  this.steps = 10;
  this.dx = 0;
  this.dy = 0;
  this.direction = -1;
  this.handle = 0;
  this.delay = 2;
  this.index = _BOXES.length;
  _BOXES[ _BOXES.length ] = this;
}

// initialisation du bloc
FloatingBox.prototype.init = function(el, dx, dy, steps)
{
  if( typeof(el) == 'string' ) el = document.getElementById(el);
  if( typeof(el) == 'object' )
  {
    el._fbox = this;
    this.element = el;
    this.elementID = el.elementID;
    el.style.overflow = 'hidden';
  }
  this.left = this.element.offsetLeft;
  this.top = this.element.offsetTop;
  this.width = this.element.offsetWidth;
  this.height = this.element.offsetHeight;
  this.x = 0;
  this.y = 0;

  if( typeof(steps) != 'undefined' ) this.steps = steps;

  if( (typeof(dx) != 'undefined') && dx ) this.dx = this.width / this.steps;
  else this.dx = 0;

  if( (typeof(dy) != 'undefined') && dy ) this.dy = this.height / this.steps;
  else this.dy = 0;

  if( this.element.className.match(/\s*invisible\s*/) ) this.direction = 1;
  else this.direction = -1;
}

// enroule / déroule le bloc
FloatingBox.prototype.switchIt = function()
{
  if( this.handle ) window.clearTimeout(this.handle);
  if( this.direction < 0 )
  {
    this.handle = window.setTimeout('_BOXES['+this.index+'].collapse();', this.delay);
  }
  else
  {
    this.element.className = this.element.className.replace(/\s*invisible\s*/,'');
    this.handle = window.setTimeout('_BOXES['+this.index+'].expand();', this.delay);
  }
  this.direction = -this.direction;
}

// déroule le bloc
FloatingBox.prototype.expand = function()
{
  var wok = true, hok = true;
  if( typeof(this.element) == 'undefined' ) return;
  if( this.dx && (this.element.offsetWidth < this.width) )
  {
    var w = this.element.offsetWidth + this.dx;
    wok = false;
    if( w > this.width )
    {
      w = this.width;
      wok = true;
    }
    this.element.style.width = w + 'px';
  }
  if( this.dy && (this.element.offsetHeight < this.height) )
  {
    var h = this.element.offsetHeight + this.dy;
    hok = false
    if( h > this.height )
    {
      h = this.height;
      hok = true;
    }
    this.element.style.height = h + 'px';
  }

  if( wok && hok )
    this.handle = 0;
  else
    this.handle = window.setTimeout('_BOXES['+this.index+'].expand();', this.delay);
}

// enroule le bloc
FloatingBox.prototype.collapse = function()
{
  var wok = true, hok = true;
  if( typeof(this.element) == 'undefined' ) return;
  if( this.dx && (this.element.offsetWidth > this.dx) )
  {
    var w = this.element.offsetWidth - this.dx;
    wok = false;
    if( w <= this.dx )
    {
      w = this.dx;
      wok = true;
    }
    this.element.style.width = w + 'px';
  }
  if( this.dy && (this.element.offsetHeight > this.dy) )
  {
    var h = this.element.offsetHeight - this.dy;
    hok = false
    if( h <= this.dy )
    {
      h = this.dy;
      hok = true;
    }
    this.element.style.height = h + 'px';
  }

  if( wok && hok )
  {
    this.element.className += ' invisible';
    this.handle = 0;
  }
  else this.handle = window.setTimeout('_BOXES['+this.index+'].collapse();', this.delay);
}

// prépare des blocs
function createFloatingBox(boxes)
{
  for(var i=0; i < boxes.length; i++)
  {
    var t = document.getElementById(boxes[i].title);
    var b = new FloatingBox();
    b.init(boxes[i].box, 0, 1);
    if( t )
    {
      t.style.cursor = 'pointer';
      t.onclick = new Function('_BOXES['+b.index+'].switchIt();');
    }
  }
}
