var count, back, img;
var max = 3;
var time = 5.5;

$(document).ready(function() {
	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(1500,function() {
					setTimeout(nextImage,time*1000);
				});
				swapback = 0;
			} else {
				//change img, fadein
				img.attr('src',this.src);
				img.fadeIn(1500,function() {
					setTimeout(nextImage,time*1000);
				});
				swapback = 1;
			}
		}
		loader.src = '/images/home-ss/' + count + '.jpg';
	}
	
	$(document).ready(function () {
		count = 1;
		back  = $('#home-ss');
		img   = $('#home-ss img');
		swapback = 1;
		setTimeout(nextImage,time*1000);
	});
});
