var downloadWindow;

// following 3 arrays should be redefined by the calling HTML (PHP) file
var pictureNames = new Array("camPicture0","camPicture1","camPicture2","camPicture3","camPicture4","camPicture5");
var thumbNames = new Array("camThumb0","camThumb1","camThumb2","camThumb3","camThumb4","camThumb5");
var camNames = new Array("MainCamera","Camera1","Camera2","Camera3","Camera4","Camera5");

//	var camSuffixes = new Array("_Thumbnail","_LowRes","_HighRes");
//	var camSuffixes = new Array("_LowRes","","");
var camSuffixes = new Array("_Thumbnail","","");
var selectedCamera;
var reloadTimer;
var loadingPic = new Image();
loadingPic.src = 'assets/loading594x405.gif';
var loadedPics = new Array()
	for ( var i in pictureNames ) {
		loadedPics[i] = 0;
	}


function closeWindows() {
	if (typeof downloadWindow != 'undefined') {
		if (!downloadWindow.closed) {
			downloadWindow.close();
		}
	}
}

function bigPic() {

//
// open a new window to display the high res version of the selected camera
//
	var href = ("highResDownload.php?"
				+ "displayWidth="  + '704'
				+ "&filename=" + makeURL(selectedCamera,2));
      	      	
	var wdata = ("width=738px,"
				+ "height=560px,"
				+ "left=280px,"
				+ "top=70px,"
				+ "scrollbars=yes,"
				+ "toolbar=no,"
				+ "menubar=no,"
				+ "status=no,"
				+ "resizable=yes");


	downloadWindow = window.open(href, 'bigPic', wdata);
	downloadWindow.focus();
}


function loadPrimaryPic() {

//
// display the selected camera as primary image
//

// if there is no previous image for this camera, diplay a loading animation

	var imageName = pictureNames[selectedCamera];
	if (!loadedPics[selectedCamera]) {	
		document.images[imageName].src = loadingPic.src;
	}
	getPictureURL(camNames[selectedCamera]+getCamSuffix(selectedCamera,1),imageName);
	loadedPics[selectedCamera] = 1;
}


function loadThumbPics() {

//
// Load the thumbnail images
//
//alert('In loadThumbPics');
	for ( var i in thumbNames ) {
		getPictureURL(camNames[i]+getCamSuffix(selectedCamera,0),thumbNames[i]);
	}
}


function loadAllPics() {

//
// Load the primary and thumbnail images
//
	loadPrimaryPic();
	loadThumbPics();
}

function changePic() {

//
// Load the primary and thumbnail images after a new camera has been selected
//
	if (selectedCamera >= pictureNames.length || selectedCamera < 0) {
		selectedCamera = 0;
	}
	loadAllPics();
}


function getCamSuffix(camera,size) {

//
// returns the suffix for a camera image of a given size
//
	var suffix = camSuffixes[size];
	if (camera == 0 && size == 1) {
		suffix = "_LowRes";
	}
	return suffix;
}



function makeURL(camera,size) {

//
// returns the URL for a camera image
//

	return getPictureURL(camNames[camera]+getCamSuffix(camera,size));
}


function getPictureURL(camName,imageName) { 

// Use AJAX to retrieve a camera image URL and either return the URL or load the image.
//
// If DOM image object name is passed, run in asynchronous mode to load the image.
// If DOM image object name is not passed, run in synchronous mode and return the URL as a string.


	var url = "";
	var req = null; 
	var async = true;
	if (!imageName) {
		async = false;
	}
//	alert(async);
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest(); 
	}
	else if (window.ActiveXObject) {
		req  = new ActiveXObject(Microsoft.XMLHTTP); 
	}

// this function is called in asychronous mode when the request state changes
	req.onreadystatechange = function() { 
//		document.ajax.dyn.value="Wait server...";
		if (req.readyState == 4) {	// the "done" state
			if(req.status == 200) {	// success
//				document.ajax.dyn.value="Received:" + req.responseText;	
				if (document.images[imageName] && req.responseText != "NOT FOUND") {
					document.images[imageName].src = req.responseText;
				}
			}	
			else {
//				document.ajax.dyn.value = "Error: returned status code " + req.status + " " + req.statusText;
			}	
		} 
	};

	var now = new Date();
 	var script = "scripts/getCurrentImageURL.pl";
	var params = "?time=" + now.getTime();
	params += "&filename=" + camName + ".jpg";

	req.open("GET", script+params, async); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null);

// for synchronous mode only
	if (!async) {
		if(req.status == 200) {
			url = req.responseText;	
		}	
		else {
			url ="NOT FOUND";
		}	
	}
	return url; 
} 


function reload() {

//
// reload thumbnails and primary image every 60 seconds
//

//alert('In reload');
	loadAllPics();
	reloadTimer = setTimeout("reload()",60000)
}

