var images = new Array("img/home/1.png", "img/home/3.png", "img/home/4.png", "img/home/5.png", "img/home/6.png", "img/home/7.png",
					   "img/home/9.png", "img/home/10.png", "img/home/11.png", "img/home/12.png", "img/home/13.png",
					   "img/home/14.png", "img/home/15.png", "img/home/16.png");
var lastImages = new Array();
var lastImage = null;
var timer;

function getRandomNumber(scale){
	var randomnumber = Math.floor(Math.random()*scale);
	return randomnumber;
}
	
function preloadImages(){
	for (var i = 0; i < images.length; i++) {
			$("<img>").attr("src", images[i]);
	}
}
	
function applyImageSettings(id){
	
	var img = new Image();
	$(img).attr("id", "newImage");
	$(img).attr("alt", "image1");
	$(img).attr("style", "position: absolute; z-index: 1;");
	$(img).load(function()
	{
		//alert("loaded");
		$("#rimg").html("");
		$("#rimg").append(this);
		if(lastImage!=null)
		{
			$(this).before('<img id="oldImage" src="'+images[lastImage].toString()+'" alt="image0" style="position: absolute; z-index: 1;"/>');
		}
		$(this).css("display", "none");
		$(this).fadeIn(1600);
	});
	$(img).attr("src", images[id].toString());
	
	/*
	$("#rimg").html('<img id="newImage" src="'+images[id].toString()+'" alt="image1" style="position: relative; z-index: 2;"/>');
	
	if(lastImage!=null)
	{
		$("#newImage").before('<img id="oldImage" src="'+images[lastImage].toString()+'" alt="image0" style="position: absolute; z-index: 1;"/>');
	}
	
	$("#newImage").css("display", "none");
	$("#newImage").fadeIn(1600);*/
}

function imgfadeIn(id){
	applyImageSettings(id);
}       
	
function displayRandomImage(){
	stopRandomImageTimer();
	
	var rn = getRandomNumber(images.length);
	while(rn == lastImages[0] || rn == lastImages[1]){
		rn = getRandomNumber(images.length-1);
	}
	var old = lastImages[0];
	lastImages[0] = rn;
	lastImages[1] = old;
	lastImage = old;
	imgfadeIn(rn);
	
	//alert("rimg");
	
	timer = setTimeout("displayRandomImage()", 8000); 
}   

function stopRandomImageTimer()
{
	clearTimeout(timer);
}


