// * Copyright 2011 Agusto Design (http://agustodesign.com). All rights reserved.

function initCarousel()
{
	// Set the slider div to the right width. 
	var totalWidth = 0;
	var items = $('#slider .slide');
	var totalItemCount = items.length;
	
	items.each(function() { totalWidth = totalWidth + $(this).width(); } );
	$('#slider').css('width', totalWidth);
	
	// Set up our current item. 
	// If we have a decent number of items, move the last item to the front
	// and move the carousel up one. 
	if (totalItemCount > 4)
	{
		$('#slider').prepend($('#slider .slide').last().remove());
		$('#slider').prepend($('#slider .slide').last().remove());
		
		// Reset the items
		items = $('#slider .slide');
		
		$('#slider').css({'left':'-'+(getItem(0).width())+'px'});	
		
		$(getItem(1)).addClass('currentSlide');	
		
		switchInfo(getItem(1));
	}
}

function getItem(idx)
{
	if ($('#slider .slide')[idx] != 'undefined')
	{
		return $($('#slider .slide')[idx]);
	}
}

function nextItem()
{
	if (!$('#slider').hasClass('animating'))
	{
		// Lock it down 
		$('#slider').addClass('animating');
		
		var current = $('#slider .currentSlide');
		var currentLeft = parseInt($('#slider').css('left'), 10);
		var newLeft = currentLeft+(-current.width());
		
		// Start info switching 
		switchInfo(current.next());
		
		$('#slider').animate({'left':+newLeft+'px'}, function()
		{
			// Move the current item along one, and pop one off the end. 	
			var nextSlide = $('#slider .currentSlide').next();
			
			$('#slider .currentSlide').removeClass('currentSlide');
			nextSlide.addClass('currentSlide');
			
			// Pop the first item off and stick it on the end. 
			var updatedLeft = parseInt($('#slider').css('left'), 10);
			var updatedNewLeft = updatedLeft+getItem(0).width();
			$('#slider').css({'left':updatedNewLeft+'px'});	
			
			//$('#slider .slide').first().hide();
			$('#slider').append($('#slider .slide').first().remove());

			//$('#slider .slide').last().hide();
			//$('#slider .slide').last().fadeIn(3000);
			
			$('#slider').removeClass('animating');
		});	
	}
	
	return false;
}

function prevItem()
{
	if (!$('#slider').hasClass('animating'))
	{
		// Lock it down 
		$('#slider').addClass('animating');
		
		var current = $('#slider .currentSlide');
		var currentLeft = parseInt($('#slider').css('left'), 10);
		
		switchInfo(current.prev());
		
		// First thing we have to do is take the last item and stick it on the front. 
		$('#slider').prepend($('#slider .slide').last().remove());
		
		// Now reset left to include the new item. 
		var newLeft = currentLeft+(-getItem(0).width());
		$('#slider').css({'left':newLeft+'px'});	
		
		// Finally move the slider right by the amount of the third slide. 
		var newRight = newLeft + getItem(1).width();
		
		$('#slider').animate({'left':+newRight+'px'}, function()
		{
			// Move the current item back one	
			var prevSlide = current.prev();
			
			current.removeClass('currentSlide');
			prevSlide.addClass('currentSlide');
			
			$('#slider').removeClass('animating')
		});	
	}
	
	return false;
}

function autoNext()
{
	nextItem();
	setTimeout(autoNext, 3000);
}
	

function switchInfo(slide)
{
	var article = $('article', slide);
	var articleID = article.attr('id');
	
	$('#slider-info').append('<div id="current-'+articleID+'" style="position:absolute;display:none;"></div>');
	$('#slider-info #current-'+articleID).html(($('article', slide).html()));
	
	if ($('#slider-info div:visible').first().length > 0)
	{
		$('#slider-info div:visible').first().fadeOut('fast', function() { $(this).remove(); });
	}
	
	$('#slider-info #current-'+articleID).fadeIn();
	
}
$(function() {
$('#news-gallery').cycle({ 
    fx:     'scrollHorz', 
    speed:  'slow', 
    timeout: 0, 
    next:   '#next', 
    prev:   '#prev' 
});
});
