$(document).ready(function(){
	
	// initialize
	var imageCount = $('#slider').find('img').size();
	var lastImg = Math.floor(Math.random()*imageCount);
	var myCurrent = [lastImg];
	var nextImg;
	var n = 1;
	var i;
	var x = 5; // number of seconds per image
	
	// display a random image
	$('#slider img:eq('+lastImg+')').css({'opacity':'1', 'display':'block', 'z-index':'101'});

	// run every x seconds
	setInterval(function() {
		
		// select new image
		i = Math.floor(Math.random()*imageCount);
		// if it has already been slected in this round, then try again
		while ( ($.inArray(i, myCurrent)>-1) || (i==lastImg) ) {
			i = Math.floor(Math.random()*imageCount);
		};
		
		// store new image in array
		myCurrent[n] = i;
		
		// show the new image behind current one
		$('#slider img:eq('+i+')').css({'z-index':'100','display':'block','opacity':'1'});
		
		// fade out current image
		$('#slider img:eq('+lastImg+')').animate({'opacity': '0'}, 1000, 'linear', function() {
			
			// move new image to foreground
			$('#slider img:eq('+i+')').css({'z-index':'101'});
			
			// hide last image
			$('#slider img:eq('+lastImg+')').css({'display':'none'});
			
			// reset randomList if all images have played
			if (myCurrent.length == imageCount) {
				myCurrent = [];
				n = -1;
			};
			
			// prepare for next loop
			lastImg = i;
			n++;
		});
		

	}, (x*1000) );
		
		
});
