var flash;
var flashCoaching;	
	
var isIE = (navigator.appName.indexOf("Microsoft") != -1);

function pageLoad()
{
	if(navigator.appName.indexOf("Microsoft") != -1) {
		flash = window.OptimalInterviewPlayer;
		flashCoaching = window.OptimalInterviewCoach;
	}else {
		flash = window.document.OptimalInterviewPlayer;
		flashCoaching = window.document.OptimalInterviewCoach;
	}
	
	/*
	if (!isIE)
		flashCoaching.src = "OptimalInterviewCoaching.swf?qid=" + question_ids[0];
	else
		flashCoaching.childNodes[1].value = "OptimalInterviewCoaching.swf?qid=" + question_ids[0];
	*/	
		
	objects = document.getElementsByTagName("object");
	for (var i = 0; i < objects.length; i++)
	{
		//objects[i].outerHTML = objects[i].outerHTML;
	}
}



function $(text) {
  return document.getElementById(text);
}


//
// Image caching
var images = new Array();
function cache_image(name)
{
	images[name] = new Array();
	images[name]["normal"] = new Image();
	images[name]["normal"].src = "images/preview/" + name + ".png";
	images[name]["hover"] = new Image();
	images[name]["hover"].src = "images/preview/" + name + "_hover.png";
}

function hover(sender, name)
{
	sender.src = images[name]["hover"].src;
}

function unhover(sender, name)
{
	sender.src = images[name]["normal"].src;
}

cache_image("play");
cache_image("stop");
cache_image("previous");
cache_image("next");




//
// Sets duration
var duration;
var time_start;

var question_index = 0;

var playing_what = "question"


function playQuestion(counter)
{
	try
	{
		/*
		if (!isIE)
			flashCoaching.src = "OptimalInterviewCoaching.swf?qid=" + question_ids[counter];
		else
			flashCoaching.childNodes[1].value = "OptimalInterviewCoaching.swf?qid=" + question_ids[counter];
		*/
		
		if ($('dhtml_goodies_id2').style.display == "block")
			flashCoaching.playCoach(question_ids[counter]);
			
		//$('div_flash_coach').style.display = "none";
	}
	catch (ex)
	{
	}
	
	playing_what = "question";
	$('div_question').innerHTML = questions[counter];
	$('div_discussion').innerHTML = (counter < discussions.length) ? discussions[counter] : "";
	$('div_answer').innerHTML = (counter < answers.length) ? answers[counter] : "";
	if ($('div_discussion').innerHTML == "") $('div_discussion').innerHTML = "No discussion available for this question";
	if ($('div_answer').innerHTML == "") $('div_answer').innerHTML = "No answer available for this question";
	//$('div_flash_coach').style.display = "block";
}

function playAnswer(counter)
{
	playing_what = "answer";
	$('div_question').innerHTML = questions[counter];
	
	if (interview_mode == "write")
	{
		$('div_written_answer').innerHTML = (counter < answers.length) ? "<b>ANSWER</b>: " + answers[counter] : "";
		$('div_written_answer_outer').style.display = "block";
	}
}

function setDuration(_duration)
{
	clearInterval(progressId);
	duration = _duration;
	$('div_time').innerHTML = get_time_format("0") + " / " + get_time_format(Math.floor(duration));
	$('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()
{
	time = (new Date).getTime();
	$('div_time').innerHTML = get_time_format(Math.floor((time -  time_start)/1000)) + " / " + get_time_format(Math.floor(duration));
	$('cell_progress').style.width = (Math.round( Math.min((time -  time_start)/1000.0,duration) * 650.0 / duration)) + "px";
	if (time - time_start >= duration*1000)
		clearInterval(progressId);	
}

//
// Sets time elapsed
function setTimeElapsed(time)
{
	$('div_time').innerHTML = get_time_format(Math.floor(time)) + " / " + get_time_format(Math.floor(duration));
	$('cell_progress').style.width = (Math.round(time * 650 / duration)) + "px";
}

//
// Starts playing
function startPlaying()
{
	flash.startPlaying();
}

//
// Stops playing
function stopPlaying()
{
	clearInterval(progressId);
	flash.stopPlaying();
}

//
// Shows hint: discussion & answer
function showHint()
{
	$('dhtml_goodies_id1').style.display='block';
}


function hide_coach()
{
	flashCoaching.stopCoach();
}

function showCoaching()
{
	$('dhtml_goodies_id2').style.display='block';
	setTimeout('playCoachingTimeout()', 500);
}

function playCoachingTimeout()
{
	try
	{
		flashCoaching.playCoach(question_ids[question_index]);
	}
	catch (ex)
	{
	}

}

function onUnloaded() {
window.onunload = null;
}