/**
 * Image gallery viewer
 * Author: zg, 2008-03-10
 */
var ImageList = new Array();

ImageList.addImage = function (ImageSrc) {
    var Img = new Image();
    Img.src = ImageSrc;
    this[ this.length ] = Img;
}

// Preload images
ImageList.addImage('img/1.jpg');
ImageList.addImage('img/2.jpg');
ImageList.addImage('img/3.jpg');
ImageList.addImage('img/4.jpg');
ImageList.addImage('img/5.jpg');
ImageList.addImage('img/6.jpg');
ImageList.addImage('img/7.jpg');
ImageList.addImage('img/8.jpg');
ImageList.addImage('img/9.jpg');
ImageList.addImage('img/10.jpg');
ImageList.addImage('img/11.jpg');
ImageList.addImage('img/12.jpg');
ImageList.addImage('img/13.jpg');
ImageList.addImage('img/14.jpg');
ImageList.addImage('img/15.jpg');
ImageList.addImage('img/16.jpg');
ImageList.addImage('img/17.jpg');
ImageList.addImage('img/18.jpg');
ImageList.addImage('img/19.jpg');

// Properties
ImageList.playing   = false;
ImageList.timer     = null;
ImageList.img       = null;
ImageList.imgFilter = null;
//ImageList.cur       = 0;
ImageList.cur       = Math.floor( Math.random( ) * (18) );
ImageList.delay     = 4000; // 2 sec.

// Functions
ImageList.play = function() {
    if ( !this.playing )
    {
        this.playing = true;
        this.next();
    }
}

ImageList.stop = function() {
    if ( this.playing )
    {
        clearTimeout(this.timer);
        this.timer = null;
        this.playing = false;
    }
}

ImageList.next = function() {
    if ( this.playing && this.length && this.img )
    {
        // Check end
        if ( this.cur >= this.length ) this.cur = 0;
        
        // Check complete status
        if ( !this[this.cur].complete )
        {
            setTimeout("ImageList.next()", 50); // wait, while image is loading
            return false;
        }
        
        if ( this.imgFilter )
        {
            // Set random transition
            if ( typeof(this.imgFilter.transition) != 'undefined' )
                this.imgFilter.transition = Math.floor(Math.random() * 99);
            
            this.imgFilter.apply();
        }
        
        // Change image
        this.img.src = this[ this.cur++ ].src;
        
        // Set next timer
        this.timer = setTimeout("ImageList.next()", this.delay);
        
        // Play filter
        if ( this.imgFilter ) this.imgFilter.play();
        
        return true;
    }
}

ImageList.setImg = function(Img) {
    this.img = Img;
    this.imgFilter = null;
    
    try
    {
        // Try to get filter (IE only)
        for (var i in Img.filters)
        {
            if ( typeof(Img.filters[i]) != 'object' ) continue;
            if ( typeof(Img.filters[i].apply) == 'undefined' ) continue;
            if ( typeof(Img.filters[i].play ) == 'undefined' ) continue;
            
            this.imgFilter = Img.filters[i];
            break;
        }
    }
    catch(e)
    {
        // No filter :'(
    }
}