var AlphaVideo = function(videoContainer,width,height,placeHolder,sourceArray,isAlpha,autoPlay,playOnPageEntry,pageObject,loop){
	this.isAlphaVideo = isAlpha;
	this.autoPlay = autoPlay;
	this.playOnPageEntry = playOnPageEntry;
	this.pageObject = pageObject;
	this.loop = loop;
	this.name = "AlphaVideo";
	this.width = width;
	this.height = height;
	this.placeHolder = placeHolder;
	this.interval = null;
	this.paused = false;
	this.events = Array();
	this.container = videoContainer.image;
	if ((this.placeHolder != "") && (this.placeHolder != null)){
		this.placeHolderImage = new Image();
		this.placeHolderImage.src = this.placeHolder;
		this.placeHolderImage.style.width = this.width+"px";
		this.placeHolderImage.style.height = this.height+"px";
		this.placeHolderImage.style.position = "absolute";
		this.placeHolderImage.parent = this;
		this.placeHolderImage.onclick = function(){
			this.parent.play();
		}
		this.container.appendChild(this.placeHolderImage);
	}
	
	this.video = document.createElement("video");
	this.video.parent = this;
	this.container.appendChild(this.video);
	if (this.autoPlay){
		this.video.setAttribute("autoplay","autoplay");
	}
	this.video.setAttribute("class","alphaVideoElement");
	this.video.setAttribute("width",width);
	if (this.isAlphaVideo){
		this.video.setAttribute("height",height * 2);
	}else{
		this.video.setAttribute("height",height);
	}
	if (this.playOnPageEntry){
		this.pageObject.appendVideo(this);
	}
	for (sourceIndex in sourceArray){
		var sourceElement = document.createElement("source");
		var sourceItem = sourceArray[sourceIndex];
		if (!(sourceItem.codecs)){sourceItem.codecs = ""};
		sourceElement.src = sourceItem.name;
		var type = "";
		type+=sourceItem.type+";";
		if (sourceItem.codecs != ""){
			type+=" codecs='"+sourceItem.codecs+"';";
		}
		sourceElement.type = type;
		this.video.appendChild(sourceElement);
	}
	if (this.autoPlay){
		this.video.play();
	}
	if (this.isAlphaVideo){
		this.bufferCanvas = document.createElement("canvas");
		this.bufferCanvas.setAttribute("class","alphaVideoBuffer");
		this.bufferCanvas.setAttribute("width",width);
		this.bufferCanvas.setAttribute("height",height * 2);
		
		this.outputCanvas = document.createElement("canvas");
		this.outputCanvas.setAttribute("class","alphaVideoOutput");
		this.outputCanvas.setAttribute("width",width);
		this.outputCanvas.setAttribute("height",height);
		
		this.container.appendChild(this.bufferCanvas);
		this.container.appendChild(this.outputCanvas);
		
		this.buffer = this.bufferCanvas.getContext('2d');
		this.output = this.outputCanvas.getContext('2d');
	}else{
		this.video.setAttribute("controls","controls");
		this.video.style.display = "block";
	}
	this.processFrame = function(){
		this.buffer.drawImage(this.video, 0, 0);
		var	image = this.buffer.getImageData(0, 0, this.width, this.height);
		var alphaData = this.buffer.getImageData(0, this.height, this.width, this.height).data;
		var len = image.data.length;
		for (var i = 3;i < len; i = i + 4) {
			image.data[i] = alphaData[i-1];
		}
		this.output.putImageData(image, 0, 0, 0, 0, this.width, this.height);
	}
	this.play = function(){
		if (this.placeHolderImage){
			//this.container.removeChild(this.placeHolderImage);
			this.placeHolderImage.style.display = "none";
		}
		this.paused = false;
		this.video.play();
		if (this.isAlphaVideo){
			clearInterval(this.interval);
			var object = this;
			this.interval = setInterval(function(){
			object.processFrame();
		}, 40);
		}
	}
	if (this.autoPlay){
		this.play();
	}
	this.pause = function(){
		this.paused = true;
		this.video.pause();
		clearInterval(this.interval);
	}
	this.stop = function(){
		this.paused = true;
		this.video.pause();
	}
	this.video.onclick = function(){
		if (this.parent.paused){
			this.parent.play();
		}else{
			this.parent.pause();
		}
	}
	this.attachPlayPauseButton = function(buttonId){
		var playButton = document.getElementById(buttonId);
		playButton.parent = this;
		playButton.onclick = function(){
			if (this.parent.paused){
				this.parent.play();
			}else{
				this.parent.pause();
			}
		}
	}
	this.video.addEventListener('ended', function(){
		this.parent.resetTimedEvents();
		if (this.parent.loop){
			this.parent.frame = 0;
			for (index in this.parent.events){
				this.parent.events[index].actioned = false;
			}
			this.parent.video.play();
		}else{
			this.parent.placeHolderImage.style.display = "block";
		}
	}, false);
	this.resetTimedEvents = function(){
		for (index in this.events){
			this.events[index].actioned = false;
		}
	}
	this.appendTimedEvent = function(time,object,functionName){
		this.events[this.events.length] = {
			time:time,
			object:object,
			functionName:functionName,
			actioned:false
		}
	}
	this.video.addEventListener('seeked', function(){
		this.parent.resetTimedEvents();
		this.parent.play();
	});
	
	this.lastTime = 0;
	this.video.addEventListener('timeupdate', function(){
		if (this.parent.lastTime > this.currentTime){
			this.parent.resetTimedEvents();
		}
		for (index in this.parent.events){
			var event = this.parent.events[index];
			if (event.actioned == false){
				if (this.currentTime > event.time){
					event.actioned = true;
					var object = event.object;
					var execString = "object."+event.functionName+"();";
					eval (execString);
				}
			}
		}
		this.parent.lastTime = this.currentTime;
	});
	return this;
}
