/**
 * 
 * Tweetticker js 
 * (c) 2010 Logat GmbH
 */

(function($){

 /*!
  * liScroll 1.0
  * Examples and documentation at: 
  * http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
  * 2007-2010 Gian Carlo Mingati
  * Version: 1.0.2 (30-MARCH-2009)
  * Dual licensed under the MIT and GPL licenses:
  * http://www.opensource.org/licenses/mit-license.php
  * http://www.gnu.org/licenses/gpl.html
  * Requires:
  * jQuery v1.2.x or later
  * 
  */
  $.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, 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).outerWidth(true); // thanks to Michael Haszprunar
				});
				$strip.width(stripWidth);			
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
				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);
				});			
		});	
  };


  $.fn.tweetticker = function( options ) {
	var _defaultOptions = {
		feedUrl: 'twitter/get-tweets'
	};

	var options = $.extend( _defaultOptions, options );
	var feedData = null;

	var container = $(this);
	$(container).hide();	
	
	var refreshTicker = function() {
		
		// setup markup install callbacks
		var markup = "<ul>";
		$(feedData).each(function(){
			var entry = this;
			markup += '<li><a href="' + entry.url + '"> +++ ' 
				+ entry.from_user + ": " 
				+ entry.text + " +++</a></li>  \n";
		});
		markup += "</ul>";
		$(container).html( markup );
		$(container).fadeIn('slow');

		$(container).find('ul').liScroll();
	
	}
	
	

	$.getJSON( options.feedUrl, function(result){
		// save feed data	
		feedData = result;	
		// refresh ticker(s)
		refreshTicker();
	});
  }


})(jQuery);

