
var captionCounter = 0;  //counter for caption containers, 
var creatingCaption = false;  //false by default. when user 
var theCaptions = new Array();
var timecodes = new Array();
var currSpeaker = null;
var currFontSize = 2;

function playerControl() {
	if ($('controllerButton').value == 'Stop') {
		//stop the video
		theVideo.stop();
		
		//rewrite the button
		$('controllerButton').value = "Play";
	} else {
		//play the video
		theVideo.defaultPlay();
		
		//rewrite the button
		$('controllerButton').value = "Stop";
	}
}

//this function changes the document's title bar with the movie time
function updateTimer() {
	if (theVideo != undefined) {
		document.title = theVideo.formatTime(theVideo.getCurrentTime());
	} else {
		document.title = "video is loading";
	}
}


function preloadCaptions(caption) {
	var transcription_id = caption.transcript_id;
	
	var startTime = caption.start_time;
	var endTime = caption.end_time;
	var style = caption.style;
	var background = caption.background;
	var speaker = caption.speaker_id;
	var caption = caption.text;
	

	
	var i = captionCounter;
	//tempCaption object
	var tempCaption = new publicCaption(transcription_id,i);
	tempCaption.setCaption(caption);
	tempCaption.setStart(startTime);
	tempCaption.setEnd(endTime);
	tempCaption.setBackground(background);
	tempCaption.setSpeaker(speaker);
	tempCaption.setStyle(style);

	theCaptions[i] = tempCaption; //store the captions in an array
	timecodes[startTime] = theCaptions[i]; //store the start timecodes in an array too

	//increment caption container counter 
	captionCounter++;

}

function invertCaptions() {
	var whiteOnBlack = Element.hasClassName($('captions'),'whiteOnBlack');
	
	if (whiteOnBlack) {
		replaceClassName($('captions'),'whiteOnBlack','blackOnWhite');
		replaceClassName($('invertButton'),'player_button_bow','player_button_wob');

	} else {
		replaceClassName($('captions'),'blackOnWhite','whiteOnBlack');
		replaceClassName($('invertButton'),'player_button_wob','player_button_bow');
	}

}

function toggleCaptions() {
	var currentDisplay = $('captions').style.display;
	
	if (currentDisplay == "none") {
		$('captions').style.display = "block";
		$('captionToggleButton').value = "hide captions";
	} else {
		$('captions').style.display = "none";
		$('captionToggleButton').value = "show captions";
	}
}


function updateCaptions() {
	

	var currTime = theVideo.formatTime(theVideo.getCurrentTime());
	
	if (timecodes[currTime] != undefined && timecodes[currTime] != 0) {
		var tempCaption = timecodes[currTime];
		
		$('captions').innerHTML = "";

		if ((currSpeaker != tempCaption.getSpeaker()) && (tempCaption.getSpeaker() != 0)) {
			currSpeaker = tempCaption.getSpeaker();
			$('captions').innerHTML += speakers[currSpeaker] + " - ";
		} else {
			currSpeaker = null;
		}
		
		var newCaption = tempCaption.getCaption();
		newCaption = newCaption.replace(/\r|\n|\r\n/g,"<br>");
		$('captions').innerHTML += newCaption;
		
		//add in background
		if (tempCaption.getBackground() != "") {
			$('captions').innerHTML += "<br>[ " + tempCaption.getBackground() + " ]";
		}
		
		//update the style of the caption
		captionStyle('caption_'+tempCaption.getStyle());
		
		
	}
}

function captionStyle(theStyle) {
	//remove caption_normal, caption_italic, caption_bold
	var captionArea = $('captions');
	
	var availableStyles = new Array('caption_normal','caption_italic','caption_bold');
	
	for(var i=0; i<availableStyles.length; i++) {
		Element.removeClassName(captionArea,availableStyles[i]);
	}
	
	//add current style
	Element.addClassName(captionArea,theStyle);
}

function replaceClassName(element,oldclass, newclass) {
	Element.removeClassName(element,oldclass);
	Element.addClassName(element,newclass);
}


function decreaseText() {
	if (currFontSize > 0) {
		$('captions').removeClassName('captionSize_'+currFontSize);
		
		currFontSize--;
		$('captions').addClassName('captionSize_'+currFontSize);
	}
	
}


function increaseText() {
	if (currFontSize < 4) {
		$('captions').removeClassName('captionSize_'+currFontSize);
		
		currFontSize++;
		$('captions').addClassName('captionSize_'+currFontSize);
	}

}

function player_init() {
	
	Event.observe('oc_container','mouseover',display_controls,false);
	Event.observe('oc_container','mouseout',hide_controls,false);
}

function display_controls() {
	$('video_controls').className = "video_controls_show row";
}

function hide_controls() {
	$('video_controls').className = "video_controls_hide";
}