/*  slider.js
    Built with mootools 1.2. Only developed for horizontal sliding at the moment -
    this is easily hackable for vertical sliding options. - 27/4/2009
    The outer div must be set to overflow:hidden - and must have an overflow.
    Arguments:  element_id:     the string of the id of the outer div for sliding.
                slide_width:    The width in px of one individual slide (ie: the
                                distance in px to move with each click)
                no_slides:      The number of total slides.
                no_visible_slides:  The number of slides visible in the div container
                                    specified by element_id
                left_button_id: String of the id of the 'scroll left' button.
                right_button_id: String of the id of the 'scroll right' button.
                buttons:        An assocciative array of links to scroll to specific
                                slides. For example: var buttons = {'slide_1':1,'slide_2':2};
                                Where 'slide_1' is the id of the button for slide 1 and '...:1'
                                corresponds to slide 1 index.
                auto_play:      Sets the slider to slide through each slide automatically
                                based on the 'period'.
                period:         The delay in milliseconds before continuing to the next
                                slide when in auto mode.
                loop:           When in auto mode, the slide will return to the 1st slide
                                when it reaches the end.
                auto_play_direction:    Slides 'left' or 'right' in auto mode.
*/

function slider(element_id,slide_width,no_slides,no_visible_slides,left_button_id,right_button_id,buttons,auto_play,period,loop,auto_play_direction){
    this.slider_element = $(element_id);
    this.left_button = $(left_button_id);
    this.left_button.self = this;
    this.left_button.addEvent('click',this.slide_left);
    this.right_button = $(right_button_id);
    this.right_button.self = this;
    this.right_button.addEvent('click',this.slide_right);
    this.buttons = buttons;
    for(var button_id in this.buttons){
        var button_temp = $(button_id);
        button_temp.self = this;
        button_temp.slide_no = buttons[button_id];
        button_temp.addEvent('click',function(){
            //alert((this.slide_no-1)*this.self.slide_width);
            this.self.auto_play=false;
            this.self.slide_to((this.slide_no-1)*this.self.slide_width);
            return false;
        });
    }
    this.slide_width = slide_width;
    this.no_slides = no_slides;
    this.no_visible_slides = no_visible_slides;
    this.current_pos = 0;
    this.max_pos = ((this.no_slides)-(this.no_visible_slides))*this.slide_width;
    this.min_pos = 0;
    this.auto_play=auto_play;
    this.period=period;
    this.auto_play_direction = auto_play_direction;
    this.loop = loop;
    this.timer_id = null;
    this.slidefx = new Fx.Scroll(this.slider_element, {
        offset: {
            'x': 0,
            'y': 0
        }
    });
    if(this.auto_play){
        if(this.auto_play_direction=='right')
            this.timer_id = this.slide.periodical(this.period,this,this.slide_width);
        else if(this.auto_play_direction=='left')
            this.timer_id = this.slide.periodical(this.period,this,-(this.slide_width));
    }
}

slider.prototype.slide_left = function(){
    this.self.slide(-(this.self.slide_width));
    this.self.auto_play=false;
    return false;
}

slider.prototype.slide_right = function(){
    this.self.slide(this.self.slide_width);
    this.self.auto_play=false;
    return false;
}

slider.prototype.slide = function(distance){
    if(!this.auto_play)
        $clear(this.timer_id);
    this.current_pos += distance;
    if(this.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;
    }

    this.slidefx.start(this.current_pos,0);
}

slider.prototype.slide_to = function(position){
    if(!this.auto_play)
        $clear(this.timer_id);
    this.current_pos = position;
    if(this.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;
    }

    this.slidefx.start(this.current_pos,0);
}

