Event.observe(window, 'load', init);

var NEXT_PHOTO = -1;

/**
 * Inits the page
 */
function init() {
	// Set the first picture
	setRandomPicture();
	
	// Set up the executer to change the picture
	new PeriodicalExecuter(function(pe){
		var element = $('photo').select('h2')[0];
		new Effect.Opacity(element, { from: 1.0, to: 0.0, duration: 0.5, queue: 'front',
			afterFinish: function(effect) {
				setRandomPicture();
			}
		});
		new Effect.Opacity(element, { from: 0.0, to: 1.0, duration: 0.5, queue: 'end'});
	}, 4);
}

/**
 * Sets a random picture for the home page
 */
function setRandomPicture() {
	var photos = [
		_makeHomeImage('4_4.jpg'),
		_makeHomeImage('autumnwest.jpg'),
		_makeHomeImage('financialstmt.jpg'),
		_makeHomeImage('wishlist.jpg'),
		_makeHomeImage('volunteer.jpg'),
		_makeHomeImage('utilityservices.jpg')
	];
	
	if(NEXT_PHOTO < 0)
		NEXT_PHOTO = Math.round(Math.random() * photos.length);
	
	var photo = photos[NEXT_PHOTO];
	NEXT_PHOTO = (NEXT_PHOTO + 1) % (photos.length);
	
	$('photo').select('h2')[0].setStyle({
		'backgroundImage': 'url(' + photo.src + ')'
	});
}

/**
 * Create and return a home page image (this makes sure the image is preloaded and cached)
 */
function _makeHomeImage(image) {
	var img = new Image();
	img.src = '/images/common/' + image;
	
	return img
}

