/* -- Detailed Script */

var sCurrentPhoto;
var totalPhotos, totalElements, photoElements;

// These offsets correspond to the positions of elements in
// each line on the photo "database"
var oTitle = 0;
var oImage = 1;
var oNumberOfElements = 3; // one more than number of element offsets to
						// account for the 'CR' after each photo
var maxTNs = 15;

function showPicFromThumbNails (index) {
	if (index < totalPhotos)
	{
		return showPic (index * oNumberOfElements);
	}
	return true;
}

function showPic (index) {

	// update placeholders with new image and text
	//alert(photoElements.childNodes[index + oTitle].value);
	document.getElementById('title').childNodes[0].nodeValue = photoElements.childNodes[index + oTitle].value;
	var image = document.getElementById('image');
	image.src = photoElements.childNodes[index + oImage].value + ".jpg";
	
	// get rid of focus lines around text or back/next buttons
	window.focus();

	return false;
}

function loadVars () 
{
	photoElements = document.getElementById('photos');
	if (!photoElements)
	{
		// TODO: Warn about obsolete browser
		return false;
	}
	totalElements = photoElements.childNodes.length;
	//alert("totalTNElements: " + totalTNElements);

	// Find totalPhotos
	// Do 'totalPhotos = (totalElements / oNumberOfElements) + 1' using integer arithmetic
	var temp = totalElements;
	var i;
	for (i = 0; temp > 0; temp -= oNumberOfElements, i++);
	totalPhotos = i;
	//alert("totalPhotos: " + totalPhotos);

	// update placeholders with thumbnail images
	var offset = 0;
	for (i = 0; i < maxTNs; i++)
	{
		var tnImage = document.getElementById('tn' + i);
		//alert("offset: " + offset + " totalTNElements: " + totalTNElements);
		if (i < totalPhotos)
		{
			var image = photoElements.childNodes[offset + oImage].value;
			tnImage.src = image + "_TN.jpg";
			offset += oNumberOfElements;
		}
		else
		{
			tnImage.src = "photos/blank_TN.jpg";
		}
	}

	//calculate a random index
    var index = Math.floor(Math.random() * totalPhotos);
	index = index * oNumberOfElements;
	
	return showPic(index);
}

