var nImages = 258;

var nDivisions = 0;
var posShift = new Array();

// Partition images into buckets, and decide which bucket will be
// chosen from for each placeholder on the page.
function initImages(nDiv)
{
    // Make number of divisions global
    nDivisions = nDiv;

    // Initialize array of consecutive numbers, one for each
    // placeholder.
    for (var i = 0; i < nDivisions; i++)
	posShift[i] = i + 1;

    // Now, shuffle the array randomly.
    for (var i = 0; i < nDivisions; i++)
    {
	// Pick random index in array
	var j = Math.floor(Math.random() * nDivisions);

	// Swap with number at that index
	var tmp = posShift[i];
	posShift[i] = posShift[j];
	posShift[j] = tmp;
    }
}

// Insert random image from a partition into the current
// placeholder, with possibly a "float:right" attribute added
// so text will wrap around it.
function insertRandomImageFloat(whichDivision, floatRight)
{
    // Convert given placeholder number into random bucket using
    // the posShift array we've already created.
    whichDivision = posShift[whichDivision - 1];    

    // Choose random image number from that bucket
    var divisionSize = nImages / nDivisions;
    var imgLow = Math.floor(divisionSize * (whichDivision - 1))
	+ 1;
    var imgHigh = Math.floor(divisionSize * whichDivision);
    var randImage = imgLow +
	Math.floor(Math.random() * (imgHigh - imgLow));

    // Pad that number with zeros and convert to filename
    var imgFileName = "";
    for (var i = 0; i < (3 - randImage.toString().length); i++)
	imgFileName += "0";
    imgFileName += randImage + ".jpg";

    // Add "float:right" attribute if floatRight set to true
    var floatRightStr = "";
    if (floatRight)
	floatRightStr = ' style="float:right" hspace="10" vspace="5"';

    // Create image tag and insert image at current placeholder
    var imageStr = '<img src="images/homepage/' +
	imgFileName + '" width="250" height="188"' + 
	floatRightStr + '>';
    document.write(imageStr);
}

// Calls insertRandomImageFloat(), forcing the image not to have
// a "float:right" attribute.
function insertRandomImage(whichDivision)
{
    insertRandomImageFloat(whichDivision, false);
}
