// our sheepies
var sheep = [
    new BouncingSheep("bouncingSheep"),
];

// things don't work on IE at this point... enable disablementification
var isIE = false;

sheep[0].leftIncrement = 4;
sheep[0].setBounceHeight(34);
sheep[0].maxHeight = 0;
sheep[0].maxLeft = window.innerWidth - 73;
sheep[0].firstLandingLeft = 0;
sheep[0].setBounces(12)
sheep[0].wrapAction = unload;
sheepSleepTime = 20;

if (navigator.platform == "iPhone") {
    sheepSleepTime *= 2;
    for (var s in sheep) {
        sheep[s].leftIncrement *= 2;
    }
} else if (navigator.userAgent.indexOf("Firefox") >= 0) {
    // default set above
} else if (navigator.userAgent.indexOf("Safari") >= 0) {
    sheep[0].setBounceHeight(40);
} else if (navigator.userAgent.indexOf("Opera") >= 0) {
    sheep[0].setBounceHeight(36);
} else if (navigator.userAgent.indexOf("MSIE") >= 0) {
    isIE = true;
}

// interval timer for the sheepies
var sheepTimer;

//
// Move the sheep one frame
//
function updateSheep() {
    for (var s = 0 ; s < sheep.length; s++) {
        sheep[s].move();
    }
}

//
// Function: startTimer()
// Start the interval timer to update the countdown once a second
//
function startTimer() {
    if (!sheepTimer) {
        sheepTimer = setInterval(updateSheep, sheepSleepTime);
    }
}

//
// Function: stopTimer()
// Remove the interval timer
//
function stopTimer()
{    
    if (sheepTimer) {
        clearInterval(sheepTimer);
        sheepTimer = null;
    }
}

//
// Function: load()
// Called by HTML body element's onload event when the widget is ready to start
//
function load()
{
    if (isIE) {
        return "Filthy bastards";
    }

    for (var s = 0 ; s < sheep.length; s++) {
        var domSheep = document.getElementById(sheep[s].sheepId);        
        sheep[s].startLeft = sheep[s].minLeft;
        domSheep.style.visibility = 'visible';
    }
    startTimer();
}

//
// Function: unload()
// Called when the sheep reaches the right side of the screen
//
// === Args
// s:: The BouncingSheep object
//
function unload(s) {
    var domSheep = document.getElementById(s.sheepId);
    domSheep.style.visibility = 'hidden';
    stopTimer();
}