var Slideshow_images = new Array();
var Slideshow_current = new Array();
var Slideshow_wait = new Array();
var Slideshow_destination = new Array();
var Slideshow_once = new Array();

function Slideshow_setwait( inst, wait )
{
	Slideshow_wait[inst] = Math.max( 0, wait );
}
function Slideshow_registerImage( inst, el )
{
	if( !Slideshow_images[inst] )
	{
		Slideshow_images[inst] = new Array( el );
		Slideshow_current[inst] = 0;
	}
	else
		Slideshow_images[inst].push( el );
}

function Slideshow_setfade( el, opacity )
{
	el.opacity = Math.max( Math.min( opacity, 1 ), 0 );
	el.style.opacity = opacity;
	el.style.filter = "alpha(opacity=" + ( opacity * 100 ) + ")";
}

function Slideshow_fadeout( inst )
{
	var prev_el = document.getElementById( Slideshow_images[inst][Slideshow_current[inst]] );
	Slideshow_setfade( prev_el, prev_el.opacity - 0.04 );
	
	if( prev_el.opacity == 0 )
	{
		prev_el.style.display = "none";
		prev_el.opacity = 1;
		var next_id = Slideshow_current[inst] + 1;
		if( next_id >= Slideshow_images[inst].length )
			next_id = 0;
		Slideshow_current[inst] = next_id;
		var next_el = document.getElementById( Slideshow_images[inst][next_id] );
		next_el.style.display = "block";
		next_el.opacity = 1;
		Slideshow_setfade( next_el, 1 );
		
		var next_id = Slideshow_current[inst] + 1;
		if( next_id >= Slideshow_images[inst].length )
			next_id = 0;

		var next_el = document.getElementById( Slideshow_images[inst][next_id] );
		var bg = document.getElementById( "GPSlideshow_" + inst );
		bg.style.backgroundImage = "url( '" + next_el.src + "' )";		

		if( next_id == 0 && Slideshow_once[inst] == true )
		{
			if( Slideshow_destination[inst].length )
			{
				setTimeout( "window.location = \"" + Slideshow_destination[inst] + "\";", Math.max( 0,  Slideshow_wait[inst] ) * 1000 );
			}
		}
		else
		{
			setTimeout( "Slideshow_fadeout( " + inst + " )", Math.max( 0,  Slideshow_wait[inst] ) * 1000 );
		}
	}
	else
	{
		setTimeout( "Slideshow_fadeout( " + inst + " )", 100 );
	}
}
function Slideshow_start( inst, once, dest )
{
	if( once )
	{
		Slideshow_destination[inst] = dest;
		Slideshow_once[inst] = true;
	}
	else
		Slideshow_once[inst] = false;

	if( Slideshow_images[inst].length > 1 )
	{
		var prev_el = document.getElementById( Slideshow_images[inst][Slideshow_current[inst]] );
		prev_el.opacity = 1;

		var next_id = Slideshow_current[inst] + 1;
		if( next_id >= Slideshow_images[inst].length )
			next_id = 0;
		var next_el = document.getElementById( Slideshow_images[inst][next_id] );
		var bg = document.getElementById( "GPSlideshow_" + inst );
		bg.style.backgroundImage = "url( '" + next_el.src + "' )";
		setTimeout( "Slideshow_fadeout( " + inst + " )", Math.max( 0,  Slideshow_wait[inst] ) * 1000 );
	}
	else
	{
		alert( "Too few images in Slideshow. There are " + Slideshow_images[inst].length );
	}
}
