//controller.js

var ETAClock = function (e) {
	this.time = false;
	this.stoptime = false;
	this.isGoing = false;
	this.Element = e;
	this.milliElement=isFlip ? this.Element.previousSibling : this.Element.nextSibling;
	
	this.reset = function () {
		if (this.isGoing) {
			this.stop();
			this.time = false;
			this.stoptime = false;
			this.start();
		} else {
			this.time = false;
			this.stoptime = false;
			this.update(true);
		}
	}
	
	this.start = function() {
		if (this.stoptime) {
			var d = new Date();
			var corrected = Date.parse(this.time) + (d - this.stoptime);
			//alert(Date.parse(this.time) + ":" + corrected);
			this.time.setTime(corrected);
			this.stoptime = false;
		} else {
			this.time = new Date();
		}
		this.isGoing = true;
	}
	
	this.stop = function() {
		if (this.isGoing) {
			this.stoptime = new Date();
			this.isGoing = false;
		}
	}

	this.update = function(forceRedraw) {
		if (this.isGoing || forceRedraw) {
			var d = new Date();
			if (!this.time) 
				var intvl = 0;
			else
				var intvl = (d - this.time);

			var ms = intvl % 1000;
			var sec = ((intvl-ms) / 1000) % 60;
			var min = ((intvl-sec*1000) / 60000) % 60;
			ms = ms / 10;
			min = min.toFixed(0).toString().padDigits(2);
			sec = sec.toFixed(0).toString().padDigits(2);
			ms = ms.toFixed(0);
			ms =  (ms % 100).toString().padDigits(2);
			

			if (isFlip) {
				this.Element.innerHTML = (min + ":" + sec).reverse();
				this.milliElement.innerHTML = (ms).reverse();
			} else { 
				this.Element.innerHTML =  min + ":" + sec // + ":" + ms;
				this.milliElement.innerHTML = ms;
			}
		}
	}
}

var PromptController = function () {

	this.skipspeed = 60; //speed at which to roll
	this.rolling=false;
	this.rolldirection=1; //1 is down, -1 is up
	this.paused=false;
	this.actualspeed = speed;  //maintain actual speed regardless of pause/rolling
	this.reachedEnd = false;
	this.clock = null;
	
var pContainer=null;
var pEnd=null;
var pPrompter=null;
var pReference=null;
var pTimecode=null;

	this.init = function() {
		this.updateSpeedLabel();
		this.updateFontSize();
		this.setPlayButton();
		pContainer=$('container');
		pPrompter=$('prompter');
		pReference=$('reference');
		if (isFlip)
			pTimecode=$('timedisplayFlip');
		else
			pTimecode=$('timedisplay');
		this.clock = new ETAClock(pTimecode);
	}

	this.setSpeed = function(s) {
		this.actualspeed=s;
		speed=s;
		this.updateSpeedLabel();
	}
	
	this.setFontSize = function(s) {
		pPrompter.style.fontSize = s;
		pReference.style.fontSize = s;
		this.updateFontSize();
	}
	
	
	
	this.start = function(resume) {
		pEnd=$('endmarker');

		promptState=PROMPT_PLAYING;
		if (!resume)
			this.reset();
		clearInterval(timerID);
		that = this;
		this.setPlayButton();
		this.clock.start();
		timerID=setInterval("that.stepDown()",30);
	}


	this.stop = function() {
		clearInterval(timerID);
		promptState=PROMPT_STOPPED;
		this.setPlayButton();
		this.clock.stop();
		this.waitTime = new Date();
	}
	
	this.reset = function () {
		//var p=$('container');
		this.stop();
		this.clock.reset();
		this.clock.stop();
		lastTop=0;
		pauseCounter=0;
		//p.scrollTop=0;
		pContainer.scrollTop=0;
		this.reachedEnd = 0;
		this.clock.stop();
		this.setPlayButton();
	}

	var lastPos=0;
	
	this.stepDown = function() {

		var tweak=0.67;
		
		if (this.rolling) {
			speed=this.skipspeed * this.rolldirection;
		} else if (this.paused) {
			speed=0;
			tweak=0;
		} else {
			speed=this.actualspeed;
			this.clock.update(pTimecode);
		}

		if (lastTop < -1) lastTop=0;
		lastTop=lastTop + (speed / 3 + tweak);
		pContainer.scrollTop=lastTop;
		
		if (lastPos==pContainer.scrollTop && !this.paused) { 
			this.reachedEnd=true;
			lastTop = pContainer.scrollTop;
			this.stop(); 
		} else {

			lastPos=pContainer.scrollTop;
	 
			var pc=$('pause' + pauseCounter);
			if (pc != null) {
			
			if (pc.offsetTop <= lastTop  )
				this.pause(1000);
			}
		}
		//printConsole(speed / 3 );
		//printConsole(pPrompter.clientHeight + ":" + lastTop + ":" + pContainer.scrollTop + ":" + pContainer.scrollHeight);
		/*if (pEnd.offsetTop <= (lastTop) ) { 
			this.stop();
			
		}*/
	}
	
	this.pause = function(delayMS) {
		that=this;
		pauseCounter++;
		this.paused=true;
		setTimeout('that.resume()',delayMS);
	}
	
	this.resume = function() {
		lastTop=lastTop + 1;
		this.paused=false;
	}
	
	this.setPlayButton = function() {
		var p = $('play');
		if (externControllerWindow) {	
			var p2=externControllerWindow.document.getElementById('play');
			
			if (p2 && promptState==PROMPT_PLAYING) {
				p2.src='images/controller/64/pause.png';
			} else {
				p2.src='images/controller/64/play.png';
			}
		}
		if (promptState==PROMPT_PLAYING) {
			p.src='images/controller/64/pause.png';
		} else {
			p.src='images/controller/64/play.png';
		}
	}

	this.toggle = function () {			
		this.togglePlay();
		this.setPlayButton();
	}
	
	this.togglePlay = function() {
	
		if (promptState==PROMPT_PLAYING)
			this.stop();
		else
			this.start(true);
	}	
	
	this.rollBack = function(enable) {
		this.clock.stop();
		this.start(true);
		this.rolling=enable;
		this.rolldirection=-1;
		this.reachedEnd=false;
	} 

	this.rollFwd = function(enable) {
		this.clock.stop();
		if (!this.reachedEnd) {
			this.start(true);
			this.rolling=enable;
			this.rolldirection=1;
		}
}
	
	this.speedUp = function() {
		if (!this.paused) {
			speed+=1;
			this.actualspeed=speed;
			this.updateSpeedLabel();
		}
	}

	this.speedDown = function() {
		if (!this.paused) {
			speed = (speed>2) ? --speed : 1;
			this.actualspeed=speed;		
			this.updateSpeedLabel();
		}
	}
	
	this.updateSpeedLabel = function() {
		$('currentspeed').innerHTML = speed;
		if (externControllerWindow) {
			externControllerWindow.document.getElementById('currentspeed').innerHTML = speed;
		}
	}

		
	this.updateFontSize = function() {
		fsize = parseInt(getStyle(pPrompter,'fontSize'));
		$('currentfont').innerHTML = fsize;
		if (externControllerWindow) {
			externControllerWindow.document.getElementById('currentfont').innerHTML = fsize;
		}
	}
	
	
	this.fontstop = function() {
		clearInterval(ftimer);
		ftimer=0;
	}
	
	this.fontDown = function() {
		fsize=fsize-1;
		pPrompter.style.fontSize=fsize + 'px';
		pReference.style.fontSize=fsize + 'px';
		this.updateFontSize();
		that = this;
		if (!ftimer) {
			ftimer=setInterval("that.fontDown()",100);
		}
	}
	
	this.fontUp = function() {
		fsize=fsize+1;
		pPrompter.style.fontSize=fsize + 'px';
		pReference.style.fontSize=fsize + 'px';
		this.updateFontSize();
		that = this;
		if (!ftimer) {
			ftimer=setInterval("that.fontUp()",100);
		}
	}		
}