//=========================================
// Start - Functionality used in Photo Gallery - AJAX

//Use a hash table to hold already retrieved HTML based on ID
var photoGalleryHashTable = new Object();

function photoGalleryFirstImage()
{
	maxIndex = Number(IDs.substring(IDs.indexOf('%')+1));
	//alert('ID before: ' + IDs);
	IDs = IDs.substring(0,IDs.indexOf('%')) + '|';
	//alert('ID after: ' + IDs);
	//alert('maxIndex: ' + maxIndex);
	//alert('curIndex: ' + curIndex);
	//alert('compID: ' + photoGalleryGetID());
	photoGalleryGoForward();
}

function photoGalleryGoBack()
{
	curIndex = curIndex - 1;
	if (curIndex == 0) {
		curIndex = maxIndex;
	}
	photoGalleryRefreshImage(photoGalleryGetID());
	photoGalleryRefreshNav();
}

function photoGalleryGoForward()
{
	curIndex = curIndex + 1;
	if (curIndex > maxIndex) {
		curIndex = 1;
	}
	photoGalleryRefreshImage(photoGalleryGetID());
	photoGalleryRefreshNav();
}

function photoGalleryRefreshNav()
{
	populateItems(curIndex + ' of ' + maxIndex + ' photos', 'photo_gallery_nav');
}

function photoGalleryGetID()
{
	var i = 1;
	var ID = '';
	var newIDs = IDs;
	while (i < curIndex+1)
	{
		ID = newIDs.substring(0,newIDs.indexOf('|'));
		newIDs = newIDs.substring(newIDs.indexOf('|')+1);
		i = i + 1;
	}
	return Number(ID.substring(ID.indexOf('-')+1));
}

function photoGalleryRefreshImage(imageID)
{
	//Ajax request
	xmlHTTPRetrievePostImage("/PhotoGallery", "gallery_photo", imageID);
}

function xmlHTTPRetrievePostImage(strURL, divName, imageID)
{
	var entry = getPhotGalleryEntry(imageID);
	//check if the hashtable has an entry for this ID
	if (entry)
	{
		populateItems(entry, divName);
	}
	else
	{
		var xmlHttpReq = false;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
			xmlHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlHttpReq.open('POST', strURL, true);
		xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			if (xmlHttpReq.readyState == 4) {
				doPhotoGalleryPopulateItems(xmlHttpReq.responseText, divName, imageID);
			}
		}
	
		//Compose and send the request string
		xmlHttpReq.send("imageID=" + imageID);
	}
}

//Catch the photo gallery populate items for storing in the hash table
function doPhotoGalleryPopulateItems(text, divName, imgID)
{
	putPhotoGalleryEntry(imgID, text);
	populateItems(text, divName);
}

//Add a photo gallery entry to our hash table
function putPhotoGalleryEntry(key, value)
{
	photoGalleryHashTable[key] = value;
}

//Return a photo gallery entry, returns undefined if no entry
function getPhotGalleryEntry(key)
{
	return photoGalleryHashTable[key];
}

// End - Functionality used in Photo Gallery - AJAX
//=========================================