var status = 0;
var totalTravel;
var defTiming;

var BasePath;
if (location.hostname.indexOf("/") > 0) {
    BasePath = location.protocol + "//" + location.hostname.substring(0, location.hostname.indexOf("/"))
}
else {
    BasePath = location.protocol + "//" + location.hostname;
}

jQuery.fn.liScroll = function(settings) {
    settings = jQuery.extend({
        travelocity: 0.04
    }, settings);
    return this.each(function() {
        var $strip = jQuery($("ul#ticker01"));
        $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($strip.find("li")[i]).width(); // jQuery($("ul#ticker01"), i).width();
        });
        $strip.width(stripWidth);
        totalTravel = stripWidth + containerWidth;
        defTiming = totalTravel / settings.travelocity;
        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($("ul#ticker01")).stop();
        },
				    function() {
				        var offset = jQuery(this).offset();
				        var residualSpace = offset.left + stripWidth;
				        var residualTime = residualSpace / settings.travelocity;
				        scrollnews(residualSpace, residualTime);
				    }
				);
    });

};

$(function() {
    $("img#imgStopPause").attr('src', BasePath + '/images/pause.gif');
    $("ul#ticker01").liScroll();
});


$(document).ready(function() {

    $("a#stop_pause").bind("click", function(e) {


        if (status == 0) {
            jQuery($("ul#ticker01")).stop();
            status = 1;

            $("img#imgStopPause").attr('src', BasePath + '/images/play.gif');
            $("img#imgStopPause").attr('alt', 'play');

        }
        else {
            status = 0;
            $("img#imgStopPause").attr('src', BasePath + '/images/pause.gif');
            $("img#imgStopPause").attr('alt', 'pause');
            function scrollnews(spazio, tempo) {
                jQuery($("ul#ticker01")).animate({ left: '-=' + spazio }, tempo, "linear", function() { $strip.css("left", containerWidth); scrollnews(totalTravel, defTiming); });
            }
            scrollnews(totalTravel, defTiming);

        }


    });

});
