// the updating code

//      Script stolen from GeneriCam - http://occonnect.com/johnqpublic
//      Thanks to the Nose, Sito and S.A. Dave 
//
//      modified for 2 camera updates, 1/27/03 P. D. Barnes, Jr.

var raiseOnRefresh = false; 
// +++ default: bring to front whenever the image refreshes.
// change to false to open the window with it disabled.

//this eliminates JavaScript error messages
onError=null;

// the camera images
var MIPPCam00 = "http://mippcam00.fnal.gov/netcam.jpg";
var MIPPCam01 = "http://mippcam01.fnal.gov/netcam.jpg";

var interval = 30;		// refresh interval

var t = 0;				// time to next refresh

var cachePic00 = new Image();   // place to hold the next image for fast screen refresh
var cachePic01 = new Image();

function stopClock() {
    t = "off";
    document.controls.clock.value = t;
}

// start the clock
//
function startClock() {
    t = interval;
    runClock();
}

// start/stop the clock
//
function toggleClock() {
	switch (document.controls.Toggle.value) {
		case "Start" :
			startClock();
			document.controls.Toggle.value = "Stop";
			break;
			
		case "Stop" :
			stopClock();
			document.controls.Toggle.value = "Start";
			break;
	}
}

function runClock() {
    if (t != "off") {                                   // not stopped
        t = t - 1;                                      // decrement timer
        if (t <= interval / 2) {
          recache()                                     // pre-cache new images
        }
        if (t == 4) { 
          reload()                                      // show the new pics
          // start early so first picture updates closer to t = 0
        }
        if (t == 0) {
          t = interval									// wrap timer
        }
        document.controls.clock.value = t;              // update display
        setTimeout("runClock()", 1000);                 // schedule my next clock tick
    }
}

function recache() {
    var now = new Date();

    var camImg00 = MIPPCam00 + "?" + now.getTime();
    var camImg01 = MIPPCam01 + "?" + now.getTime();
    
    cachePic00.src = camImg00;
    cachePic01.src = camImg01;
}

function reload() {
    document.images.MIPPCam00.src = cachePic00.src;
    document.images.MIPPCam01.src = cachePic01.src;
}

function reloadPage() {
    window.location.reload();
}
