var busy=0;
var buffer='';
$.fn.startTicker=function(ax){
   loadbuffer(true,ax,$(this));
}
function loadbuffer(firsttime,ax,obj){
   busy=1;
   t=new Date();
//   $load=$("#load").text("LOADING");
   $.get("/lib/scroll_data.php",{offsetTime:t.getTimezoneOffset()},function(d){
     busy=0;buffer=d;
// $load.text('');
     if(firsttime){obj.everyTime("60s",function(){loadbuffer()}).ticker(ax,firsttime);};
//     if(buffer.length==0){$("#score_scroller").hide(2000);} else {$("#score_scroller").show(5000);}
     if(buffer.length==0){$("#score_scroller").fadeOut(2000);} else {$("#score_scroller").fadeIn(5000);}

   });
}
$.fn.ticker=function(ax,firsttime){
   var n=0;
   var i=1;
   if($(this).find("li").length==0){$(this).html("<ul class=elements><li></li></ul>");}
   $(this).scrollTo("li:first",{axis:ax,onAfter:
     function(){
       if(busy==0){
         v=buffer.split("\n");
         list='<li>'+$(this).find("li:first").html()+'</li>';
         for (j in v) {list=list+'<li>'+v[j]+'</li>';}
         n=$(this).html("<ul class=elements>"+list+"</ul>").find("li").length;
//         if(n<=2){$("#score_scroller").hide(2000);} else {$("#score_scroller").show(2000);}
         i=1;
       }
     }
   });
   $(this).everyTime("5s","stimer",function(){
     $(this).scrollTo("li:eq("+i+")",500,{axis:ax,onAfter: 
       function(){
//         $("#ticker-container2").text(i+" "+$(this).height()+" "+n);
         if(i==2){$(this).find("li:first").html($(this).find("li:last").html());}
         i++;
         if(i>=n){$(this).stopTime("stimer").each(function(){$(this).ticker(ax);});}
       }
      });
   });
}

