//init page
$(function(){
	showNews();
});
function showNews(){
	var activeClass = 'active';
	var duration = 300;
	var holders = $('.slider > ul');
	holders.each(function(){
		var holder = $(this);
		var t;
		var rows = holder.children();
		var activeRow = rows.filter('.active').index();
		var oldRow;
		var maxIndex = rows.length-1;
		if(!activeRow.length){
			activeRow = 0;
			rows.eq(activeRow).addClass(activeClass);
		}
		rows.find('.sliding').css('marginTop',holder.height());
		rows.eq(activeRow).find('.sliding').css('marginTop',0);
		var flag = false;
		rows.hover(function(){
			clearInterval(t);
			flag = true;
		},function(){
			flag = false;
			changeInterval();
		});
		
		function changeRow(){
			if(activeRow < maxIndex){
				oldRow = activeRow;
				activeRow++;
			}
			else{
				activeRow = 0;
				oldRow = rows.length-1;
			}
			changeGo();
			
			function changeGo(){
				clearInterval(t);
				var title = rows.eq(activeRow).find('.sliding');
				var titleOld = rows.eq(oldRow).find('.sliding')
				rows.eq(activeRow).addClass(activeClass);
				rows.eq(oldRow).removeClass(activeClass).animate({opacity:0},{queue:false,duration:duration});
				titleOld.animate({
					marginTop:-holder.height()
				},{
					queue:false,
					duration:duration*2,
					complete: function(){
						title.animate({
							marginTop:0
						},{
							queue:false,
							duration:duration*2
						});
						titleOld.css({marginTop:holder.height()});
						rows.eq(oldRow).css('opacity','');
						changeInterval();
					}
				});
			}
		}
		function changeInterval(){
			if (!flag) {
				clearInterval(t);
				t = setInterval(changeRow, 3000);
			}
		}
		changeInterval();
	});
}

