/*!***********************************************************************
	Copyright 2011 Darren Berry (design@darrenberry.co.uk)

	All rights reserved. The information contained in this document
	is confidential and may also be proprietary and trade secret. It
	is the intellectual property of Darren Berry, and without
	prior written approval from Darren Berry no part of this
	document may be reproduced or transmitted in any form or by any
	means, including but not limited to electronic, mechanical,
	photocopying or stored in any retrieval system of whatever nature.
	The use of copyright notice does not imply unrestricted public
	access to any part of this document. 
*************************************************************************/

var slideImage = new Array(),
	slideCurrent = slideCount = 0,
	slideTimer = faderTimer = null,
	fadeToggle = false;

function initSlideshow()
{
	if (slideCount)
	{
		fadeToggle = false;
		setSlide(0, false);
		playSlideshow();
	}
}

function playSlideshow()
{
	if (slideTimer)
		clearTimeout(slideTimer);
	slideTimer = setTimeout('nextSlide(true)', 5000);
}

function stopSlideshow()
{
	if (slideTimer)
		clearTimeout(slideTimer);
	if (faderTimer)
		clearTimeout(faderTimer);
}

function addSlide(image, title)
{
	slideImage[slideCount] = new Image();
	slideImage[slideCount].src = image;
	slideImage[slideCount].alt = title;
	slideCount++;
}

function nextSlide(fader)
{
	setSlide(slideCurrent + 1, fader);
}

function setSlide(slide, fader)
{
	slide %= slideCount;
	slideCurrent = slide % slideCount;

	if (obj = document.getElementById((fadeToggle) ? "slide2" : "slide1"))
	{
		obj.src = slideImage[slideCurrent].src;
		obj.alt = obj.title = slideImage[slideCurrent].alt;
	}
	if (obj = document.getElementById((fadeToggle) ? "slide1" : "slide2"))
		obj.alt = obj.title = slideImage[slideCurrent].alt;

	if (faderTimer)
		clearTimeout(faderTimer);
	faderTimer = setTimeout('fadePhotoIn(' + ((fader) ? 0 : 100) + ')', 0);
}

function fadePhotoIn(opacity)
{
	if (obj = document.getElementById((fadeToggle) ? "slide2" : "slide1"))
		setOpacity(obj, opacity);
	
	if (obj = document.getElementById((fadeToggle) ? "slide1" : "slide2"))
		setOpacity(obj, 100 - opacity);
	
	if (opacity < 100)
	{
		if (faderTimer)
			clearTimeout(faderTimer);
		faderTimer = setTimeout('fadePhotoIn(' + Math.min(opacity + 4, 100) + ')', 25);
	}
	else
	{
		fadeToggle = !fadeToggle;
		playSlideshow();
	}
}

function setOpacity(obj, opacity)
{
	obj.style.opacity = opacity / 100;
	obj.style.filter = 'alpha(opacity=' + opacity + ')';
}


