﻿function ImageGallery()
{
    this.index = 0;
    this.ControlID = '';
    this.gallery = new Array();
    this.lnkMain = null;
    this.imgMain = null;
    this.divThumbs = null;
    
    this.Create = function(ControlID, Gallery) {
        this.ControlID = ControlID;
        try {
            this.gallery = Gallery.Images;    
        } catch (e) {
            this.gallery = new Array();
        }
        this.lnkMain = document.getElementById('lnkMain_' + ControlID);   
        this.imgMain = document.getElementById('imgMain_' + ControlID);   
        this.divThumbs = document.getElementById('divThumbs_' + ControlID);
        
        this.DisplayImage(0);
        this.ConstructThumbs();
    },
    
    this.ConstructThumbs = function() {
        if (this.gallery.length > 1) {
            for (var i=0; i<this.gallery.length; i++) {
                var img = document.createElement("IMG");
                var href = document.createElement("A");
                
                img.src= this.gallery[i].Small;
                img.border = "0";
                href.href = "javascript:imageGallery_" + this.ControlID + ".DisplayImage(" + i + ");";
                href.appendChild(img);
                this.divThumbs.appendChild(href);      
            }
            this.divThumbs.style.display = 'block';
        } else {
            this.divThumbs.style.display = 'none';
        }
    },
    
    this.DisplayImage = function(Index) {
        try {
            if ((Index >= 0) && (Index < this.gallery.length)) {
                this.index = Index;
                this.imgMain.src = this.gallery[this.index].Medium;    
                this.lnkMain.href = this.gallery[this.index].Full;
            }
        } catch (e) {
            alert(e.message);
        }
    } 
}