﻿var VerticalSlider = function(slider, nextButton, prevButton, options) {
    var me = this;

    this.$slider = $j(slider);
    this.$nextBtn = $j(nextButton);
    this.$prevBtn = $j(prevButton);
    this.$curSlide = this.$slider.children(':first');
    this.options = {
        duration: 'normal',
        scrollOptions: {}
    };

    if (options) {
        $j.extend(this.options, options);
    }

    this.slide = function(direction) {
        if (me.$slider.children().length == 0) {
            return false;
        } else if (me.$curSlide.find(':visible').length == 0) {
            me.$curSlide = me.$slider.children(':first');
        }

        var sliderHeight = me.$slider.innerHeight();
        var $slides = me.$slider.children();
        var $newElm = me.$curSlide;
        var totalHeight = (direction > 0) ? $newElm.outerHeight(true) : 0;

        while (1) {
            if (direction > 0) {
                if ($newElm.get(0) == me.$slider.children(':last').get(0)) {
                    break;
                }
                $newElm = $newElm.next();
            } else {
                if ($newElm.get(0) == me.$slider.children(':first').get(0)) {
                    break;
                }
                $newElm = $newElm.prev();
            }

            totalHeight += $newElm.outerHeight(true);

            if (totalHeight > sliderHeight) {
                break;
            }
        }

        if ($newElm.get(0) != me.$curSlide.get(0)) {
            me.$curSlide = $newElm;
            me.$slider.scrollTo(me.$curSlide, me.options.duration, me.options.scrollOptions);
        }
    };

    this.$nextBtn.click(function() {
        me.slide(-1);
        return false;
    });

    this.$prevBtn.click(function() {
        me.slide(1);
        return false;
    });
};
