// WebCam Auto-Updater v1.8

//

// by Cowboy - 1/16/04 - cowboy@cowboyscripts.org

//

// (You can use this script, but credit me .. because I'm the man and you know it)

//

// http://cowboyscripts.org/

// http://rj3.net/ckb/

// http://rj3.net/cowboy/

//



function webCamRegisterCam(camName, camURL) {

	

	if (typeof webCamURL != "object") {

		webCamURL = new Array();

		webCamName = new Array();

	}



	var idx = webCamName.length;

	

	webCamName[idx] = camName;

	webCamURL[idx] = camURL;

}











// ----- USER SETTINGS HERE -----

//



// Width and height of each cam pic

webCamWidth = 320;

webCamHeight = 240;



// Time in sec to update images

webCamUpdateInterval = 10;



// Register each webcam here. Note that they are indexed in the order in which they

// are defined (the first is index 0, second is index 1, and so on..)

//

// Usage:

//

//   webCamRegisterCam(camName, camURL);

//

// The first image is the error/default image

webCamRegisterCam("Error", "camera.gif");



// Here are the WebCam images:

webCamRegisterCam("Inside", "http://www.helmroos.com/cam/camera_2.jpg");

webCamRegisterCam("Outdoor", "http://www.helmroos.com/cam/cam_1.jpg");





//webCamRegisterCam("", "");



//

// ----- END USER SETTINGS -----







// Set your page's onload= to this function, otherwise it won't do anything!

//

function webCamInit() {

	webCamUpdateFunction();

	

	webCamUpdateTimeLeft = webCamUpdateInterval;

	webCamInterval = setInterval("webCamUpdate();", 1000);

}



// Use this function in-line with your HTML to generate the WebCam images

//

// Usage:

//

//   webCamDraw(n);

//

//   n is the array index. You can override the default webCamWidth and webCamHeight

//   by overloading the function: webCamDraw(n, width, height, altText);

//

function webCamDraw(camNum, w, h, altText) {

	var wStr = " width='" + ((typeof w == "undefined") ? webCamWidth : w) + "'";

	var hStr = " height='" + ((typeof h == "undefined") ? webCamHeight : h) + "'";



	if (w == "") wStr = "";

	if (h == "") hStr = "";



	var altText = (typeof altText == "string") ? altText : ((camNum == 0 && typeof wc_spareImgAltText == "string" && wc_spareImgAltText != "") ? wc_spareImgAltText : webCamName[camNum]);



	var theDate = new Date();

	

	document.write("<img name='webCam_" + camNum + "' src='" + webCamURL[0] + "'" + wStr + hStr + " border='0' alt=\"" + altText + "\">");

}



// Display #sec remaining until next update in the status bar

//

function webCamUpdate() {

	webCamUpdateTimeLeft--;



	window.status = "WebCams: Reload in " + webCamUpdateTimeLeft + " seconds";

	

	if (webCamUpdateTimeLeft <= 0) {

		webCamUpdateFunction();

		webCamUpdateTimeLeft = webCamUpdateInterval;

	}

}



// Intelligent image preloader. Reloads each image into a separate image object,

// when that object is loaded it refreshes the visible WebCam pic so there isn't

// any flickering

//

function webCamImagePreloaded() {

	document["webCam_" + this.camNum].src = document["webCamPreload_" + this.camNum].src;



	window.status = "WebCams: Reloaded " + webCamName[this.camNum];

}



function webCamImageError() {

	document["webCam_" + this.camNum].src = webCamURL[0];



	window.status = "WebCams: Error reloading " + webCamName[this.camNum];

}



function webCamPreloadImage(camNum, imgURL) {

	theImage = new Image();

	theImage.src = imgURL;

	theImage.onerror = webCamImageError;

	theImage.onload = webCamImagePreloaded;

	theImage.camNum = camNum;

	

	return theImage;

}



function webCamUpdateWithPreload() {

	if (document.images) {

		var theDate = new Date();



		for (var i = 1; i < webCamURL.length; i++) {

			if (typeof document["webCam_" + i] == "object" && typeof document["webCam_" + i].src == "string") {

				document["webCamPreload_" + i] = webCamPreloadImage(i, webCamURL[i] + "?" + parseInt(theDate.getTime() / 1000));

			}

		}

	}

}





// Brute-force updating the images. No preloading or error-handling, but

// it works.

//

function webCamUpdateNoPreload() {

	if (document.images) {

		var theDate = new Date();

		

		for (var i = 1; i < webCamURL.length; i++) {

			if (typeof document["webCam_" + i] == "object" && typeof document["webCam_" + i].src == "string") {

				document["webCam_" + i].src = webCamURL[i] + "?" + parseInt(theDate.getTime() / 1000);

			}

		}

	}

}





webCamUpdateFunction = webCamUpdateWithPreload;





// Test to see if preloading images is supported (there are probably much

// better ways to do this, but I'm limited on time!) If preloading doesn't

// work, use the non-preloading webCamUpdateNoPreload function.

//

function testImagePreload() {

	if (typeof this.src != "string") {

		webCamUpdateFunction = webCamUpdateNoPreload;

	}

}

testImage = new Image();

testImage.onload = testImagePreload;

testImage.src = webCamURL[0];









