/**
 * News Scroller
 */

var news = {
	num_features : null,
	feature_width : null,
	timeout : null,
	wait_delay : null,
	animation_delay : null,
	current_feature_index : null,
	handles : {
		$container: null,
		$viewport : null, 
		$feature_controls : null, 
		$feature_repository : null, 
		$frame : null, 
		$feature_controls : null
	}
};

$(document).ready(function() {
	news.current_feature_index = 0;
	news.num_features = $('#featured_news .features .feature').length;

	news.feature_width = $('#featured_news .features .feature:first').width();
	news.timeout = null;
	news.wait_delay = 8000;
	news.animation_delay = 700;
	
	
	// Initial setup
	news.handles.$container = $('#featured_news');
	news.handles.$container.prepend('<div class="feature_viewport"><div class="feature_frame"></div></div><div class="feature_controls"></div>');
	news.handles.$viewport = $('#featured_news .feature_viewport');
	news.handles.$feature_controls = $('#featured_news .feature_controls');
	news.handles.$feature_repository = $('#featured_news .features');
	
	news.handles.$frame = $('#featured_news .feature_viewport .feature_frame');
	news.handles.$frame.append($('#featured_news .features .feature:eq('+news.current_feature_index+')').clone());
	
	
	
	// feature repository
	news.handles.$feature_repository.hide();
	
	
	// feature controls
	for(var i = 0; i < news.num_features; i++) {
		news.handles.$feature_controls.append('<span class="feature_control'+((i==0)?' current':'')+'" data-index="'+i+'">'+(i+1)+'</span>');
	}
	
	if (news.num_features > 1) { 
		// jump to specific feature
		news.handles.$feature_controls.children('.feature_control').click(function() {
			news_scroll_left(parseInt($(this).attr('data-index')));
		});
		
		news.timeout = setTimeout('news_scroll_left(false)', news.wait_delay);		
	}
	
	
});


function news_scroll_left(jump_to) {
	if (jump_to !== false) {
		news.current_feature_index = jump_to;
	} else {
		news.current_feature_index++;
	}
	
	if (news.current_feature_index >= news.num_features) {
		news.current_feature_index = 0;
	}
	
	// Select next feature from repository
	news.handles.$frame.append(news.handles.$feature_repository.children('.feature:eq('+news.current_feature_index+')').clone());
	news.handles.$frame.width(2 * news.feature_width);
	
	// Animate scroll
	news.handles.$frame.animate({
		left: -1 * news.feature_width
	}, news.animation_delay, function() {
			// Switch alignment edge
			news.handles.$frame.css({
				'right': 0,
				'left': 'auto'
			});
			
			news.handles.$frame.children('.feature:first').remove();
			news.handles.$frame.css({
				'width': 'auto',
				'left': 0,
				'right': 'auto'
			});
			
			news_update_current();
			
			if (news.timeout) {
				news_resetTimeout();
			}
	});
}		

function news_scroll_right() {
	news.current_feature_index--;
	if (news.current_feature_index < 0) {
		news.current_feature_index = news.num_features - 1;
	}
	
	// Switch alignment edge
	news.handles.$frame.css({
		'right': 0,
		'left': 'auto'
	});
	
	// Add next feature to start
	news.handles.$frame.append(news.handles.$feature_repository.children('.feature:eq('+news.current_feature_index+')').clone());
	news.handles.$frame.width(2 * news.feature_width);
	
	// Animate scroll
	news.handles.$frame.animate({
		'right' : -1 * news.feature_width
	}, news.animation_delay, function() {
			// Switch alignment edge
			news.handles.$frame.css({
				'left' : 0,
				'right' : 'auto'
			});
			
			// Remove previous feature
			news.handles.$frame.children('.feature:last').remove();
			news.handles.$frame.css({
				'width': 'auto'
			});
			
			news_update_current();
			
			if (news.timeout) {
				news_resetTimeout();
			}
	});
}
	
function news_resetTimeout() {
	clearTimeout(news.timeout);
	news.timeout = setTimeout('news_scroll_left(false)', news.wait_delay);
}

function news_update_current() {
	news.handles.$feature_controls.children('.feature_control').removeClass('current');
	news.handles.$feature_controls.children('.feature_control:eq('+news.current_feature_index+')').addClass('current');
}
