//momofinoa@qq.com
(function($){
  $.fn.tbGallery=function(){
	  if(this.size()>1) return this.each(function(){$(this).tbGallery()})
	  var T=this.addClass("tbGallery").wrapInner("<div class='tbGalleryInner'></div>"),
	      w=T.width(),h=T.height(),
	      S=T.find("div:eq(0)").width(w).height(h),
		  imgs=T.find("img").width(w).height(h-22),span=T.find("span").width(w).css("opacity",0.8),
	      sinaTimmer,len=imgs.size(),index=0,t=4000;
	  for(var i =len;i>0;i--){
		  $("<b>"+i+"</b>").insertAfter(S).css("right",21*(len-i))
		  }
	  var tabs=T.find("b"),
	  to=function(i){
			  index=i;
			  tabs.removeClass("cur").eq(i).addClass("cur");
			  span.fadeOut("fast").eq(i).fadeIn("slow");
			  S.stop().animate({"scrollTop":i*h},300)
			  },
		  next=function(){
			  index++;
			  if(index>len-1) index=0;
			  to(index)
			  },
			  auto=function(){
				  sinaTimmer=setInterval(next,t)
				  }
		to(0);
        auto();
	   tabs.each(function (i){$(this).mouseover(function(){
          if(index==i) return false;
          clearInterval(sinaTimmer);
          to(i);
          auto();
          })
        })
	  }
})(jQuery)