/* Image Gallery Javascript */

function sdsImageGallery( image, caption, numeration, pictures, captions, links, autoloop, interval )
{
        this.frame = image;
        this.caption = caption;
        this.numeration = numeration;
        this.images = Array();
        this.pictures = pictures || Array();
        this.captions = captions || Array();
        this.links = links || Array();
        this.autoLoop = autoloop || false;
        this.interval = interval || 500;
        this.current = 0;
        this.intervalID = null;

        this.preloadImages();

        this.startAutoLoop();

        if (document.all) {
                this.interval += 2000;
        }
}

sdsImageGallery.prototype.startAutoLoop = function()
{
        var _this = this;
        
        if (this.autoLoop) {
                this.intervalID = setInterval(function(){ _this.nextLoop(); }, this.interval);
        }
}

sdsImageGallery.prototype.stopAutoLoop = function()
{
        if (this.autoLoop && this.intervalID != null) {
                window.clearInterval(this.intervalID);
        }
}
        
sdsImageGallery.prototype.preloadImages = function()
{
        var total = this.pictures.length;

        for (var i=0; i<total; i++) {
                this.images[this.images.length] = new Image();
                (this.images[this.images.length - 1]).src = this.pictures[i];
        }
}

sdsImageGallery.prototype.debug = function()
{
}

sdsImageGallery.prototype.previous = function()
{       
        if (this.current == 0) {
                this.current = this.pictures.length - 1;
        } else {
                this.current--;
        }

        this.stopAutoLoop();

        var object = document.getElementById( this.frame );
        object.src = this.pictures[this.current];

        this.update();
        this.startAutoLoop();
}

sdsImageGallery.prototype.select = function( $position )
{
        if ($position != this.current && $position > -1 && $position < this.pictures.length) {
                this.current = $position;
        } else {
                return;
        }

        this.stopAutoLoop();

        var object = document.getElementById( this.frame );
        object.src = this.pictures[this.current];

        this.update();
        this.startAutoLoop();
}

sdsImageGallery.prototype.next = function()
{
        if (this.current == this.pictures.length - 1) {
                this.current = 0;
        } else {
                this.current++;
        }

        this.stopAutoLoop();

        var object = document.getElementById( this.frame );
        object.src = this.pictures[this.current];

        this.update();
        this.startAutoLoop();
}

sdsImageGallery.prototype.nextLoop = function()
{
        if (this.current == this.pictures.length - 1) {
                this.current = 0;
        } else {
                this.current++;
        }
        
        var object = document.getElementById( this.frame );
        
	if( document.all ) {
                object.style.filter="blendTrans(duration=2)";
                object.filters.blendTrans.Apply();
  	}
	
  	object.src = this.pictures[this.current];
  	
  	if( document.all ) {
                object.filters.blendTrans.Play();
  	}

        this.update();
}

sdsImageGallery.prototype.update = function()
{
        this.changeNumeration();
        this.changeCaption();
        this.changeLink();
}

sdsImageGallery.prototype.changeNumeration = function()
{
        if (this.numeration) {
                var object = document.getElementById(this.numeration);
                if (object) {
                        object.innerHTML = (this.current+1) + '/' + this.pictures.length;
                }
        }
}

sdsImageGallery.prototype.changeCaption = function()
{
        if (this.caption) {
                var object = document.getElementById( this.caption );
                if (object) {
                        object.innerHTML = this.captions[this.current];
                }
        }
}

sdsImageGallery.prototype.changeLink = function()
{
        var link = this.links[this.current];

        if (link != '') {
                var frame = document.getElementById(this.frame);
                var frameParent = frame.parent;

                var newLink = document.createNode('<a>');
                alert(newLink);
        }
}

sdsImageGallery.prototype = new sdsImageGallery( );
