// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
/**
 * Javascript for spip tuning
 *
 * @author     Bertrand Gugger <bertrand@toggg.com>
 * @copyright  2006 bertrand Gugger / toggg
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 * @version    CVS: $Id: Exp $
 * @link       http://toggg.com/
 */
/**
 * Diaporama handler
 */
var diaporamas = new Array();
function Diaporama(options)
{
    this.courante = new Object;
    this.target = new Object;
    this.id = diaporamas.length;
    this.timeOut = 3000;
    this.maxH = 0;
    this.maxL = 0;
    for (opt in options) {//alert(opt+':'+options[opt]);
        this[opt] = options[opt];
    }
    diaporamas[this.id] = this;
    this.tabimg = new Array();
    this.length = arguments.length - 1;
    this.maxH = 150;
    this.maxL = 200;
    for (var i = 0; i < this.length; ++i) {
        this.tabimg[i] = new Object;
        this.tabimg[i].src    = arguments[i + 1].src;
        this.tabimg[i].heigth = this.maxH;        
        this.tabimg[i].width  = this.maxL;
    }
}
Diaporama.prototype.prochaine = function(img, timeOut)
{
    if (!img.id) {
        return;
    }
    if (!timeOut) {
        timeOut = this.timeOut;
    }
    if (typeof(this.courante[img.id]) == 'undefined') {
        this.courante[img.id] = 1;
        this.target[img.id] = img;
    } else {
        this.courante[img.id] = (this.courante[img.id] + 1) % this.length;
    }
    if (!this.tabimg[this.courante[img.id]].loaded) {
        this.tabimg[this.courante[img.id]].loaded = new Image();
        this.tabimg[this.courante[img.id]].loaded.src = this.tabimg[this.courante[img.id]].src;
    }
    this.delai(img.id, timeOut);
}
Diaporama.prototype.delai = function(imgId, timeOut)
{
if (window.location.search.match(/debug=ie6/)) {
    setTimeout(
        new Function('diaporamas[' + this.id + '].putImage("' + imgId + '");'),
        timeOut);
} else {
    setTimeout('diaporamas[' + this.id + '].putImage("' + imgId + '");', timeOut);
}
}
Diaporama.prototype.putImage = function(imgId)
{
    if (!this.tabimg[this.courante[imgId]].loaded.complete) {
if (window.location.search.match(/debug=complete/)) {
    alert('image pas complete');
}
        this.delai(imgId, 100);
        return;
    }
    with (this.target[imgId]) {
        src = this.tabimg[this.courante[imgId]].loaded.src;
        heigth = this.tabimg[this.courante[imgId]].heigth;
        width = this.tabimg[this.courante[imgId]].width;
        style.marginLeft = ((this.maxL - width ) >> 1) + 'px';
        style.marginTop  = ((this.maxH - heigth) >> 1) + 'px';
    }
    this.prochaine(this.target[imgId]);
}


