//JavaScript Animation object copyright 2008, Joshua Wait

function Animation() {
	this;
}

//Parent element to the image frame
Animation.prototype._elmId;

Animation.prototype.setElmId = function (elmId) {
	this._elmId = elmId;
}

Animation.prototype.getElmId = function () {
	return this._elmId;
}

//Image
Animation.prototype._imgId;

Animation.prototype.setImgId = function (imgId) {
	this._imgId = imgId;
}

Animation.prototype.getImgId = function () {
	return this._imgId;
}

//Hortizontal distance to travel in one frame
Animation.prototype._xDistance; 

Animation.prototype.setXDistance = function (xDistance) {
	this._xDistance = xDistance;
}

Animation.prototype.getXDistance = function () {
	
	if (this._xDistance) {
		return this._xDistance;	
	} else {
		return 1;
	}

}

//Vertical distance to travel in one frame
Animation.prototype._yDistance; 

Animation.prototype.setYDistance = function (yDistance) {
	this._yDistance = yDistance;
}

Animation.prototype.getYDistance = function () {
	
	if (this._yDistance) {
		return this._yDistance;	
	} else {
		return 1;
	}

}

//Horizontal direction
Animation.prototype._xDirection; 

Animation.prototype.setXDirection = function (xDirection) {
	this._xDirection = xDirection;
}

Animation.prototype.getXDirection = function () {
	
	if (this._xDirection) {
		return this._xDirection;	
	} else {
		return 1;
	}

}

//Vertical direction
Animation.prototype._yDirection; 

Animation.prototype.setYDirection = function (yDirection) {
	this._yDirection = yDirection;
}

Animation.prototype.getYDirection = function () {
	
	if (this._yDirection) {
		return this._yDirection;	
	} else {
		return 1;
	}

}

//Left limit
Animation.prototype._leftLimit; 

Animation.prototype.setLeftLimit = function (leftLimit) {
	this._leftLimit = leftLimit;
}

Animation.prototype.getLeftLimit = function () {
	
	if (this._leftLimit) {
		return this._leftLimit;	
	} else {
		return 0;
	}

}

//Right limit
Animation.prototype._rightLimit; 

Animation.prototype.setRightLimit = function (rightLimit) {
	this._rightLimit = rightLimit;
}

Animation.prototype.getRightLimit = function () {
	
	if (this._rightLimit) {
		return this._rightLimit;	
	} else {
		var docWidth = document.viewport.getWidth()
		return docWidth;
	}

}

//Set top limit
Animation.prototype._yCeiling; 

Animation.prototype.setYCeiling = function (yCeiling) {
	this._yCeiling = yCeiling;
}

Animation.prototype.getYCeiling = function () {
	
	if (this._yCeiling) {
		return this._yCeiling;	
	} else {
		return 0;
	}

}

//Set bottom limit
Animation.prototype._yFloor; 

Animation.prototype.setYFloor = function (yFloor) {
	this._yFloor = yFloor;
}

Animation.prototype.getYFloor = function () {
	
	if (this._yFloor) {
		return this._yFloor;	
	} else {
		var docHeight = document.viewport.getWidth()
		return docHeight;
	}

}

//Set bottom limit
Animation.prototype._frameRate; 

Animation.prototype.setFrameRate = function (frameRate) {
	this._frameRate = frameRate;
}

Animation.prototype.getFrameRate = function () {
	
	if (this._frameRate) {
		return this._frameRate;	
	} else {
		return 50;
	}

}

//Frame number
Animation.prototype._frameNumber;

Animation.prototype.setFrameNumber = function (frameNumber) {

	this._frameNumber = frameNumber;
	
}

Animation.prototype.getFrameNumber = function () {
	if (this._frameNumber) {
		return this._frameNumber;
	} else {
		this._frameNumber = 1;
		return 1;
	}
}

//Images for the frames
Animation.prototype._frames;

Animation.prototype.setFrames = function (frames) {
	this._frames = frames;
}

Animation.prototype.getFrames = function () {
	return this._frames;	
}

//Image preloader
Animation.prototype.preloadImages = function (imgURLs) {

    if (document.images) {
		var imgObj 	= new Image();
		
		for (var i = 0; i <= imgURLs.length; i++) {
			imgObj.src = imgURLs[i];       
		}
    }

}

Animation.prototype.animate = function () {

	//Get parameters
	var elmId 			= this.getElmId();
	var imgId	  		= this.getImgId();
	var xDistance 		= this.getXDistance();
	var yDistance 		= this.getYDistance();
	var xDirection		= this.getXDirection();
	var yDirection		= this.getYDirection();
	var leftLimit 		= this.getLeftLimit();	
	var rightLimit 		= this.getRightLimit();
	var yCeiling  		= this.getYCeiling();
	var yFloor	  		= this.getYFloor();
	var frameRate		= this.getFrameRate();

	//Current data for object
	var elm 			= $(elmId); // Parent to image element
	var imgElm			= $(imgId); //Image for frame

	//The element doesn't exist
	if (!elm) {
		return;
	}

	//Element position
	var xPos			= parseInt(elm.getStyle('left'));
	var yPos			= parseInt(elm.getStyle('top'));
	
	//Element dimensions
	var elmWidth		= parseInt(elm.getWidth());
	var elmHeight		= parseInt(elm.getHeight());	
	var elmBorder		= parseInt(elm.getStyle('border-width-top')) ? parseInt(elm.getStyle('border-width-top')) : 2;
	var elmHorzEdge		= elmWidth + elmBorder;
	var elmVertEdge		= elmHeight + elmBorder;
	
	//Check for random motion
	var randNum			= Math.round(Math.random() * 10);
	var xRand			= xDirection.toString();
	var yRand			= yDirection.toString();

	if (xRand == 'random') {
		xDirection			= randNum % 2 ? -1 : 1;
	}
	
	if (yRand == 'random') {
		yDirection			= randNum % 2 ? -1 : 1;
	}

	//Limits must occur after the random section above
	//to assure we end up a number instead of the string 'random'
	var leftLimit		= leftLimit + (elmHorzEdge * xDirection);
	var rightLimit		= rightLimit + (elmVertEdge * yDirection);
	
	//Set position
	var newXPos 		= xPos + (xDistance * xDirection);
	var newYPos			= yPos + (yDistance * yDirection);

	/* ---------
	Action
	------------*/

	//Image has hit our left limit
	if (newXPos < leftLimit) {
		this.stop(elm);
		return;	
	}

	//Image has hit our right limit
	if (newXPos > rightLimit) {
		this.stop(elm);
		return;	
	}

	//Image has hit our bottom limit
	if (newYPos > yFloor) {
		this.stop(elm);
		return;	
	}

	//Image has hit our top limit
	if (newYPos < yCeiling) {
		this.stop(elm);
		return;	
	}

	/* ---------
	Frames 
	------------*/

	// Get frame number		
	var frameNum	= this.getFrameNumber();
	var frames		= this.getFrames();
	
	//Update the source image
	imgElm.src 		= frames[frameNum];
	var numFrames	= frames.length - 1;
	
	//Update Frame
	if (frameNum == numFrames) {
		frameNum = 0;
	} else {
		frameNum++;	
	}
	
	this.setFrameNumber(frameNum);

	/* ---------
    Position 
    ------------*/

    //Set new horizontal position
    elm.style.left = newXPos + 'px';

    //Set new vertical position
    elm.style.top = newYPos + 'px';	

	//$('stats').innerHTML = 'newXPos: ' + newXPos + ' leftLimit: ' + leftLimit + ' newYPos: ' + newYPos + ' rightLimit: ' + rightLimit + ' border: ' + elmBorder + ' yDistance: ' + yDistance + ' frameNum: ' + frameNum;
	
	//Recursively call our function
	if (!this.interval) {
		this.interval = setInterval(this.animate.bind(this), frameRate);	
	}

}

Animation.prototype.stop = function (elm) {
	elm.fade();
}

var Butterfly = {};

//Mover
Butterfly.animate = function () {

	//Parameters	
	var id 				= 'flutterBy';
	var imgId			= 'butterFly';
	var xDistance		= 3;
	var yDistance		= 3;
	var leftLimit		= 0;
	var rightLimit		= 800; //Keeps image from going over right side limit
	var yCeiling		= -22;
	var yFloor			= 129;
	var frameRate		= 70;
	var xDirection		= -1;
	var yDirection		= 'random';

	//Get parameters
	var	animator	= new Animation();
	animator.setElmId(id);
	animator.setImgId(imgId);
	animator.setXDistance(xDistance);
	animator.setYDistance(yDistance);
	animator.setXDirection(xDirection);
	animator.setYDirection(yDirection);	
	animator.setLeftLimit(leftLimit);	
	animator.setRightLimit(rightLimit);
	animator.setYCeiling(yCeiling);
	animator.setYFloor(yFloor);
	animator.setFrameRate(frameRate);

	var imgPath	= 'http://www.bluerivers.org/images/';
     	
   	// Set image urls
   	var imgURLs = new Array();
   	imgURLs[0] = imgPath + 'Flutterby_01.png';
   	imgURLs[1] = imgPath + 'Flutterby_02.png';
   	imgURLs[2] = imgPath + 'Flutterby_03.png';

	animator.preloadImages(imgURLs);
	animator.setFrames(imgURLs);
	animator.animate();
	
}

