var slider = new Class({
    Implements:[Options,Events],
    options:{
        element_id:'panel',
        slide_width:100,
        no_slides:6,
        no_visible_slides:3,
        left_button_id:false,
        right_button_id:false,
        buttons:false,
        auto_play:true,
        period:3000,
        loop:true,
        auto_play_direction:'next',
        orientation:'horizontal'
    },
    initialize:function(options){
        this.setOptions(options);
        this.timer_id = null;
        if(this.options.left_button_id)$(this.options.left_button_id).addEvent('click',this.slide_left.bind(this));
        if(this.options.right_button_id)$(this.options.right_button_id).addEvent('click',this.slide_right.bind(this));
        if(this.options.buttons){
            for(var button_id in this.options.buttons){
                var button_temp = $(button_id);
                button_temp.slide_no = this.options.buttons[button_id];
                button_temp.addEvent('click',function(e){
                    this.options.auto_play=false;
                    this.slide_to((e.target.slide_no-1)*this.options.slide_width);
                    return false;
                }.bind(this));
            }
        }
        this.current_pos = 0;
        if(this.options.orientation=='horizontal')this.max_pos = (this.options.no_slides*this.options.slide_width)-($(this.options.element_id).getSize().x);
        else this.max_pos = (this.options.no_slides*this.options.slide_width)-($(this.options.element_id).getSize().y);
        this.min_pos = 0;
        this.slidefx = new Fx.Scroll(this.options.element_id,{
            offset: {
                'x': 0,
                'y': 0
            }
        });
        if(this.options.auto_play){
            if(this.options.auto_play_direction=='next')
                this.timer_id = this.slide.periodical(this.options.period,this,this.options.slide_width);
            else if(this.options.auto_play_direction=='previous')
                this.timer_id = this.slide.periodical(this.options.period,this,-(this.options.slide_width));
        }
    },
    slide:function(distance){
        if(!this.options.auto_play)
            $clear(this.timer_id);
        this.current_pos += distance;
        if(this.options.loop){
            if(this.current_pos>this.max_pos)this.current_pos=this.min_pos;
            if(this.current_pos<this.min_pos)this.current_pos=this.max_pos;
        }else{
            if(this.current_pos>this.max_pos)this.current_pos=this.max_pos;
            if(this.current_pos<this.min_pos)this.current_pos=this.min_pos;
        }
        if(this.options.orientation=='horizontal')this.slidefx.start(this.current_pos,0);
        else this.slidefx.start(0,this.current_pos);
    },
    slide_to:function(position){
        if(!this.options.auto_play)
            $clear(this.timer_id);
        this.current_pos = position;
        if(this.options.loop){
            if(this.current_pos>this.max_pos)this.current_pos=this.min_pos;
            if(this.current_pos<this.min_pos)this.current_pos=this.max_pos;
        }else{
            if(this.current_pos>this.max_pos)this.current_pos=this.max_pos;
            if(this.current_pos<this.min_pos)this.current_pos=this.min_pos;
        }
        if(this.options.orientation=='horizontal')this.slidefx.start(this.current_pos,0);
        else this.slidefx.start(0,this.current_pos);
    },
    slide_left:function(){
        this.slide(-(this.options.slide_width));
        this.options.auto_play=false;
        return false;
    },
    slide_right:function(){
        this.slide(this.options.slide_width);
        this.options.auto_play=false;
        return false;
    }
});
