function Gallery()
{
    // some contants
    var timeout = 3000;
    
    // ids of the used html elements
    var galleryImage = "GalleryImage";
    var galleryToggleImage = "GalleryToggleImage";
    var galleryImageDescription = "GalleryImageDescription";
    var galleryPosition = "GalleryPosition";
    var positionFormat = "{0}/{1}";

    
    // implementation

    this.current_image = 0;
    this.images = new Array();
    
    this.setImage = function(i) 
    {
        obj = document.getElementById(galleryImage);
        if (obj != null) {
            obj.src = this.images[i].file;
            this.current_image = i;
        }
        
        var o_div = MM_findObj(galleryPosition);
        if (o_div)
            o_div.innerHTML = positionFormat.replace('{0}', this.current_image + 1).replace('{1}',this.images.length);

        var o_desc = MM_findObj(galleryImageDescription);
        if (o_desc)
            o_desc.innerHTML = this.images[i].description;
    }
    
    this.move = function (direction)
    {
        switch (direction) {
            case "previous":
                if (this.current_image>0) 
                    this.current_image--;
                else
                    this.current_image = this.images.length - 1;                    
                break;
                
            case "next":
                if (this.current_image < this.images.length - 1)
                    this.current_image++;
                else
                    this.current_image=0;                
                break;
                
            case "first":
                this.current_image = 0;
                break;
                
            case "last":
                this.current_image = this.images.length - 1;
                break;
        }
        
        this.setImage(this.current_image);        
    }    

    // Slideshow specific stuff

    this.slideshowStop = function()
    {
        showStopped_ = true;
        this.setImage(0);
        
        var o_image = MM_findObj(galleryToggleImage);
        if (o_image) o_image.src = '/media/gallery/icon_start.gif';
    }

    this.slideshowToggle = function()
    {
        showStopped_ = !showStopped_;

        var o_image = MM_findObj(galleryToggleImage);
        if (o_image)
            o_image.src = showStopped_ ? '/media/gallery/icon_start.gif' : '/media/gallery/icon_break.gif';
        
        window.setTimeout(Slideshow, timeout);
    }
    
    var that = this;
    var showStopped_ = true;

    function Slideshow()
    {
        if (!showStopped_)
        {
            that.move('next');
            window.setTimeout(Slideshow, timeout);
        }
    }
}

fotogallery = new Gallery();
