var flash;

var isIE = (navigator.appName.indexOf("Microsoft") != -1);

function pageLoad()
{

 //$("#div_interview").show();
  $("#div_flash").show();
	if(navigator.appName.indexOf("Microsoft") != -1) {
		flash = window.OptimalInterviewPlayer;
	}else {
		flash = window.document.OptimalInterviewPlayer;
	}
}



function $a(text) {
  return document.getElementById(text);
}




//
// Sets duration
var duration;
var time_start;

var question_index = 0;

var playing_what = "question"


function playQuestion(counter)
{
	playing_what = "question";
	$a('div_question').innerHTML = questions[counter];
	$a('div_discussion').innerHTML = (counter < discussions.length) ? discussions[counter] : "";
	$a('div_answer').innerHTML = (counter < answers.length) ? answers[counter] : "";
	if ($a('div_discussion').innerHTML == "") $a('div_discussion').innerHTML = "No discussion available for this question";
	if ($a('div_answer').innerHTML == "") $a('div_answer').innerHTML = "No answer available for this question";
}

function playAnswer(counter)
{
	playing_what = "answer";
	$a('div_question').innerHTML = questions[counter];
	
	if (interview_mode == "write")
	{
		$a('div_written_answer').innerHTML = (counter < answers.length) ? "<b>ANSWER</b>: " + (answers[counter]) : "";
		$a('div_written_answer_outer').style.display = "block";
	}
}

function setDuration(_duration)
{
	clearInterval(progressId);
	duration = _duration;
	$a('div_time').innerHTML = get_time_format("0") + " / " + get_time_format(Math.floor(duration));
	//$a('cell_progress').style.width = "0px";
}

function bufferFlushed()
{
	if (!(playing_what == "answer" && interview_mode == "write"))
	{
		time_start = (new Date).getTime();
		progressId = setInterval(progress, 10);
	}
}

var progressId;

//
// Function returns the seconds in format xx:yy (x - min, y - sec)
function get_time_format(seconds_total)
{
	var minutes = Math.floor(seconds_total / 60);
	var seconds = (seconds_total - minutes * 60);
	
	if (seconds < 10) seconds = "0" + seconds;
	
	return minutes + ":" + seconds;
}

function progress()
{
	try
	{
		time = (new Date).getTime();
		$a('div_time').innerHTML = get_time_format(Math.floor((time -  time_start)/1000)) + " / " + get_time_format(Math.floor(duration));
		//$a('cell_progress').style.width = (Math.round( Math.min((time -  time_start)/1000.0,duration) * 650.0 / duration)) + "px";

		setSliderPosition('img_time_slider', ((time -  time_start) * 100 )/ (duration * 1000));


		if (time - time_start >= duration*1000)
			clearInterval(progressId);
	}
	catch (exception)
	{
				clearInterval(progressId);	
	}
}

//
// Sets time elapsed
function setTimeElapsed(time)
{
	$a('div_time').innerHTML = get_time_format(Math.floor(time)) + " / " + get_time_format(Math.floor(duration));
	//$a('cell_progress').style.width = (Math.round(time * 650 / duration)) + "px";
	setSliderPosition('img_time_slider', (time * 100) / duration);
}

//
// Starts playing
function startPlaying()
{
	flash.startPlaying();
}

//
// Stops playing
function stopPlaying()
{
	clearInterval(progressId);
	flash.stopPlaying();
}


function setWrittenAnswer(question_index)
{
	$a('div_written_answer').innerHTML = (question_index < answers.length) ? "<b>ANSWER</b>: " + (answers[question_index]) : "";
	$a('div_written_answer_outer').style.display = "block";
}


function nextUserAnswer()
{
	if (question_index < questions.length-1)
	{
		question_index++;
		//$('div_written_answer_outer').style.display = 'none';

		if (interview_mode == "write")
		{
			setWrittenAnswer(question_index);
		}
		else
		{
			stopPlaying();
			setSliderPosition('img_time_slider', 0);
			$("#div_time").html("");
			flash.playQuestion(question_index, questions[question_index]);
		}

		$("#cell_recording_play").hide();
		$("#cell_recording_previous").show();
		$("#div_question_label").html(questions[question_index]);
		$("#div_question_count").html((question_index+1) + " of " + questions.length);

		
		$("#cell_recording_previous").css('visibility', 'visible');
		if (question_index == questions.length - 1)
			$("#cell_recording_next").css('visibility', 'hidden');
	} 
	
		
}


function previousUserAnswer()
{
	if (question_index > 0 )
	{
		question_index--;
		$("#cell_recording_next").css('visibility', 'visible');
		//$('div_written_answer_outer').style.display = 'none';

		if (interview_mode == "write")
		{
			setWrittenAnswer(question_index);
		}
		else
		{
			stopPlaying();
			setSliderPosition('img_time_slider', 0);
			$("#div_time").html("");
			flash.playQuestion(question_index, questions[question_index]);
		}

		$("#cell_recording_play").hide();
		$("#cell_recording_previous").show();
		$("#div_question_label").html(questions[question_index]);
		$("#div_question_count").html((question_index+1) + " of " + questions.length);

		

		if (question_index == 0)
			$("#cell_recording_previous").css('visibility', 'hidden');
	}


}


function setSliderPosition(id, percentage)
{
	var node = $("#" + id)[0];
	var parentNode = node.parentNode;
	var parentWidth = parseInt(parentNode.style.width);
	if (percentage == 99 || percentage > 100)
		percentage = 100;
	node.style.left = ((parentWidth * percentage / 100) - 5) + "px";
}


function textToHtml(value)
{
	value = $('<div/>').text(value).html();
	value = value.replace(/\n/g, "<br>");
	return value;
}

function htmlToText(value)
{
	value = $('<div/>').html(value).text();
	value = value.replace(/<br>/ig, "\n");
	return value;
}


function playUserAnswer()
{
	//alert(document.OptimalInterviewRecorder);
	//document.OptimalInterviewRecorder.continueInterview();
	//alert('success');
		
	$("#div_question_label").html(questions[question_index]);
	if (interview_mode == 'write')
	{
		setWrittenAnswer(question_index);
	}
	else
	{
		flash.playQuestion(question_index, questions[question_index]);
	}
	$("#cell_recording_play").hide();
	$("#cell_recording_previous").show();
	$("#cell_recording_previous").css('visibility', 'hidden');
}

function closePreview()
{
	$("#div_dialog_interview").dialog('close');
}


function unloadWebcam()
{
	
}