var movieName = "swfile";//ID OF FLASH OBJECT AND NAME OF FLASH EMBED
//var audioPath = "SoundFiles/";//Sound file directory, not being used atm

function thisMovie(movieName) {
  // IE and Netscape refer to the movie object differently.
  // This function returns the appropriate syntax depending on the browser.
  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window[movieName]
  } else {
    return document[movieName]
  }
}

function playmovie() {
  thisMovie(movieName).Play();
}

// Checks if movie is completely loaded.
// Returns true if yes, false if no.
function movieIsLoaded (theMovie) {
  // First make sure the movie's defined.
  if (typeof(theMovie) != "undefined") {
    // If it is, check how much of it is loaded.
    return theMovie.PercentLoaded() == 100;
  } else {
    // If the movie isn't defined, it's not loaded.
    return false;
  }
}

// Call this function to play soundfile
function play(soundfile, audioPath) {
  //alert (audioPath + soundfile);
  if (movieIsLoaded(thisMovie(movieName))) {
    // Set Flash variable with soundfile path + soundfile name
	 thisMovie(movieName).SetVariable("soundfile",audioPath+soundfile);
    //alert ("here" + movieName);
    thisMovie(movieName).Play();
  }
}

// Call this function to play soundfile
function play_multiple(soundfile) {
  //alert (audioPath + soundfile);
  if (movieIsLoaded(thisMovie(movieName))) {
    // Set Flash variable with soundfile path + soundfile name
	 thisMovie(movieName).SetVariable("soundfile",soundfile);
    //alert ("here" + movieName);
    thisMovie(movieName).Play();
  }
}

// Call this function to play soundfile
function play_advanced(soundfile, lineArray, delayFirstSpeaker, speakers) {
  playButtonImgObj = document.getElementById("advanced_play_button");
  playButtonImgObj.style.display = "none";

  playButtonImgOffObj = document.getElementById("advanced_play_button_off");
  playButtonImgOffObj.style.display = "inline";

  practice_multiple(soundfile, lineArray, delayFirstSpeaker, speakers)
}

// Call this function to play soundfile
function practice_multiple(soundfile, lineArray, delayFirstSpeaker, speakers) {
  if (movieIsLoaded(thisMovie(movieName))) {
    // Set Flash variable with soundfile path + soundfile name
	 thisMovie(movieName).SetVariable("soundfile",soundfile);
	 thisMovie(movieName).SetVariable("lineArray",lineArray);
	 thisMovie(movieName).SetVariable("delayFirstSpeaker",delayFirstSpeaker);	 
	 thisMovie(movieName).SetVariable("speakers",speakers);	 	 
    //alert ("here" + movieName);
    thisMovie(movieName).Play();
  }
}



function hideAllDefinitions()
{
	//Grab all the objects with class lineDefinitions
	var ObjectPhraseArray = new Array();
	ObjectPhraseArray = getElementsByClass("lineDefinitions");
	
	for (x in ObjectPhraseArray)
	{
		ObjectPhraseArray[x].innerHTML = "";
	}

	//Grab all the objects with class wordDefinitions
	var ObjectWordsArray = new Array();
	ObjectWordsArray = getElementsByClass("wordDefinitions");
	
	for (x in ObjectWordsArray)
	{
		ObjectWordsArray[x].innerHTML = "";
	}
}


function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


function hideAllDefinitionsDrills()
{
	//Grab all the objects with class wordDefinitions
	var ObjectWordsArray = new Array();
	ObjectWordsArray = getElementsByClass("wordDefinitions");
	
	for (x in ObjectWordsArray)
	{
		ObjectWordsArray[x].innerHTML = "";
	}
}


// Call this function to play soundfile AND display definition
function playWithDefDrills(soundfile, audioPath, definition, spanidnumber)
{
	//Hide all the line definitions
	hideAllDefinitionsDrills();	
	displayLineDef(definition, spanidnumber);		
	play(soundfile, audioPath);	
}


// Call this function to play soundfile AND display definition
function playLineWithDef(soundfile, audioPath, definition, spanidnumber)
{
	//Hide all the line definitions
	hideAllDefinitions();	
	
	displayLineDef(definition, spanidnumber);		
	play(soundfile, audioPath);	
}

function displayLineDef(definition, spanidnumber)
{
	//Grab the 1 definition that is meant to be shown
	var LineDefObj = document.getElementById("span" + spanidnumber);
	LineDefObj.innerHTML = definition;
}

// Call this function to play soundfile AND display definition
function playWithDef(soundfile, audioPath, definition, spanidnumber)
{
	
	//Hide all the line definitions
	hideAllDefinitions();
	
	displayDef(definition, spanidnumber);		
	play(soundfile, audioPath);	
}

function displayDef(definition, spanidnumber)
{
	//Then, Grab the 1 definition that is meant to be shown
	var LineDefObj = document.getElementById("spanWord" + spanidnumber);
	LineDefObj.innerHTML = definition;
}

/*
* This function will not return until (at least)
* the specified number of milliseconds have passed.
* It does a busy-wait loop.
*/
function pause(numberMillis)
{
   var now = new Date();
   var exitTime = now.getTime() + numberMillis;
   while (true)
   {
      now = new Date();
      if (now.getTime() > exitTime)
            return;
   }
}



function playwholeDialogue(soundfile)
{
	play_multiple(soundfile);
	//alert ("soundfile: " + soundfile);
}
// Call this function to play soundfile


function playwholeDialogueAndHide(soundfile,phrasesToHideArray)
{
	var phraseArray = phrasesToHideArray.split("|"); 	
	for(var i= 0;  (i <  phraseArray.length-1); i++)
	{
		var LineDefObj = document.getElementById("span" + phraseArray[i]);
		LineDefObj.innerHTML = "";
	}
	play_multiple(soundfile);
}


function playwholeDialogueOLD(soundfile, audioPath)
{
	var soundFileArray = soundfile.split("|"); 
	var functionCall;
	var timedelay = 0;
	for(var i= 0;  i <  soundFileArray.length; i++)
	{
		// Set Flash variable with soundfile path + soundfile name
		thisMovie(movieName).SetVariable("soundfile",audioPath+soundFileArray[0]);
		var functionCall = "play('" + soundFileArray[i] + "','" + audioPath + "');"
		setTimeout(functionCall, timedelay);
		timedelay = timedelay + 2500;
	}
}


function practicewholeDialogueOLD(soundfile, audioPath, idArray)
{
	//First clear any lines that already happen to be shaded
//NEW: Clear any rows in datagrid
	var phraseidArray = idArray.split("|");
	for(var c=0;  c <  phraseidArray.length; c++)
	{
		speakerContainerClear = "SpeakerSpan" + phraseidArray[c];
		speakerContainer = document.getElementById(speakerContainerClear);
		speakerContainer.style.backgroundColor = 'white';
	}	
	for(var c=0;  c <  phraseidArray.length; c++)
	{
		repeaterContainerClear = "RepeaterSpan" + phraseidArray[c];
		repeaterContainer = document.getElementById(repeaterContainerClear);
		repeaterContainer.style.backgroundColor = 'white';
	}	
//END NEW
	
	var soundFileArray = soundfile.split("|"); 

//NEW
	var speakerObj = document.getElementById("SpeakerSpan" + phraseidArray[0]);
	speakerObj.style.backgroundColor = '#FFD9D8';
	var RepeaterObj = document.getElementById("RepeaterSpan" + phraseidArray[0]);
	RepeaterObj.style.backgroundColor = '#FFD9D8';
//END NEW
	
	//Figure out who starts dialogue
	var newStartDelay;
	var newsoundFileArray = new Array()
	var SpeakerObj1 = document.getElementById("DIVSpeaker1");
	if (SpeakerObj1.className == "speakerButtonSelected")
	{
		//remove 1st, 3rd, etc...
		newStartDelay = true;
		var j=1;
		for(var i=0;  i <  soundFileArray.length; i++)
		{
			newsoundFileArray[i] = soundFileArray[j];
			j = j + 2;
		}
	}
	else
	{
		//remove 2nd, 4th, etc..
		newStartDelay = false;	
		var j=0;
		for(var i= 0;  i <  soundFileArray.length; i++)
		{
			newsoundFileArray[i] = soundFileArray[j];
			j = j + 2;
		}
	}


	if (newStartDelay == true)
	{
		var timedelay = 4000;
		var timedelayLineColours = 4000;		
	}
	else
	{
		var timedelayLineColours = 2000;	
		var timedelay = 0;
	}

	
	for(var i= 0;  i <  newsoundFileArray.length; i++)
	{
		
		// Set Flash variable with soundfile path + soundfile name
		thisMovie(movieName).SetVariable("soundfile",audioPath+soundFileArray[0]);
		var functionCall = "play('" + newsoundFileArray[i] + "','" + audioPath + "');"
		setTimeout(functionCall, timedelay);
		
		timedelay = timedelay + 4000;
	}


	for(var j=0; j < soundFileArray.length ; j++)
	{
		if (j == (soundFileArray.length-1))
		{
//NEW
			SpeakerSpanCurrent = "SpeakerSpan" + parseInt(phraseidArray[j]);
			SpeakerSpanNext = "SpeakerSpan" + parseInt(phraseidArray[j+1]);
			var functionCall7 = "updateLineHighlightLast('" + SpeakerSpanCurrent + "');" 
			setTimeout(functionCall7, timedelayLineColours);

			RepeaterSpanCurrent = "RepeaterSpan" + parseInt(phraseidArray[j]);
			RepeaterSpanNext = "RepeaterSpan" + parseInt(phraseidArray[j+1]);
			var functionCall8 = "updateLineHighlightLast('" + RepeaterSpanCurrent + "');" 
			setTimeout(functionCall8, timedelayLineColours);
//END NEW
		}
		else
		{
//NEW
			SpeakerSpanCurrent = "SpeakerSpan" + parseInt(phraseidArray[j]);
			SpeakerSpanNext = "SpeakerSpan" + parseInt(phraseidArray[j+1]);
			var functionCall8 = "updateLineHighlight('" + SpeakerSpanCurrent + "','" + SpeakerSpanNext + "');"
			setTimeout(functionCall8, timedelayLineColours);

			RepeaterSpanCurrent = "RepeaterSpan" + parseInt(phraseidArray[j]);
			RepeaterSpanNext = "RepeaterSpan" + parseInt(phraseidArray[j+1]);
			var functionCall9 = "updateLineHighlight('" + RepeaterSpanCurrent + "','" + RepeaterSpanNext + "');"
			setTimeout(functionCall9, timedelayLineColours);
//END NEW
		}
		
		timedelayLineColours = timedelayLineColours + 2000;
	}
}



function practicewholeDialogue(soundfile, idArray, emptysoundfile, speakers)
{
	//Turn off the step thru dialogue button
	var StepThruButtonObj = document.getElementById("spanOneLine");
	var StepThruButtonDisabledObj = document.getElementById("OneLineDisabled");

	StepThruButtonObj.style.display = "none";
	StepThruButtonDisabledObj.style.display = "";

	//First clear any lines that already happen to be shaded
	var phraseidArray = idArray.split("|");
	for(var c=0;  c <  phraseidArray.length; c++)
	{
		speakerContainerClear = "SpeakerSpan" + phraseidArray[c];
		speakerContainer = document.getElementById(speakerContainerClear);
		speakerContainer.style.backgroundColor = 'white';
	}	
	for(var c=0;  c <  phraseidArray.length; c++)
	{
		repeaterContainerClear = "RepeaterSpan" + phraseidArray[c];
		repeaterContainer = document.getElementById(repeaterContainerClear);
		repeaterContainer.style.backgroundColor = 'white';
	}	
	
	var speakerObj = document.getElementById("SpeakerSpan" + phraseidArray[0]);
	speakerObj.style.backgroundColor = '#FFD9D8';
	var RepeaterObj = document.getElementById("RepeaterSpan" + phraseidArray[0]);
	RepeaterObj.style.backgroundColor = '#FFD9D8';
	

	//Scroll through the speakers and only keep the ones that are speaking
	//Also, insert an empty speaker to correspond to empty sounds where needed.
	var speakeridArray = speakers.split("|"); 
	var newspeakerString = "";
	var SpeakerObj;
	for(var i=0;  i <  speakeridArray.length; i++)
	{
		SpeakerObj = document.getElementById("DIVSpeaker" + speakeridArray[i]);
		if (SpeakerObj.className == "speakerButtonSelected")
		{
			newspeakerString = newspeakerString + speakeridArray[i] + "|" + "0" + "|";
		}
		else
		{
			newspeakerString = newspeakerString + "0" + "|" + "0" + "|";		
		}
	}

	//Add an empty sound between each 'real' sound to simulate pauses between dialogue lines.
	var soundFileArray = soundfile.split("|"); 
	var newsoundFileString = "";
	for(var i=0;  i <  soundFileArray.length; i++)
	{

		newsoundFileString = newsoundFileString + soundFileArray[i] + "|" + emptysoundfile + "|";	
	}

	var newStartDelay;
	var SpeakerObj1 = document.getElementById("DIVSpeaker" + speakeridArray[0]);	
	
	//alert (newsoundFileString);
	
	newsoundFileString2 = "";
	emptysoundfile2 = "http://www.takeawaylanguages.com/Sounds/Empty/empty.mp3";
	
	if (SpeakerObj1.className == "speakerButtonSelected")
	{
		newStartDelay = false;
		newsoundFileString2 = newsoundFileString;
	}
	else
	{
		newStartDelay = true;
		
		//This is a band-aid but need to remove the first sound file and change to an empty one if delay...
		var band_aid_sound_Array = newsoundFileString.split("|");

		for(var k=0;  k < band_aid_sound_Array.length; k++)
		{
			if (k == 0)
			{
				newsoundFileString2 = emptysoundfile2 + "|";
			}
			else
			{
				newsoundFileString2 = newsoundFileString2 + band_aid_sound_Array[k] + "|";
			}
		}	
	}

	practice_multiple(newsoundFileString, idArray, newStartDelay, newspeakerString);
}




function practiceoneLine(soundfile, audioPath, idArray, speakers)
{
	DivLineCountObj = document.getElementById("DivLineCount");
	currentLine = DivLineCountObj.innerHTML;

	var soundFileArray = soundfile.split("|"); 
	var phraseidArray = idArray.split("|");


//	alert (soundFileArray[0]);


	//Figure out who starts dialogue
	var newStartDelay;
	var newsoundFileArray = new Array()

	var speakeridArray = speakers.split("|"); 

	//Scroll through the speakers and only keep the ones that are speaking
	var newspeakerString = "";
	var SpeakerObj;
	for(var i=0;  i <  speakeridArray.length; i++)
	{
		SpeakerObj = document.getElementById("DIVSpeaker" + speakeridArray[i]);
		if (SpeakerObj.className == "speakerButtonSelected")
		{
			newsoundFileArray[i] = soundFileArray[i];
		}
	}

	thisMovie(movieName).SetVariable("delayFirstSpeaker",false);	 

/*	if (SpeakerObj1.className != "speakerButtonSelected")
	{
		//remove 1st, 3rd, etc...
		newStartDelay = true;
		var j=1;
		for(var i= 0;  i <  soundFileArray.length; i++)
		{
			newsoundFileArray[i] = soundFileArray[j];
			j = j + 2;
		}
	}
	else
	{
		//remove 2nd, 4th, etc..
		newStartDelay = false;	
		var j=0;
		for(var i= 0;  i <  soundFileArray.length; i++)
		{
			newsoundFileArray[i] = soundFileArray[j];
			j = j + 2;
		}
	}*/

	play(newsoundFileArray[currentLine], audioPath);

/*	if (newStartDelay == true)
	{
		if ((currentLine % 2) != 0)
		{
			thisMovie(movieName).SetVariable("delayFirstSpeaker",false);	 
			play(newsoundFileArray[(currentLine-1)/2], audioPath);	
		}
	}
	else
	{
		if ((currentLine % 2) == 0)
		{
			play(newsoundFileArray[currentLine/2], audioPath);	
		}
	}*/

	if (soundFileArray.length > currentLine)
	{
//NEW
		var SpeakerSpanCurrent;
		updateOneLineHighlightDataGrid(phraseidArray[parseInt(currentLine)]);

		if (currentLine > 0)
		{
			var prevlineSpeaker = "SpeakerSpan" + phraseidArray[currentLine-1];
			SpeakerLineObjPrev = document.getElementById(prevlineSpeaker);
			SpeakerLineObjPrev.style.backgroundColor = 'white';
	
			var prevlineRepeater = "RepeaterSpan" + phraseidArray[currentLine-1];
			RepeaterLineObjPrev = document.getElementById(prevlineRepeater);
			RepeaterLineObjPrev.style.backgroundColor = 'white';
		}
//END NEW
		DivLineCountObj.innerHTML = parseInt(currentLine) + 1;
	}
	else
	{
//NEW
		var lastlineSpeaker = "SpeakerSpan" + phraseidArray[parseInt(currentLine-1)];
		SpeakerLineObjCurrent = document.getElementById(lastlineSpeaker);
		SpeakerLineObjCurrent.style.backgroundColor = 'white';

		var lastlineRepeater = "RepeaterSpan" + phraseidArray[parseInt(currentLine-1)];
		RepeaterLineObjCurrent = document.getElementById(lastlineRepeater);
		RepeaterLineObjCurrent.style.backgroundColor = 'white';
//END NEW
		DivLineCountObj.innerHTML = "0";		
	}
}


function updateOneLineHighlightDataGrid(phraseid)
{
	var LineObjCurrent;
	var id = "SpeakerSpan" + phraseid;
	LineObjCurrent = document.getElementById("" + id + "");
	LineObjCurrent.style.backgroundColor = '#FFD9D8';

	var RepeaterLineObjCurrent;
	var id = "RepeaterSpan" + phraseid;
	RepeaterLineObjCurrent = document.getElementById("" + id + "");
	RepeaterLineObjCurrent.style.backgroundColor = '#FFD9D8';
}








function updateLineHighlight(LineSpanCurrentLocal, LineSpanNextLocal)
{
//	alert ("in function");
	var LineObjCurrent;
	var LineObjNext;
	
	//alert ("currentline: " + LineSpanCurrentLocal);
	//alert ("nextline: " + LineSpanNextLocal);

	LineObjCurrentSp = document.getElementById("SpeakerSpan" + LineSpanCurrentLocal);
	LineObjNextSp = document.getElementById("SpeakerSpan" + LineSpanNextLocal);

	LineObjCurrentRp = document.getElementById("RepeaterSpan" + LineSpanCurrentLocal);
	LineObjNextRp = document.getElementById("RepeaterSpan" + LineSpanNextLocal);

	if (typeof(LineSpanCurrentLocal) != "undefined")
	{	
		LineObjCurrentSp.style.backgroundColor = 'white';
	}
	if (typeof(LineSpanNextLocal) != "undefined")
	{
		LineObjNextSp.style.backgroundColor = '#FFD9D8';
	}

	if (typeof(LineSpanCurrentLocal) != "undefined")
	{	
		LineObjCurrentRp.style.backgroundColor = 'white';
	}
	if (typeof(LineSpanNextLocal) != "undefined")
	{
		LineObjNextRp.style.backgroundColor = '#FFD9D8';
	}

		
/*	LineObjCurrent = document.getElementById(LineSpanCurrentLocal);
	LineObjNext = document.getElementById(LineSpanNextLocal);

	LineObjCurrent.style.backgroundColor = 'white';
	LineObjNext.style.backgroundColor = '#FFD9D8';*/
}



function updateLineHighlightLast(LineSpan)
{
	var LineObj;
		
	LineObj = document.getElementById(LineSpan);

	LineObj.style.backgroundColor = 'white';
}
