$(document).ready(function(){
	var $images = $('#slideshow img.slide');
	var $images_list = new Array();
	var $current = 0;
	var $next = 1;
	var $imageCount = $images.length;
	
	// Preload images.
	$images.each(function(i) {
		$img = $(this).attr('src');
		$images_list.push($img);
	});
	
	$('#slideshow').append('<img class="loader-img" src="public/themes/townsend/images/loader.gif">');
	
	$.loadImages($images_list, function() {
		$('img.loader-img').fadeOut(300);
		$images.fadeIn(300);
		startSlideshow();
	});
	
	function startSlideshow() {
		setInterval(function() {

			if ($current == ($imageCount - 1)) {
				$current = -1;
			}
			
			$c = $images.eq($current);
			$n = $images.eq($current + 1);
			
			$c.animate({opacity : 0.0}, 600, function(){
				$(this).removeClass('active');
				$(this).css({opacity : 1.0});
			});
			
			$n.hide().addClass('active').fadeIn(600);
			
			$current++;
			
		}, 4000);
	}
});
