/**
 * Description goes here.
 * @class   ScrollingCollage
 * @author  Neal Mckinney
 * @date    13.11.2011
 * @version 0.1
**/

function ScrollingCollage(target, startY, exitY, endY) {
	this.target = target;
	this.startY = startY;
	this.exitY = exitY;
	this.endY = endY;
	this.initObjects();
	this.initEvents();
}

ScrollingCollage.prototype.toString = function() {
	return "ScrollingCollage";
}

// INITIALIZATION =======================================================================================================

ScrollingCollage.prototype.initObjects = function() {
	this.hidden = false;
	this.speed = .09;
}

ScrollingCollage.prototype.initEvents = function() {

}

// ACTIONS ==============================================================================================================

ScrollingCollage.prototype.update = function(top) {
	if (top < this.endY) {
		this.show();
		this.animate(top);
	} else {
		this.hide();
	}
	
	if (this.videoSource) {
		if ((top > this.videoEndY || top < this.videoStartY) && this.videoPlaying) {
			this.pauseVideo();
		} else if (top < this.videoEndY && top > this.videoStartY && !this.videoPlaying) {
			this.playVideo();
		}
	}
};

ScrollingCollage.prototype.animate = function(top) {
	var destY;
	if (top > this.exitY) {
		var exitMultiplier = (this.exitY == 0) ? 1000 : this.exitY*2;
		var percent = top/exitMultiplier;
		var easeSpeed = this.speed*(2*percent);
		if (easeSpeed < this.speed) easeSpeed = this.speed;
		//console.log("easeSpeed: "+easeSpeed);
		destY = this.startY - top*easeSpeed;
	} else {
		destY = this.startY - top*this.speed;
	}
	this.target.setStyle("top", destY);
};


ScrollingCollage.prototype.show = function() {
	if (this.hidden) {
		this.hidden = false;
		this.target.setStyle("display", "block");
		//if (this.videoSource) this.playVideo();
	}
};

ScrollingCollage.prototype.hide = function() {
	if (!this.hidden) {
		this.hidden = true;
		this.target.setStyle("display", "none");
		//if (this.videoSource) this.pauseVideo();
	}
};

ScrollingCollage.prototype.addVideo = function(video, canvas, width, height, startY, endY) {
	this.videoSource = video;
	this.videoCanvas = canvas;
	this.videoWidth = width;
	this.videoHeight = height;
	this.videoStartY = startY;
	this.videoEndY = endY;
	this.context = this.videoCanvas.getContext('2d');
	//this.playVideo();
};

ScrollingCollage.prototype.playVideo = function() {
	console.log("playVideo");
	this.videoSource.play();
	if (this.videoTickID) clearInterval(this.videoTickID);
	this.videoTickID = setInterval(this.videoTick.bind(this), 60);
	this.videoPlaying = true;
};

ScrollingCollage.prototype.pauseVideo = function() {
	console.log("pauseVideo");
	this.videoSource.pause();
	if (this.videoTickID) clearInterval(this.videoTickID);
	this.videoPlaying = false;
};

ScrollingCollage.prototype.videoTick = function() {
	this.context.drawImage(this.videoSource, 0, 0, this.videoWidth, this.videoHeight);
};




// EVENTS ===============================================================================================================



