var slidesHolder;
var StorySlider = Class.create({
    
    id: null,
    selector: null,
    executer: null,
    numSlides: 0,
    slidesHolder: null,
    currentIndex: 0,
    width: 320,
    transitionType: 'slide',
    module_prefix: 'slider-module-',
    
    initialize: function (sliderId, options){
    
        this.id = sliderId;
    
        this.selector = '#'+sliderId;
        this.numSlides = $$(this.selector+' .slider-modules-holder .slider-module').length;
        slidesHolder = $$(this.selector+' .slider-modules-holder').first();
        
        if(options){
            if(options.width){
                this.width = options.width;
            }
            if(options.transitionType == 'fade'){
                this.transitionType = 'fade';
            }else{
                this.transitionType = 'slide';
            }
            if(options.module_prefix){
                this.module_prefix = options.module_prefix;
            }
        }
        
        // create id if not already present
        if(!slidesHolder.id){
            slidesHolder.id = sliderId+'-modules-holder';
        }
        
        this.slidesHolder = slidesHolder;
        this.slidesHolder.style.width = this.width*this.numSlides+'px';
        
        if(navigator.appName && navigator.appName == 'Microsoft Internet Explorer'){
            var new_id = this.module_prefix + '0';
            $(new_id).style.display = 'block';
        }
    
    },
    
    next: function(){
        if(this.numSlides == (this.currentIndex+1)){
            // already on the last slide
            this.reset();
        }else{
            // still more slides to come
            this.goTo(this.currentIndex+1);
        }
    },
    
    slide: function(numSlides){
        var xPos = numSlides*this.width*-1;
        new Effect.Move(this.slidesHolder, {x: xPos, y:0, transition: Effect.Transitions.sinoidal, duration: 1.1});
    },
    
    goTo: function (slideNum){
        if(navigator.appName && navigator.appName == 'Microsoft Internet Explorer'){
            var new_id = this.module_prefix + slideNum;
            $(new_id).style.display = 'block';
            var old_id = this.module_prefix + this.currentIndex;
            $(old_id).style.display = 'none';
            // alert(navigator.appName);
        }else{
            var sDiff = slideNum-this.currentIndex;
            this.slide(sDiff);
        }
        this.currentIndex = slideNum;
    },
    
    reset: function(){
        var sDiff = this.currentIndex;
        var xPos = sDiff*this.width;
        
        this.currentIndex = 0;
        
        if(navigator.appName && navigator.appName == 'Microsoft Internet Explorer'){
            var old_id = this.module_prefix + this.currentIndex;
            $(old_id).style.display = 'none';
            var new_id = this.module_prefix + '0';
            $(new_id).style.display = 'block';
        }else{
            this.slidesHolder.fade({duration: 0.4});
            new Effect.Move(this.slidesHolder, {x: xPos, y:0, transition: Effect.Transitions.sinoidal, duration: 0.01, delay:0.5});
            new Effect.Appear(this.slidesHolder.id, {duration: 0.5, delay:0.6});
        }
    }

});