/***********************/
/* Movie Player Object */
/***********************/
// Properties
MoviePlayer.prototype._movies;
Movie.prototype._framesPerSecond;

//Constructors
function MoviePlayer(framesPerSecond) {
	this._movies = new Array();
	this._framesPerSecond = framesPerSecond;
}

// Public Methods
MoviePlayer.prototype.addMovie = function(movieInstance) {
	this._movies.push(movieInstance);
}

MoviePlayer.prototype.loadNextFrames = function() {
	for (var i1 = 0; i1 < this._movies.length; i1++) {
		this._movies[i1].loadNextFrame();
	}
}

/****************/
/* Movie Object */
/****************/
// Properties
Movie.prototype._title;
Movie.prototype._tweens;
Movie.prototype._frameCount;
Movie.prototype._currentFrame;

// Constructors
function Movie(title, frameCount) {
	this._title = title;
	this._frameCount = frameCount;
	this._tweens = new Array();
	this._currentFrame = 0;
}

// Public Methods
Movie.prototype.addTween = function(tweenInstance) {
	/*var tweenEndFrame = tweenInstance._startFrame + tweenInstance._tweenLength;
	
	if (this._frameCount < tweenEndFrame) {
		this._frameCount = tweenEndFrame;
	}*/
	this._tweens.push(tweenInstance);
}

Movie.prototype.loadNextFrame = function() {
	if (this._frameCount > 0) {
		this._currentFrame = (this._currentFrame + 1) % this._frameCount;
		for (var i1 = 0; i1 < this._tweens.length; i1++) {
			this._tweens[i1].loadFrame(this._currentFrame);
		}
	}
}

/****************/
/* Tween Object */
/****************/
// Properties
Tween.prototype._elementID;
Tween.prototype._startFrame;
Tween.prototype._tweenLength;
Tween.prototype._startPosition;
Tween.prototype._endPosition;
Tween.prototype._currentPosition;
Tween.prototype._callback;

// Constructors
function Tween(elementID, startFrame, tweenLength, startPosition, endPosition, callback) {
	//Set the initial size and position from the dom element if not provided
	var domElement = document.getElementById(elementID);
	if (domElement) {
		if (startPosition._x == null) startPosition._x = domElement.offsetLeft;
		if (startPosition._y == null) startPosition._y = domElement.offsetTop;
		if (startPosition._width == null) startPosition._width = domElement.scrollWidth;
		if (startPosition._height == null) startPosition._width = domElement.scrollHeight;
		if (endPosition._x == null) endPosition._x = domElement.offsetLeft;
		if (endPosition._y == null) endPosition._y = domElement.offsetTop;
		if (endPosition._width == null) endPosition._width = domElement.scrollWidth;
		if (endPosition._height == null) endPosition._width = domElement.scrollHeight;
	}
	//Set the Properties
	this._elementID = elementID;
	this._startFrame = startFrame;
	this._tweenLength = tweenLength;
	this._startPosition = startPosition;
	this._endPosition = endPosition;
	this._currentPosition = this._startPosition;
	this._callback = callback;	
}

// Public Methods
Tween.prototype.loadFrame = function(frame) {
	var newPosition = this._getPositionAtFrame(frame);
	this._renderElementAtPosition(newPosition);
	this._currentPosition = newPosition;
	if (this._callback != null && frame == this._startFrame + this._tweenLength) eval(this._callback);
}

// Private Methods
Tween.prototype._getPositionAtFrame = function(frame) {
	if (frame <= this._startFrame) {
		return this._currentPosition;
	} else if (frame >= this._startFrame + this._tweenLength) {
		return this._endPosition;
	} else {
		var currFrame = frame - this._startFrame;
		var newPosition = new TweenPosition(null, null, null, null, null);
		if (this._startPosition._x != null) newPosition._x = this._startPosition._x + (currFrame * ((this._endPosition._x - this._startPosition._x) / this._tweenLength));
		if (this._startPosition._y != null) newPosition._y = this._startPosition._y + (currFrame * ((this._endPosition._y - this._startPosition._y) / this._tweenLength));
		if (this._startPosition._width != null) newPosition._width = this._startPosition._width + (currFrame * ((this._endPosition._width - this._startPosition._width) / this._tweenLength));
		if (this._startPosition._height != null) newPosition._height = this._startPosition._height + (currFrame * ((this._endPosition._height - this._startPosition._height) / this._tweenLength));
		if (this._startPosition._alpha != null) newPosition._alpha = this._startPosition._alpha + (currFrame * ((this._endPosition._alpha - this._startPosition._alpha) / this._tweenLength));
		return newPosition;
	}
}

Tween.prototype._renderElementAtPosition = function(newPosition) {
  var domElement = document.getElementById(this._elementID);
	if (domElement) {
		// Set Position & Size
		if (newPosition._x != null && newPosition._x != this._currentPosition._x) domElement.style.left = newPosition._x + 'px';
		if (newPosition._y != null && newPosition._y != this._currentPosition._y) domElement.style.top = newPosition._y + 'px';
		if (newPosition._width != null && newPosition._width != this._currentPosition._width) domElement.style.width = newPosition._width + 'px';
		if (newPosition._height != null && newPosition._height != this._currentPosition._height) domElement.style.height = newPosition._height + 'px';
		// Set Alpha
		if (newPosition._alpha != null && newPosition._alpha != this._currentPosition._alpha) {
			if (domElement.filters && domElement.filters[0]) {
				if (typeof domElement.filters[0].opacity == "number") { //if IE6+
					domElement.filters[0].opacity = newPosition._alpha;
				} else { //else if IE5.5-
					domElement.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+newPosition._alpha+")";
				}
			} else if (domElement.style.MozOpacity) {
				domElement.style.MozOpacity = newPosition._alpha / 100;
			} else if (domElement.style.KhtmlOpacity) {
				domElement.style.KhtmlOpacity = newPosition._alpha / 100;
			}
		}
	}
}

/*************************/
/* Tween Position Object */
/*************************/
// Properties
TweenPosition.prototype._x;
TweenPosition.prototype._y;
TweenPosition.prototype._width;
TweenPosition.prototype._height;
TweenPosition.prototype._alpha;

// Constructors
function TweenPosition(x, y, width, height, alpha) {
	this._x = x;
	this._y = y;
	this._width = width;
	this._height = height;
	this._alpha = alpha;
}