//17.02.2008
//powered by sandr


function webCamRegisterCam(camName, camURL) {
	
	if (typeof webCamURL != "object") {
		webCamURL = new Array();
		webCamName = new Array();
		webCamFlag = new Array();
	}

	var idx = webCamName.length;
	
	webCamName[idx] = camName;
	webCamURL[idx] = camURL;
	webCamFlag[idx] = false;
}


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

// URL to a video server
urlToServer = "http://86.110.175.206";
urlToServer = "http://86.110.175.206:8081/Jpeg/CamImg.jpg";

// Width and height of each cam pic
webCamWidth = 640;
webCamHeight = 480;

// Time in sec to update images
webCamUpdateInterval = 10;              // 1 seconds
tickInterval         = 100;

// 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", urlToImg+"I-cam-9000.jpg");

// Here are the WebCam images:
//webCamRegisterCam("yoics0", "http://10.10.11.10/usr/yoics1.jpg");
/*
webCamRegisterCam("Camera 1", urlToServer + "/usr/yoics0.jpg");
webCamRegisterCam("Camera 2", urlToServer + "/usr/yoics1.jpg");
webCamRegisterCam("Camera 3", urlToServer + "/usr/yoics2.jpg");
webCamRegisterCam("Camera 4", urlToServer + "/usr/yoics3.jpg");
*/
webCamRegisterCam("Camera 1", urlToServer);
webCamRegisterCam("Camera 2", urlToServer);
webCamRegisterCam("Camera 3", urlToServer);
webCamRegisterCam("Camera 4", urlToServer);


//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();", tickInterval);
}

// 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) {
    webCamFlag[camNum] = true;
	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.getElementById("webcam"+camNum).innerHTML = "<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--;

	if(!(webCamUpdateTimeLeft%2))
         var statusFlag = false;
         for (var i = 1; i < webCamFlag.length; i++) {
            //alert(webCamFlag[i]);
            if (webCamFlag[i] == true) {
               statusFlag = true;
            }
         }
         if (statusFlag == true) {
            window.status = "WebCams: Reload in " + webCamUpdateTimeLeft/10 + " seconds";
            //document.getElementById("stopAll").style.display = '';
            //document.getElementById("startAll").style.display = 'none';
         } else {
            window.status = "WebCams: all the cameras is stoped";
            //document.getElementById("stopAll").style.display = 'none';
            //document.getElementById("startAll").style.display = '';
         }
         
   
        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;
}

//
// First attempt at dynamic load, experimental.  mike*yoics.com
//
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" && webCamFlag[i] == true) 
            {
			    // only update if last load was complete, seems to only work for IE
                //if(!document["webCamPreload_" + i].complete==true)
            	    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") 
            {
                //if(document["webCam_" + i].complete)
                    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];


//
// Update the interval, need to update this to fix the dropdown not updating current selection mike*yoics.com
//
function update_interval(form) 
{
        var myindex=form.select1.selectedIndex
        
        if (form.select1.options[myindex].value != 0) 
        {
       
            webCamUpdateInterval=form.select1.options[myindex].value;

        }
}

function stopCam(n) {
   webCamFlag[n] = false;
   document.getElementById("stopCamera"+n).style.display = 'none';
   document.getElementById("startCamera"+n).style.display = '';
   return false;
   
}

function startCam(n) {
   webCamFlag[n] = true;
   document.getElementById("stopCamera"+n).style.display = '';
   document.getElementById("startCamera"+n).style.display = 'none';
   return false;
}

function stopAll() {
   stopCam(1);
   stopCam(2);
   stopCam(3);
   stopCam(4);
   document.getElementById("stopAll").style.display = 'none';
   document.getElementById("startAll").style.display = '';

   return false;
}

function startAll() {
   startCam(1);
   startCam(2);
   startCam(3);
   startCam(4);
   document.getElementById("stopAll").style.display = '';
   document.getElementById("startAll").style.display = 'none';
   return false;
}
