/**
*
*   Gallery class
*   Support slideshows as well.
*
*/
function Gallery(image, description, url) {

    this.index          = 0;
    this.photos         = new Array();
    this.wait_interval  = 3;
    
    // Containers
    this.container_img  = document.getElementById(image);
    
    if(description)
        this.container_desc = document.getElementById(description);
        
    if(url)
        this.container_url  = document.getElementById(url);    
    
    // Be certain the instance is known
    if (!window.galleries) {
        window.galleries = {};
    }
    
    if (!window.last_gallery_id) {
        window.last_gallery_id = 1;
    } else {
        window.last_gallery_id++;
    }

    this.gallery_id = window.last_gallery_id;
    window.galleries[this.gallery_id] = this;
}

Gallery.photo = function(id, name, desc, src, alt, url) {
    
    this.id     = id;
    this.name   = name;
    this.desc   = desc;
    this.src    = src;
    this.alt    = alt;
    this.url    = url;
}

Gallery.prototype.add = function(id, name, desc, src, alt, url) {

    this.photos.push(new Gallery.photo(id, name, desc, src, alt, url));
}

Gallery.prototype.next = function() {
    
    return this.byIndex(this.index +1);
}

Gallery.prototype.previous = function() {
    
    return this.byIndex(this.index -1);
}

Gallery.prototype.byIndex = function(index) {
    
    this.index                      = this.parseIndex(index);
    this.container_img.src          = this.photos[this.index].src;
    this.container_img.alt          = this.photos[this.index].alt;
    
    if(this.container_desc)
        this.container_desc.innerHTML   = this.photos[this.index].desc;
        
    if(this.container_url)
        this.container_url.href         = this.photos[this.index].url;   
}

Gallery.prototype.length = function() {
    return this.photos.length;
}

Gallery.prototype.getIndex = function() {
    return this.index;
}

// If out of range, returns MIN or MAX 
Gallery.prototype.parseIndex = function(index) {
   
    var MIN = 0; 
    var MAX = this.photos.length -1;
    
    if(index < MIN)       
        if(MAX >= MIN)
            return MAX;
        else
            return MIN;
    if(index > MAX)
        return MIN;
    
    return index;
}

// Support for slideshows
Gallery.prototype.start = function() {
   
    this._showtime();
}

Gallery.prototype._showtime = function() {    
    
    this.next();    
    window.setTimeout('window.galleries[' + this.gallery_id + ']._showtime()', this.wait_interval * 1000);
}

Gallery.prototype.open = function() {
    
    window.open(this.photos[this.index].url);
}



Gallery.prototype.openPopup = function() {
    
    ProjectPopup(this.photos[this.index].url);
}