Gallery = function(config) {

    this.config = config;
    this.index = 0;

    this.setMediaType(this.config.initial);
    this.setShowingAlt(false);
    
    this.setupImages();
}

Gallery.prototype.setupImages = function() {
    //load images
    if (this.hasImages()) {
        for (var i=0; i<this.config.images.length; i++) {
            //there may not be any thumbs
            if (this.config.images[i].thumb != null) {
                var image = new Image();
                image.src = this.config.images[i].thumb;    
                this.config.images[i].thumbImage = image;
            }
            //pre load full images
            if (this.config.images[i].full != null) {
                var image = new Image();
                image.src = this.config.images[i].full;                
                this.config.images[i].fullImage = image;                
            }
        }    
    }
}

Gallery.prototype.image = function(idx) {
    if (!this.isShowingImage()) {
        this.swapMedia();
    }
    this._showImage(idx, Gallery.FX_FADE);
}

Gallery.prototype.hasImages = function() {
    return this.config.images != null && this.config.images.length > 0;
}

Gallery.prototype.isShowingImage = function() {
    return (this.mediaType == Gallery.TYPE_IMAGE);
}

Gallery.prototype.setMediaType = function(type) {
    this.mediaType = type;
}

Gallery.prototype._showImage = function(idx, fx) {
    if (idx == this.index) {
        return;
    }
        
    if (!this.hasImages()) {
        return;
    }
    
    var oldIndex = this.index;
         
    if (idx >= this.config.images.length) {
        idx = 0;
    } else if (idx < 0) {
        idx = this.config.images.length - 1;
    }
    
    this.changeThumbImageState(Gallery.TYPE_IMAGE, this.index, idx);     

    this.getActiveImage().src = this.config.images[idx].fullImage.src;
    
    this.index = idx;    
}

Gallery.prototype.getActiveImage = function() {
    if (this.isShowingAlt()) {
        return this.getAltFullImage();
    } else {
        return this.getFullImage();
    }
}

Gallery.prototype.isShowingAlt = function() {
    return this.showingAlt;
}

Gallery.prototype.setShowingAlt = function(val) {
    return this.showingAlt = val;
}

Gallery.prototype.changeThumbImageState = function(type, prevIndex, nextIndex) {
    var prevImg = this.getThumbAnchor(type, prevIndex);
    var nextImg = this.getThumbAnchor(type, nextIndex);  

    if (prevImg != null) {  
        prevImg.className = this.config.image.thumb.cssClass;
    }
    if (nextImg) {
        nextImg.className = this.config.image.thumb.cssActiveClass;
    }
}

Gallery.prototype.getFullImage = function() {
    return document.getElementById(this.config.image.full.elementId);
}

Gallery.prototype.getViewer = function() {
    return document.getElementById(this.config.viewer.elementId);
}

Gallery.prototype.getThumbAnchorId = function(type, index) {
    var id = null;
    if (type == Gallery.TYPE_IMAGE) {
        id = this.config.image.thumb.elementId + index;
    } else {
        id = this.config.movie.thumb.elementId + index;
    }
    return id;
}

Gallery.prototype.getThumbAnchor = function(type, index) {
    return document.getElementById(this.getThumbAnchorId(type, index));
}