/* 
 * BannerSlide
 * options:
 *   delay
 *   duration
 */

var BannerSlide = new Class.create();
BannerSlide.prototype = {
    initialize: function(element, nav, options) {
        banner = this;
        this.options = Object.extend({
            delay : 1.0 ,
            duration : 1.0
        }, options || {});
        this.slides = $(element).select('.slide');
        this.nav = nav.select('a');
        this.currentBanner = 0;
        this.locked = false;
        this.nav.each(function(item){
            item.observe('click',(function(event){
                event.stop();
                slideRel = $(this).readAttribute('rel');
                patt = /slide([0-9]+)/i;
                result = patt.exec(slideRel);
                if(result){
                    if( result[1] <= banner.slides.size() && banner.currentBanner != result[1]-1 ){
                        if(!banner.locked){
                            Effect.Queues.get('myscope').invoke('cancel');
                            banner.showBanner(result[1]-1,0);
                        }
                    }
                }
            }))
        });
        new Effect.Appear(this.slides[this.currentBanner],{
            duration: this.options.duration,
            afterFinish: (function (){
                this.showNextBanner();
            }).bind(this)
        });

    },
    showNextBanner: function(delay){
        if(typeof delay == 'undefined')
            delay = this.options.delay;
        if(this.currentBanner < this.slides.size()-1)
            next = this.currentBanner+1;
        else
            next = 0;
        this.showBanner(next,delay);
    },
    showBanner: function(index, delay){
        banner = this;
        if(index < banner.currentBanner){
            new Effect.Fade(banner.slides[banner.currentBanner],{
                delay: delay,
                duration: banner.options.duration,
                afterSetup: function(){
                    banner.locked = true;
                    banner.navChange(index);
                    if(Prototype.Browser.IE){
                        banner.slides[banner.currentBanner].select('.banner_content')[0].hide();
                        banner.slides[index].show();
                    }else{
                        Effect.Appear(banner.slides[index],{duration: banner.options.duration});
                    }
                },
                afterFinish: (function (){
                    if(Prototype.Browser.IE){
                        banner.slides[banner.currentBanner].select('.banner_content')[0].show();
                    }
                    banner.currentBanner = index;
                    banner.locked = false;
                    banner.showNextBanner();
                }),
                queue: {position: 'end', scope: 'myscope',limit:2}
            });
        }else{
            new Effect.Appear(banner.slides[index],{
                delay: delay,
                duration: banner.options.duration,
                afterSetup: function(){
                    banner.locked = true;
                    banner.navChange(index);
                    if(Prototype.Browser.IE){
                        banner.slides[banner.currentBanner].select('.banner_content')[0].hide();
                    }else{
                        Effect.Fade(banner.slides[banner.currentBanner],{duration: banner.options.duration});
                    }
                },
                afterFinish: (function (){
                    if(Prototype.Browser.IE){
                        banner.slides[banner.currentBanner].hide();
                        banner.slides[banner.currentBanner].select('.banner_content')[0].show();
                    }
                    banner.currentBanner = index;
                    banner.locked = false;
                    banner.showNextBanner();
                }),
                queue: {position: 'end', scope: 'myscope', limit:2}
            });
        }
    },
    navChange: function(newIndex){
        banner = this;
        currentNav = banner.nav.find(function(link){
            if ( link.readAttribute('rel') != 'slide'+(banner.currentBanner+1) )
                return false;
            return link;
        });
        nextNav = banner.nav.find(function(link){
            if ( link.readAttribute('rel') != 'slide'+(newIndex+1) )
                return false;
            return link;
        });
        currentNav.toggleClassName('selected');
        nextNav.toggleClassName('selected');
    }
}

