var current = 0;
var total = desc_arr.length - 1;
var timer;
var isPaused = false;

function initCycle() {
	
	$( 'day' ).set( 'text', weekday );
	$( 'month' ).set( 'text', month );
	$( 'date' ).set( 'text', date );
	
	$( 'cycle_prev' ).addEvent( 'click', function(){

		$clear( timer );
		isPaused = true;
		
		( ( current - 1 ) >= 0 ) ? current-- : current = total;
		
		updateCycle( current );
		
	});
	
	$( 'cycle_next' ).addEvent( 'click', function(){

		$clear( timer );
		isPaused = true;
		
		( (current + 1 ) <= total) ? current++ : current = 0;
		
		updateCycle( current );
		
	});
	
	updateCycle( current );
	
}

function updateCycle( pos ) {
	
	$( 'cycle_title' ).set( 'text', titles_arr[ pos ] );
	$( 'cycle_title' ).setProperty( 'href', locations_arr[ pos ] );
	$( 'cycle_image' ).setProperty( 'src', images_arr[ pos ] );
	$( 'cycle_copy' ).set( 'html', desc_arr[ pos ] );
	$( 'cycle_time' ).set( 'html', time_arr[ pos ] );
	$( 'cycle_count' ).set( 'text', ( pos + 1 ) + ' of ' + ( total + 1 ) );
	
	if ( !isPaused ) timer = updateCount.periodical( 4000 );
	
}

function updateCount() {
	
	$clear( timer );
	
	( current != total ) ? current++ : current = 0;
	
	updateCycle( current );
	
}

window.addEvent('domready', function() {
	
	if( $( 'rotate_content' ) ) initCycle();
	
});
