//
// $Id: sheep.js 602 2008-07-09 07:10:28Z kilpatjr $
//

// The time in ms to sleep between position updates.
// This is constant for all sheep for efficiency reasons.  deal.
var sheepSleepTime = 20;

// The initial left starting position of the sheep
BouncingSheep.prototype.startLeft;

// The left coordinate of the sheep following a wraparound
BouncingSheep.prototype.minLeft;

// The left coordinate at which a wraparound occurs.
BouncingSheep.prototype.maxLeft;

// The height of each bounce
BouncingSheep.prototype.bounceHeight;

// The first landing spot in pixels to the right of minLeft
BouncingSheep.prototype.firstLandingLeft;

// The maximum distance of the sheep from the top of the window, i.e. the
// distance of the sheep from the top of the window at crest of the bounce
// in pixels.
BouncingSheep.prototype.maxHeight;

// How far to move the sheep on each update in pixels.  This can be any 
// reasonable non-zero integer
BouncingSheep.prototype.leftIncrement;

// A function to be called when the sheep reaches the end and is ready to
// wrap around the screen.
BouncingSheep.prototype.wrapAction;

//
// BouncingSheep constructor
//
// === Args
// +domId+:: The DOM identifier of the sheep image to move around.
//
function BouncingSheep(domId) {
    this.startLeft = 30;

    this.minLeft = 0;
    this.maxLeft = 250;  // absolute position
    //this.maxLeft = window.innerWidth; // broswer window width

    this.firstLandingLeft = 0;
    this.maxHeight = 50;

    this.leftIncrement = 2;

    this.sheepId = domId;

    this.sheepTop;
    this.sheepLeft = this.startLeft;

    this.setBounces(2);
    this.setBounceHeight(145);

    this.sheepUpdateInterval;
}

//
// Set the number of bounces the sheep takes across the screen
//
BouncingSheep.prototype.setBounces = function(bounces) {
    this.bounces = bounces;
    this.updateBounceWidth();
    this.updateHeightScale();
}

//
// Set the height of the bounces.
//
BouncingSheep.prototype.setBounceHeight = function(bounceHeight) {
    this.bounceHeight = bounceHeight;
    this.updateHeightScale();
}

//
// Recalculate the bounceWidth.
// This should be done automatically when proper setters are used.
//
BouncingSheep.prototype.updateBounceWidth = function()  {
    this.bounceWidth = (this.maxLeft - this.minLeft) / (this.bounces);
}

//
// Recalculate the heightScale.
// This should be done automatically when proper setters are used.
//
BouncingSheep.prototype.updateHeightScale = function() {
    this.updateBounceWidth();
    this.heightScale = (1 / this.bounceHeight) * Math.pow(this.bounceWidth / 2, 2);
}

//
// Execute one frame of movement.
//
BouncingSheep.prototype.move = function() {
    var sheep = document.getElementById(this.sheepId);
    var wrap = false;

    this.sheepLeft += this.leftIncrement;
    if (this.sheepLeft >= this.maxLeft) {
        this.sheepLeft = this.minLeft;
        wrap = true;
    }
    else if (this.sheepLeft < this.minLeft) {
        this.sheepLeft = this.maxLeft;
        wrap = true;
    }
    
    if (wrap && this.wrapAction != null) {
        this.wrapAction(this);
    }

    // calculate the next top position
    var x = ((this.sheepLeft + this.firstLandingLeft) % this.bounceWidth) - (this.bounceWidth / 2);
    this.sheepTop =  (Math.pow(x, 2) / this.heightScale) + this.maxHeight;

    // move the sheep
    sheep.style.top  = this.sheepTop + 'px';
    sheep.style.left = this.sheepLeft + 'px';
}

