﻿(function ($) {
    $.fn.vScroll = function (options) {
        // set defaults
        var defaults = { speed: 700, upID: "#up-arrow", downID: "#down-arrow" };
        // override the defaults with options passed in
        var options = $.extend(defaults, options);

        // each time function called
        return this.each(function () 
        {
            // create obj variable and set css overflow
            obj = $(this);
            obj.css("overflow", "hidden");
            // loop through child objects 
            obj.children().each(function (intIndex) 
            {
                // add css class with index appended to it <div class="la-item vscroll-0"> TO <div class="la-item vscroll-41">
                $(this).addClass("vscroll-" + intIndex);
            });


            // set counter to 0
            var itemCount = 0;

            /* #### SCROLL DOWN ### */

            // set click event for arrow with downID e.g (#down-arrow)
            $(options.downID).click(function () 
            {
                // increment count
                var nextCount = itemCount + 1;
                // if this item exists (has length so not zero)
                if ($('.vscroll-' + nextCount).length) 
                {
                    //alert(nextCount);
                    // max 11 scrolls before list goes off screen
                    if (nextCount < 7) {
                        // calculate height of item X 4 
                        var divH = ($('.vscroll-' + itemCount).outerHeight() * 6);
                        //alert(divH + " speed=" + options.speed);
                        itemCount++;
                        $("#vscroller").animate({ top: "-=" + divH + "px" }, options.speed);
                    }
                }
            });

            // set mouseenter event for arrow with downID e.g (#down-arrow)
            $(options.downID).mouseenter(function () 
            {
                // increment count
                var nextCount = itemCount + 1;
                // if this item exists (has length so not zero)
                if ($('.vscroll-' + nextCount).length)
                {
                    //alert(nextCount);
                    // max 11 scrolls before list goes off screen
                    if (nextCount < 7) 
                    {
                        // calculate height of item X 4 
                        var divH = ($('.vscroll-' + itemCount).outerHeight() * 6);
                        //alert(divH + " speed=" + options.speed);
                        itemCount++;
                        $("#vscroller").animate({ top: "-=" + divH + "px" }, options.speed);
                    }
                }
            });

            /* #### SCROLL UP ### */

            // set click event for arrow with upID e.g (#up-arrow)
            $(options.upID).click(function () 
            {
                // scroll the list up the height of it
                var prevCount = itemCount - 1;
                if ($('.vscroll-' + prevCount).length) {
                    itemCount--;
                    var divH = ($('.vscroll-' + itemCount).outerHeight() * 6);
                    $("#vscroller").animate({ top: "+=" + divH + "px" }, options.speed);
                }
            });

            // set mouseenter event for arrow with upID e.g (#up-arrow)
            $(options.upID).mouseenter(function () 
            {
                // scroll the list up the height of it
                var prevCount = itemCount - 1;
                if ($('.vscroll-' + prevCount).length) {
                    itemCount--;
                    var divH = ($('.vscroll-' + itemCount).outerHeight() * 6);
                    $("#vscroller").animate({ top: "+=" + divH + "px" }, options.speed);
                }
            });


            // finally wrap all child objects in a DIV
            obj.children().wrapAll("<div style='position: relative; top: 0px' id='vscroller'></div>");
        });
    };
})(jQuery);
