VideoPlayer = function()
{
	this.state = "initializing";
	return this;
}


VideoPlayer.prototype.setFile = function(_file)
{
		this.file = _file;
}


VideoPlayer.prototype.init = function(_control, userContext, _rootElement, instance)
{
		this.scope	= _rootElement;
		this.media	= this.scope.findName("media");
		this.statusCopy =  this.scope.findName("statusCopy"); 
		this.initBehaviors();
		
	    try{
			this.media.source = this.file;
			this.state = "playing"; 
		} catch(err){
			 // alert("ouch!")
		}
		
		
}


VideoPlayer.prototype.initBehaviors = function()
{
	
	/*-- 964x270 --*/
	
	var buttons = new Array();
	var button	= null;
	
	buttons.push(this.playBtn  = this.scope.findName("play"));
	buttons.push(this.pauseBtn = this.scope.findName("pause"));
	buttons.push(this.rwdBtn   = this.scope.findName("rewind"));
	
	for (var i in buttons){
		button = buttons[i];
		button.Cursor = "Hand";
		button.addEventListener("MouseEnter", Helper.createDelegate(this, this.toggleOverOut, "over", button));
		button.addEventListener("MouseLeave", Helper.createDelegate(this, this.toggleOverOut, "out", button));
		
	};
	
	this.playBtn.addEventListener("MouseLeftButtonUp", Helper.createDelegate(this, this.togglePlayPause,this.playBtn));
	this.pauseBtn.addEventListener("MouseLeftButtonUp", Helper.createDelegate(this, this.togglePlayPause,this.pauseBtn));
	this.rwdBtn.addEventListener("MouseLeftButtonUp", Helper.createDelegate(this,this.setPlayHead, "rwd"));
	this.media.addEventListener("MediaEnded", Helper.createDelegate(this,this.onMediaEnded));
	this.media.addEventListener("DownloadProgressChanged", this.onProgress);
	this.failToken = this.media.addEventListener("MediaFailed", Helper.createDelegate(this, this.onMediaFailed));
	
}


VideoPlayer.prototype.toggleOverOut = function(state, button)
{
	var endO = null;
	var activeCanvas = (this.state == "playing") ? this.pauseBtn : this.playBtn;
	if(activeCanvas!=button) return;
	trace("activeCanvas: "+activeCanvas.name);
	if(state == "over") endO = 1;
	if(state == "out") endO = .8;
	//TweenLite.to(activeCanvas, .5, {Opacity:endO});
}


VideoPlayer.prototype.togglePlayPause = function()
{

	this.state = (this.state == "playing") ? "paused" : "playing";
	this.playBtn.Opacity = (this.state == "playing") ? 0 : 1;
	this.pauseBtn.Opacity = (this.state == "playing") ? 1 : 0;

	if(this.state == "playing"){
		if(this.mediaEnded){
			this.mediaEnded = false;
			this.media.position = "00:00:00";
		} 
		this.media.Play();
	}else{
		this.media.Pause();	
	}
}


VideoPlayer.prototype.setPlayHead = function (dir)
{
	var pos = (dir=="rwd") ? "00:00:00" : this.formatTime(this.media.NaturalDuration.seconds);
	//this.togglePlayPause(this.playBtn);
    this.media.position = pos;
}

VideoPlayer.prototype.formatTime = function(time) {
	trace("got time: "+time)
	
	var hours, minutes, seconds, hoursStr, minutesStr, secondsStr;
    hours = Number(time) / 3600;
    minutes = Number(time) / 60;
    seconds = Number(time) % 60;

    hoursStr = hours.toString();
    hoursStr = hoursStr.substring(0, hoursStr.indexOf('.'));
    minutesStr = minutes.toString();
    minutesStr = minutesStr.substring(0, minutesStr.indexOf('.'));
    secondsStr = seconds.toString();
    secondsStr = secondsStr.substring(0, secondsStr.indexOf('.'));
    
    if (hoursStr.length==1)
        hoursStr = "0" + hoursStr;
        
    if (minutesStr.length==1)
        minutesStr = "0" + minutesStr;
    
    if (secondsStr.length==1)
        secondsStr = "0" + secondsStr;

    return hoursStr + ":" + minutesStr + ":" + secondsStr;
}


VideoPlayer.prototype.onProgress = function()
{
	// trace("MEDIA PROGRESS!!!");
}   


VideoPlayer.prototype.onMediaEnded = function ()
{
	trace("MEDIA ENDED!!!");
	this.togglePlayPause(this.pauseBtn);
	this.mediaEnded = true;
}

VideoPlayer.prototype.onMediaFailed = function(sender, eventArgs)
{      
	try{  
		this.media.source = this.file;
		this.statusCopy.text = "Reloading video..."   	
		this.state = "playing"; 
		this.media.removeEventListener("MediaFailed", this.failToken);
	} catch(e){ 
		trace("catch error!")                                      
		//this.media.removeEventListener("MediaFailed", this.failToken);
		//this.media.source = this.file; 
		this.statusCopy.text = "Error loading video."
	} 
	
}

