<!--

/**
 * @author Ferdy Perdaan
 * @copyright Copyright (c) 2010, WFIT
 * @version 1.0
 */

/** GALLERY ------------------------------------------------------------------------------------------------------------------------------------- **/
function Gallery(){return {
    _init : function(container, config){
        this.meta    = {container:container, gallery:new Array(), shown:new Array(), current:(config && config.currentScreen ? config.currentScreen : 0), manual:false, timer:null};
        this.config  = {order:'SHUFFLE', repeat:true, time:{checkManual:30000, delay:3000}}.merge(config);
        this.animation = null;

        this.meta.shown[0] = this.meta.current;

        //CAPTURE ALL ITEM ELLEMENTS IN THE GALLERY
        var item = Element.getElementsByClassName(container, 'Item');
        for(var i in item){
            if(item[i].tagName)
                this.meta.gallery[i] = item[i];
        }
	
		//UNABLE TO AUTO CONTINUE WHEN USING IE
		if(getIEVersionNumber() !== 0)
			this.config.time.checkManual = 0;
    },

    _setNext : function(){
        var key = this.meta.shown.getKey(this.meta.current);
        if(key === null || key == this.meta.shown.length - 1){
            switch(this.config.order){
                case 'SHUFFLE':                             //SHUFFLE
                    if(this.meta.shown.length < this.meta.gallery.length){
                        do{
                            this.meta.current = Math.floor(Math.random() * this.meta.gallery.length);
                        }while(this.meta.shown.inObject(this.meta.current));
                    }else if(this.config.repeat){
                        this.meta.shown = [this.meta.current];
                        this._setNext();
                    }else{
                        this.meta.current = this.meta.shown[0];
                    }
                    break;
                case 'DESC':                                //DESC
                    if(this.meta.shown.length < this.meta.gallery.length){
                        do{
                            this.meta.current--;
                            if(this.meta.current < 0)
                                this.meta.current = this.meta.gallery.length - 1;
                        }while(this.meta.shown.inObject(this.meta.current));
                    }else if(this.config.repeat){
                        this.meta.shown = [this.meta.current];
                        this._setNext();
                    }else{
                        this.meta.current = this.meta.shown[0];
                    }
                    break;
                default:                                    //ASC
                    if(this.meta.shown.length < this.meta.gallery.length){
                        do{
                            this.meta.current++;
                            if(this.meta.current >= this.meta.gallery.length)
                                this.meta.current = 0;
                        }while(this.meta.shown.inObject(this.meta.current));
                    }else if(this.config.repeat){
                        this.meta.shown = [this.meta.current];
                        this._setNext();
                    }else{
                        this.meta.current = this.meta.shown[0];
                    }
            }
            if(!this.meta.shown.inObject(this.meta.current))
                this.meta.shown[this.meta.shown.length] = this.meta.current;
        }else if(key !== null){
            this.meta.current = this.meta.shown[parseInt(key) + 1];
        }
    },

    _setPrevious : function(){
        var key = this.meta.shown.getKey(this.meta.current);
        if(key !== null && key - 1 >= 0)
            this.meta.current = this.meta.shown[parseInt(key) - 1];
        else
            this.meta.current = this.meta.shown[this.meta.shown.length - 1];
    },

    _checkManual : function(screen){
        if(this.meta.manual && (this.config.repeat || this.meta.shown.length < this.meta.gallery.length)){
            if(screen == this.meta.current)
                this.start();
            else if(this.config.time.checkManual)
            	setTimeout(function(object, screen){object._checkManual(screen)}, this.config.time.checkManual, this, this.meta.current);
        }
    },

    next : function(_userAction){
        this._next();

        if(_userAction){
            this.meta.manual = 'NEXT';
            if(this.config.time.checkManual)
            	setTimeout(function(object, screen){object._checkManual(screen)}, this.config.time.checkManual, this, this.meta.current);
        }
    },

    previous : function(_userAction){
        this._previous();

        if(_userAction){
            this.meta.manual = 'PREVIOUS';
            if(this.config.time.checkManual)
            	setTimeout(function(object, screen){object._checkManual(screen)}, this.config.time.checkManual, this, this.meta.current);
        }
    },

    //PANEL SUPPORT
    start : function(){
        if(this.meta.manual){
            this.meta.manual = false;
            this._start();
        }
    },

    stop : function(){
        if(!this.meta.manual){
            if(this.meta.timer != null){
                clearTimeout(this.meta.timer);
                this.meta.timer = null;
            }
            if(this.config.time.checkManual)
            	setTimeout(function(object, screen){object._checkManual(screen)}, this.config.time.checkManual, this, this.meta.current);

            this.meta.manual = 'NONE';
            this._stop();
        }
    }
}}

/** FADE GALLERY -------------------------------------------------------------------------------------------------------------------------------- **/
FadeGallery.prototype = new Gallery();

function FadeGallery(container, config){
    this._init(container, config);

    //SET ANIMATION
    this.animation  = new Array();
	this.meta.timer = setTimeout(callbackTimeout(this, this.next), this.config.time.delay); 
	
    //PREPARE SWITCHING
    for(var i in this.meta.gallery){
        if(this.meta.gallery[i].tagName){
            this.animation[i] = new FadeAnimation(this.meta.gallery[i], this.config.animation, this);
            Element.setOpacity(this.meta.gallery[i], this.animation[i].config.fade.min);
        	this.meta.gallery[i].style.position   = 'absolute';
            this.meta.gallery[i].style.top        = '0px';
            this.meta.gallery[i].style.left       = '0px';
            this.animation[i].meta.fadeAway       = false;
        }
    }
    this.animation[this.meta.current].meta.fadeAway = true;
    Element.setOpacity(this.meta.gallery[this.meta.current], this.animation[this.meta.current].config.fade.max);
}

FadeGallery.prototype._next = function(){
    if(this.animation[this.meta.current].inAnimation())
        this.animation[this.meta.current].meta.fadeAway = !this.animation[this.meta.current].meta.fadeAway;
    else
        this.animation[this.meta.current].start();

    this._setNext();

    if(this.animation[this.meta.current].inAnimation())
        this.animation[this.meta.current].meta.fadeAway = !this.animation[this.meta.current].meta.fadeAway;
    else
        this.animation[this.meta.current].start();
}

FadeGallery.prototype._previous = function(){
    if(this.animation[this.meta.current].inAnimation())
        this.animation[this.meta.current].meta.fadeAway = !this.animation[this.meta.current].meta.fadeAway;
    else
        this.animation[this.meta.current].start();

    this._setPrevious();

    if(this.animation[this.meta.current].inAnimation())
        this.animation[this.meta.current].meta.fadeAway = !this.animation[this.meta.current].meta.fadeAway;
    else
        this.animation[this.meta.current].start();
}

//PANEL SUPPORT
FadeGallery.prototype._start = function(){
    var start = false;
    for(var i in this.animation){
        if(this.animation[i].start && Element.getStyle(this.meta.gallery[i], 'opacity') > this.animation[i].config.fade.min / 100 && Element.getStyle(this.meta.gallery[i], 'opacity') < this.animation[i].config.fade.max / 100){
            this.animation[i].start();
            start = true;
        }
    }

    if(!start)
        this.next();
}

FadeGallery.prototype._stop = function(){
    for(var i in this.animation){
        if(this.animation[i].stop)
            this.animation[i].stop();
    }
}

//HANDLER
FadeGallery.prototype.afterStop = function(animation){
    if(!this.meta.manual && Element.getStyle(this.meta.gallery[this.meta.current], 'opacity') >= this.animation[this.meta.current].config.fade.max / 100)
        this.meta.timer = setTimeout(callbackTimeout(this, this.next), this.config.time.delay);
}

/** SLIDE GALLERY ------------------------------------------------------------------------------------------------------------------------------- **/
SlideGallery.prototype = new Gallery();

function SlideGallery(container, config){
    this._init(container, config);

    this.config        = {move:1.5}.merge(this.config);
    this.meta.previous = null;
    this.meta.viewport = Element.getDimension(this.meta.container);

    for(var i in this.meta.gallery){
        if(this.meta.gallery[i].tagName){
            this.meta.gallery[i].style.position = 'absolute';
            this.meta.gallery[i].style.top      = '0px';
            this.meta.gallery[i].style.left     = (-this.meta.viewport.width) + 'px';
            this.meta.gallery[i].style.width    = this.meta.viewport.width + 'px';
            
            this.meta.gallery[i].style.paddingLeft   = '0px';
        }
    }
    this.meta.gallery[this.meta.current].style.left  = '0px';

    //START
    this.meta.timer = setTimeout(callbackTimeout(this, this.next), this.config.time.delay);
}

SlideGallery.prototype._next = function(){
    this.meta.previous = this.meta.current;
    this._setNext();
    this._animate();
}

SlideGallery.prototype._previous = function(){
    this.meta.previous = this.meta.current;
    this._setPrevious();
    this._animate();
}

//PANEL SUPPORT
SlideGallery.prototype._start = function(){
    this._animate();
}

SlideGallery.prototype._stop = function(){

}

//ANIMATION
SlideGallery.prototype._animate = function(){
    this.meta.gallery[this.meta.previous].style.paddingLeft = (parseInt(Element.getStyle(this.meta.gallery[this.meta.previous], 'padding-left')) + this.config.move) + 'px';
    this.meta.gallery[this.meta.current].style.paddingLeft = (parseInt(Element.getStyle(this.meta.gallery[this.meta.current], 'padding-left')) + this.config.move) + 'px';

    if(parseInt(Element.getStyle(this.meta.gallery[this.meta.previous], 'padding-left')) >= this.meta.viewport.width){
        this.meta.gallery[this.meta.previous].style.left = (-this.meta.viewport.width) + 'px';
        this.meta.gallery[this.meta.current].style.left  = '0px';

        this.meta.gallery[this.meta.previous].style.paddingLeft = '0px';
        this.meta.gallery[this.meta.current].style.paddingLeft  = '0px';

        if(!this.meta.manual)
            this.meta.timer = setTimeout(callbackTimeout(this, this.next), this.config.time.delay);
    }else{
    	setTimeout(callbackTimeout(this, this._animate), this.config.time.speed);
    }
}

/** SOME GALLERY -------------------------------------------------------------------------------------------------------------------------------- **/
SomeGallery.prototype = new Gallery();

function SomeGallery(container, config){
}

SomeGallery.prototype._next = function(){

}

SomeGallery.prototype._previous = function(){

}

//PANEL SUPPORT
SomeGallery.prototype._start = function(){

}

SomeGallery.prototype._stop = function(){

}

//HANDLER
SomeGallery.prototype.afterStop = function(animation){

}


//-->
