var newWindow;  // These five variables used in admin funtions
var thumbPath;
var thumbName;
var fileID;
var imagePathx;

function modifyDoc(largeImage, place, IDENT, operation, thumb) {
	var myImage = '';
	slideShow = 0;
	var largePhoto = largeImage;
	imagePathLarge=new String("photos/" + place + "/large/");
	imagePath = String('photos/' + place + '/very_large/');
	imagePathx=place + "/large/";  // required for administrative function of photo gallery
	thumbPath=new String(place);
	thumbName = thumb;
	fileID=IDENT;
	
	initial_index = getInitialIndex(fileID)

	if(operation == "photos") {
		if(document.photosizer.slide_show.checked) slideShow = 1;
		myImage=FIND(IDENT);
		prevSize = getViewportSize();
		if(document.photosizer.normal_size.checked) writeData();  // Show large photo (not very large photo)
//		else embedd_java(myImage, slideShow, largePhoto, thumbPath, thumbName, fileID);
		else embedd_java(initial_index, initial_index);
		
	}
	if(operation == "deletePhotos") {
		newWindow = window.open("photo_deletion_process.php","photo_delete_window","height=400,width=600");
	}
	if(operation == "modifyCaption") {
		newWindow = window.open("modify_caption_process.php?category=" + thumbPath + "&thumb_file=" + thumbName,"photo_delete_window","height=700,width=950");
	}
}





	
	
	
	function getImageSizeAndPosition(ind, w, h) {
		var maxw, maxh, toppos, leftpos, filename;
		direction = "right";
		index = ind;
		var screenAspectRatio, imageAspectRatio, fileInfo, filePrefix, filePath;
	
		filename = new String(la_arr_file_names[index]);
			
		var pos = filename.search("x");											// locate width
		var imageWidth = filename.substr(0, pos);
		var subName = new String(filename.substr(pos + 1));
		pos = subName.search("_");					// locate height
		var imageHeight = subName.substr(0, pos);
		screenAspectRatio=h/w;
		imageAspectRatio = imageHeight/imageWidth;
	
		if ((imageHeight <= h) && (imageWidth <= w)) {
			maxw=imageWidth;
			maxh=maxw * imageAspectRatio; 
			leftpos=(w / 2) - (maxw / 2);
			toppos=(h / 2) - (maxh / 2);		
		} else {
//			alert("screen = " + screenAspectRatio + "    image = " + imageAspectRatio);
			if (screenAspectRatio <= imageAspectRatio) {
				maxh=h;
				maxw=maxh / imageAspectRatio;
				toppos=0;
				leftpos=(w / 2) - (maxw / 2);
			} else {
				maxw=w;
				maxh=maxw * imageAspectRatio;
				leftpos=0;
				toppos=(h / 2) - (maxh / 2);
			}
		}
		toppos = parseInt(toppos);
		leftpos = parseInt(leftpos);
		return values = [maxw, maxh, toppos, leftpos, filename];
	}
	
function updateIndex(j) {  // Called by Java
	index = j;
}

function reSizePhoto() {
	if(document.photosizer.normal_size.checked) {
		var k = index;
		setCurrentImage(k);
	}
	else embedd_java(initial_index, index);
}

function setCurrentImage(k) {
	var h, w, dim, vals, maxw, maxh, toppos, leftpos, filename, ident, identTxt, factorx, xleft, xtop;
	var text_obj;
//	dim = setSize();
	w = prevSize[0];
	h = prevSize[1];
	vals = getImageSizeAndPosition(k, w, h);
	maxw = vals[0];
	maxh = vals[1];
	toppos = vals[2];
	leftpos = vals[3];
	filename = vals[4];
	ident = "b" + k;
	identTxt = ident + "txt";
	factorx = maxh/600;
	xleft = factorx * (parseInt(leftpos) + parseInt(arr_left_pos[k]));
	xtop = factorx * (parseInt(toppos) + parseInt(arr_top_pos[k]));
	img_obj = FIND(ident);
	img_obj.style.left = leftpos + "px";
	img_obj.style.top = toppos + "px";  
	img_obj.style.width = maxw + "px";  
	img_obj.style.height = maxh + "px";
	identTxt = ident + "txt";

	text_obj = FIND(identTxt);
	text_obj.style.left = xleft + "px";
	text_obj.style.top = xtop + "px";  
	text_obj.style.width = maxw + "px";  
	text_obj.style.height = maxh + "px";
	
	img_obj.style.visibility = 'visible';
	text_obj.style.visibility = 'visible';
}
	
	
var id4;
var indx;
function preloadTest(ind) {
	indx = ind;
	id4 = setTimeout("getOut()", 100);
}

function getOut() {
	// load next image in cache
	var i, file_name, loc_index, path;
	if(direction == "right") {
		loc_index = indx + 1;
		if(loc_index >= (la_arr_file_names.length)) loc_index=0;
	}
	if(direction == "left") {
		loc_index = indx - 1;
		if(loc_index < 0) loc_index=la_arr_file_names.length - 1;
	}
	i = "b" + loc_index;

	an_image = FIND(i);

	path = an_image.src;

	if(path=="") {
		file_name = la_arr_file_names[loc_index];
		an_image.src = imagePathLarge + file_name;
	}
}



function prev_photo() {
	var ident, file_name;
	var an_image_cur, an_image, an_text_cur, an_text, path;
	direction = "left";
	ident = "b" + index;
	identTxt = ident + "txt";
	an_image_cur = FIND(ident);
	an_text_cur = FIND(identTxt);
	index--;
	if(index < 0) index=la_arr_file_names.length - 1;
	ident = "b" + index;
	an_image = FIND(ident);
	path = an_image.src;

	if(path=="") {
		file_name = la_arr_file_names[index];
		an_image.src = imagePathLarge + file_name;
	}
	setCurrentImage(index);

	an_image_cur.style.visibility = 'hidden';
	an_text_cur.style.visibility = 'hidden';
}	


function next_photo() {
	var ident, file_name;
	var an_image_cur, an_image, an_text_cur, an_text, path;
	direction = "right";
	ident = "b" + index;
	identTxt = ident + "txt";
	an_image_cur = FIND(ident);
	an_text_cur = FIND(identTxt);
	index++;
	if(index >= (la_arr_file_names.length)) index=0;
	ident = "b" + index;
	an_image = FIND(ident);
	path = an_image.src;
	if(path=="") {
		file_name = la_arr_file_names[index];
		an_image.src = imagePathLarge + file_name;
	}
	setCurrentImage(index);

	an_image_cur.style.visibility = 'hidden';
	an_text_cur.style.visibility = 'hidden';
}


function getInitialIndex(fileID) { 
	var s = new String(fileID);
	return parseInt(s.substr(1));  // Initial Index
}



function startTiming(j) {
	var k = j;
	preloadTest(k, imagePathLarge);
	if(slideShow == 1) {	
		slideTimer(initial_index);
	}
}

var id3;
function slideTimer() {
	id3 = setTimeout("slideTimerex()",5000);
}

var id1, id2;
function slideTimerex() {
	var ident, identTxt;
	var file_name;
	var an_image_cur, an_image, an_text_cur, an_text;
	ident = "b" + index;
	identTxt = ident + "txt";
	an_image_cur = FIND(ident);
	an_text_cur = FIND(identTxt);
	var path = an_image_cur.src;

	if(path=="") {
		id1 = setTimeout("slideTimerex()",500);			// set timer to wait 500 millisecs more for image to be preloaded
		return;
	}
	index++;
	if(index >= (la_arr_file_names.length)) index=0;
	if(index == initial_index) {
		an_image_cur.style.visibility = 'hidden';
		an_text_cur.style.visibility = 'hidden';
		showEndingMsg();
		return;
	}
	ident = "b" + index;
	an_image = FIND(ident);
	setCurrentImage(index);
	
	an_image_cur.style.visibility = 'hidden';
	an_text_cur.style.visibility = 'hidden';
	id2 = setTimeout("slideTimerex()",5000);
}


function writeData() {
	enlargedPhoto = 1;
	var dim, vals, maxw, maxh, toppos, leftpos, filename, ident, identTxt, factorx, xleft, xtop;
	var h, w;
	w = prevSize[0];
	h = prevSize[1];
	var prev_arr='';
	var next_arr='';

	div_text = '<div id="objx" style="position:absolute;left:0px;top:0px;">';
	div_text += '<img name="closer" id="closer" style="position:absolute;top:5px;left:5px;z-index:10;" onclick="closeUP();" src="photos/images/close.gif">';
	if(slideShow == 0) {
		MM_preloadImages('photos/images/prev_arr_over.gif','photos/images/next_arr_over.gif');
		div_text += '<img name="prev" id="prev" style="position:absolute;top:5px;left:35px;z-index:10;" onclick="prev_photo();" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'prev\', \'\', \'photos/images/prev_arr_over.gif\',1)" src="photos/images/prev_arr.gif">';
		div_text += '<img name="next" id="next" style="position:absolute;top:5px;left:105px;z-index:10;" onclick="next_photo();" onMouseOut="MM_swapImgRestore()"  onMouseOver="MM_swapImage(\'next\', \'\', \'photos/images/next_arr_over.gif\',1)" src="photos/images/next_arr.gif">';
	}	
	var first_img, first_caption, other_imgs, other_captions;
		
	for(k = 0; k < la_arr_file_names.length; k++) { 

		vals = getImageSizeAndPosition(k, w, h);
		maxw = vals[0];
		maxh = vals[1];
		toppos = vals[2];
		leftpos = vals[3];
		filename = vals[4];
		ident = "b" + k;
		identTxt = ident + "txt";
		factorx = maxh/600;
		xleft = factorx * (parseInt(leftpos) + parseInt(arr_left_pos[k]));
		xtop = factorx * (parseInt(toppos) + parseInt(arr_top_pos[k]));
		if(k == initial_index) {
			div_text += '<img name="' + ident + '" id="' + ident + '" style="visibility:visible;position:absolute;left:' + leftpos + 'px;top:' + toppos + 'px;height:' + maxh + 'px;width:' + maxw + 'px;z-index:8;" onload="startTiming(' + k + ')" >';
			div_text += '<div name="' + identTxt + '" id="' + identTxt + '" style="visibility:visible;position:absolute;left:' + xleft + 'px;top:' + xtop + 'px;height:' + maxh + 'px;width:' + maxw + 'px;z-index:9;font_family:' + arr_font_family[k] + ';font-type:' + arr_font_type[k] + ';font-size:' + arr_font_size[k] + 'pt;color:' + arr_font_color[k] + ';" >' + arr_captions[k] + '</div>';
		} else {
			div_text += '<img name="' + ident + '" id="' + ident + '" style="visibility:hidden;position:absolute;left:' + leftpos + 'px;top:' + toppos + 'px;height:' + maxh + 'px;width:' + maxw + 'px;z-index:8;" onload="preloadTest(' + k + ')" >';
			div_text += '<div name="' + identTxt + '" id="' + identTxt + '" style="visibility:hidden;position:absolute;left:' + xleft + 'px;top:' + xtop + 'px;height:' + maxh + 'px;width:' + maxw + 'px;z-index:9;font_family:' + arr_font_family[k] + ';font-type:' + arr_font_type[k] + ';font-size:' + arr_font_size[k] + 'pt;color:' + arr_font_color[k] + ';" >' + arr_captions[k] + '</div>';
		}
	}
	div_text += '</div>';
	div_obj = document.getElementById('im2');
	div_obj.style.width = w + "px";
	div_obj.style.height = h + "px";
	div_obj.style.background = "#000000";
	div_obj.innerHTML = div_text;
	div_obj.style.visibility="visible";
	var all = document.getElementById('all');
	all.style.display = "none"; 

	index = initial_index;
/*********************************************************/
	var identx, file_namex;
	var an_imagex, pathx;
	identx = "b" + initial_index;
	an_imagex = FIND(identx);
	file_namex = la_arr_file_names[initial_index];
	an_imagex.src = imagePathLarge + file_namex;
}

var id5;
function showEndingMsg() {
	var w, h;
	w = prevSize[0];
	h = prevSize[1];
	if(id1) clearTimeout(id1);
	if(id2) clearTimeout(id2);
	if(id3) clearTimeout(id3);
	if(id4) clearTimeout(id4);
	enlargedPhoto = 0;	
	var div_obj = document.getElementById('im2');
	var prev_obj = document.getElementById('objx');
	var div_text = '<div id="objx" style="visibility:visible;position:absolute;left:0px;top:0px;width:' + w + 'px;height:' + h + 'px;z-index:8;background:#000000;padding:20px 0px 0px 0px;">';
	div_text += '<img name="closer" id="closer" style="position:absolute;top:5px;left:5px;z-index:10;" onclick="closeUP();" src="photos/images/close.gif">';
	div_text += '<h1 id="endh1" align="center" style="color:#FFFFFF;">End of Slideshow - Closing ...</h1>';
	div_text += '</div>';
	
	div_obj.innerHTML = '';	
	div_obj.innerHTML = div_text;
	
	id5 = setTimeout("closeUP()", 2000);

}

var id6;
function closeUP() {
	enlargedPhoto = 0;
	if(id1) clearTimeout(id1);
	if(id2) clearTimeout(id2);
	if(id3) clearTimeout(id3);
	if(id4) clearTimeout(id4);
	if(id5) clearTimeout(id5);
	if(id5) clearTimeout(id5);
	var div_obj = document.getElementById('im2');
	div_obj.innerHTML = '';
	div_obj.style.visibility = "hidden";
	id6 = setTimeout("timed_close()", 500);   //  Timeout needed for netscape 7.0 before reflow to display the main window section properly
	changeTheSize();
}

function timed_close() {
	if(id6) clearTimeout(id6);
	reflow = document.getElementById('all');
	reflow.style.display = "none";
	reflow.style.display = "block";
	changeTheSize();
}


function restoreWindow() {
	window.resizeTo(windowWidth, windowHeight);
	window.moveTo(windowLeft, windowTop);
}

function MouseOverRed(IDENT) {
	myImage=FIND(IDENT); 
	myImage.style.borderColor="#FF0000";
}


function MouseOutBlack(IDENT) {
	myImage=FIND(IDENT);
	var bordStyle = myImage.style.borderStyle;
	if((bordStyle.substr(0,5)) == "solid") {  // netscape and mozilla return four words containing solid
		myImage.style.borderColor="#FFFFCC";
	} else {
		myImage.style.borderColor="#000000";
	}
}

function ImageViewed(IDENT) {
	myImage=FIND(IDENT);	
	myImage.style.borderColor="#000000";
	myImage.style.borderStyle="groove";
}


/*** KEEP FOLLOWING SECTION FOR PHOTO ADMINISTOR ************/

function findObj(ID, win) {
	if( window.mmIsOpera ) return(win.document.getElementById(ID));
	if (window.document.all) return(win.document.all[ID]);
	if (window.document.getElementById) return(win.document.getElementById(ID));
	return(false);
}

function setNextCategory(page_id_str, operation) {
	if(operation == "photos") location.href = page_id_str + '&size_type=' + document.photosizer.normal_size.checked + '&display_type=' + document.photosizer.slide_show.checked;
	else location.href = page_id_str;
}


function closeHelp()  {
	if (newHelpWindow) newHelpWindow.close();
}

function embedd_java(init, current){
	enlargedPhoto = 1;
	var w, h;
	w = prevSize[0];
	h = prevSize[1];
	var div_text = '';
	if (isMSIE) {
		div_text += '<object id="objx" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=1,5,0,0" style="width:' + w + 'px;height:' + h + 'px;background:#000000;">';
		div_text += '<PARAM NAME="code" VALUE="ImageDisplayApplet.class">';
		div_text += '<PARAM NAME="codebase" VALUE = "." >';
		div_text += '<PARAM NAME="archive" VALUE="ImageDisplayApplet.jar" >';
		div_text += '<PARAM NAME="type" VALUE="application/x-java-applet;version=1.5">';
		div_text += '<PARAM NAME="mayscript" VALUE="true">';
		div_text += '<PARAM NAME="id" VALUE="objx">';
		div_text += '<PARAM NAME="imageNames" VALUE="' + vl_StringFileNames + '">';
		div_text += '<PARAM NAME="captions" VALUE="' + StringCaptions + '">';
		div_text += '<PARAM NAME="fontFamily" VALUE="' + StringFontFamily + '">';
		div_text += '<PARAM NAME="fontType" VALUE="' + StringFontType + '">';
		div_text += '<PARAM NAME="fontSize" VALUE="' + StringFontSize + '">';
		div_text += '<PARAM NAME="fontColor" VALUE="' + StringFontColor + '">';
		div_text += '<PARAM NAME="capLeftPos" VALUE="' + StringLeftPos + '">';
		div_text += '<PARAM NAME="capTopPos" VALUE="' + StringTopPos + '">';
		div_text += '<PARAM NAME="imagePath" VALUE="' + imagePath + '">';
		div_text += '<PARAM NAME="slideShow" VALUE="' + slideShow + '">';
		div_text += '<PARAM NAME="firstIndex" VALUE="' + init + '">';
		div_text += '<PARAM NAME="Index" VALUE="' + current + '">';
		div_text += '</object>';
		var div_obj = document.getElementById('im2');
		div_obj.innerHTML = '';
		div_obj.style.visibility="visible";
		var all = document.getElementById('all');
		all.style.display = "none"; 
		div_obj.innerHTML = div_text;

	} else {
		div_text += '<EMBED id="objx" type = "application/x-java-applet;version=1.5"';
    	div_text += ' CODE = ImageDisplayApplet.class';
    	div_text += ' JAVA_CODEBASE = "."';
    	div_text += ' ARCHIVE = "ImageDisplayApplet.jar"';
    	div_text += ' width="' + w + '" height="' + h + '"';
		div_text += ' id = "objx"';
    	div_text += ' imageNames =' + vl_StringFileNames + '';
    	div_text += ' captions =' + StringCaptions + '';
    	div_text += ' fontFamily =' + StringFontFamily + '';
		div_text += ' fontType =' + StringFontType + '';
		div_text += ' fontSize =' + StringFontSize + '';
		div_text += ' fontColor =' + StringFontColor + '';
		div_text += ' capLeftPos =' + StringLeftPos + '';
		div_text += ' capTopPos =' + StringTopPos + '';
    	div_text += ' imagePath =' + imagePath + '';
		div_text += ' slideShow =' + slideShow + '';
    	div_text += ' firstIndex =' + init + '';
		div_text += ' Index =' + current + '';
		div_text += ' mayscript = true';
		div_text += ' pluginspage = "http://java.sun.com/products/plugin/index.html#download">';
		div_text += '<NOEMBED>';
        div_text += '</NOEMBED>';
		div_text += '</EMBED>';
		var div_obj = document.getElementById('im2');
		div_obj.innerHTML = '';
		div_obj.style.visibility="visible";
		var all = document.getElementById('all');
		all.style.display = "none"; 
		div_obj.innerHTML = div_text;
	}
}

function goHelp() {
	fetchData('photos/photoHelp.php', 1, 'im3');
}

function closeDirections() {
	var button_obj = document.getElementById('close_button');
	button_obj.innerHTML = '';
}

function goCloseHelp() {
	help_obj = document.getElementById('im3');
	help_obj.style.visibility = 'hidden';
	help_obj.innerHTML = '';
	var button_obj = document.getElementById('close_button');
	button_obj.style.display = 'none';
	var base_layer = document.getElementById('start_main');
	base_layer.style.display = 'block'; 
}


function setSizeNotice() {
	var note_text = '<div style="visibility:visible;position:relative;top:30px;left:10px;border:2px inset #000000;background-color:#FF9966;">'
		+ 'FOR BEST VIEWING, CHECK "Photo Help"'
		+ '</div>';
	notice_obj = document.getElementById('im3');
	notice_obj.innerHTML = note_text;
	notice_obj.style.visibility = 'visible';
}

