/*
 * Some wrapper code for Timothy Groves's Crossfader (http://www.brandspankingnew.net/archive/2006/09/javascript_css_crossfader.html)
 *
 * Code in this wrapper is tied closely to the HTML page (e.g., div and button id's are hard-coded).  There's
 * also some Javascript embedded with the HTML so it gets executed as the page is loaded -- helps the slide
 * show to degrade gracefully if the user doesn't have JS enabled.
 *
 * Gregory Smith
 * http://beaglewriter.com
 * beaglewriter@att.net
 */

var cf;
var divsToFade = new Array('stkitts1','stkitts2','stkitts3','stkitts4');

/*
 * Executes when the page onload event is raised, meaning all the images have been loaded and the show can start
 */
function Loaded(loadingId)
{
	document.getElementById(loadingId).style.display = "none";
	cf = new Crossfader( divsToFade, 500, 2000, true );
	StartStopOnClick(true);

	if (document.addEventListener)
	{
		document.getElementById("stopbutton").addEventListener("click", Stop, false);
		document.getElementById("startbutton").addEventListener("click", Start, false);
		document.getElementById("setnewdelaybutton").addEventListener("click", SetDelay, false);
		document.getElementById("setnewfadebutton").addEventListener("click", SetFade, false);
	}
	else if (document.attachEvent)
	{
		document.getElementById("stopbutton").attachEvent("onclick", Stop);
		document.getElementById("startbutton").attachEvent("onclick", Start);
		document.getElementById("setnewdelaybutton").attachEvent("onclick", SetDelay);
		document.getElementById("setnewfadebutton").attachEvent("onclick", SetFade);
	}
	else
	{
		alert ("Error:  cannot activate control buttons");
	}
}

function StartStopOnClick (startStop)
{
	for (var i = 0; i < divsToFade.length; i++)
	{
		if (document.addEventListener)
		{
			if (startStop)
			{
				document.getElementById(divsToFade[i]).addEventListener("click", StartStop, false);
			}
			else
			{
				document.getElementById(divsToFade[i]).removeEventListener("click", StartStop, false);
			}
		}
		else if (document.attachEvent)
		{
			if (startStop)
			{
				document.getElementById(divsToFade[i]).attachEvent("onclick", StartStop);
			}
			else
			{
				document.getElementById(divsToFade[i]).detachEvent("onclick", StartStop);
			}
		}
	}
}

function StartStop(e)
{
	if (cf.stopped)
	{
		cf.StartFade();
	}
	else
	{
		cf.StopFade();
	}
}

/*
 * Executes when the Stop button is clicked
 */
function Stop(e)
{
	cf.StopFade();
}

/*
 * Executes when the Start button is clicked
 */
function Start(e)
{
	cf.StartFade();
}

/*
 * Executes when the Set button is clicked for the Display Time
 */
function SetDelay(e)
{
	var success = cf.SetDelay(document.getElementById("newdelay").value);
	if (success.length > 0)
	{
		alert (success);
	}
}

/*
 * Executes when the Set button is clicked for the Fade Time
 */
function SetFade(e)
{
	var success = cf.SetFade(document.getElementById("newfade").value);
	if (success.length > 0)
	{
		alert (success);
	}
}

/*
 * Executes when the Click-to-Start/Stop checkbox is clicked
 */
function ClickToStartStopClicked()
{
	StartStopOnClick(document.getElementById("clicktostartstop").checked);
}