// 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/
//
//
//
// This script modified by mike*yoics.com for dynamic rate control and base rate control.
// 1/11/2007
//

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 = 340;
webCamHeight = 280;

// Time in sec to update images
webCamUpdateInterval = 20; // number of tenths of a second 
tickInterval = 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", "/I-cam-9000.jpg");

// Here are the WebCam images:
//webCamRegisterCam("yoics0", "http://10.10.11.10/usr/yoics1.jpg");
webCamRegisterCam("De-Landerijen_5", "http://birdcams.dyndns.tv/usr/yoics0.jpg");
webCamRegisterCam("De-Landerijen_4", "http://birdcams.dyndns.tv/usr/yoics1.jpg");
webCamRegisterCam("Klein-Quarakter", "http://birdcams.dyndns.tv/usr/yoics2.jpg");
webCamRegisterCam("Koolmezenkast-te-huur", "http://birdcams.dyndns.tv/usr/yoics3.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();", 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) {
        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--;

        if(!(webCamUpdateTimeLeft%2))
            window.status = "WebCams: Reload in " + webCamUpdateTimeLeft/10 + " 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;
}

//
// 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") 
            {
                            // 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;
        }
}

