$(document).ready(function(){
	var totWidth=0;
	var positions = new Array();
	var slidesWidth = $('#slides .slide').length;
	
	$('#slides .slide').each(function(i){		
		positions[i]= totWidth;
		totWidth += $(this).width();
	});
	
	$('#slides').width(totWidth);

	function moving(pos){
	   $('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
	   $('#current').html(pos+1);
	}
 
    $('#lft,#rht').click(function(){
		clearInterval(itvl);
		current_pos = parseInt($('#current').html())-1;
		
		$(this).attr('id')=='lft'?current_pos--:current_pos++;
        if(current_pos>=0 && current_pos<=(slidesWidth-1)) moving(current_pos);	
	});	
	
	function autoAdvance(){
		if(current==-1) return false;

		moving(current%slidesWidth);
		current++;
	}

	var current=1;
	var itvl = setInterval(function(){autoAdvance()},10*1000);	
});
