//
// montage.js
//

var monBlock	= null;
var monImg		= null;
var monIdx		= 0;
var monContainer= null;
var monTimeout	= 2000;
var monEffect	= 1;
var monHref;
var monSrc;
var monCaption;

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 ) );
}

function montage( href, src, caption, width, animate )
{


	if( monBlock )
	{
		return;
		
	}

	monHref		= href;
	monSrc		= src;
	monCaption	= caption;

	// this is the case for browsers that don't support filters...
	var cycle	= Math.floor( Math.random() * href.length );

	// switch off animation if we're not on broadband
	 var m_montage	= ( typeof(animate) != "undefined" ? animate : hasBroadband() );
	 m_montage = true;
	 if( m_montage == false )
	 {
		 document.writeln( montagePicker( cycle ) );
		return;
	}
	
	// go build all of the nested DIVs out
	monIdx		= cycle;

	
	var nextImage	= (monIdx + 1) % src.length;

	document.writeln( "<div id=\"container\" style=\"width:" + width +"px; height=10px; \">" );
	
	for( i = 0; i < src.length; i++ )
	{
	
		// set up a placeholder
		document.write( "<div id=\"montage" + i + "\" style=\"display:none;\">" );
		
		if( i == nextImage )
		{
			// only prefetch the first image we will display
			document.write( montagePicker( i ) );
		}
		
		document.write( "</div>" );
	}

	document.writeln( "</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( "montage" + i );
		
		if( i == nextImage )
		{
			monImg[i] = document.getElementById( "monimg" + i );
		}
		else
		{
			monImg[i] = null;
		}
	}

	monContainer = document.getElementById("container");
	
	montageEffects();
}

function montageEffects()
{
	var nextImage	= (monIdx + 1) % monImg.length;
	// run the transition
	readIEVer = true;
	if( readIEVer == true && monEffect > 0 )
	{
			
		if( monEffect == 1 )
		{
			monContainer.style.filter = "blendTrans(duration=4)";
			monContainer.filters(0).apply();
 			montageSelect( nextImage );
			monContainer.filters(0).play();
		}
		else
		{
			monContainer.style.filter = "blendTrans(duration=8) revealTrans(duration=8,transition=7)";
			monContainer.filters(0).apply();
			monContainer.filters(1).apply();
 			montageSelect( nextImage );
			monContainer.filters(0).play();
			monContainer.filters(1).play();
		}
	}
	else
	{
		montageSelect( nextImage );
	}

	// asked to be called again a little later
	setTimeout( "montagePrep()", monTimeout - 1500 );
	setTimeout( "montageSwap()", monTimeout );
}

function montageSelect( nextImage )
{
	monBlock[monIdx].style.display = "none";
	monIdx = nextImage;
	monBlock[monIdx].style.display = "block";
}		

function montagePrep()
{
	// prefetch the next image if we don't already have it
	var nextImage	= (monIdx + 1) % monImg.length;

	if( !monImg[nextImage] )
	{
		monBlock[nextImage].innerHTML = montagePicker( nextImage );
		monImg[nextImage] = document.getElementById( "monimg" + nextImage );
	}
}

function montageSwap()
{
	if( monImg[monIdx].complete )
	{
		// move the image index along
		montageEffects();
	}
	else
	{
		// check again 3 seconds later
		setTimeout( "montageSwap()", 3000 );
	}
}

function montagePicker( cycle )
{
	var divHtml;

	if( monHref[cycle] != null ) 
	{
		divHtml = "<A href=\"" + monHref[cycle] + "\"><IMG src=\"" + monSrc[cycle] + "\" alt=\"" + monCaption[cycle] + "\" BORDER=\"0\" ID=\"monimg" + cycle + "\"></a>";
	}
	else
	{
		divHtml = "<IMG src=\"" + monSrc[cycle] + "\" ID=\"monimg" + cycle + "\">";
	}
	
	return divHtml;
}

function hasBroadband()
{
	if( readIEVer() < 5.0 )
	{
		return false;
	}
	
	
}

// -------------------------------------------------------------
// end of montage.js
// -------------------------------------------------------------
