//
//this file contains functions to control the playback and position of the embedded quicktime player
//

var timeCheck = "";

function QT(qtEmbedded) {
	this.player = qtEmbedded;
	this.runTimeInSeconds = 0;
}

QT.prototype.play = function() {
	this.player.Play();
}

QT.prototype.stop = function() {
	this.player.Stop();
}


QT.prototype.playSection = function (start,end) {
	this.stop();
	this.player.SetIsLooping(false);
	
	var startFrame = this.timeToFrames(start);
	var endFrame = this.timeToFrames(end);
	
	this.player.SetTime(startFrame);
	this.player.SetStartTime(startFrame); //set quicktime's start and stop points for the loop
	this.player.SetEndTime(endFrame);
	
	this.play(); //play the video

}

QT.prototype.loopSection = function (start,end) {
	this.stop();
	
	var startFrame = this.timeToFrames(start);
	var endFrame = this.timeToFrames(end);
	
	this.player.SetIsLooping(true);
		
	this.player.SetStartTime(startFrame);
	//this.player.SetStartTime(startFrame); //set quicktime's start and stop points for the loop
	this.player.SetEndTime(endFrame);
	

	
	this.play();
}

QT.prototype.defaultPlay = function() {
	this.player.SetIsLooping(false);
	this.player.SetStartTime(0); //set quicktime's start and stop points for the loop
	this.player.SetEndTime(this.player.GetDuration());
	this.play();
}



/*************************************************************************/
/*    time functions for quicktime player 
/*************************************************************************/

//return the movie run time in seconds
QT.prototype.getRunTime = function() {
	if (this.player.GetMaxTimeLoaded != undefined) {	
		this.runTimeInSeconds = (this.player.GetDuration()/this.player.GetTimeScale());
	} else {
		window.setTimeout('getRunTime()',500);
	}
}

QT.prototype.timeToFrames = function(_time) {  //accepts 00:00:00 formatted time
	var timeunits = new Array();
	timeunits = _time.split(':');
	
	var hours = Number(timeunits[0]);
	var minutes = Number(timeunits[1]);
	var seconds = Number(timeunits[2]);
	
	var frames = (((hours*3600) + (minutes*60) + seconds) * this.player.GetTimeScale());
	return frames;

}

//return the current time in the movie
QT.prototype.getCurrentTime = function() {
	var curTime = Math.round(this.player.GetTime() / this.player.GetTimeScale());
	return curTime;
}

QT.prototype.increaseTime = function(_time) {
	//convert to frames
	var frames = this.timeToFrames(_time) * 1; //*1 to convert to a number

	//remove one second
	frames = (frames + (this.player.GetTimeScale()*1));

	//convert to readable time
	var newTime = this.formatTime((frames*1)/this.player.GetTimeScale());
	
	return newTime;
}

QT.prototype.decreaseTime = function(_time) {
	//convert to frames
	var frames = this.timeToFrames(_time) * 1; //*1 to convert to a number
	//remove one second
	frames = (frames - (this.player.GetTimeScale()*1));
	//convert to readable time
	var newTime = this.formatTime((frames*1)/this.player.GetTimeScale());
	
	return newTime;
}

//format the time to hh:mm:ss
QT.prototype.formatTime = function(curTime) {
	//format hours
	if (curTime > 60*60)
	{
		hours = Math.floor(curTime/(60*60));
		curTime = curTime - hours*60*60;
		
		if (hours < 10) {
			hours = "0" + hours;
		}

	}
	else
	{
		hours = "00";
	}

	//format minutes
	if (curTime >= 60) {
		minutes = Math.floor(curTime/60);
		curTime = curTime - minutes*60;
		
		if (minutes < 10) {
			minutes = "0" + minutes;
		}
		
	} else { 
		minutes = "00";
	}
	
	//format seconds
	seconds = Math.floor(curTime%60);
	if (seconds < 10) { 
		seconds = "0" + seconds; 
	}
	
	//prepare new time
	newTime = hours + ":" + minutes + ":" + seconds;
	return newTime;
}




