var time;
var check=0;
function topstorycounter()
{
	  var size = $('div#list>div').size();
	  var cur = $('div#list>div:visible');
		if (cur)
		{
			var currentIndex = cur.prevAll().length+1; 
			if (currentIndex<size)                                                
			var next_cur = cur.next('div.show');
			else                                                    
			next_cur=$('div#list>div:first-child');
			cur.animate({ opacity: "hide" }, "slow",function(){
				 next_cur.animate({ opacity: "show" }, "slow");
			 });
			

		}
}
function start()
{
  
  time = setInterval(function(){topstorycounter();},5000);
  check=0;
}

$(function(){
	
	$('div#list>div:not(:first-child)').hide();
	time = setInterval(function(){topstorycounter();},5000);                                           
	var size = $('div#list>div').size();
	$('a#next').bind("click",function(){
		if (!check)
		 {
			 clearInterval(time);
			 setTimeout(function(){start()},5000);
			 check=1;
		 }
		var cur = $('div#list>div:visible');
		if (cur)
		{
			var currentIndex = cur.prevAll().length+1; 
			if (currentIndex<size)                                                
			var next_cur = cur.next('div.show');
			else                                                    
			next_cur=$('div#list>div:first-child');
			
			 cur.animate({ opacity: "hide" }, "slow",function(){
				 next_cur.animate({ opacity: "show" }, "slow");
			 });
			
		}
	});
	 $('a#prev').bind("click",function(){
		 if (!check)
		 {
			 clearInterval(time);
			 setTimeout(function(){start()},5000);
			 check=1;
		 }
		var cur = $('div#list>div:visible');
		if (cur)
		{
			var currentIndex = cur.prevAll().length; 
			if (currentIndex>0)                                                
			var prev_cur = cur.prev('div.show');
			else     
			prev_cur = $('div#list>div:last-child');                                               
			cur.animate({ opacity: "hide" }, "slow",function(){
				 prev_cur.animate({ opacity: "show" }, "slow");
			 });
		}
	});
});
