AMLI.Images = {};

AMLI.Images.Gallery = function() {

    return {
        init: function(galleryData) {

            this.albums = new Array();

            for (var i in galleryData.albs) {

                var album;

                if (galleryData.albs[i].is360 == 'true') {

                    album = new AMLI.Images.Tour();
                } else {

                    album = new AMLI.Images.Album();
                }

                album.init(galleryData.albs[i], i.toString() + '_gallerydiv');
                this.albums[this.albums.length] = album;
            }
        },
        showImage: function(albumID, imageID) {

            if (this.albums) this.albums[albumID].swap(imageID);
        }
    };
};

// TODO: This whole class to control the 360 tours.
AMLI.Images.Tour = function() {
    return {
        init: function(albumData, mainid) {

            this.container = _get(mainid);

            if (!this.container) {
                //alert('Developer error!');
                return;
            }

            this.data = albumData;
            this.elements = new Array();

            for (var i in this.data.imgs) {
                var img = this.data.imgs[i];
                var index = this.elements.length;

                // TODO
            }
        },
        swap: function(imageIndex) {

            // TODO
        }
    };
}

AMLI.Images.Album = function() {

    return {
        init: function(albumData, mainid) {

            this.container = _get(mainid);

            if (!this.container) {
                alert('Developer error!');
                return;
            }

            this.data = albumData;
            this.elements = new Array();

            for (var i in this.data.imgs) {
                var img = this.data.imgs[i];
                var index = this.elements.length;

                this.elements[index] = this.container.appendChild(document.createElement('div'));
                this.elements[index].image = this.elements[index].appendChild(document.createElement('img'));
                this.elements[index].image.src = img.f;

                //trying to prevent incorrectly sized images like the ones in 2nd from messing things up
                this.elements[index].style.width = '373px';
                this.elements[index].style.height = '276px';
                this.elements[index].image.style.width = '373px';

                this.elements[index].style.position = 'absolute';
                this.elements[index].style.visibility = (i == '_0') ? 'visible' : 'hidden';
            }

            this.activeImg = this.elements[0];
        },
        swap: function(imageIndex) {

            if (this.elements) {

                if (this.activeImg != this.elements[imageIndex]) {

                    if (!this.activeImg.image.style.opacity || this.activeImg.image.style.opacity == 1) {

                        this.activeImg.animation = new AMLI.Effects.Animation(
                                        [{ element: this.activeImg, type: 'opacity', duration: 450 }
                                        , { element: this.elements[imageIndex], type: 'opacity', duration: 450}]
                                    );
                        this.activeImg.animation.run([1, 0], [0, 1]);
                        this.activeImg = this.elements[imageIndex];

                        //AMLI.Images.SwapStop = true;

                        setTimeout('AMLI.Images.SwapStop=false;', 500);

                        //this.activeImg.style.display = 'none';
                        //this.activeImg.style.display = 'block';
                        //this.activeImg.animation.run([0, 1]);
                    }
                }
            }
        }
    };
};