// JavaScript Document
var quote_timer = setInterval(startAnimateQuote, 3000);	// animation timer
var animate_timer = null;
var opacity = 100;
var fade = true;
var max_count = slideshow_count;
//var count = 1 + Math.round((max_count-1)*Math.random());
var count = slideshow_first;
preloadNextImage (getNext(count)); // preload next image

var next_pic;	// variable pointing to the next picture... used to check if it is complete or not

function getNext (count)
{
	if (count == max_count)
	{
		next = 1;
	}
	else
	{
		next = count + 1;
	}
	return next;
}

function preloadNextImage (next)
{
	// preload one next image, to make for smooth animation
	next_pic = new Image ();
	next_pic.src = "images/slideshow/slideshow" + next + ".jpg";
}

function startAnimateQuote ()
{
	if (animate_timer != null)
	{
		clearInterval (animate_timer);
		animate_timer = null;
	}
	// only start the animation if the next picture has loaded, otherwise skip until next timer click
	if (next_pic.complete)
	{
		animate_timer = setInterval(animateQuote, 60);
	}
}

function animateQuote ()
{
	if (fade)
	{
		opacity -= 10;
		if (opacity <= 0)
		{
			opacity = 0;
			fade = false;
			
			// switch the image and text
			count = getNext (count);
			setOpacity ('slideshow_image', 0);
			document.getElementById('slideshow_image').src = 'images/slideshow/slideshow' + count + '.jpg';
			preloadNextImage (getNext(count));	// preload next image
		}
	}
	else
	{
		opacity += 20;
		if (opacity >= 100)
		{
			// stop animation
			opacity = 100;
			fade = true;
			if (animate_timer != null)
			{
				clearInterval (animate_timer);
				animate_timer = null;
			}
		}
	}
	setOpacity ('slideshow_image', opacity/100.0);
}

function setOpacity(element, opacity)
{
  e = document.getElementById(element);
  e.style['opacity'] = opacity;	// standard CSS3
  e.style.filter = "alpha(opacity:"+(100*opacity)+")";	// IE
  e.style.KHTMLOpacity = opacity;	// KDE
  e.style.MozOpacity = opacity;		// Mozilla 
}
