var container = 'announcementScroller';
var interval = 5000;
var numberOfItemsToRotate = 1;
var announcementItemClass = 'announcementItem';
var previousItemHandleID = 'announcementsPreviousButton';
var nextItemHandleID = 'announcementsNextButton';
var isHandlerActive = false;
var totalItems = 0;
var itemWidth = 0;
var itemMargin = 0;
var containerWidth = 0;

$(function() {

		
	// Get the total number of items, the width of the items and their left margin.
	totalItems = $('#' + container + ' .' + announcementItemClass).length;
	itemWidth = $('.' + announcementItemClass).outerWidth();
	itemMargin = $('.' + announcementItemClass).css('margin-left');
	
	
	// Strip the 'px' from the value.
	itemMargin = itemMargin.substring(0,itemMargin.length - 2);
	
	// Set the width of the container object.
	containerWidth = (totalItems * itemWidth) + ((totalItems + 1) * itemMargin);
	$('#' + container).css('width', containerWidth + 'px');

	// Next item click handler
	$('#' + nextItemHandleID).click(function() {
		
		MoveNext();
		
	});

	$('#' + previousItemHandleID).click(function() {

		MovePrevious();
		
	});
	
	$('.announcementItem').click(function() {
		
		$('.announcementView').remove();
		
		isHandlerActive = true;
		
		// Get the current right and left positions of the container.
		var currentRightPosition = $('#' + container).css('right');
		currentRightPosition = currentRightPosition.substring(0,currentRightPosition.length - 2);
		
		var currentLeftPosition = $('#' + container).css('left');
		currentLeftPosition = currentLeftPosition.substring(0,currentLeftPosition.length - 2);
		
		var containerWidth = $('#' + container).outerWidth();
		var parentWidth = $('#' + container).parent().outerWidth();
		var leftPosition = Math.round(containerWidth) + Math.round(currentRightPosition) - Math.round(parentWidth);
		
		var announcementID = $(this).attr('announcementID');

		$.ajax (
			{
				type: 'get',
				url: URLRoot + 'cfc/content/announcements.cfc',
				data: {
					method: 'ajaxGetAnnouncement',
					AnnouncementID: announcementID
				},
				dataType: 'json',
				success: function(retStruct) {
					
					$('<div id="divAnnouncementDetails" class="lightBox"><span id="announcementDetailTitle" class="announcementTitleDetail"></span><span id="announcementDetailText" class="announcementTextDetail"></span></div>').appendTo(document.body);
					
					$('#announcementDetailTitle').text(retStruct.title);
					$('#announcementDetailText').html(retStruct.text);
					
					$.lightbox({
						divID: 		'divAnnouncementDetails',
						fadeSpeed:	'medium'
					})
					
					isHandlerActive = false;
					
					/*$('<div class="announcementItem"><span>' + retStruct.title + '</span><span>' + retStruct.text + '</span></div>').appendTo('#' + options.container).animate({left: leftPosition, top: 0, className: 'announcementView'}, 100).click(function() {
						$(this).remove();
					});*/
				},
				error: function() {
					alert('Your request could not be completed at this time.');
				}
			});
		
	});
	
	// Set the duration
	var speed = 8000;
	
	var run = setInterval('MoveNext()', speed);

});

function MovePrevious()
{
	// Get the current right and left positions of the container.
	var currentRightPosition = $('#' + container).css('right');
	currentRightPosition = currentRightPosition.substring(0,currentRightPosition.length - 2);
	
	var currentLeftPosition = $('#' + container).css('left');
	currentLeftPosition = currentLeftPosition.substring(0,currentLeftPosition.length - 2);
	
	// Calculate how much to move the container.
	var amountToMove = (Math.round(itemMargin) + Math.round(itemWidth)) * numberOfItemsToRotate;
	
	// Calculate the new position of the container.
	var newPosition = Math.round(currentLeftPosition) + Math.round(amountToMove);
	
	// As long as we are not at the end of the item list, slide the container.
	if (currentLeftPosition < -1)
	{
		$('#' + container).animate({left: newPosition}, 100);
	}
	else
	{
	}
	
}


function MoveNext()
{
	
	// Get the current right and left positions of the container.
	var currentPosition = $('#' + container).position();
	
	var currentRightPosition = currentPosition.left * -1;
	
	
	//currentRightPosition = currentPosition.substring(0,currentRightPosition.length - 2);
	
	var currentLeftPosition = currentPosition.left;
	//currentLeftPosition = currentLeftPosition.substring(0,currentLeftPosition.length - 2);
	
	// Calculate how much to move the container.
	var amountToMove = (Math.round(itemMargin) + Math.round(itemWidth)) * numberOfItemsToRotate;
	
	
	// Calculate the new position of the container.
	var newPosition = Math.round(currentLeftPosition) - Math.round(amountToMove);
	
	if(isDebugging)
	{
		/*console.log('Right: ' + currentRightPosition);
		console.log('Amount to move: ' + amountToMove);
		console.log('New Position: ' + newPosition);
		console.log(Math.abs(currentLeftPosition) + ' - ' + containerWidth);*/
	}
	
	// As long as we are not at the end of the item list, slide the container.
	if (Math.abs(currentLeftPosition) <= containerWidth - 1195)
	{
		$('#' + container).animate({left: newPosition}, 100);
	}
	else
	{
		$('#' + container).animate({left: 0}, 300);
	}
	
}
