
// http://cowboyscripts.org/
// 
// 
//

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 = 290;
webCamHeight = 200;

// Time in sec to update images
webCamUpdateInterval = 20;

// 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", "webcam_default.gif");

// Here are the WebCam images:
webCamRegisterCam("BUZZCAM1", "http://50.22.1.75/~cuecamsc/kabzcam.jpg");
webCamRegisterCam("BUZZCAM2", "http://50.22.1.75/~cuecamsc/kabzcam.jpg");
webCamRegisterCam("BUZZCAM2", "http://50.22.1.75/~cuecamsc/kabzcam1.jpg");
webCamRegisterCam("BUZZCAM3", "http://50.22.1.75/~cuecamsc/kabzcam1.jpg");


// ----- 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];





