
faderVal = 0;
scaleParent = 0;
isNested = 0;
timeoutID = false;

function startImgLoop(parent, id, path, list, delay, generate, scale, titlesuffix) {
	// Basic checks
	if (!$(parent)) {
		alert("RandomImage: '"+parent+"' not found in page, aborting.");
		return;
	}
	if (!$(id)) {
		alert("RandomImage: '"+id+"' not found in page, aborting.");
		return;
	}
	if (!titlesuffix) // Not set, remove extension: '.jpg'
		titlesuffix = 4;
	
	// Is id nested in parent?
	oParent = $(parent);
	for(var i=0; i<oParent.childNodes.length; i++) {
		if (oParent.childNodes[i].id == id)
			isNested = 1;
	}
	
	scaleParent = generate && scale;
	
	// Make sure img is invisible
	setOpacity($(id), 0);
	
	// Start looping
	nextImg(parent, id, path, list, 0, delay, titlesuffix);
}

function nextImg(parent, id, path, list, num, delay, titlesuffix) {
	// Clear timeout
	if (timeoutID)
		window.clearTimeout(timeoutID);
	
	// Initial
	if (num == 0) {
		// Set current image to background
		var cur_img = list[num].split("'").join("%27");
		setImg(parent, path+'/'+cur_img);
		if (scaleParent)
			window.setTimeout("setParentHeight('"+parent+"', '"+id+"', 0);", 250);
		// Only one image, stop here
		if (list.length == 1)
			return;
		// Preload next image in img
		num++;
		var next_img = list[num].split("'").join("%27");
		setImg(id, path+'/'+next_img);
	} else {
		if (num == list.length) // Wrap back to start
			num = 0;
		var next_img = list[num].split("'").join("%27");
		// Fade in/out
		if (isVisible(id))
			fadeOutImg(parent, id, path+'/'+next_img);
		else
			fadeInImg(parent, id, path+'/'+next_img);
	}
	
	if ($('RandImgTitle')) { // Update the title if it's present
		var cur_title = list[num-1];
		if (num == 0)
			cur_title = list[list.length-1];
		cur_title = cur_title.substr(0, cur_title.length - titlesuffix);
		$('RandImgTitle').innerHTML = cur_title;
	}
	
	num++;
	
	// Set timeout for nextImg
	timeoutID = window.setTimeout("nextImg('"+parent+"', '"+id+"', '"+path+"', [\""+list.join("\", \"")+"\"], "+num+", '"+delay+"', '"+titlesuffix+"');", delay);
}

function fadeInImg(parent, id, img) {
	faderInterval = window.setInterval("fadeImg('"+parent+"', '"+id+"', '"+img+"', '"+(1)+"')", 10);
}
function fadeOutImg(parent, id, img) {
	setOpacity($(parent), 100);
	faderInterval = window.setInterval("fadeImg('"+parent+"', '"+id+"', '"+img+"', '"+(-1)+"')", 10);
}

function fadeImg(parent, id, img, direction) {
	if (faderVal > 100) {
    	window.clearInterval(faderInterval);
    	faderVal = 0;
    	
		// Preload the next image now that we're done
		if (direction > 0) {
			if (!isNested)
				setOpacity($(parent), 0);
			setImg(parent, img);
		} else {
			setImg(id, img);
		}
	} else {
		if (direction > 0) {
			if (scaleParent)
				setParentHeight(parent, id, faderVal);
			setOpacity($(id), faderVal);
		} else {
			if (scaleParent)
				setParentHeight(id, parent, faderVal);
			setOpacity($(id), 100-faderVal);
		}
		faderVal += 5;
	}
}

function isVisible(id) {
	if ($(id).style.display == 'none')
		return false;
	
	if (navigator.userAgent.indexOf("MSIE") != -1) {
		if ($(id).style.filter == "alpha(opacity=0)")
			return false;
	} else {
		if ($(id).style.opacity == 0)
			return false;
	}
	return true;
}

function setImg(id, src) {
	if (!$(id))
		return;
	if ($(id).tagName.toLowerCase() == 'img')
		$(id).src = src;
	else
		$(id).style.backgroundImage = "url('"+src+"')";
}

function setParentHeight(parent, id, val) {
	var ph = $(parent).height;
	var ih = $(id).height;
	var contain = $(parent).parentNode;
	contain.style.height = (ph * (1-val/100) + ih * (val/100)) + 'px';
}

