/**
 * Slider component
 * 
 * @copyright: Solid Parnters i-design B.V.
 * @author: Sven Moleman
 * @requirements: Mootools 1.2 
 * @param {SlideShow} SlideShow
 */
if (console == undefined) {
    var console = {
        log: function(value) {
            alert(value);
        }
    };
}

var SlideShow = new Class({
    options:{
        'slideSpeed': 5000,
        'slideComplete': function(){}
    },
    initialize: function(object,options){
        this.setOptions(options);

        this.object = object;
        this.slides = object.getChildren();
        this.slides.setStyle('opacity','0');
        this.selectedIndex = object.getChildren().length - 1;
        this.slideLength = object.getChildren().length;
    },
    start: function()   {
//      console.log('starting periodical slideshow');
        this.slides[this.selectedIndex].setStyle('opacity',1);
        this.periodicalId = this.slide.periodical(this.options.slideSpeed, this);
        return this.periodicalId;
    },
    stop: function()    {
//      console.log('stopping periodical slideshow (id:'+this.periodicalId+')');
        $clear(this.periodicalId);
    },
    slide: function(index)  {
        if (index == undefined) {
            index = ((this.selectedIndex+1) >= this.slideLength) ? 0 : (this.selectedIndex+1);
        }
//      console.log('sliding: '+index);
        this.slides.each(function(slide,slideIndex)  {
            if (slideIndex == index)    {
//              console.log('show');
                //new Fx.Style(slide, 'opacity', {duration:this.options.fadeSpeed, wait:false}).start(1);
                slide.set('tween',{duration: 1200});
                slide.fade(1);
                //this.fireEvent('slideComplete',this);
                this.slideComplete();
            }
            if (slideIndex == this.selectedIndex)   {
//              console.log('hide');
                //new Fx.Style(slide, 'opacity', {duration:this.options.fadeSpeed, wait:false}).start(0);
                slide.set('tween',{duration: 1200});
                slide.fade(0);
            }
        }.bind(this));
        this.selectedIndex = index;
    },
    slideComplete: function() {
        this.options.slideComplete();
    }
});
SlideShow.implement(new Options);

SlideShow.implement(new Events);