/*

Javascript functions for handling webcam updates to a page

@copyright 2008 SiteCrafting, Inc
@author Ken Foubert
@version 1.0.0 2008/04/21
*/

var intWebCamRefreshes = 0;

/*
 * Refreshes the image for the web cam and updates the last updated time label
 *
 * @param string strCameraId Element ID of web cam image
 * @param string strLastUpdatedId Element ID of last updated label
 * @returns false - prevents link from working
**/
function refreshWebCam(strCameraId, strLastUpdatedId) {

	// get the web cam image object
	var imgWebCam = document.getElementById(strCameraId);
	
	// get the last updated label object
	var lblLastUpdated = document.getElementById(strLastUpdatedId);

	var strSource = "";

	// ---------------------
	// update the web camera	
	intWebCamRefreshes = intWebCamRefreshes + 1;
	
	// save the original source of the image
	strSource = imgWebCam.src + "&" + intWebCamRefreshes;
	
	// now clear the image source of the image
	imgWebCam.src = "";
	
	// now put the original source back in, we should get the latest image
	imgWebCam.src = strSource;
	
	// --------
	// update the time for last updated
	var dateUpdated = new Date();
	
	// .setHours( hours, minutes, seconds, milliseconds)
	// testing dateUpdated.setHours(3, 6);
	
	var intHours = dateUpdated.getHours();
	
	var strMinutes = dateUpdated.getMinutes();
	
	var strAmPm = "am";
	
	// convert from 24 hours to 12 hour clock
	if (intHours   > 11) { strAmPm = "pm"; }

	if (intHours   > 12) { intHours = intHours - 12; }

	if (intHours   == 0) { intHours = 12; }
	
	// pad zero to the hours
	if( intHours.toString().length == 1 )
	{
		intHours = "0" + intHours;
		
	}	//end if
	
	// pad zero to the minutes
	if( strMinutes.toString().length == 1 )
	{
		strMinutes = "0" + strMinutes;
		
	}	//end if
	
	lblLastUpdated.innerHTML = intHours + ":" + strMinutes + " " + strAmPm;
	
	return true;
	
}

// udpdates the labels and links for the selected camera. 
function switchCameras(intCameraId)
{
	var strTitle = "";
	
	var strCamera1Class = "";
	
	var strCamera2Class = "";
	
	var strImageCamSrc = "";
	
	var strLargeCameraLink = "";
	
	switch(intCameraId)
	{
		// street camera
		case 1:
		
			strTitle = "Live Street Cam";
			
			strCamera1Class = "camara_icon camera_active";
			
			strCamera2Class = "camara_icon";
			
			strImageCamSrc = "webcam_axis241s.php?camera=1";
			
			strLargeCameraLink = "/cameras.php?camera=1";
		
			break;
			
		// in-gate camera
		case 2:
		
			strTitle = "Live In-gate Cam";
			
			strCamera1Class = "camara_icon";
			
			strCamera2Class = "camara_icon camera_active";
			
			strImageCamSrc = "webcam_axis241s.php?camera=2";
			
			strLargeCameraLink = "/cameras.php?camera=2";
		
			break;
			
		// do nothing
		default:
		
			return true;
			
			break;
	
	}	//end switch
	
	// update all the labels, images
	document.getElementById("lblCameraName").innerHTML = strTitle;
	
	document.getElementById("imgCameraIcon1").className = strCamera1Class;

	document.getElementById("imgCameraIcon2").className = strCamera2Class;

	// ---------------------
	// update the web camera	
	intWebCamRefreshes = intWebCamRefreshes + 1;
	
	// clear the camera source
	document.getElementById("WebCamImage").src = "";

	// now update the source to get the latest image
	document.getElementById("WebCamImage").src = strImageCamSrc + "&" + intWebCamRefreshes;

	document.getElementById("aViewCameraPage").href = strLargeCameraLink;
	
}	//end function

