$(function(){

	function selectArticle(id, effect)
	{
		$("#news-list li").removeClass('selected');
		$("#article" + id).addClass('selected');	
		
		$("#news-bloc .news-photo").hide();
		
		if(effect == 'fade')
			$("#article-preview" + id).fadeIn(600);
		else
			$("#article-preview" + id).show();
	}
	
	ArticleList = function()
	{
		this.list = null;
		this.current = 0;
		this.length = 0;
		
		this.init = function( params )
		{
			this.list = params.list;
			this.length = params.list.length;
		}
		
		this.next = function()
		{
			if(this.length == 0)
				return;
				
			if(this.current < this.length - 1)
				this.current++;
			else
				this.current = 0;
			
			if(this.list[this.current] != undefined)
				selectArticle(this.list[this.current].id.replace('article', ''), 'fade');
		}
		
		this.startScroll = function( startElem )
		{
			var self = this;
			this.current = startElem ? startElem - 1 : this.length - 1;
			$("#news-list").everyTime( 5000, 'ms', function(){ self.next(); } );
		}
		
		this.stopScroll = function()
		{
			$("#news-list").stopTime();
		}
	}
	
	var article_list = new ArticleList;
	article_list.init({ list: $("#news-list li") });
	
	$("#news-list li").mouseover( function(){ article_list.stopScroll(); selectArticle(this.id.replace('article', ''), 'show'); } );
	$("#news-list").mouseout( function(){ article_list.startScroll(); } );
	
	article_list.startScroll(1); 
});
