(function($) {   // Compliant with jquery.noConflict()
$.fn.newsTicker = function(o) {
    o = $.extend({
        speed: 5000,
        feedUrl:'',
        refresh:0,
        tooltip:true
    }, o || {});

    return this.each(function() {  // Returns the element collection. Chainable.
    	var $div = $(this);
    	var $tt = $("<div id='tooltip'/>").insertBefore($div);
    	var refresh;
 		
    	function startAuto() {
            stopAuto();
            refresh = setInterval(function() {scroll();}, o.speed);
        };

        function stopAuto() {clearInterval(refresh);};
        
       	$div.hover(function(){stopAuto();},function(){startAuto();});
       	
    	function scroll(){    		 
    		var $ul = $("ul",$div);
        	 $("li:first",$ul).animate({opacity:'0'},'slow').slideUp(1000,function(){
        		 $(this).appendTo($ul);
        		 $(this).animate({opacity:'1'},'slow').fadeIn('slow');
        	 });
        };
        
        function loadData(){
        	stopAuto();
        	if(!o.feedUrl || o.feedUrl=='') return false;
        	$.getScript(o.feedUrl,function(){
            	$div.html('<ul></ul>');
        		var $ul=$("ul",$div);
        		if(article){ //article is the var containing the news items
        			$(article).each(function(){        				
        				headLineItem(this,$ul);
        			});
        			startAuto();
        		};
        	});
          };
        
          function headLineItem(article,$parent){
         	 if(article.title!='void'){
 	      		var $header=$("<a/>").attr({'href':article.file_pointer,'target':'_blank'}).html(article.title); 	    		
 	    		var $teaser = $("<span class='teaser'>"+ article.nib.replace(/<\/?[^>]+>/gi, '').substring(0,75) + "</span>");
 	    		var $liItem = $('<li/>').addClass('headline').append($header).append($teaser).appendTo($parent);
    			    			
    			if(o.tooltip){
    				var tooltip = getTooltip(article.nib.replace(/<\/?[^>]+>/gi, ''));
    				$liItem.hover(function(e){
	    				var pos = getPosition(e,$tt,$(this));
	    				$tt.css(pos).text(tooltip).fadeIn().animate({top: '+=15'}, 500);
	    			},function(){
	    				$tt.fadeOut();
	    			});
    			};
         	 };
           };
           
           function getTooltip(tooltip){
        	   if(tooltip.length>400){
        		   var space = tooltip.indexOf(" ",400);
        		   if(space!=-1){
        			   return (tooltip.substring(0,space) + " (...)");
        		   }else{
        			   return tooltip;
        		   }
        	   }else{
        		   return tooltip;
        	   }
           }
           
           function getPosition(e,$tip,$trigger) {
   			// get origin top/left position 
   			var top = $trigger.offset().top
   			var left = ($trigger.offset().left/2);
   			top  -= $tip.outerHeight();
   			//left += $trigger.outerWidth();   			
   			//var height = $tip.outerHeight() + $trigger.outerHeight();  			
   			//var width = $tip.outerWidth() + $trigger.outerWidth();
    		return {top: top, left: left};
   		}		
           
        loadData();
    });
};
})(jQuery);
