var ie = document.all;
var ff = !document.all && document.getElementById;

if (ie)
{
	var initialOpacity = 110;
	var finalOpacity = -110;
	var opacityIncrement = 10;
	var opacityChange = 110;
}
else if (ff)
{
	var initialOpacity = 1.1;
	var finalOpacity = -1.1;
	var opacityIncrement = 0.1;
	var opacityChange = 1.1;
}

var nextImage;
var imgBox, imgCaption;
var xopen, xclose;
var status = true;
var imgMouseOver = false;
var onCaption = true;
var loadImage;
var loadImage2;
var countImg = 0;

var imageNotLoaded = true;
var startTimer = true;
var imageTimer;

window.onload = function loader()
{
	imgBox        = document.getElementById("imgBox");
	imgCaption    = document.getElementById("imgCaption");
	imgCaptionDiv = imgCaption.getElementsByTagName("div")[0];
	
	imageLoader();
	setInterval('changeImage()',8000);
}

function imageLoader()
{
	var randomNum = Math.floor(Math.random()*imgArray.length);
	var randomImage = path + imgArray[randomNum];
	countImg = randomNum;
	imgBox.style.backgroundImage = "url(" + randomImage + ")";
	
	loadNextImage();
}

function changeImage()
{
	countImg++;
	if (countImg == imgArray.length)
	{
		countImg = 0;
	}
	
	loadImage = new Image();
	loadImage.src  = path + imgArray[countImg];
	nextImage = loadImage.src;
	
	loadNextImage();
	loadNextImage2();
	
	hideCaption();
	fadeOut(nextImage);
}

function fadeOut(nextImage)
{
	if (opacityChange.toFixed(1) != finalOpacity)
	{
		opacityChange -= opacityIncrement;
		ff ? imgBox.style.opacity = opacityChange : "";
		ie ? imgBox.style.filter = "alpha(opacity=" + opacityChange + ")" : "";
		setTimeout("fadeOut(nextImage)", 50);
	}
	else
	{
		/*while (!loadImage.complete && imageNotLoaded)
		{
			if (startTimer)
			{
				alert(2);
				imageTimer = setTimeout("cancelLoad()", 2000);
				startTimer = false;
			}
		}
		
		imageNotLoaded = true;
		startTimer = true;
		clearTimeout(imageTimer);*/
		
		imgBox.style.backgroundImage = "url(" + nextImage + ")";
		fadeIn();
	}
}

function cancelLoad()
{
	imageNotLoaded = false;
	alert(1);
}

function fadeIn()
{
	if (opacityChange.toFixed(1) != initialOpacity)
	{
		opacityChange += opacityIncrement;
		ff ? imgBox.style.opacity = opacityChange : "";
		ie ? imgBox.style.filter = "alpha(opacity=" + opacityChange + ")" : "";
		setTimeout("fadeIn()", 50);
	}
	
	if (imgMouseOver)
	{
		status = false;
		clearTimeout(xopen);
		showCaption();
	}
}

function showCaption()
{
	if (!status)
	{
		status = true;
		typeof(xclose) == "number" ? clearTimeout(xclose) : "";
	}
	
	if (parseInt(imgCaption.style.height) == 2)
	{
		imgCaptionDiv.innerHTML = captionArray[countImg];
		imgCaption.style.visibility = "visible";
	}
	
	if (parseInt(imgCaption.style.height) != 32)
	{
		imgCaption.style.height = parseInt(imgCaption.style.height) + 2 + "px";
		xopen = setTimeout("showCaption()", 25);
	}
}

function hideCaption()
{
	if (status)
	{
		status = false;
		typeof(xopen) == "number" ? clearTimeout(xopen) : "";
	}
	
	if (parseInt(imgCaption.style.height) == 2)
	{
		imgCaption.style.visibility = "hidden";
	}
	
	if (parseInt(imgCaption.style.height) != 0)
	{
		imgCaption.style.height = parseInt(imgCaption.style.height) - 2 + "px";
		xclose = setTimeout("hideCaption()", 25);
	}
}

function loadNextImage()
{
	if (countImg == imgArray.length - 1)
	{
		loadImage2 = new Image();
		loadImage2.src = path + imgArray[0];
	}
	else
	{
		loadImage2 = new Image();
		loadImage2.src = path + imgArray[countImg + 1];
	}	
}

function loadNextImage2()
{
	if (countImg == imgArray.length - 1)
	{
		loadImage2 = new Image();
		loadImage2.src = path + imgArray[0];
	}
	else
	{
		loadImage2 = new Image();
		loadImage2.src = path + imgArray[countImg + 2];
	}	
}