var LSBMontageBlock	= null;
var LSBMontageAltText;
var LSBMontageContainer= null;
var LSBMontageEffect	= 2;
var LSBMontageIdx		= 0;
var LSBMontageImg		= null;
var LSBMontageImgSrc;
var LSBMontageTimeout	= 7000;

function montage(ImgSrc,AltText,Effect,animate){
	if(LSBMontageBlock){
		return;
	}

	// Copy the parameters to the global variables for ease of use later on.
	LSBMontageImgSrc		= ImgSrc;
	LSBMontageAltText	= AltText;
	LSBMontageEffect = Effect;

	// For browsers that don't support filters or are not on a broadband connection, we'll need to pick just one image to display.
	// This will also serve as the first image to be displayed in an animation, so the visitor will likely see a different image to start with each time he hits the page.
	var RndImg	= Math.floor(Math.random()*ImgSrc.length);

	// Obey the animation directive if it's been given. Otherwise, animate if we have a broadband connection.
	var OkayToAnimate=(typeof(animate)!="undefined" ? animate : hasBroadband());

	// Switch off animation based on the above determination and if we have enough images to animate in a non DHTML environment.
	// In this case, just show the single image and we're done.
	if(!OkayToAnimate || ImgSrc.length <= 1 || !isDHTML){
		//alert("Switch off animation\nOkayToAnimate = " + OkayToAnimate + "\nImgSrc.length = " + ImgSrc.length + "\nisDHTML = " + isDHTML + "\nhasBroadband() = " + hasBroadband());
		document.writeln(ImgTagForImage(RndImg));

	// If we're okay to animate, then build all the DIVs inside the container DIV. Randomly pick an image to start with.
	}else{
		LSBMontageIdx = RndImg;
		var nextImage	= (LSBMontageIdx+1) % ImgSrc.length;
		document.writeln("<div id='container' style='width:550px;height:178px'>");

		// Set up a placeholder DIV for each image.
		for(i=0;i<ImgSrc.length;i++){
			document.write("<div id='montage"+i+"' style='display:none'>");

			// Prefetch the first image to be displayed.
			if(i==nextImage){
				document.write(ImgTagForImage(i));
			}
			document.write("</div>");
		}
		document.writeln("</div>");

		// Snag the images.
		LSBMontageBlock	= new Array(ImgSrc.length);
		LSBMontageImg		= new Array(ImgSrc.length);

		for(i = 0; i < ImgSrc.length; i++){
			LSBMontageBlock[i]	= document.getElementById("montage"+i);

			if(i == nextImage){
				LSBMontageImg[i] = document.getElementById("monimg"+i);
			}else{
				LSBMontageImg[i] = null;
			}
		}

		LSBMontageContainer = document.getElementById("container");

		montageEffects();
	}
}

function montageEffects(){
	var nextImage	= (LSBMontageIdx+1) % LSBMontageImg.length;

	// run the transition
	if(readIEVer() >= 4.0 && LSBMontageEffect > 0){
		try{
			if(LSBMontageEffect == 1){
				LSBMontageContainer.style.filter = "blendTrans(duration=0.6)";
				LSBMontageContainer.filters(0).apply();
 				montageSelect(nextImage);
				LSBMontageContainer.filters(0).play();
			}else{
				LSBMontageContainer.style.filter = "blendTrans(duration=1.5) revealTrans(duration=1.0,transition=7)";
				LSBMontageContainer.filters(0).apply();
				LSBMontageContainer.filters(1).apply();
 				montageSelect(nextImage);
				LSBMontageContainer.filters(0).play();
				LSBMontageContainer.filters(1).play();
			}
		}catch(e){ montageSelect(nextImage);}
	}else{
		montageSelect(nextImage);
	}

	// asked to be called again a little later
	setTimeout("montagePrep()",LSBMontageTimeout - 1500);
	setTimeout("montageSwap()",LSBMontageTimeout);
}

function montageSelect(nextImage){
	LSBMontageBlock[LSBMontageIdx].style.display = "none";
	LSBMontageIdx = nextImage;
	LSBMontageBlock[LSBMontageIdx].style.display = "block";
}

function montagePrep(){
	// prefetch the next image if we don't already have it
	var nextImage	= (LSBMontageIdx+1) % LSBMontageImg.length;

	if(!LSBMontageImg[nextImage]){
		LSBMontageBlock[nextImage].innerHTML = ImgTagForImage(nextImage);
		LSBMontageImg[nextImage] = document.getElementById("monimg"+nextImage);
	}
}

function montageSwap(){
	if(LSBMontageImg[LSBMontageIdx].complete){
		// move the image index along
		montageEffects();

	// Check again 3 seconds later.
	}else{
		setTimeout("montageSwap()",3000);
	}
}

function ImgTagForImage(ImgNum){
	return "<img src='"+LSBMontageImgSrc[ImgNum]+"' alt=\""+LSBMontageAltText[ImgNum]+"\" width=550 height=178 border=0 id='monimg"+ImgNum+"'>";
}

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;
	}
}