	var tcount = 0;
	var tw = null;	/* thumbnail width arrays */
	var ti = null;	/* thumbnail image elements */
	var tc = null;	/* thumbnail caption elements */

	var focus = 0;

	function setup_thumbs( )
	{
		tcount = t.length;
		//tw = new Array(tcount);
		ti = new Array(tcount);
		tc = new Array(tcount);
	
		for (var i = 0; i < tcount; i++)
			{
			ti[i] = document.getElementById("t"+i);
			tc[i] = document.getElementById("c"+i);
			}

		setupSizes();

		// redraw thumbnails, but don't set the focus
		//focus = 0;
		scatter();
	}

	function setupSizes( )
	{
		tw = new Array(tcount);
		
		var w = null;
		if ( window.innerWidth )
			w = window.innerWidth-24;
		else if ( document.documentElement && typeof(document.documentElement.clientWidth)=='number' && document.documentElement.clientWidth > 0 )
			w = document.documentElement.clientWidth-24;
		else if ( document.body && document.body.clientWidth )
			w = document.body.clientWidth-24;
		else if ( document.width )
			w = document.width;
		
		if (w)
			FIT_WIDTH = Math.max(w-8,(30*tcount));
		else
			FIT_WIDTH = 540; // Math.min(540,40*tcount);
	}
	
	function scatter( )
	{
		var l = LEFT;
		var w = 0;
		if ( !tw[focus] )
			tw[focus] = calcWidths(focus);
		
		for (var i = 0; i < tcount; i++)
			{
			w = tw[focus][i];
			ti[i].style.width = w+"px";
			ti[i].style.left = l+"px";
			l += (w+GAP);
			}
	}
	
	function setFocus(f)
	{
		if ( f != focus )
			{
			tc[focus].style.visibility = "hidden";

			focus = f;
			scatter();
			}

		// Moved this statement out of the conditional.
		// Safari seems to repeatedly send tout()/tover() pairs when dragging around.
		// The result is that tout() hides the caption, but the second time tover() gets
		//	called it leave it invisible because f==focus and setFocus() assumes it's
		//	already visible.
		tc[f].style.visibility = "visible";
	}
	
	var dfocus = 0;

	function tclick(f)
	{
		if ( f != dfocus )
			{
			document.getElementById("d"+dfocus).style.display = "none";
			document.getElementById("d"+f).style.display = "block";
			document.getElementById("the_img").src = iURL(f); //t[f].iURL;
			document.getElementById("detail").style.width = t[f].iw/*+(DETAIL_PADDING*2)*/+"px";
			dfocus = f;

			setFocus(f);
			}
		
		return false;
	}
	
	function tover(f)
	{
		//if (window.console)
		//	window.console.log("tover("+f+")");
		setFocus(f);
		return false;
	}
	
	function tout(f)
	{
		//if (window.console)
		//	window.console.log("tout("+f+"), focus="+focus);
		tc[focus].style.visibility = "hidden";
		return false;
	}

	function calcWidths( n )
	{
		var fit = FIT_WIDTH-((tcount-1)*GAP);
		var widths = new Array(tcount);
		var remaining = 0;
		var w;
		
		for (var i=0; i < tcount; i++ )
		{
			w = t[i].tw;
			remaining += w;
			widths[i] = w;
		}
		
		if ( n<0 || n>=tcount )
			n = 0;
		
		if ( remaining > fit )
		{
			var total_width = 0;
			var scale = 1;
			var thumb = null;
			var scaled_width;
			var j;

			for ( var k=0; (n-k) >= 0 || (n+k) < tcount; k++ )
			{
				j = n+k;
				if ( j < tcount )
				{
					thumb = t[j];
					scaled_width = Math.round(thumb.tw*scale);
					widths[j] = scaled_width;
					total_width += scaled_width;
					remaining -= thumb.tw;
				}
				j = n-k;
				if ( j >= 0 && k != 0 )
				{
					thumb = t[j];
					scaled_width = Math.round(thumb.tw*scale);
					widths[j] = scaled_width;
					total_width += scaled_width;
					remaining -= thumb.tw;
				}
				
				scale *= SCALE;
				
				if ( remaining*scale < (fit-total_width) )
				{
					scale = (fit-total_width)/remaining;
				}
			}
		}
		
		return widths;
	}
	
