var slideDelay = 6000;
var slideImageList = [];
var slideFirstImage = [];
var running = null;
var startFunction = new Function("");
var timer = null;

function stopSlide()
{
	running = 0;
	
	var button = EL('startButton');
	
	button.innerHTML = "START";
	button.href = "javascript: startSlide();";
	EL('slideRemote').style.background = "#990000";
	
}

function startSlide()
{
	if( running == 1 )
	{
		return;
	}
	
	var button = EL('startButton');
	
	button.innerHTML = "STOP";
	button.href = "javascript: stopSlide();";
	EL('slideRemote').style.background = "#444";
	
	running = 1;
	
	startFunction();
}

function preloadImages(imagesPath, imgList, parentNode)
{
	slideImageList[parentNode] = [];
	
	for(var i = 0; i < imgList.length; i ++ )
	{
		var img = new Image();
		
		if( typeof imagesPath != 'undefined' )
		{
			imgList[i] = imagesPath	+ imgList[i];
		}
		
		img.src = imgList[i];
		
		slideImageList[parentNode].push(img);
	} 
}

function renderImages(imagesPath, imgList, parentNode)
{
	preloadImages(imagesPath, imgList, parentNode);
	
	var firstIdx = Math.round(Math.random() * (imgList.length - 1));
	
	slideFirstImage[parentNode] = firstIdx;
	
	for( var i = 0; i < imgList.length; i ++ )
	{
		renderImage(imgList[i], parentNode, parentNode + "slideimage" + i, (slideFirstImage[parentNode] != i) );
	}
	
	imageShown(firstIdx);
}

function renderImage(img, parentNode, id, hidden)
{
	parentNode = document.getElementById(parentNode);
	
	var d = document.createElement('div');
	d.id = id;
	
	d.style.position = "absolute";
	d.className = "photocontainer";
	
	var im = document.createElement('img');
	im.src =img;
	//with(im.style)
	with(d.style)
	{
		width = parentNode.style.width;
		height = parentNode.style.height;
	}
	
	d.style.textAlign = 'center';
	
	d.appendChild(im);
	
	if( hidden )
	{
		Valraiso.util.changeOpacity(d, 0);
	}
	
	parentNode.appendChild(d);
}

function slideImages(imgList, parentNode, delay, fromIdx)
{
	if( running == null )
	{
		running = 1;
	}
	
	if( running == 0 )
	{
		startFunction = function() { slideImages(imgList, parentNode, delay, fromIdx); };
		clearTimeout(timer);
		return;
	}
	
	if( imgList.length < 2 )
	{
		return;	
	}
	
	if( typeof parentNode == 'string' )
	{
		parentNode = document.getElementById(parentNode);
	}
	
	if( delay == null )
	{
		delay = slideDelay;	
	}
	
	if( fromIdx == null )
	{
		fromIdx = slideFirstImage[parentNode.id];
	}
	
	var nextIdx = fromIdx + 1;
	
	if( nextIdx > imgList.length - 1 )
	{
		nextIdx = 0;
	}
	
	if( slideImageList[parentNode.id] == null || !slideImageList[parentNode.id][nextIdx].complete )
	{
		setTimeout( function() { slideImages(imgList, parentNode, delay, fromIdx) } , 200 );
		return;
	}
	
	Valraiso.util.fading(parentNode.id + "slideimage" + fromIdx, 100, 0, 600);
	Valraiso.util.fading(parentNode.id + "slideimage" + nextIdx, 0, 100, 600);
	
	setTimeout( function() { imageHidden(nextIdx); }, 10);	
	setTimeout( function() { imageShown(nextIdx); }, 500);	
	
	
	

	clearTimeout(timer);
	timer = setTimeout(function() { slideImages(imgList, parentNode, delay, nextIdx)}, delay);
}

function imageHidden()
{
	
}

function imageShown()
{
		
}