jQuery(function($){
	var car = new carousel('#news' , '.lastNews');
	var carContainer = $('#news');
	carContainer
		.mouseover(function()
			{
				car.timer.stop();
			}
		)
		.mouseout(function()
			{
				car.timer.start();
			}
		);
		
	car.goTo(0);
	var news = $('.lastNews',carContainer);
	var newsNavContainer = $('#newsNavContainer');
	var nbNews = news.length;
	if(nbNews > 0)
	{
		for(i = 0 ; i < nbNews; i++)
		{
			link = document.createElement("a");
			link.setAttribute('href','#');
			link.className = 'li'+i;
			if( i == 0 ) link.className += ' selected';
			text = document.createTextNode( i+1 );
			link.appendChild(text);
			$(link).click( function(){ elt = $(this); car.goTo( elt.text() -1 ); car.timer.stop(); return false } );
			newsNavContainer.append(link);
		}
		car.timer.start();
	}
}
);


function clock (statement) {
	this.time = null;
	state = statement;
	
	
	this.start = function()
	{
		if(this.time == null)
		{	
			this.time = setInterval("repeat()",5000);
		}
	}
	
	repeat = function()
	{
		state.next();
	}
	
	this.stop = function()
	{
		
		if(this.time != null)
		{
			clearInterval(this.time);
		}
	}
}

function carousel (container , object) {
	this.container = $(container);
	this.movable = $(object , this.container);
	this.length = this.movable.length;
	this.current = 0;
	this.timer = new clock( this );

	this.next = function()
	{
		this.current = ++this.current % this.length;
		this.show();
	}
	
	this.prev = function()
	{
		this.current--;
		if( 0 > this.current)
		{
			this.current = 0;
		}
		this.show();
	}
	
	this.goTo = function( i )
	{	
		this.current = i;
		this.show();
	}
	
	this.show = function()
	{
		$(this.movable).removeClass('show');
		$(this.movable[this.current]).addClass('show');
		links = $('#newsNavContainer a' , this.container);
		links.removeClass('selected');
		$(links[this.current]).addClass('selected');
	}
	
}
