jQuery(document).ready(function() {

	jQuery("ul.news-slider").liSlider({
		 slideSpeed   : 0.6,
		 slideInterval: 0
	});

});


(function($) {
$.fn.liSlider = function(options) {
   var defaults = {
	  slideSpeed   : 0.5,
	  slideInterval: 1000
   },
   settings = $.extend({}, defaults, options);
   return this.each(function () {
	  var $this = jQuery(this),
	  	stripWidth = 0,
		containerWidth = 0;
	  containerWidth = $this.parent().width();
	  $this.find("li").each(function (i) {
		 stripWidth += jQuery(this, i).width();
	  });
	  $this.width(stripWidth);
	  var totalSpace = stripWidth + containerWidth;
	  var totalTime = Math.floor(totalSpace / (settings.slideSpeed / 10));

	  function scrollnews(space, time) {
		 //console.log(":: containerWidth: %s - stripWidth: %s - space: %s - time: %s", containerWidth, stripWidth, space, time);
		 $this.animate({
			left: '-=' + space
		 }, time, "linear", function () {
			$this.css("left", containerWidth);
			setTimeout(function () {
			   scrollnews(totalSpace, totalTime);
			}, settings.slideInterval);
		 });
	  }

	  scrollnews(totalSpace, totalTime);

	  $this.hover(function () {
		 jQuery(this).stop();
	  }, function () {
		 var offset = jQuery(this).position();
		 var remainingSpace = Math.ceil(stripWidth + offset.left);
		 var remainingTime =  Math.ceil(remainingSpace / (settings.slideSpeed / 10));
		 //console.log("containerWidth: %s - stripWidth: %s - offset.left: %s - remainingSpace: %s - remainingTime: %s", containerWidth, stripWidth, offset.left, remainingSpace, remainingTime);
		 scrollnews(remainingSpace, remainingTime);
	  });
   });
};
})(jQuery);


