﻿/*
* jQuery vScroll
* Copyright (c) 2010 Simon Hibbard
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:

* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE. 
*/

/*
* Version: V1.0.0
* Release: 06-08-2010
* Based on jQuery 1.4.2
*/

(function ($) {
    $.fn.vScroll = function (options) {
        var move = 0;
        var defaults = {
            speed: 500,
            upID: "#up-arrow",
            downID: "#bottom-arrow",
            hasSeparator: false,
            separatorImg: ''
        };
        var options = $.extend(defaults, options);

        return this.each(function () {
            var hideArrows = true;
            obj = $(this);
            obj.css("overflow", "hidden");
            obj.children().each(
                function (intIndex) {
                    $(this).addClass("vscroll-" + intIndex);
                    if (intIndex > 4) {
                        hideArrows = false;
                    }
                });

            if (hideArrows) {
                $(options.upID).hide();
                $(options.downID).hide();
            }

            var itemCount = 0;

            $(options.downID).click(function () {
                if (move < ($('#vscroller').children().size() - 5)) {
                    var nextCount = itemCount + 1;
                    if ($('.vscroll-' + nextCount).length) {
                        var divH = $('.vscroll-' + itemCount).outerWidth();
                        itemCount++;
                        $("#vscroller").animate({
                            left: "-=" + divH + "px"
                        }, options.speed);
                        if (options.hasSeparator == true) {
                            var c = itemCount + 2;
                            $('.vscroll-' + c).css({ 'background-image': 'url(' + options.separatorImg + ')' });
                            c++;
                            $('.vscroll-' + c).css({ 'background-image': 'none' });
                        }
                    }
                    move++;
                }
            });

            $(options.upID).click(function () {
                if (move > 0) {
                    var prevCount = itemCount - 1;
                    if ($('.vscroll-' + prevCount).length) {
                        itemCount--;
                        var divH = $('.vscroll-' + itemCount).outerWidth();
                        $("#vscroller").animate({
                            left: "+=" + divH + "px"
                        }, options.speed);
                        if (options.hasSeparator == true) {
                            var c = itemCount + 3;
                            $('.vscroll-' + c).css({ 'background-image': 'none' });
                            c++;
                            $('.vscroll-' + c).css({ 'background-image': 'url(' + options.separatorImg + ')' });
                        }
                    }
                    move--;
                }
            });
            var itemWidth = obj.children(0).outerWidth();
            obj.children().wrapAll("<div style='position: relative; top: 0; width: " + ((obj.children().size() - 1) * itemWidth) + "px;' id='vscroller'></div>");

        });

    };

})(jQuery);

