// Webcam functions for pbnradio.com/


// --------------------------------------
// Global Variables 
// --------------------------------------

// Change these settings as needed
var webcam_reloadDelay  = 30 * 1000,      // Seconds
    webcam_toggleDelay  =  7 * 1000;      // Seconds

var weather_reloadDelay = 15 * 60*1000;   // Minutes


var webcam_currentCamera=1;
var webcam_url = [
	"http://pbnradio.com/webcam/webcam.jpg",
	"http://pbnradio.com/webcam/webcam2.jpg"
	];
	
var webcam_cookie = "webcam";     // Cookie used to remember last webcam (format= camnum,alt?, e.g.: "1,false")



// -------------------------------------- 
// Webcam functions
// --------------------------------------


// Get the current alternate checkbox setting
function getAltCheckbox() {
	var obj = getObject("altcheckbox");
	var webcam_alternate = obj ? obj.checked : false;
	return webcam_alternate;
}


// Set the current alternate checkbox setting
function setAltCheckbox(objValue) {
	var obj = getObject("altcheckbox");
	if (obj) obj.checked = objValue;
}



// Show the current webcam image (unless it hasn't changed)
// The URL won't change if the user is idle.  This is done to save bandwidth.
var webcam_lastUrl = "";
function showWebcam() {
	var imageUrl = webcam_url[webcam_currentCamera-1] + "?" + webcam_lastLoadTimeValue;
	if (imageUrl != webcam_lastUrl)
		reloadImage("webcam", imageUrl);
	webcam_lastUrl = imageUrl;
}



// Update the webcam image
// This function should be called on an interval (only)
// Call at the rate the webcam image updates (30 seconds?)
var webcam_lastLoadTimeValue = "";  // Last reload time value.  This is updated to force an image to reload on next display
var activeMsg = "";  // Saved screen text for idle/not idle
var idleCheckInterval = null;  // Make extra checks while idle
function updateWebcam() {
	var txtObj = getObject("refreshmsg");

	// Set a new current time in seconds to force reload at this time (by changing the URL)
	// Do this only if the user is not idle to save bandwidth
	if (bIdle) {
		if (txtObj && txtObj.firstChild && txtObj.firstChild.nodeValue && activeMsg == "") {
			activeMsg = txtObj.firstChild.nodeValue;   // Save original message
			txtObj.firstChild.nodeValue = "Idle.";
			idleCheckInterval = setInterval("updateWebcam();", 3.5 * 1000);  // secs
		}
	}
	else {
		var now = new Date();
		webcam_lastLoadTimeValue = now.getTime();
		
		if (txtObj && txtObj.firstChild && txtObj.firstChild.nodeValue && activeMsg != "") {
			txtObj.firstChild.nodeValue = activeMsg;   // Restore original message
			activeMsg = "";
		}
		
		if (idleCheckInterval) {
			clearInterval(idleCheckInterval);
			idleCheckInterval = null;
		}
		
		// Now show the image
		showWebcam();
	}
}



// Auto-switch camera view, if we're alternating
// This function should be called on an interval (only)
// Call at the rate you want the camera to switch (7 seconds?)
function switchCamera() {
	// Switch image, if we're alternating
	if (getAltCheckbox()) {
		// Advance the camera number and show the image
		webcam_currentCamera = ((webcam_currentCamera) % webcam_url.length) + 1;
		showWebcam();
	}
}


// Show the specified camera
// Called from camera buttons or on page load
var webcam_SwitchIntervalID = null;  // Interval timer ID (so we can reset the timer)
function setWebcam(cameraNumber) {
	webcam_currentCamera = cameraNumber;
	showWebcam();
	
	// Set or reset switch interval timer
	clearInterval(webcam_SwitchIntervalID);
	webcam_SwitchIntervalID = setInterval("switchCamera();", webcam_toggleDelay);

	return false;  // Prevent the form from being processed
}







// When the page unloads, save the webcam settings in a cookie for next time
function saveWebcam() {
	var cookie_value = webcam_currentCamera + "," + (getAltCheckbox() ? "true" : "false");
	createCookie(webcam_cookie, cookie_value, 5*365);  // Remember for 5 years (why not?)
	//alert("Cookie set to="+cookie_value);
}



// Initialize webcam page (called when webcam page loads)
function webcamSetup() {
	var cookie, autoCheckbox, webcam_alternate;
	
	// Get last-used webcam settings
	cookie = readCookie(webcam_cookie);
	//alert("cookie='"+cookie+"'");
	if (!cookie) cookie = "1,false";
	
	cookie = "1,false";   /* Only one webcam right now! */
	
	cookie = cookie.split(",");
	
	
	// Load image and start the reload process
	// Do this before setting the checkbox (so initial camera shows first, otherwise it'll switch first)
	setWebcam(cookie[0]);
	setInterval("updateWebcam();", webcam_reloadDelay);


	// Set checkbox as they left it (via cookie)
	setAltCheckbox(cookie[1]=="true");

	// Set up to save webcam settings in a cookie on exit (for next time)
	addUnloadHandler(saveWebcam);
	
	
	
	// Reload the weather image periodically
   setInterval("reloadImage2('weatherpic');", weather_reloadDelay);
}




// Do setup after the page has laoded
addLoadHandler(webcamSetup);


