$(document).ready(function(){	
	$("#slider").easySlider({
		auto: false,
		continuous: true 
	});
});

function resizeBackground() {
	var $ = document.id;
	
	var baseWidth = 1600;
	var baseHeight = 1200;

	var container = $( "fading-background-container" );
	var cSize = container.getSize();
	
	var width = null;
	var height = null;
	if ( baseWidth / baseHeight > cSize.x / cSize.y ) {
		height = cSize.y;
		width = height / baseHeight * baseWidth;
	} else {
		width = cSize.x;
		height = width / baseWidth * baseHeight;
	}
	
	var img = $( "fading-background" );
	img.setStyle( "width", width );
	img.setStyle( "height", height );
	img.setStyle( "margin-left", width / -2 );
	img.setStyle( "margin-top", height / -2 );
}

function shuffleBackground( images ) {
	if ( images.length < 2 ) {
		return;
	}

	var $ = document.id;

	var img = $( "fading-background" );
	
	var currentBackground = img.getProperty( "src" );
	var newBackground = null;
	while( newBackground === null || newBackground == currentBackground ) {
		newBackground = images[ $random( 0, images.length -1 ) ];
	}
	
	img.setProperty( "src", newBackground );
}

window.addEvent( "domready", function() {
	resizeBackground();
	window.addEvent( "resize", resizeBackground );

	var images = [ 'images/fading-background/01.jpg', 'images/fading-background/02.jpg', 'images/fading-background/03.jpg' ];
	new Asset.images( images, { onComplete: function() {
		shuffleBackground( images );
		shuffleBackground.periodical( 20000, null, [ images ] );
	} } );
} );

