﻿aImages = new Array();
popArr = new Array();
currentFrame = 0;
isPlaying = false;
hasClicked = false;
theProfile = 0;
dragMin = 45;
dragLen = 300;

function loaderUpdate() {
    loadedSuccess = 0;
    for (i = 0; i < aImages.length; i++) if (aImages[i][1].complete) loadedSuccess++;
    if ((obj = getDivFromName('loader')) != null) {
        theHTML = "";
        if (loadedSuccess != aImages.length) {
            theHTML = "<p align=\"center\">Loading image " + loadedSuccess.toString() + " of " + aImages.length.toString() + "...</p>";
            setTimeout('loaderUpdate()', 1000);
        }
        writeDivHTML(obj, true, true, theHTML);
    }
}

function playOver() {
    if (isPlaying) {
        MM_swapImage('play', '', 'assets/button_stop_m.gif', 1);
    } else {
        MM_swapImage('play', '', 'assets/button_play_m.gif', 1);
    }
}

function playOut() {
    if (isPlaying) {
        MM_swapImage('play', '', 'assets/button_stop.gif', 1);
    } else {
        MM_swapImage('play', '', 'assets/button_play.gif', 1);
    }
}

function playClick() {
    if (isPlaying) {
        clearTimeout(document.webcamPlayTimer);
        isPlaying = false;
        MM_swapImage('play', '', 'assets/button_play_m.gif', 1);
    } else {
        MM_swapImage('play', '', 'assets/button_stop_m.gif', 1);
        webcamPlay();
        isPlaying = true;
        hasClicked = true;
    }
}

function playClick() {
    if (isPlaying) {
        clearTimeout(document.webcamPlayTimer);
        isPlaying = false;
        MM_swapImage('play', '', 'assets/button_play_m.gif', 1);
    } else {
        MM_swapImage('play', '', 'assets/button_stop_m.gif', 1);
        webcamPlay();
        isPlaying = true;
        hasClicked = true;
    }
}

function webcamPlay() {
    nextFrame = currentFrame;
    do {
        nextFrame--;
        if (nextFrame < 0) nextFrame = aImages.length - 1;
        if (aImages[nextFrame][1].complete) {
            currentFrame = nextFrame;
            document.images.webcam.src = aImages[currentFrame][1].src;
            document.populationTimer = setTimeout('populationUpdate()', 100);
            if ((obj = getDivFromName('dragger')) != null) {
                initDivSize(obj); initDivPos(obj);
                moveDivToX = dragLen - Math.floor(dragLen * (currentFrame / (aImages.length - 1))) + dragMin;
                if (moveDivToX < dragMin) moveDivToX = dragMin;
                if (moveDivToX > dragLen + dragMin) moveDivToX = dragLen + dragMin;
                moveDivTo(obj, moveDivToX, 0);
            }
            document.webcamPlayTimer = setTimeout('webcamPlay()', 100);
            return;
        }
    } while (nextFrame != currentFrame)
}

function populationUpdate() {
    clearTimeout(document.populationTimer);

    popNewArr = new Array();
    if (aImages[currentFrame][2] == 1) {
        popNewArr['hawk'] = 10;
        popNewArr['heron'] = 0;
        popNewArr['bat'] = 0;
        popNewArr['vole'] = 40;
        popNewArr['minnow'] = 40;
        popNewArr['frog'] = 30;
        popNewArr['dragonfly'] = 40;
        popNewArr['mosquito'] = 0;
        popNewArr['daphnia'] = 60;
        popNewArr['aquatic'] = 30;
    } else if (aImages[currentFrame][2] == 2) {
        popNewArr['hawk'] = 20;
        popNewArr['heron'] = 20;
        popNewArr['bat'] = 20;
        popNewArr['vole'] = 40;
        popNewArr['minnow'] = 40;
        popNewArr['frog'] = 40;
        popNewArr['dragonfly'] = 30;
        popNewArr['mosquito'] = 20;
        popNewArr['daphnia'] = 40;
        popNewArr['aquatic'] = 50;
    } else if (aImages[currentFrame][2] == 3) {
        popNewArr['hawk'] = 30;
        popNewArr['heron'] = 30;
        popNewArr['bat'] = 40;
        popNewArr['vole'] = 60;
        popNewArr['minnow'] = 60;
        popNewArr['frog'] = 50;
        popNewArr['dragonfly'] = 70;
        popNewArr['mosquito'] = 80;
        popNewArr['daphnia'] = 90;
        popNewArr['aquatic'] = 100;
    } else if (aImages[currentFrame][2] == 4) {
        popNewArr['hawk'] = 30;
        popNewArr['heron'] = 30;
        popNewArr['bat'] = 30;
        popNewArr['vole'] = 50;
        popNewArr['minnow'] = 50;
        popNewArr['frog'] = 40;
        popNewArr['dragonfly'] = 50;
        popNewArr['mosquito'] = 20;
        popNewArr['daphnia'] = 60;
        popNewArr['aquatic'] = 80;
    }

    updateAgain = false;
    for (population in popNewArr) {
        if (isNaN(popArr[population])) popArr[population] = 0;
        if (popArr[population] < popNewArr[population]) {
            popArr[population] += 5;
            if (popArr[population] >= popNewArr[population]) popArr[population] = popNewArr[population]; else updateAgain = true;
        } else if (popArr[population] > popNewArr[population]) {
            popArr[population] -= 5;
            if (popArr[population] <= popNewArr[population]) popArr[population] = popNewArr[population]; else updateAgain = true;
        }
        moveDivToX = (100 - popArr[population]) * -2.2;
        if ((obj = getDivFromName('pop_' + population)) != null) moveDivTo(obj, moveDivToX, 0);
    }
    if (hasClicked == true && theProfile != aImages[currentFrame][2]) {
        if ((theDesc = MM_findObj("profile_" + theProfile)) != null) theDesc.style.display = 'none';
        theProfile = aImages[currentFrame][2];
        if ((theDesc = MM_findObj("profile_" + theProfile)) != null) theDesc.style.display = 'inline';
    }
    if (updateAgain) document.populationTimer = setTimeout('populationUpdate()', 100);
}

function draggerStart(e) {
    //capture mousemove and mouseup events on the page
    if (_dom == 2 || _dom == 1) { //ie
        document.attachEvent("onmousemove", draggerMove);
        document.attachEvent("onmouseup", draggerStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (_dom == 4 || _dom == 3) { //netscape
        document.addEventListener("mousemove", draggerMove, true);
        document.addEventListener("mouseup", draggerStop, true);
        e.stopPropagation();
        e.preventDefault();
    }
    hasClicked = true;
}

function draggerStop(e) {
    //stop capturing mousemove and mouseup events
    if (_dom == 2 || _dom == 1) { //ie
        document.detachEvent("onmousemove", draggerMove);
        document.detachEvent("onmouseup", draggerStop);
    }
    if (_dom == 4 || _dom == 3) { //netscape
        document.removeEventListener("mousemove", draggerMove, true);
        document.removeEventListener("mouseup", draggerStop, true);
    }
}

function draggerMove(e) {
    if (_dom == 2 || _dom == 1) { //ie
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (_dom == 4 || _dom == 3) { //netscape
        e.stopPropagation();
        e.preventDefault();
    }
    if ((obj = getDivFromName('dragger')) != null) {
        initDivSize(obj); initDivPos(obj);
        eventPageX = getLeftFromEvent(e);
        moveDivToX = eventPageX - 12;
        newOffset = 0;
        for (var parent = obj; parent = parent.offsetParent; parent != null) {
            newOffset += parent.offsetLeft;
        }
        moveDivToX -= newOffset;
        if (moveDivToX < dragMin) moveDivToX = dragMin;
        if (moveDivToX > dragLen + dragMin) moveDivToX = dragLen + dragMin;
        moveDivTo(obj, moveDivToX, 0);
    }
    nextFrame = (aImages.length - 1) - Math.floor(((moveDivToX - dragMin) / dragLen) * (aImages.length - 1));
    do {
        if (aImages[nextFrame][1].complete) {
            currentFrame = nextFrame;
            document.images.webcam.src = aImages[currentFrame][1].src;
            document.populationTimer = setTimeout('populationUpdate()', 100);
            return;
        }
        nextFrame--;
        if (nextFrame < 0) nextFrame = aImages.length - 1;
    } while (nextFrame != currentFrame)
}
