/*
 *	Add code to be executed when the script is loaded here.
 */

/** Add OnLoad event handlers here **/

FRtOnLoadDo( FRtImageChangerInit );


/*
 *	General support functions
 */

/*
 *	FRtOnLoadDo
 *
 *	Function to queue up a function that is to be executed once a page loads.
 */

function FRtOnLoadDo( onLoadNew )
{
	var onLoadCurrent;


	/** Access the current onLoad function **/

	onLoadCurrent = window.onload;


	/** Add the new function to the queue **/

	if( typeof onLoadCurrent == 'function' )
	{
		window.onload = function()
				{
					onLoadCurrent();
					onLoadNew();
				}
	}
	else
	{
		window.onload = onLoadNew;
	}

}


/*
 *	FRtElementGetByID( ID )
 *
 *	Cross-browser support for the getElementById() member function.
 */

function FRtElementGetByID( ID )
{
//	return document.getElementById( ID );

//alert( "FRtElementGetByID " + ID + " " + document.getElementById( ID ) );

    if( document.getElementById )
	{
        return document.getElementById( ID );
	}
    else if( document.all )
	{
		return document.all[ID];
	}
	else if( document.layers )
	{
		return document.layers[ID];
	}
	

	return null;
}


/*
 *	FRtElementsGetByTag( Tag )
 *
 *	Cross-browser support for the getElementsByTagName() member function.
 */

function FRtElementsGetByTag( Tag )
{
//	return document.getElementById( Tag );

//alert( "FRtElementsGetByTagName " + Tag + " " + document.getElementsByTagName( Tag ) );

    if( document.getElementsByTagName )
	{
        return document.getElementsByTagName( Tag );
	}
	

	return null;
}


/*
 *	FRtElementClassGet( Element )
 *
 *	Cross-browser support for getting the class of an element.
 */

function FRtElementClassGet( Element )
{

	if( document.all )
	{
		return Element.className;
	}

    if( document.getElementById )
	{
		return Element.getAttribute( "class" );
	}


	return Element.getAttribute( "class" );
}


/*
 *	FRtStyleRuleAccess
 *
 *	Function to access a specific CSS rule from a specific style sheet.
 */

function FRtStyleRuleAccess( Sheet, RuleSelector )
{
	var i;
	var theRule;
	var cssRules;


	theRule = null;
	if( document.styleSheets )
	{
		/** Style sheets present - find the sheet in question **/

		for( i = 0; i < document.styleSheets.length; i++ )
		{
			if( document.styleSheets[i].title == Sheet )
			{
				break;
			}
		}

		if( i < document.styleSheets.length )
		{
			/** Found the sheet - find the specified rule **/

			cssRules = null;
			Sheet = document.styleSheets[i];
			if( Sheet.cssRules )
			{
				cssRules = Sheet.cssRules;
			}
			else if( Sheet.rules )
			{
				cssRules = Sheet.rules;
			}

			if( cssRules )
			{
				for( i = 0; i < cssRules.length; i++ )
				{
					if( cssRules[i].selectorElement == RuleSelector )
					{
						theRule = cssRules[i];
						break;
					}
				}
			}
		}

	}


	return theRule;
}
	

/*
 *	FRtImageChanger
 *
 *	Function to check if a block with images to be faded has been defined, and, if so, to initiate fading through this list.
 */

FRtImages = new Array();
FRtImageCur = 0;

function FRtImageLoad( Timer )
{
	var		nImages;

	nImages = FRtImages.length;
	FRtImages[nImages] = this;
	FRtImages[nImages].Timer = Timer;
}

function FRtImageChangerInit()
{

	/** Any images? **/

	Images = FRtElementsGetByTag( "img" );
	if( Images != null )
	{
		/** Yes - build a list of images to be changed **/

		for( i = 0; i < Images.length; i++ )
		{
			if( ( FRtElementClassGet( Images[i] ) == "FRtImageChangeFirst" )
				|| ( FRtElementClassGet( Images[i] ) == "FRtImageChange" ) )
			{
				/** Found an image to fade - add this to the list **/

				FRtImages[FRtImages.length] = Images[i];
			}
		}


		if( FRtImages.length > 0 )
		{
			/** Set the opacity of the first image **/

			FRtImages[0].style.display = "block";
			FRtImages[0].Timer = 3000;
			FRtImageCur = 0;


			/** Set the opacity of the rest of the images **/

			for( i = 1; i < FRtImages.length; i++ )
			{
				FRtImages[i].style.display = "none";
				FRtImages[i].Timer = 500;
			}


			/** Start the fading process **/

			if( FRtImages.length > 1 )
			{
				setTimeout( FRtImageChanger, FRtImages[0].Timer );
			}
		}
	}

}

function FRtImageChanger()
{

	/** Establish the next image **/

	ImageNext = FRtImageCur + 1;
	if( ImageNext == FRtImages.length )
	{
		ImageNext = 0;
	}

	FRtImages[FRtImageCur].style.display = "none";
	FRtImages[ImageNext].style.display = "block";
	FRtImageCur = ImageNext;

	setTimeout( FRtImageChanger, FRtImages[FRtImageCur].Timer );

}



