// JavaScript Document


function GAL_ActionNext(){
	//alert("GAL_ActionNext" + ":" + curImg);
	GAL_Action("Next");
}

function GAL_Action(imageAction) {
	
	var imgList, numPics, imgDir, imgFile;
	var imgArray = new Array();

	imgDir = CFG_PHOTODIR + "/" + document.getElementById("gal_imageDir").value;		
	imgList = document.getElementById("gal_imageList").value;

	imgDir = imgDir + "/";
	if (imgList.length > 0){
		imgArray = imgList.split(",");
		numPics = imgArray.length;
	} else {
		alert("No photos found.");
		numPics = 0;
	}
	
	//alert(imageAction + ":" + curImg);
	switch(imageAction)
	{
		case "First":
			curImg = 0;
			
			break;
		case "Last":
			curImg = numPics-1;
			break;
		case "Next":
			if (curImg < (numPics - 1)){
				curImg = curImg + 1;
			} else if (timer !== 0){ //if in slide show mode
				curImg = 0;
			}
			break;
		case "Prev":
			if (curImg > 0){
				curImg = curImg - 1;
			}
			break;
		case "Play":
			
			if (timer == 0){ // if not already in slideshow mode
				
				if (curImg == -1){
					curImg = 0;
				}
				//document.body.style.cursor = 'wait';
				timer = setInterval(GAL_ActionNext, 5000);
			}

			break;
		case "Stop":
			if (timer !== 0){ //if in slide show mode
				clearInterval(timer);
				timer = 0;
				document.body.style.cursor = 'default';
				document.slideshow.next.disabled=false;
				document.slideshow.prev.disabled=false;
				document.slideshow.last.disabled=false;
				document.slideshow.first.disabled=false;
			}
			break;
		default:
		
	};
	//alert(t);
	if (curImg < (numPics - 1)){
		// preload next image
			//document.preImage.src = imgDir + imgArray[curImg + 1];
	}
	//alert(curImg + ":" + imgArray[curImg]);
	document.mainImage.style.visibility="hidden";
	document.getElementById("gal_imageslide").style.cursor="wait";
	document.mainImage.style.width = "auto";
	document.mainImage.style.height = "auto";
	if (document.mainImage.filters){
		document.mainImage.filters.revealTrans.apply();
	}
	document.mainImage.src= imgDir + imgArray[curImg];		


}


function GAL_RevealImage()
{		
	// if a portrait
	if (document.mainImage.height > document.mainImage.width)
	{
		document.mainImage.vspace = 5;
		document.mainImage.style.width = "55%";
		document.mainImage.style.height = "90%";
	} else
	{
		document.mainImage.vspace = 25;
		document.mainImage.style.width = "90%";
		document.mainImage.style.height = "66%";
	}
		
	document.mainImage.style.visibility="visible";
	document.getElementById("gal_imageslide").style.cursor="default";
	if (document.mainImage.filters){
		document.mainImage.filters.revealTrans.play();
	}
}

function gal_AJAXPost(action, opt_params){

	
	document.body.style.cursor = "wait";
	
	//document.news.storylist.style.cursor = "wait";

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
 	{
 		alert ("Browser does not support HTTP Request");
 		return;
 	}
	var url="../gallery/gallery_common_AJAX.php";
	var params="action=" + action + opt_params;
	//alert("POST:" + url + params);
	xmlHttp.onreadystatechange=function(){
		gal_AJAXReceive(action);
	};

	xmlHttp.open("POST",url,true);
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//xmlHttp.setRequestHeader("Content-length", params.length);
	//xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);

	
}

function gal_AJAXReceive(action) 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 { 
	 	var response = xmlHttp.responseText;
		var msg = ' ';		
		//alert("ACTION=" + action + ":response=" + response);
		switch (action){		
		
		case "GAL_GETFILELIST":	
			GAL_SetGallery(response);
			break;

		}
	 } 
	 document.body.style.cursor = "default";

}

function GAL_GetFileList(dir)
{
	params="&dir="+dir;
	gal_AJAXPost("GAL_GETFILELIST", params);
	//alert(dir);
	document.getElementById("gal_imageDir").value = dir;
	document.getElementById("gal_title").innerHTML = "Viewing Gallery:" + dir;
}

function GAL_SetGallery(imgList)
{
	document.getElementById("gal_imageList").value = imgList;	
	//document.getElementById("gal_imageDir").value;
	GAL_Action("Play");

}

// find the currently select item of the cmb
function getIndex(dir){
	var ssi = document.slideselect.imageSel;
	var ssio = ssi.options;
	//alert("in getindex dir="+ dir);
	for (i=0;i<ssi.length;i++)
    {
		if (ssio[i].value == dir){
			return i;
		}    	
    }

	return 0;
}

function GAL_ViewGallery(gallery){
	// gallery is just a directory within photo home
	
	
}