var count, back, img;
var max = 4;
var time = 3000;
function nextImage() {
	count = (count >= max) ? 1 : count+1;
	var loader = new Image();
	loader.onload = function() {
		if (swapback) {
			//change back, fadeout front
			back.css({
				'background-image': 'url(' + this.src + ')'
			});
			img.fadeOut(time,function() {
				setTimeout(nextImage,time);
			});
			swapback = 0;
		} else {
			//change img, fadein
			img.attr('src',this.src);
			img.fadeIn(time,function() {
				setTimeout(nextImage,time);
			});
			swapback = 1;
		}
	}
	loader.src = '/images/home/' + count + '.jpg';
}

$(document).ready(function () {
	count = 1;
	back  = $('.slideshow');
	img   = $('.slideshow img');
	swapback = 1;
	setTimeout(nextImage,time);
});
