//
// CThai.js
//

var CThBlock	= null;
var CThImg		= null;
var CThIdx		= 0;
var CThContainer= null;
var CThTimeout	= 7000;

function CThai( href, src, caption, width, animate )
{	
	// this is the case for browsers that don't support filters...
	var cycle	= Math.floor( Math.random() * href.length );
	//var cycle	= 0;
	
	// switch off animation if we're not on broadband
	var m_CThai	= ( typeof(animate) != "undefined" ? animate : hasBroadband() );

//	if( m_CThai == false || src.length <= 1 || !( isIE4 || isW3C ) )
//	{
//		CThaiPicker( href, src, caption, cycle );
//		return;
//	}	
	
	// go build all of the nested DIVs out
	CThIdx		= cycle;

	document.writeln( "<div id=\"container\" style=\"width:" + width +"px;height:149px\">" );

	for( i = 0; i < src.length; i++ )
	{
		// set up a placeholder
		document.writeln( "\t<div id=\"CThai" + i + "\" style=\"display:none\">" );
		
		CThaiPicker( href, src, caption, i );

		document.writeln( "\t</div>" );
	}

	document.writeln( "</div>" );

	// pull the images out
	CThBlock	= new Array( src.length );
	CThImg		= new Array( src.length );

	for( i = 0; i < src.length; i++ )
	{
		CThBlock[i]	= document.getElementById( "CThai" + i ).style;
		CThImg[i]	= document.getElementById( "CThimg" + i );
		
		if( CThImg[i] == null )
		{
			CThaiPicker( href, src, caption, i );
			return;
		}
	//	alert( "foo" );
	}

	CThContainer = document.getElementById("container");
	CThaiEffects();
}

function CThaiEffects()
{
	var nextImage	= (CThIdx + 1) % CThImg.length;
	
	// run the transition
	if( readIEVer() >= 4.0 )
	{
		CThContainer.style.filter = "blendTrans(duration=1.5) revealTrans(duration=1.0,transition=7)";
	
		CThContainer.filters(0).apply();
		CThContainer.filters(1).apply();
		
 		CThaiSelect( nextImage );
		
		CThContainer.filters(0).play();
		CThContainer.filters(1).play();
	}
	else
	{
		CThaiSelect( nextImage );
	}

	// asked to be called again a little later
	setTimeout( "CThaiSwap()", CThTimeout );
}

function CThaiSelect( nextImage )
{
	//alert( CThImg[CThIdx].style.visibility );
	CThBlock[CThIdx].display = "none";
	CThIdx = nextImage;
	CThBlock[CThIdx].display = "block";
}		

function CThaiSwap()
{
	if( CThImg[CThIdx].complete )
	{
		// move the image index along
		CThaiEffects();
	}
	else
	{
		// check again 3 seconds later
		setTimeout( "CThaiSwap()", 3000 );
	}
}

function CThaiPicker( href, src, caption, cycle )
{
	if( href[cycle] != null ) 
	{
		document.writeln( "\t\t<A HREF=\"" + href[cycle] + "\"><IMG SRC=\"" + src[cycle] + "\" alt=\"" + caption[cycle] + "\" BORDER=\"0\" ID=\"CThimg" + cycle + "\"></a>" );
	}
	else
	{
		document.writeln( "\t\t<IMG SRC=\"" + src[cycle] + "\" ID=\"CThimg" + cycle + "\">" );
	}
}

function hasBroadband()
{
	if( readIEVer() < 5.0 )
	{
		return false;
	}
	
	try
	{
		document.body.addBehavior ("#default#clientCaps");
	
		return ( typeof(document.body.connectionType) != "undefined" && document.body.connectionType == "lan" );
	}
	catch( e )
	{
		return false;
	}
}

// -------------------------------------------------------------
// end of CThai.js
// -------------------------------------------------------------