/**
 * Background Slideshow
 * 
 * @author Ben Auld <ben@popcornwebdesign.co.uk>
 */

/**
 * Transition speed
 * 
 * @var integer
 */
var slideshowSpeed = 5000;

/**
 * Array of photos
 * 
 * @var array
 */
var photos = new Array(
	"/img/backgrounds/1.jpg",
	"/img/backgrounds/2.jpg",
	"/img/backgrounds/3.jpg",
	"/img/backgrounds/4.jpg",
	"/img/backgrounds/5.jpg",
	"/img/backgrounds/6.jpg",
	"/img/backgrounds/7.jpg"
);


$(document).ready(function() {
	if ($("body").hasClass("slideshow")) {
		var imagesHTML = "";
		
		for (var i in photos) {
			imagesHTML += '<img src="' + photos[i] + '" alt=""' + (i == 0 ? ' class="active"' : '') + ' />';
		}
		
		var divHTML = '<div class="background">' + imagesHTML + '</div>';
		
		$("body").prepend(divHTML);
		
		setInterval("switchImage()", slideshowSpeed);
	}
});

function switchImage() {
	var active = $('.background img.active');
	
	if (active.length == 0) {
		active = $('.background img:last');
	}
	
	var next =  active.next().length ? active.next() : $('.background img:first');
	
	active.addClass('last-active');
	
	next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 500, function() {
			active.removeClass('active last-active');
		}
	);
}
