//Setup some vars we'll use
var postContainer = $('.news-posts-container');
var posts = $('.news-post');
var firstPost = $('.news-post:first-child');
var lastPost = $('.news-post:last-child');
var postControl = $('#news-position-controller a');
var intervalDuration = 5000;

//Get the width of all the child elements of the slider
var postWidth = posts.width();
var numNewsItems = posts.length;

//Set the aggregate width of all child elements to be width of slide container
var totalWidth = postWidth * numNewsItems;
var singleWidth = postWidth / numNewsItems;
var slideStyle = 'width:'+totalWidth+'px';
postContainer.attr('style', slideStyle);

$(document).ready(function(){

	//hide all news posts
	//$('.news-updates .news-post').hide();
	
	//show the first
	firstPost.addClass('active');

	//set the timeout
	newsInterval();
	
	//when a user clicks the post controller
	postControl.click(function(){
		var position = $(this).index();
		var position = position + 1;
		postControl.removeClass('active')
		postController(position);
		$(this).addClass('active');
		return false;
	});

	//if the sliders are hovered over
	posts.hover(
		function(){
			stopNewsInterval();
		},
		//and start again if hovered off
		function(){
			newsInterval();
		}
	);	
});

//function for controlling slides with arrows
function postController(position){
	stopNewsInterval();
	
	//hide the existing post
	currentPosition = $('.news-post.active').index();
	currentPosition = currentPosition + 1;

	if (position > currentPosition)
	{
		newPosition = position - currentPosition;
		newPosition = newPosition * postWidth;
		$('.news-post.active').removeClass('active');
	
		//go to the correct post
		posts.eq(position - 1).addClass('active');
    
    	postContainer.animate({
    		left: '-='+newPosition
  		}, 1000, function() {
    		// Animation complete.
  		});
  	}
  	else
  	{
		newPosition = currentPosition - position;
		newPosition = newPosition * postWidth;
		$('.news-post.active').removeClass('active');
	
		//go to the correct post
		posts.eq(position).addClass('active');
    
    	postContainer.animate({
    		left: '+='+newPosition
  		}, 1000, function() {
    		// Animation complete.
  		});  	
  	}
  	
  	newsInterval();
}

function removeActiveClass(){
	$('#news-position-controller a.active').removeClass('active');
}

//the function that swaps to next post
function nextPost(){

	var position = $('.news-post.active').index();
	var position = position + 1;
								
	if ( $('.news-post.active').is(':last-child'))
	//checks to see if this is the last post
	{
		var resetWidth = totalWidth - postWidth;
    	$('.news-posts-container').animate({
    		left: '+='+resetWidth
  		}, 1000, function() {
    		// Animation complete.
  		});
		//alert(resetWidth);
		$('.news-post.active').removeClass('active');
		firstPost.addClass('active');
		$('#news-position-controller a.active').removeClass('active');
		$('#news-position-controller a:first-child').addClass('active');
	}
	else
	//this is not the last post
	{
		$('.news-post.active').removeClass('active').next().addClass('active');
		$('#news-position-controller a.active').removeClass('active').next().addClass('active');
    	$('.news-posts-container').animate({
    		left: '-='+postWidth
  		}, 1000, function() {
    		// Animation complete.
  		});		
	}
}

//the function that sets the timer which activates the nextSlide function
function newsInterval(){
	newsTimer = setInterval(nextPost, intervalDuration);
}

//the timeout stop function
function stopNewsInterval(){
	clearInterval(newsTimer);
}
