function shuffle(array) {
	var listPos = [];
	var listHas = {};
	while (listPos.length < array.length) {
		var c = Math.round(Math.random() * (array.length - 1));
		if (listHas[c]) continue;
		listHas[c] = true;
		listPos.push(c);
	}
	var array2 = [];
	for (var n = 0; n < listPos.length; n++) array2.push(array[listPos[n]]);
	return array2;
}

//alert(shuffle(['a', 'b', 'c', 'd']));

var featured = function() { };

// CONFIG
{
	featured.imageList = shuffle([
		'img/featured/tov001.jpg',
		'img/featured/tov002.jpg',
		'img/featured/tov003.jpg',
		'img/featured/tov004.jpg',
		'img/featured/tov005.jpg',
		'img/featured/tov006.jpg'
	]);
	featured.transitionTime = 2000;
	featured.waitTime       = 7000;
}

featured.imageListPos   = 0;

//alert(featured.imageList);

featured.changeImage = function(imgUrl, callback, time) {
	var i = new Image();
	i.onload = function() {
		$('#featured_front').css('backgroundImage', "url('" + imgUrl + "')");
		$('#featured_front').fadeTo( 0, 0.0 );
		$('#featured_front').css('display', 'block');
		$('#featured_front').fadeTo( time, 1.0, function() {
			$('#featured_back').css('backgroundImage', "url('" + imgUrl + "')");
			$('#featured_front').fadeTo( 0, 0.0 );
			$('#featured_front').css('display', 'none');
			if (callback) setTimeout(callback, 0);
		} );
	};
	i.src = imgUrl;
};

featured.changeImageRotate = function() {
	setTimeout(function() {
		featured.changeImage(
			featured.imageList[featured.imageListPos = (featured.imageListPos + 1) % featured.imageList.length],
			featured.changeImageRotate,
			featured.transitionTime
		)
	}, featured.waitTime);
}

$('load', function() {
	featured.changeImageRotate();
});