//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

$(document).ready(function() {

	// Variables
	var total = 0;
	var speed = 'fast';
	var rotation_speed = 6; // ms
	var selected = 0;
	var container = $('#news-scroll');
	var total = container.find('.total').html();
	
	setInterval(function() {
		$(container).find('.news-scroll-pagination a.right').trigger('click');
	}, rotation_speed*1000);
	
	// Arrow events
	$(container).find('.news-scroll-pagination a').live('click', function() {
		
		var direction = $(this).attr('class');
		
		if(direction == 'left') {
			if(selected == 0) {
				goto(total-1);
			} else {
				goto(selected-1);
			}
		}
		
		if(direction == 'right') {
			if(selected == total-1) {
				goto(0);
			} else {
				goto(selected+1);
			}
		}
		
		return false;
		
	});
	
	function goto(index) {
	
		selected = index;
	
		// Fade news
		//$(container).find('.news-scroll-item').fadeOut(speed);
		
		$(container).find('.news-scroll-pagination .current').html(selected+1);
		// Set title
		
		$(container).find('li:visible').fadeOut(speed, function() {
			container.find('li').eq(selected).fadeIn(speed);
		}); //fadeIn(speed);		

	}

});
