// load a random image into the header
var headerImg = ['header-1.jpg', 'header-2.jpg', 'header-3.jpg', 'header-4.jpg', 'header-5.jpg', 'header-6.jpg'];
var headerImgMax = headerImg.length, headerImgWait = 300000, headerImgPause = null, i = 0;
// load a random image into the home page content
var contentImg = ['homepage-1.jpg', 'homepage-2.jpg', 'homepage-3.jpg', 'homepage-4.jpg', 'homepage-5.jpg'];
var contentImgMax = contentImg.length, contentImgWait = 10000, contentImgPause = null, c = 0, which = 1, contentImgRnd = [];
$().ready(function() {
	if ($('#header').length > 0) {
		// randomise the order of the array
		headerImg.sort(function() { return 0.5 - Math.random() });
		// add a div with background image to fade in/out
		$('#header').prepend($('<div />').attr('id', 'header-img').css({'position':'absolute', 'top':'0', 'right':'10px', 'width':'500px', 'height':'225px', 'background':'url(/interface/images/page/'+headerImg[i]+') no-repeat right top'}));
		// set up next image swap
		loadOrPreload();
	}
	if ($('#intro').length > 0) {
		// randomise the order of the array
		contentImg.sort(function() { return 0.5 - Math.random() });
		// add a div with background image to fade in/out
		$('#intro').prepend($('<div />').attr('id', 'intro-img-2').css({'display':'none', 'position':'absolute', 'top':'0', 'left':'0', 'width':'180px', 'height':'250px', 'background':'url(/uploads/images/'+contentImg[1]+') no-repeat left top'}));
		$('#intro').prepend($('<div />').attr('id', 'intro-img').css({'position':'absolute', 'top':'0', 'left':'0', 'width':'180px', 'height':'250px', 'background':'url(/uploads/images/'+contentImg[0]+') no-repeat left top'}));
		c ++;
		if (c >= contentImgMax) c = 0;
		// set up next image swap
		loadOrPreloadContent();
	}
});
function loadNextImage() {
	clearTimeout(headerImgPause);
	// fade out
	$('#header-img').fadeOut(300, function() {
		// swap image
		$('#header-img').css('background-image', 'url(/interface/images/page/'+headerImg[i]+')');
		// fade back in
		$('#header-img').fadeIn(300, function() {
			loadOrPreload();
		});
	});
}
function loadOrPreload() {
	i ++;
	if (i >= headerImgMax) {
		// set timeout for next image to load
		i = 0;
		headerImgPause = setTimeout('loadNextImage()', headerImgWait);
	}
	else {
		// pre-load next image, then set timeout for next image to load
		$('<img />').attr('src', '/interface/images/page/'+headerImg[i]).load(function(){ 
			headerImgPause = setTimeout('loadNextImage()', headerImgWait);
		});
	}
}
function loadNextContentImage() {
	clearTimeout(contentImgPause);
	// fade out
	var fo = (which == 2) ? '-2' : '';
	var fi = (which == 2) ? '' : '-2';
	which = (which == 2) ? 1 : 2;
	$('#intro-img'+fo).fadeOut(400, function() {
		// swap image
		$('#intro-img'+fo).css('background-image', 'url(/uploads/images/'+contentImg[c]+')');
	});
	// fade back in
	$('#intro-img'+fi).fadeIn(400, function() {
		loadOrPreloadContent();
	});
}
function loadOrPreloadContent() {
	c ++;
	if (c >= contentImgMax) {
		// set timeout for next image to load
		c = 0;
		contentImgPause = setTimeout('loadNextContentImage()', contentImgWait);
	}
	else {
		// pre-load next image, then set timeout for next image to load
		$('<img />').attr('src', '/uploads/images/'+contentImg[c]).load(function(){ 
			contentImgPause = setTimeout('loadNextContentImage()', contentImgWait);
		});
	}
}
