var slideshows = {};
function start_slideshow(sDivId, iInterval, iDelay)
{
	iDelay = parseInt(iDelay, 10) || 0;
	iInterval = parseInt(iInterval, 10) || 0;
	if ( !iInterval ) {
		iInterval = 7500;
	}
	
	var oContainer = document.getElementById(sDivId);
	if ( !oContainer ) {
		return;
	}
	
	var oChildren = new Array();
	for (var i = 0; i < oContainer.childNodes.length; i++) {
		if ( oContainer.childNodes[i].nodeType == 1 && oContainer.childNodes[i].tagName == 'DIV' ) {
			oChildren.push(oContainer.childNodes[i]);
		}
	}
	if ( oChildren.length < 2 ) {
		return;
	}
	
	for (var i = 1; i < oChildren.length; i++) {
		oChildren[i].style.display = 'none';
	}
	
	slideshows[sDivId] = {'children': oChildren, 'interval': iInterval, 'fade': 0, 'show': 0, 'next': 0};
	slideshows[sDivId]['next'] = window.setTimeout(function() { next_slide(sDivId); }, slideshows[sDivId]['interval'] + iDelay);
	return;
}
function next_slide(sDivId)
{
	window.clearTimeout(slideshows[sDivId]['next']);
	for (var i = 0, iCurrent = -1; slideshows[sDivId]['children'].length; i++) {
		if ( slideshows[sDivId]['children'][i].style.display != 'none' ) {
			iCurrent = i;
			break;
		}
	}
	if ( iCurrent == -1 ) {
		return;
	}
	
	var aPosition = getPosition(slideshows[sDivId]['children'][iCurrent]);
	var iWidth = slideshows[sDivId]['children'][iCurrent].offsetWidth;
	slideshows[sDivId]['children'][iCurrent].style.position = 'absolute';
	slideshows[sDivId]['children'][iCurrent].style.left = aPosition[0];
	slideshows[sDivId]['children'][iCurrent].style.top = aPosition[1];
	slideshows[sDivId]['children'][iCurrent].style.width = iWidth + 'px';
	
	var iNext = (iCurrent + 1) % slideshows[sDivId]['children'].length;
	for (var i = 0; i < slideshows[sDivId]['children'].length; i++) {
		if ( i != iCurrent && i != iNext ) {
			setOpacity(slideshows[sDivId]['children'][i], 0);
			slideshows[sDivId]['children'][i].style.display = 'none';
		}
	}
	
	setOpacity(slideshows[sDivId]['children'][iCurrent], 0);
	setOpacity(slideshows[sDivId]['children'][iNext], 100);
	slideshows[sDivId]['children'][iCurrent].style.display = 'none';
	slideshows[sDivId]['children'][iNext].style.display = "";
	
	var aPosition = getPosition(slideshows[sDivId]['children'][iNext]);
	var iWidth = slideshows[sDivId]['children'][iNext].offsetWidth;
	slideshows[sDivId]['children'][iNext].style.position = 'absolute';
	slideshows[sDivId]['children'][iNext].style.left = aPosition[0];
	slideshows[sDivId]['children'][iNext].style.top = aPosition[1];
	slideshows[sDivId]['children'][iNext].style.width = iWidth + 'px';
	
	setOpacity(slideshows[sDivId]['children'][iCurrent], 100);
	setOpacity(slideshows[sDivId]['children'][iNext], 0);
	slideshows[sDivId]['children'][iCurrent].style.display = "";
	slideshows[sDivId]['children'][iNext].style.display = 'none';
	
	fade_slide(slideshows[sDivId]['children'][iCurrent], sDivId);
	show_slide(slideshows[sDivId]['children'][iNext], sDivId);
	return;
}
function fade_slide(oElement, sDivId)
{
	window.clearTimeout(slideshows[sDivId]['fade']);
	var iOpacity = getOpacity(oElement);
	if ( iOpacity > 0 ) {
		iOpacity = Math.max(0, iOpacity - 5);
		setOpacity(oElement, iOpacity);
		slideshows[sDivId]['fade'] = window.setTimeout(function() { fade_slide(oElement, sDivId); }, 20);
	} else {
		oElement.style.display = 'none';
		oElement.style.position = "";
		oElement.style.left = "";
		oElement.style.top = "";
		oElement.style.width = "";
	}
	return;
}
function show_slide(oElement, sDivId)
{
	window.clearTimeout(slideshows[sDivId]['show']);
	oElement.style.display = "";
	var iOpacity = getOpacity(oElement);
	if ( iOpacity < 100 ) {
		iOpacity = Math.min(100, iOpacity + 5);
		setOpacity(oElement, iOpacity);
		slideshows[sDivId]['show'] = window.setTimeout(function() { show_slide(oElement, sDivId); }, 20);
	} else {
		setOpacity(oElement, 100);
		oElement.style.position = "";
		oElement.style.left = "";
		oElement.style.top = "";
		oElement.style.width = "";
		slideshows[sDivId]['next'] = window.setTimeout(function() { next_slide(sDivId); }, slideshows[sDivId]['interval']);
	}
	return;
}
function getOpacity(oElement)
{
	iOpacity = 100;
	if ( oElement.style.opacity ) {
		iOpacity = parseFloat(oElement.style.opacity) * 100;
	} else if ( oElement.style.MozOpacity ) {
		iOpacity = parseFloat(oElement.style.MozOpacity) * 100;
	} else if ( oElement.style.KhtmlOpacity ) {
		iOpacity = parseFloat(oElement.style.KhtmlOpacity) * 100;
	} else if ( oElement.style.filters ) {
		if ( oElement.style.filters.alpha ) {
			iOpacity = parseInt(oElement.style.filters.alpha, 10);
		}
	}
	return iOpacity;
}
function setOpacity(oElement, iOpacity)
{
	if ( iOpacity >= 100 ) {
		oElement.style.opacity = 1;
		oElement.style.MozOpacity = 1;
		oElement.style.KhtmlOpacity = 1;
		oElement.style.filter = "";
	} else {
		oElement.style.opacity = (iOpacity / 100).toString();
		oElement.style.MozOpacity = (iOpacity / 100).toString();
		oElement.style.KhtmlOpacity = (iOpacity / 100).toString();
		oElement.style.filter = 'alpha(opacity=' + iOpacity.toString() + ')';
	}
	return;
}
function getPosition(oNode, iHorz, iVert)
{
	var sHorz = 0;
	var sVert = 0;
	if ( typeof iHorz == 'number' ) {
		sHorz = iHorz;
	}
	if ( typeof iVert == 'number' ) {
		sVert = iVert;
	}
	if ( oNode.offsetParent ) {
		while ( oNode.offsetParent ) {
			sHorz += oNode.offsetLeft;
			sVert += oNode.offsetTop;
			oNode  = oNode.offsetParent;
		}
		sHorz += oNode.offsetLeft;
		sVert += oNode.offsetTop;
	} else if ( oNode.x && oNode.y ) {
		sHorz += oNode.x;
		sVert += oNode.y;
	}
	return [sHorz.toString() + 'px', sVert.toString() + 'px'];
}

