    
/*
    newsTicker.js v1.0
    Last updated: 11 May 2011
    Created by: Perform Group
*/

(function($) {
    
    $.fn.newsticker = function(options) {
        
        var config = {
            velocity: 0.05,
            type: "scroll",  // scroll, fade
            direction: "ltr"
        };
        
    if(options) $.extend(config, options);
    
    return this.each(function(){
         
        var ticker = $(this);
        ticker.addClass('ticker');
        
        var tickerWrap = ticker.wrap("<div class='tickerWrap'></div>"); // This wrap is the mask
        var tickerWidth = 0;
        var containerWidth = tickerWrap.width();    // This sets the width of the mask
        var individualTickerWidth = 0;
        var itemWidth;
    
        // Find the li's within the ticker and work out the total width
        ticker.find("li").each(function(i){
      if(i==0){
        itemWidth = jQuery(this, i).outerWidth(true);
      }
            tickerWidth += jQuery(this, i).outerWidth(true);
    });
    
    
        ticker.width(tickerWidth);
        
        // Calculate the distance and timing for the ticker items to follow
        var totalDistance = tickerWidth + containerWidth;
        var loopTime = totalDistance/config.velocity;

    switch(config.type){
      case "fade":
        fadeTicker();
        break;
      case "scroll":
        scrollTicker();
    }

    function fadeTicker() {
      var newsInterval;
      var finalLeft = (ticker.children().size() - 1) * -itemWidth;
      var leftage = 0;
      var playing = true;
      
      function moveNext() {
        if (leftage <= finalLeft) {
          leftage = 0;
        } else {
          leftage = leftage - itemWidth
        }
        ticker.css("left", leftage);
      };
      
      function movePrevious() {
        if (leftage >= 0) {
          leftage = finalLeft;
        } else {
          leftage = leftage + itemWidth
        }
        ticker.css("left", leftage);
      };
      
      function fadeNext(){
        ticker.fadeOut(2000, function(){
          moveNext();
          ticker.fadeIn(2000);
        });
      };
      
      function startPlaying(backwards){
        newsInterval = setInterval(function(){fadeNext()}, 4000);
        playing = true;
      };
      
      function stopPlaying(){
        clearInterval(newsInterval);
        ticker.show();
        playing = false;
      };
      
      
      $(".rewind").bind('click', function() {
        stopPlaying();
        movePrevious();
      });
      
      $(".fastforward").bind('click', function() {
        stopPlaying();
        moveNext();
      });
      
      $(".pause").bind('click', function() {
        if(playing){
          stopPlaying();
        }else{
          startPlaying();
        }
      });
      
      startPlaying();
    }
    
    function scrollTicker() {
      // Scroll direction
      if(config.direction == "ltr") {
        ticker.css("left", containerWidth); // Set the left CSS for the UL to outside the right of the mask
        function scrollLeft(distance, velocity){
          ticker.animate({left: '-='+ distance}, velocity, "linear", function(){
            ticker.css("left", containerWidth);
            scrollLeft(totalDistance, loopTime);
          });
        }
        scrollLeft(totalDistance, loopTime);
        
        ticker.hover(function() {
          ticker.stop();
        },
        function(){
          var offset = ticker.offset();   // find current relative position of ticker
          var remainingDistance = offset.left + tickerWidth;  // work out the remaining distance to travel
          var remainingTime = remainingDistance/config.velocity; // work out the remaining time to complete the loop
          scrollLeft(remainingDistance, remainingTime);
        });
      }        
      else {
        ticker.css("left", -tickerWidth);   // Set the left CSS for the UL to outside the left of the mask
        function scrollRight(distance, velocity){
          ticker.animate({left: '+='+ distance}, velocity, "linear", function(){
            ticker.css("left", -tickerWidth);
            scrollRight(totalDistance, loopTime);
          });
        }
        scrollRight(totalDistance, loopTime);
        
        ticker.hover(function() {
          ticker.stop();
        },
        function(){
          var offset = ticker.offset();   // find current relative position of ticker
          var remainingDistance = containerWidth - offset.left; // work out the remaining distance to travel
          var remainingTime = remainingDistance/config.velocity; // work out the remaining time to complete the loop
          scrollRight(remainingDistance, remainingTime);
        });
      }
    }
    });
    };    
})(jQuery);
