﻿pageLoaded = 0; 			// Prevent access to layers until they're loaded
DEBUG_ON = false;

delayedPreloadImages = new Array();
onloadFunctions = new Array();

// Set the pageLoaded variable to denote that the layers are ready to be used 
function doLoadProc() {
    if (DEBUG_ON) {
        dbg = document.createElement("textarea");
        dbg.id = "debugBox";
        dbg.value = "";
        dbg.cols = 110;
        dbg.rows = 20;
        document.body.appendChild(dbg);
    }

    for (i in delayedPreloadImages) {
        if (typeof delayedPreloadImages[i] != "function") {
            eval(i + " = preload('" + delayedPreloadImages[i] + "')");
        }
    }

    for (i in onloadFunctions) {
        if (i != "getIndex") {
            onloadFunctions[i]();
        }
    }

    pageLoaded = 1;
}

// swapImg - swaps an image for another that has already been preloaded.
function swapImg(imgName, preloadedImg) {
    if (document[imgName]) {
        document[imgName].src = preloadedImg.src;
    }
}

function preload(imgSrc, onload) {
    img = new Image();
    if (onload) {
        img.onload = onload;
    }
    img.src = imgSrc;
    return img;
}

function delayedPreload(imgName, imgSrc) {
    if (!pageLoaded) {
        eval(imgName + " = new Image()");
        delayedPreloadImages[imgName] = imgSrc;
    } else {
        eval(imgName + " = preload('" + imgSrc + "')");
    }
}

function addOnLoad(func) {
    onloadFunctions[onloadFunctions.length] = func;
}

function arrayFindValue(array, val) {
    var idx = null;
    for (var i = 0; i < array.length; i++) {
        if (array[i] == val) {
            idx = i;
        }
    }
    return idx;
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(";");
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == " ") c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return "";
}

function createCookie(name, value, days) {
    expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 86400000));
        expires = "; expires=" + date.toGMTString();
    }
    path = "; path=/";
    domain = "; domain=" + document.location.hostname;
    document.cookie = name + "=" + value + expires + path + domain;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

debug = function(dbgText) {
    if (DEBUG_ON) {
        document.getElementById("debugBox").value += dbgText;
        document.getElementById("debugBox").value += "\n";
        document.getElementById("debugBox").scrollTop = document.getElementById("debugBox").scrollHeight;
    }
}

eWepCustomWindowObj = null;
function eWEPCustomWindow(url, width, height) {
    eWepCustomWindowObj = window.open(url, "eWEPCustomWin", "width=" + width + ",height=" + height + ",scrollbars=1,resizable=1");
    return false;
}
