//
// transition.js
//

var monBlock	= null;
var monImg		= null;
var monIdx		= 0;
var monContainer= null;
var monTimeout	= 8000;
var isCSS 			= false;
var isW3C 			= false;
var isIE4 			= false;
var isNN4 			= false;
var isIE6 			= false;
var isGecko 		= false;
var isOpera 		= false;
var isDHTML 		= false;
var suppressMenus	= false;
var legacyMode		= false;
var timerID			= null;
var subtimerID		= null;


function doTransitioning(strTransitions)
{
	
	monTimeout = 8000;
	var urls = new Array();
	var imgs = new Array();
	var caps = new Array();

	//Parse the argument string, a sample string will be like:
	//http://www.1.com,images/transition1.jpg,1 alt$http://www.2.com,images/transition2.jpg,2 alt$http://www.3.com,images/transition3.jpg,3 alt$http://www.4.com,images/transition4.jpg,4 alt
	//i.e. the syntax is
	//url1,image1,alt1$url2,image2,alt2$url3,image3,alt3$ .....
	arrTransitions = strTransitions.split("$");
	for (i=0; i< arrTransitions.length; i++)	
	{
		strTransition111 = new String(arrTransitions[i].toString());	
		arrTransitionElements = strTransition111.split(",");		

		urls[i] = arrTransitionElements[0]; 
		imgs[i] = arrTransitionElements[1];		
		caps[i] = arrTransitionElements[2];
	}
	transition( urls, imgs, caps, 483 );		
}

function doBrowserSettings()
{
	if(document && document.images)
	{
		isCSS		= (document.body && document.body.style) ? true : false;
		isW3C		= (isCSS && document.getElementById) ? true : false;
		isIE4		= (isCSS && document.all && readIEVer() >= 4.0) ? true : false;
		isNN4		= (document.layers) ? true : false;
		isGecko		= (isCSS && navigator && navigator.product && navigator.product == "Gecko");
		isOpera		= (isCSS && navigator.userAgent.indexOf( "Opera") != -1 );
		isIE6CSS	= (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
		isIE6		= ( isIE6CSS && readIEVer() >= 6.0 );
		isDHTML		= isCSS && ( isIE4 || isGecko || isOpera );
		
		if( suppressMenus || ( isOpera && readOperaVer() < 7 ) || ( isGecko && navigator.productSub <= 20011022 ) || ( isGecko && navigator.productSub == 20030107 ) )
		{
			isDHTML	= false;
		}				
	}			
}

function transition( href, src, caption, width, animate )
{
	// this is the case for browsers that don't support filters...
	var cycle	= Math.floor( Math.random() * href.length );
	
//	alert(cycle);
	//var cycle = 3;

	// switch off animation if we're not on broadband
	var m_transition = ( typeof(animate) != "undefined" ? animate : hasBroadband() );
	
	if( m_transition == false || src.length <= 1 || !( isIE4 || isW3C ) )
	{
		transitionPicker( href, src, caption, cycle );
		return;
	}
	
	// go build all of the nested DIVs out
	monIdx		= cycle;

	document.write( "<div id=\"container\" style=\"width:" + width +"px;height:149px\">" );

	for( i = 0; i < src.length; i++ )
	{
		// set up a placeholder
		document.write( "\t<div id=\"transition" + i + "\" style=\"display:none\">" );
		
		transitionPicker( href, src, caption, i );

		document.write( "\t</div>" );
	}

	document.write( "</div>" );

	// pull the images out
	monBlock	= new Array( src.length );
	monImg		= new Array( src.length );

	for( i = 0; i < src.length; i++ )
	{
		monBlock[i]	= document.getElementById( "transition" + i ).style;
		monImg[i]	= document.getElementById( "monimg" + i );
		
		if( monImg[i] == null )
		{
			transitionPicker( href, src, caption, i );
			return;
		}
	//	alert( "foo" );
	}

	monContainer = document.getElementById("container");
	
	transitionEffects();
}

function transitionEffects()
{
	var nextImage	= (monIdx + 1) % monImg.length;
	
	// run the transition
	if( readIEVer() >= 4.0 )
	{
		monContainer.style.filter = "blendTrans(duration=1.5) revealTrans(duration=1.0,transition=7)";
	
		monContainer.filters(0).apply();
		monContainer.filters(1).apply();
		
 		transitionSelect( nextImage );
		
		monContainer.filters(0).play();
		monContainer.filters(1).play();
	}
	else
	{
		transitionSelect( nextImage );
	}

	// asked to be called again a little later
	setTimeout( "transitionSwap()", monTimeout );
}

function transitionSelect( nextImage )
{
	//alert( monImg[monIdx].style.visibility );
	monBlock[monIdx].display = "none";
	monIdx = nextImage;
	monBlock[monIdx].display = "block";
}		

function transitionSwap()
{
	if( monImg[monIdx].complete )
	{
		// move the image index along
		transitionEffects();
	}
	else
	{
		// check again 3 seconds later
		setTimeout( "transitionSwap()", 3000 );
	}
}

function transitionPicker( href, src, caption, cycle )
{
	if( href[cycle] != null ) 
	{
		document.write( "\t\t<A HREF=\"" + href[cycle] + "\"><IMG SRC=\"" + src[cycle] + "\" alt=\"" + caption[cycle] + "\" BORDER=\"0\" ID=\"monimg" + cycle + "\"></a>" );
	}
	else
	{
		document.write( "\t\t<IMG SRC=\"" + src[cycle] + "\" ID=\"monimg" + 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;
	}
}

function readIEVer()
{
	var agent	= navigator.userAgent;
	var offset	= agent.indexOf( "MSIE" );
	if( offset < 0 )
	{
		return 0;
	}
	return parseFloat( agent.substring( offset + 5, agent.indexOf( ";", offset ) ) );
}

function readOperaVer()
{
	var agent	= navigator.userAgent;
	var offset	= agent.indexOf( "Opera" );
	if( offset < 0 )
	{
		return 0;
	}
	return parseFloat( agent.substring( offset + 6 ) );
}

