
// <meta http-equiv="content-type" content="text/html; charset=utf-8">

if ( typeof( Array.prototype.push ) == "undefined" ) {
	Array.prototype.push2 = function( el ) { this[this.length] = el; }
}

var kThumbExt = "_thumb";

function thumbnailName( pictName )
{
	var parts = pictName.split(".");
	return parts[0] + kThumbExt + "." + parts[1];
}

// generateGallery
//
//   pictureList - array of objects, each with 'src' and 'caption' property
//   urlBase - url prepended onto all url's
//   pictWidthMax - max width of the *thumbnail* image!!
//   pictsPerRow - number of picts in a single row
//
function generateGallery( pictureList, urlBase, pictWidthMax, pictsPerRow )
{
	var outStr = new Array();
	
	var tablewidth = pictWidthMax * pictsPerRow;
	outStr.push( "<table class='picturetable' cellspacing='0' cellpadding='0' style='width: " + tablewidth + "px;' >" );
	
	var pictCount = 0;
	var rowIndex = 0;
	while ( pictCount < pictureList.length ) {
		
		// begin a row if needed
		if ( rowIndex == 0 ) {
			outStr.push( "<tr>" );
		}
		
		// generate picture
		var pictItem = pictureList[pictCount];
		
		if ( pictsPerRow == 2 && ( pictCount + 1 ) == pictureList.length && rowIndex == 0 ) {
			outStr.push( "<td colspan='2'>" );
		} else {
			outStr.push( "<td>" );
		}
		outStr.push( "<a href='" + urlBase + pictItem.src + "' target='_blank'>" );
		outStr.push( "<img src='" + urlBase + thumbnailName( pictItem.src ) + "' title='" + pictItem.caption + "' />" );
		outStr.push( "</a><br />" );
		outStr.push( "<span class='caption'>" + pictItem.caption + "</span>" );
		outStr.push( "</td>" );
		
		// next row
		pictCount++;
		rowIndex++;
		if ( rowIndex == pictsPerRow ) {
			outStr.push( "</tr>" );
			rowIndex = 0;
		}
	}

	if ( rowIndex != 0 ) {
		outStr.push( "</tr>" );			
	}
	
	outStr.push( "</table>" );
	
	return outStr.join("");
}

