﻿// Vertical Scroll
// Modified version of http://net.tutsplus.com/tutorials/javascript-ajax/build-a-simple-jquery-news-ticker/
jQuery.fn.vScroll = function() {
    return this.each(function() {

        //cache the ticker
        var ticker = jQuery(this);

        //wrap li in divs
        ticker.children().filter("li").each(function() {
            var li = $(this),
		        container = $("<div>");
            li.next().appendTo(container);
            li.prependTo(container);
            container.appendTo(ticker);
        });
        //hide the scrollbar
        ticker.css("overflow", "hidden");

        // Bind pause and resume to the ticker
        ticker.bind('pauseresume', function() {
            // Stop animation
            ticker.children().stop();
            //Resume animation
            animator(ticker.children(":first"));
        });

        // Once ever 60 sec, pause/resume the scrolling animation to stop memory leaks in IE
        setInterval(function() {
            ticker.trigger('pauseresume');
        }, 60000);


        //animator function
        function animator(currentItem) {
            //ticker.children().stop();
            //work out new anim duration
            var distance = currentItem.height();
            duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.015;
            //animate the first child of the ticker
            currentItem.animate({ marginTop: -distance }, duration, "linear", function() {
                //move current item to the bottom
                currentItem.appendTo(currentItem.parent()).css("marginTop", 0);
                //recurse                       
                animator(currentItem.parent().children(":first"));

            });
        };

        //start the ticker
        animator(ticker.children(":first"));
        //set mouseenter
        ticker.mouseenter(function() {
            //stop current animation
            ticker.children().stop();
        });
        //set mouseleave
        ticker.mouseleave(function() {
            //resume animation
            animator(ticker.children(":first"));
        });
    });
};


// Horizontal Scroll
// Modified version of http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
jQuery.fn.hScroll = function(settings) {
    settings = jQuery.extend({
        travelocity: .020
    }, settings);
    return this.each(function() {
        var $strip = jQuery(this);
        $strip.addClass("newsticker")
        var stripWidth = 0;
        var $mask = $strip.wrap("<div class='mask'></div>");
        var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");
        var containerWidth = $strip.parent().parent().width(); //a.k.a. 'mask' width 	
        $strip.find("li").each(function(i) {
            stripWidth += jQuery(this, i).width();
        });
        $strip.width(stripWidth);
        var defTiming = stripWidth / settings.travelocity;
        var totalTravel = stripWidth + containerWidth;
        function scrollnews(spazio, tempo) {
            $strip.animate({ left: '-=' + spazio }, tempo, "linear", function() { $strip.css("left", containerWidth); scrollnews(totalTravel, defTiming); });
        }
        scrollnews(totalTravel, defTiming);
        $strip.hover(function() {
            jQuery(this).stop();
        },
				function() {
				    var offset = jQuery(this).offset();
				    var residualSpace = offset.left + stripWidth;
				    var residualTime = residualSpace / settings.travelocity;
				    scrollnews(residualSpace, residualTime);
				});
    });
};
