var ImageGallery;

ImageGallery = Class.create({

    params: {
        wait : 5000,
        imageCount : 0,
        increment : 660,
        fadeEffect: true,
        galleryContainer : "gallery_container_inner",
        controlNext : "next_gallery_control",
        controlPrev : "prev_gallery_control",
        moveEffect : Effect.Transitions.full,
        moveRestartEffect : Effect.Transitions.full
    },

    play: null,
    index: 0,

    // get and set current instance to use as singleton
    getThisObject: function(key) {
        if (undefined == ImageGallery.prototype.thisObject || null == ImageGallery.prototype.thisObject) {
            ImageGallery.prototype.thisObject = new Object();
        }
        return ImageGallery.prototype.thisObject[key];
    },
    setThisObject: function(key, val) {
        if (undefined == ImageGallery.prototype.thisObject || null == ImageGallery.prototype.thisObject) {
            ImageGallery.prototype.thisObject = new Object();
        }
        ImageGallery.prototype.thisObject[key] = val;
    },

    initialize: function(params) {
        this.params = Object.extend(this.params, params);
        if (this.params.imageCount > 0) {
            this.setThisObject('obj', this);
            this.index = 0;
            this.assignEventNext();
            this.assignEventPrev();
            this.initPlay();
        }
    },

    initPlay: function() {
        var thisObj = ImageGallery.prototype.getThisObject('obj');
        thisObj.play = setInterval(this.autoNext, this.params.wait);
    },

    restartPlay: function() {
        var thisObj = ImageGallery.prototype.getThisObject('obj');
        clearInterval(thisObj.play);
        thisObj.play = setInterval(this.autoNext, this.params.wait);
    },

    setToLimitPosition: function(last) {
        var thisObj = ImageGallery.prototype.getThisObject('obj');
        thisObj.index = 0;
        if (last) {
            thisObj.index = thisObj.params.imageCount - 1;
        }
        thisObj._move(true);
    },

    autoNext : function() {
        var thisObj = ImageGallery.prototype.getThisObject('obj');
        thisObj.index++;
        if (thisObj.index > thisObj.params.imageCount - 1) {
            thisObj.setToLimitPosition(false);
            return;
        }
        thisObj._move();
    },

    assignEventNext : function() {
        var thisObj = ImageGallery.prototype.getThisObject('obj');
        Event.observe($(thisObj.params.controlNext), "click", function(event) {
            event.stop();
            thisObj.restartPlay();
            thisObj.index++;
            if (thisObj.index > thisObj.params.imageCount - 1) {
                thisObj.setToLimitPosition(false);
                return;
            }
            thisObj._move();
        }, false);
    },

    assignEventPrev : function() {
        var thisObj = ImageGallery.prototype.getThisObject('obj');
        Event.observe($(thisObj.params.controlPrev), "click", function(event) {
            event.stop();
            thisObj.restartPlay();
            thisObj.index--;
            if (thisObj.index < 0) {
                thisObj.setToLimitPosition(true);
                return;
            }
            thisObj._move();
        }, false);
    },

    _move: function(restart) {
        var thisObj = ImageGallery.prototype.getThisObject('obj');
        var increment = -thisObj.params.increment * thisObj.index;
        var effect = thisObj.params.moveEffect;
        if (!restart || thisObj.params.fadeEffect) {
            new Effect.Fade(thisObj.params.galleryContainer, {duration: 0.5});
                setTimeout(function(){
                    new Effect.Move(
                        thisObj.params.galleryContainer, {
                            x: increment,
                            y: 0,
                            transition: thisObj.params.moveEffect,
                            mode: 'absolute'
                        }
                    );
                },800);
                setTimeout(function(){
                    new Effect.Appear(thisObj.params.galleryContainer, {duration: 0.5});
                },900);
        } else {
            new Effect.Move(
                thisObj.params.galleryContainer, {
                    x: increment,
                    y: 0,
                    transition: thisObj.params.moveRestartEffect,
                    mode: 'absolute'
                }
            );
        }

    }
});
