// Script To Fade In/Out Some Images
//
// Released Under LGPL License However This Is Still (C)2003 All Rights Reserved
//
// Re-Use In Your Own Programs Is Permitted, Provided You Retain
// These Copyright Messages And LGPL Statement AND Adhere To
// The LGPL In Any Distribution Of This Code You Make
//
// Program Revision 1.6
// Last Amended On  2005/12/13 21:16:07
// Last Amended By  jim
//
// Program amendment history is at the end of this file
//
//
var timerHandle;		// Handle For The Timer Script
var timerRunning = false;
var timerDelay = 25;	// in ms
var maxImages = 0;		// No Images In Our List To Begin With


// Image State Array
var jarvieFaderImageID = new Array();
var jarvieFaderImageState = new Array();	// 0=hidden, 1=fade-in, 2=visible, 3=fade-out
var jarvieFaderImageTransparency = new Array();	// 0 - 100%
var jarvieFaderImageFadeInTargetTransparency = new Array();	// 0 - 100%
var jarvieFaderImageFadeOutTargetTransparency = new Array();	// 0 - 100%
var jarvieFaderImageStepValue = new Array();	// 0 - 100%
var jarvieFaderImageOnCompletion = new Array();	// Script, eval called on this
var jarvieFaderImageStateLocked = new Array();	// Used To Lock In Faded Out Or Faded In

function addToList(id) {
	// Add Noted Image To Our List Of Images We Can Fade In/Out
	// state 1 = starts visible, state 0 = starts invisible (hidden)

	if (arguments.length >= 2) {
		state = arguments[1];
	} else {
		state = 0;
	}

	if (arguments.length >= 3) {
		fadeintrans = arguments[2];
	} else {
		fadeintrans = 100;
	}

	if (arguments.length >= 4) {
		fadeouttrans = arguments[3];
	} else {
		fadeouttrans = 0;
	}

	if (arguments.length >= 5) {
		fadestep = arguments[4];
	} else {
		fadestep = 10;
	}

	if (arguments.length >= 6) {
		execoncompletion = arguments[5];
	} else {
		execoncompletion = "";
	}

	jarvieFaderImageID.push(id);
	maxImages = jarvieFaderImageID.length;

	if (state == 0) {
		// Hidden
		jarvieFaderImageState.push(0);
		jarvieFaderImageTransparency.push(0);
		if (is_ie5_5up) {
			// Set The Opacity Stylesheet Directly
			document.getElementById(id).style.filter = "alpha(opacity=" + fadeouttrans + ")";
		}
		if (is_ie4up) {
			if (document.getElementById(id).filters) {
				// MSIE Method
				document.getElementById(id).filters.alpha.opacity = fadeouttrans;
			}
		} else if ((is_gecko) || (is_nav6up)) {
			document.getElementById(id).style.MozOpacity = fadeouttrans / 100;
		}
		document.getElementById(id).style.visibility='hidden'
	} else {
		// Visible
		jarvieFaderImageState.push(2);
		jarvieFaderImageTransparency.push(fadeintrans);
		if (is_ie5_5up) {
			// Set The Opacity Stylesheet Directly
			document.getElementById(id).style.filter = "alpha(opacity=" + fadeintrans + ")";
		}

		if (is_ie4up) {
			if (document.getElementById(id).filters) {
				// MSIE Method
				document.getElementById(id).filters.alpha.opacity = fadeintrans;
			}
		} else if ((is_gecko) || (is_nav6up)) {
			document.getElementById(id).style.MozOpacity = fadeintrans / 100;
		}
		document.getElementById(id).style.visibility='visible'
	}
	jarvieFaderImageFadeInTargetTransparency.push(fadeintrans);
	jarvieFaderImageFadeOutTargetTransparency.push(fadeouttrans);
	jarvieFaderImageStepValue.push(fadestep);
	jarvieFaderImageOnCompletion.push(execoncompletion);
	jarvieFaderImageStateLocked.push(0);	// Not Locked, 1 = is Locked
}

function processStateChanges() {
	// See If Any Images Require To Fade In Or Fade Out
	var imageNo;
	var anythingChanged = false;
	var somethingChanged;

	timerRunning = false;

	for (imageNo = 0; imageNo < maxImages; imageNo++) {
		if (jarvieFaderImageStateLocked[imageNo]) {
			// This Item Is Locked, So It Can't Change
			somethingChanged = false;
		} else {
			somethingChanged = true;		// Unless We Find Otherwise
			// Check For Anything Needing To Change State...
			if (jarvieFaderImageState[imageNo] == 1) {
				// Fade In State
				if (jarvieFaderImageTransparency[imageNo] < jarvieFaderImageFadeInTargetTransparency[imageNo]) {
					// Still In Progress
					jarvieFaderImageTransparency[imageNo] += jarvieFaderImageStepValue[imageNo];
					if (is_ie4up) {
						if (document.getElementById(jarvieFaderImageID[imageNo]).filters) {
							// MSIE Method
							document.getElementById(jarvieFaderImageID[imageNo]).filters.alpha.opacity = jarvieFaderImageTransparency[imageNo];
						}
					} else if ((is_gecko) || (is_nav6up)) {
						document.getElementById(jarvieFaderImageID[imageNo]).style.MozOpacity = jarvieFaderImageTransparency[imageNo] / 100;
					}
				} else {
					// Target Met - We Are Now Officially Visible
					jarvieFaderImageState[imageNo] = 2;

					// Execute The Completion Script, If Any
					if (jarvieFaderImageOnCompletion[imageNo] != "") {
						task = jarvieFaderImageOnCompletion[imageNo]
						jarvieFaderImageOnCompletion[imageNo] = "";	// Event Done, Don't Re-Do It
						eval(task);
					}

				}
			} else if (jarvieFaderImageState[imageNo] == 3) {
				// Fade Out State
				if (jarvieFaderImageTransparency[imageNo] > jarvieFaderImageFadeOutTargetTransparency[imageNo]) {
					// Still In Progress
					jarvieFaderImageTransparency[imageNo] -= jarvieFaderImageStepValue[imageNo];

					if (is_ie4up) {
						if (document.getElementById(jarvieFaderImageID[imageNo]).filters) {
							// MSIE Method
							document.getElementById(jarvieFaderImageID[imageNo]).filters.alpha.opacity = jarvieFaderImageTransparency[imageNo];
						}
					} else if ((is_gecko) || (is_nav6up)) {
						document.getElementById(jarvieFaderImageID[imageNo]).style.MozOpacity = jarvieFaderImageTransparency[imageNo] / 100;
					}
				} else {
					// Target Met - We Are Now Officially Hidden
					jarvieFaderImageState[imageNo] = 0;
					if (jarvieFaderImageTransparency[imageNo] == 0) {
						// Only turn off if transparency is 0 (invisible)
						document.getElementById(jarvieFaderImageID[imageNo]).style.visibility = 'hidden';
					}
				}
			} else {
				// Nothing Changed This Time Round
				somethingChanged = false;
			}
		}
		anythingChanged |= somethingChanged;
	}

	if (anythingChanged) {
		tickleFaderTimer();
	}
}

function tickleFaderTimer() {
	if (!timerRunning) {
		timerHandle = setTimeout("processStateChanges()", timerDelay);
		timerRunning = true;
	}
}

function lockState(id) {
	var imageNo;
	var isFound = false;

	for (imageNo = 0; ((!isFound) && (imageNo < maxImages)); imageNo++)	{
		if ((jarvieFaderImageState[imageNo] != 1) && (jarvieFaderImageID[imageNo] == id)) {
			jarvieFaderImageStateLocked[imageNo] = 1;
			isFound = true;
		}
	}
}

function unlockState(id) {
	var imageNo;
	var isFound = false;

	for (imageNo = 0; ((!isFound) && (imageNo < maxImages)); imageNo++)	{
		if ((jarvieFaderImageState[imageNo] != 1) && (jarvieFaderImageID[imageNo] == id)) {
			jarvieFaderImageStateLocked[imageNo] = 0;
			isFound = true;
		}
	}
}

function elementState(id) {
	var imageNo;
	var isFound = false;

	for (imageNo = 0; ((!isFound) && (imageNo < maxImages)); imageNo++)	{
		if (jarvieFaderImageID[imageNo] == id) {
			return jarvieFaderImageState[imageNo];
		}
	}

	return 0;	// Invalid ID, So Always Hidden
}

function startFadeIn(id) {
	// We Need This To Start The Fade-In Process
	var imageNo;
	var endCommand;
	var isFound = false;

	if (arguments.length >= 2) {
		endCommand = arguments[1];
	} else {
		endCommand = "";
	}

	for (imageNo = 0; ((!isFound) && (imageNo < maxImages)); imageNo++)	{
		if ((jarvieFaderImageState[imageNo] != 1) && (jarvieFaderImageID[imageNo] == id)) {
			document.getElementById(jarvieFaderImageID[imageNo]).style.visibility = 'visible';
			jarvieFaderImageState[imageNo] = 1;
			isFound = true;

			if (endCommand != "") {
				jarvieFaderImageOnCompletion[imageNo] = endCommand;
			}
		}
	}

	tickleFaderTimer();
}

function startFadeOut(id) {
	// We Need This To Start The Fade-Out Process
	var imageNo;
	var endCommand;
	var isFound = false;

	if (arguments.length >= 2) {
		endCommand = arguments[1];
	} else {
		endCommand = "";
	}

	for (imageNo = 0; ((!isFound) && (imageNo < maxImages)); imageNo++)	{
		if ((jarvieFaderImageState[imageNo] != 3) && (jarvieFaderImageID[imageNo] == id)) {
			if (jarvieFaderImageState[imageNo] == 1) {
				// Still Fading In, Queue The Fade Out...
				jarvieFaderImageOnCompletion[imageNo] = "startFadeOut('" + id + "','" + endCommand + "')";
			} else {
				jarvieFaderImageState[imageNo] = 3;

				if (endCommand != "") {
					jarvieFaderImageOnCompletion[imageNo] = endCommand;
				}
			}
			isFound = true;
		}
	}

	tickleFaderTimer();
}


// fader.js,v
// Revision 1.6  2005/12/13 21:16:07  jim
//
// Updated fader system to be a bit more resillient.
//
// Revision 1.5  2003/12/18 21:30:08  jim
//
// Added prototype for push() method on array objects - not present in
// IE5, so we need to make our own (i.e. use an example someone else has
// already published !)
//
// Revision 1.4  2003/11/29 21:01:39  jim
//
// More MSIE shenannegins to try and get the alpha filter to work.
// (Plus a silly mistake fixed in the IE code when identifying the
// object we were working with).
//
//