var currentPage = 0;

function loadImageSetForward() {
	currentPage++;
	loadImageSet();
	return false;
}
function loadImageSetBackward() {
	currentPage--;
	loadImageSet();
	return false;
}
function loadImageSet() {
	if (currentPage == 0) {
		document.getElementById('previousButton').style.display = "none";
		if (document.getElementById('previousButton2') != undefined) {
			document.getElementById('previousButton2').style.display = "none";
		}
	}
	else {
		document.getElementById('previousButton').style.display = "block";
		if (document.getElementById('previousButton2') != undefined) {
			document.getElementById('previousButton2').style.display = "block";
		}
	}
	if (currentPage >= Math.floor((imageFileNames.length-1)/pageInfo.slotCount)) {
		document.getElementById('nextButton').style.display = "none";
		if (document.getElementById('nextButton2') != undefined) {
			document.getElementById('nextButton2').style.display = "none";
		}
	}
	else {
		document.getElementById('nextButton').style.display = "block";
		if (document.getElementById('nextButton2') != undefined) {
			document.getElementById('nextButton2').style.display = "block";
		}
	}

	// Hide all of the images first
	for(var slotNum=0; (slotNum < pageInfo.slotCount); slotNum++) {
		hideImage(slotNum);
	}

	// Now load any images that are available.
	var currentIndex = (currentPage * pageInfo.slotCount);
	for(var slotNum = 0; slotNum < pageInfo.slotCount*2; slotNum++) {
		var slot = $("slot"+slotNum);
		var slotLink = $("slot"+slotNum+"_link");
		if (currentIndex < imageFileNames.length) {
			// Load the image into our in-memory array object (if it doesn't already exist)
			if (imageObjs[currentIndex] == null) {
				imageObjs[currentIndex] = document.createElement('img');
				imageObjs[currentIndex].src = pageInfo.pathToImages+"thumbs/"+imageFileNames[currentIndex]+".jpg";
			}
			
			// If the slot is on the current page
			if (slotNum < pageInfo.slotCount) {
				if (pageInfo.type == 'full') {
					// Redirect the image link to open lightbox
					slotLink.setAttribute('href', pageInfo.pathToImages+imageFileNames[currentIndex]+'.jpg');
					slotLink.setAttribute('rel', 'lightbox[nate]');
					prepareSlotImage(slot, slotLink, imageObjs[currentIndex], true);
				}
				else if (pageInfo.type == 'half') {
					slotLink.href = 'javascript: void(0)';
					slotLink.index = currentIndex;
					slotLink.onclick = function() { switchMainImage(this); };
					prepareSlotImage(slot, slotLink, imageObjs[currentIndex], false);
				}
				setTimeout("loadImage("+slotNum+", imageFileNames["+currentIndex+"])", 100+(slotNum*100), "javascript");
			}
		}
		// Handle empty slots on the current page
		else if (slotNum < pageInfo.slotCount) {
			var blankImage = document.createElement("img");
			blankImage.src = "/images/thumbbox_blank.gif"
			slotLink.href = 'javascript: void(0)';
			prepareSlotImage(slot, slotLink, blankImage);
			setTimeout("loadImage("+slotNum+", imageFileNames["+currentIndex+"])", 100+(slotNum*100), "javascript");
		}
		currentIndex++;
	}
}
function prepareSlotImage(slot, slotLink, imageObj, prepareLightbox) {
	slot.style.display = "none";
	while (slotLink.firstChild) { slotLink.removeChild(slotLink.firstChild); };
	imageObj.style.display = "none";
	slotLink.appendChild(imageObj);
	disableRightClicks(imageObj);
}
function hideImage(slotNum) {
	var slot = document.getElementById("slot"+slotNum);
	Effect.Fade(slot, {duration: 0.1});
	return false;
}
function loadImage(slotNum, imageName) {
	var slot = document.getElementById("slot"+slotNum);
	slot.firstChild.firstChild.style.display = "block";
	Effect.Appear(slot, {duration: 0.3});
	return false;
}
function switchMainImage(elem) {
	var imageSrc = pageInfo.pathToImages+imageFileNames[elem.index]+".jpg";
	var imageTitle = names[elem.index];
	var mainImg = document.getElementById('mainImage');
	var mainImageTitleElem = document.getElementById('mainImageTitle');

	Effect.Fade(mainImg, {duration: 0.3});
	for (var i = 0; i < mainImageTitleElem.childNodes.length; i++) {
		Effect.Fade(mainImageTitleElem.childNodes[i], {duration: 0.3});
	}

	document.getElementById('loadingImage').style.display = "block";
	document.getElementById('loadingImage2').style.display = "block";

	mainImg.onload = function() { this.style.border = "1px solid #513502"; Effect.Appear(this, {duration: 0.5}); document.getElementById('loadingImage').style.display = "none"; document.getElementById('loadingImage2').style.display = "none"; };
	disableRightClicks(mainImg);

	setTimeout("loadMainImage('"+imageSrc+"', '"+imageTitle+"')", 400);
	window.status='';
	return false;
}
function loadMainImage(imageSrc, imageTitle) {
	var mainImg = document.getElementById('mainImage');
	var mainImageTitleElem = document.getElementById('mainImageTitle');

	mainImg.src = imageSrc;
	if ((typeof imageTitle == 'string') && (imageTitle != 'undefined')) {
		while (mainImageTitleElem.firstChild) { mainImageTitleElem.removeChild(mainImageTitleElem.firstChild); };
		textArray = unescape(imageTitle).split(" ");
		for (var i = 0; i < textArray.length; i++) {
			var text = textArray[i].replace(/^\s*/, '').replace(/\s*$/, '');
			var newImage = document.createElement('img');
			newImage.alt = text;
			newImage.title = text;
			newImage.style.display = "none";
			if (text == '&') {
				newImage.onload = function() { Effect.Appear(this, {duration: 1.5}); };
			}
			else {
				newImage.onload = function() { Effect.Appear(this, {duration: 1.0}); };
			}
			newImage.src = 'images/text/?style=imageTitle&text='+escape(text);
			mainImageTitleElem.appendChild(newImage);
			mainImageTitleElem.appendChild(document.createElement('br'));
		}
	}
	return false;
}

