﻿
$.fn.loopingAnimation = function(props, dur, checkContinue) {

    if (this.data('loop') == true && checkContinue()) {
        $(this).animate(props, {
            duration: dur,
            easing: 'linear',
            complete: function() { $(this).loopingAnimation(props, dur, checkContinue) }
        });
    }
    return this; // Don't break the chain
}

jQuery.fn.GMWScroll = function(height, innerCss) {
    var contentHeight = $(this).innerHeight();
    var contentWidth = $(this).innerWidth();
    //Set element's height and width

    //Create scroll container
    var scrollContainer = $("<div>").addClass("scrollContainer");

    var topArrow = $("<div>").addClass("upArrow");
    var bottomArrow = $("<div>").addClass("downArrow");
    $(topArrow).appendTo(scrollContainer);
    $(bottomArrow).appendTo(scrollContainer);



    var content = $(this).html();
    var innerContent = $("<div>").addClass("innerContent");
    if (innerCss) {
        $(innerContent).attr("style", innerCss);
    }
    $(innerContent).html(content);
    $(innerContent).css("width", contentWidth);
    $(innerContent).css("height", contentHeight);
    $(innerContent).appendTo(scrollContainer);
    $(this).html("");
    $(scrollContainer).appendTo($(this));


    //Register Events
    /*
    $(topArrow).click(function() {
    if ($(innerContent).position().top < 0) {
    $(innerContent).animate(
    { top: "+=50" },
    200, function() {
    if ($(innerContent).position().top == 0) {
    $(innerContent).stop();
    }
    });
    }

    });*/

    $(topArrow).mousedown(function() {
        $(innerContent).data('loop', true).stop().loopingAnimation({ top: "+=35" }, 200,
            function() { return ($(innerContent).position().top < 0) });
    });
    $(topArrow).mouseup(function() {
        $(innerContent).data('loop', false);
        // Now our animation will stop after fully completing its last cycle
    });

    $(topArrow).mouseout(function() {
        $(innerContent).data('loop', false);
        // Now our animation will stop after fully completing its last cycle
    });

    $(bottomArrow).mousedown(function() {
        $(innerContent).data('loop', true).stop().loopingAnimation({ top: "-=35" }, 200,
         function() { return ($(scrollContainer).innerHeight() - 100 - contentHeight < $(innerContent).position().top) });
    });
    $(bottomArrow).mouseout(function() {
        $(innerContent).data('loop', false);
        // Now our animation will stop after fully completing its last cycle
    });

    $(bottomArrow).mouseup(function() {
        $(innerContent).data('loop', false);
        // Now our animation will stop after fully completing its last cycle
    });

    /*

    $(bottomArrow).click(function() {
    if ($(scrollContainer).innerHeight() - 100 - contentHeight < $(innerContent).position().top) {
    $(innerContent).animate({ top: "-=50" }, 200,
    function() {
    if ($(scrollContainer).innerHeight() - 100 - contentHeight >= $(innerContent).position().top) {
    $(innerContent).stop();
    }
    });
    }
    });
    */
    //Wrap inner Content

    var scrollerWidth = contentWidth + $(topArrow).outerWidth();
    var scrollerHeight = height + $(topArrow).outerHeight() + $(bottomArrow).outerHeight(); ;

    $(this).css("top", 0);
    $(this).css("left", scrollerWidth - contentWidth);

    //Set Sizes


    $(scrollContainer).css("width", scrollerWidth);
    $(scrollContainer).css("height", scrollerHeight);

};
