﻿$.fn.slider = function (options) {
    var opts = $.extend({}, $.fn.slider.defaults, options);
    return this.each(function () {
        var $this = $(this);
        var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
        
        $(o.leftControl).css({ display: "none" }).bind("click", function (e) { $.fn.slider.move($this, 1, o); });
        if (o.max < o.increment) $(o.rightControl).css({ display: "none" });
        $(o.rightControl).bind("click", function (e) { $.fn.slider.move($this, -1, o); });
    });
};
$.fn.slider.move = function ($wrapper, dir, o) {
    if (o.scrolling) return;
    var mod = "marginTop";
    if(o.plane=="horizontal") mod = "marginLeft";
    x = $(o.scroller).css(mod) == "auto" ? 0 : parseInt($(o.scroller).css(mod));

    if (x + (dir * o.increment) < -o.max || x + (dir * o.increment) > 0)
        return;

    if (dir < 0)
        x -= o.increment;
    else
        x += o.increment;
    o.scrolling = true;
    $.fn.slider.check(o, x)

    if(mod=="marginTop")
        $(o.scroller).animate({ marginTop: x }, o.duration, null, function (e) {
            o.scrolling = false;
        });
    else
        $(o.scroller).animate({ marginLeft: x }, o.duration, null, function (e) {
            o.scrolling = false;
        });
}

$.fn.slider.check = function (o, x) {
    if (x == 0) {
        $(o.leftControl).fadeOut(o.duration);
    }
    else
        $(o.leftControl).fadeIn(o.duration);
    if (x == -o.max)
        $(o.rightControl).fadeOut(o.duration);
    else
        $(o.rightControl).fadeIn(o.duration);
}

$.fn.slider.defaults = {
    increment: 10,
    max: 100,
    duration: 200,
    plane: "vertical", //"horizontal"
    scroller: '#inner',
    leftControl: '#leftControl',
    rightControl: '#rightControl'
};

