var xmlThumbHttp;
var currentStartIndex;
var currentLength;

function initializeSlider(iniLength, iniSearchKey){
	currentStartIndex = 0;
	currentLength = iniLength;
	getFourThumbs(currentStartIndex, currentLength, iniSearchKey);
	return;
}

function slideToStart(slideStartLength, slideStartSearchKey){
	currentStartIndex = 0;
	getFourThumbs(currentStartIndex, slideStartLength, slideStartSearchKey);
	return;
}

function slideRightByOne(right1Length, right1SearchKey){
	currentStartIndex++;
	currentLength = right1Length;
	getFourThumbs(currentStartIndex, right1Length, right1SearchKey);
	return;
}

function slideLeftByOne(left1Length, left1SearchKey){
	if(currentStartIndex > 0){
		currentStartIndex--;
		currentLength = left1Length;
		getFourThumbs(currentStartIndex, left1Length, left1SearchKey);
	}
	return;
}

function slideRightByLength(rLength, rLengthSearchKey){
	currentStartIndex += rLength;
	currentLength = rLength;
	getFourThumbs(currentStartIndex, rLength, rLengthSearchKey);
	return;
}

function slideLeftByLength(lLength, lLengthSearchKey){
	if(currentStartIndex > 0){
		currentStartIndex -= lLength;
		currentLength = lLength;
		getFourThumbs(currentStartIndex, lLength, lLengthSearchKey);
	}
	return;
}

function slideToEnd(endLength, endSearchKey){
	currentLength = endLength;
	// The gallery_fillthumbs.php script automatically corrects excessive indexes.
	// In this case, an outrageous number like 10000 will be adjusted to list max minus endLength.
	// 10000 will work as long as the list doesn't actually contain 10000 or more items.
	getFourThumbs(10000, endLength, endSearchKey);
	return;
}

function getFourThumbs(startingAt, returnLength, theSearchKey){
	xmlThumbHttp = GetXmlHttpThumbObject();
	if(xmlThumbHttp == null){
 		alert("Browser does not support HTTP Request");
 		return;
 	} 
	
	var url = "gallery/gallery_fillthumbs.php";
	url = url + "?q=" + startingAt + "|" + returnLength + "|" + theSearchKey;
	url = url + "&sid=" + Math.random();
	xmlThumbHttp.onreadystatechange = stateHasChanged ;
	xmlThumbHttp.open("GET", url, true);
	xmlThumbHttp.send(null);

	return;
}

function showIMG(str){ 
	xmlThumbHttp = GetXmlHttpThumbObject();
	if(xmlThumbHttp == null){
		alert("Browser does not support HTTP Request");
 		return;
	}
	var url = "gallery/gallery_getimg.php";
	url = url + "?q=" + str;
	url = url + "&sid=" + Math.random();
	xmlThumbHttp.onreadystatechange = stateHasChanged;
	xmlThumbHttp.open("GET", url, true);
	xmlThumbHttp.send(null);
	return;
}

function stateHasChanged() { 
	if(xmlThumbHttp.readyState == 4 || xmlThumbHttp.readyState == "complete"){ 
		if(xmlThumbHttp.responseText.substring(0, 15) === "<selectedImage>"){
			document.getElementById("txtHint").innerHTML = xmlThumbHttp.responseText.slice(15);
		}else{
			var responseArray = xmlThumbHttp.responseText.split("|");
			document.getElementById("thumb_container").innerHTML = responseArray[0];
			currentStartIndex = Number(responseArray[1]) - 1;
			document.getElementById("thumb_status").innerHTML = "<p>"+(currentStartIndex+1)+" - "+(currentStartIndex+currentLength)+" of "+(responseArray[2])+"</p>";
		}
	}
	return; 
}

function GetXmlHttpThumbObject(){
	var xmlThumbHttp = null;

	try{
		// Firefox, Opera 8.0+, Safari
		xmlThumbHttp = new XMLHttpRequest();
	}catch(E){
		// Internet Explorer
		try{
			xmlThumbHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (E){
			xmlThumbHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlThumbHttp;
}
