var speed = 200;
var holdDelay = 1500;
var preLoad = new Array();
var crntImage = 0;
var maxLoop = 10;
var imageref;
var loopOK = 1;
var loopTimer = 0;
var initRc, initImg, initText, initCaption;

function changeActiveLooper(idx) {
    toggleLoop();
    ch = document.getElementsByName('chLocalMap' + idx)[0];
    rc = document.getElementsByName('radarchoice' + idx)[0]
    wi = document.getElementsByName('weatherimagery' + idx)[0]
    cap = 'map_caption' + idx;
    rc.selectedIndex = 0;
    initRadar(rc.value, wi, rc.options[rc.selectedIndex].text, cap);
}

function freeImage() {
    if (preLoad.length > 0) {
       for (i=preLoad.length-1; i >= 0; i--) {
           preLoad[i] = null;
       }
    }
}

function toggleLoop() {
    if (loopTimer != 0) {
        clearTimeout(loopTimer); 
    }
    if (loopOK != 1) {
        loopTimer = setTimeout('privLoop()', speed);
    }
    loopOK = -(loopOK);
}

function changeStyle(newClass, elID){
    el = document.getElementById(elID);
    el.className=newClass;
}

function privLoop() {
    if (loopOK == 1) {
        imageref.src = preLoad[crntImage].src;
        crntImage = crntImage - 1;
        if (crntImage < 0) {
              crntImage = maxLoop;
              clearTimeout(loopTimer);
              loopTimer = setTimeout('privLoop()', holdDelay);
        } else {
              clearTimeout(loopTimer);
              loopTimer = setTimeout('privLoop()', speed);
        }
    } else {
        crntImage = 0;
        imageref.src = preLoad[crntImage].src;
    }
}

function resetRadarLoop(rc, img, text, caption, startLoop) {
    loopOK = 1;
    toggleLoop();

    if (startLoop==1) {
        imgUrl = rc
        imageref = img;

        if (imgUrl != null) {
            imageref.src='http://media.rockymountainnews.com/drmn/content/img/weather/imagery/loading.png';
            imgPath = imgUrl.slice(0,-4);
            imgExt = imgUrl.slice(-4);

            freeImage();

            for (i=0; i <= maxLoop; i++) {
                preLoad[i] = new Image();
                if (i > 0) {
                    preLoad[i].src = imgPath + '-' + i + imgExt;
                } else {
                    preLoad[i].src = imgUrl;
                }
            }
            toggleLoop();
        }
    } else {
        imageref = img;
        imgUrl = rc
        freeImage();
        if (imgUrl != null) {
            imageref.src = imgUrl;
        }
    }

    mc = document.getElementById(caption);
    if (mc) {
        mc.innerHTML = text;
    }
}

function swapRadarImage(rc, img, text, caption) {
    iu = rc
    if (iu.indexOf("severe") > 0 || iu.indexOf("snow") > 0 || iu.indexOf("flight") > 0 ||
        iu.indexOf("high") > 0 || iu.indexOf("low") > 0 || iu.indexOf("map") > 0 ||
        iu.indexOf("uv") > 0 || iu.indexOf("tom") > 0 || iu.indexOf("tod") > 0 ||
        iu.indexOf("ton") > 0) {
        loop = 0;
    } else {
        loop = 1;
    }
    resetRadarLoop(rc, img, text, caption, loop);
}

function startRadar() {
    resetRadarLoop(initRc, initImg, initText, initCaption, 1);
}

function initRadar(rc, img, text, caption) {
    initRc = rc;
    initImg = img;
    initText = text;
    initCaption = caption;
    setTimeout('startRadar()', 1500);
}

function resetWxSearch(wxname) {
    el = document.getElementById(wxname);
    el.term.value = "Enter a city, state or zipcode"
}
