/* ----------------------------------------------------------------------------------------
File Name: SoundHandling.js

	This file handles all sounds, including recording.
	
	Written By: Idan Doitch
	Date:       17.12.2006

----------------------------------------------------------------------------------------*/
var MPlayer_SoundSrc		= "" //holds the path to next sound to be played
var SoundCounter_next	= 0 //a global variable
sound_El_Array			= new Array ()
soundArray				= new Array () //holds the sound paths (for the Next button -> DMS)
var RecordedArray		= new Array()
var Chosen_row			= 0 //holds the chosen row -> SR1
var global_logic		= ""
var MCI_was_used		= false;//must use MCI before recording first time
var srcAfterRecorded	= false;//Play the original sound after the recorded one, if it hasn't been played yet.
var RECORD_LENGTH		= 0;//for the setInterval and clearInterval functions in the recordings.
var stop				= 1;
var size				= "";
var sound				= "";
var ChosenRowSound		= "";
var NumberOfRecorders   = 0; //Holds the number of sounds for cleaner
/******************************************************************************************/


function StartRecord(El,type){   
    //debugger
	//var	mmc						= document.all.MMControl;
	var obj_IFRAME				= document.frames("ifr_ExerciseInterface")
	var array_paragraphs_in_XML	= new Array();
	var MCI_RECORD_OVERWRITE	= 1;
	var	MCI_RECORD_INSERT		= 0;
		RECORD_LENGTH			= 30000;
	
	DisableSoundButtons(El,true)
	sound = Play_or_Record_or_Stop_Sound(El)
	
	if(sound == "Record")
	{
		obj_IFRAME.document.all.Play.disabled = true;
		
		if(!MCI_was_used)//initialize it once
		{   
			InitQSR()
			MCI_was_used	= true;
		}
		
		Chosen_row	= ChosenRow;
		obj_IFRAME.document.all.item("par_" + Chosen_row).className = "title_show_table";//incase no row is selected, the default is the 1st one.
		
		//DeleteLongFile(Chosen_row)
		
		array_paragraphs_in_XML = oExerciseXml.documentElement.getElementsByTagName("Paragraph")
		NumOfRows				= array_paragraphs_in_XML.length
		//RecordedArray.length	= NumOfRows;// Array for user's recording
		
		//calculate_str_PATH(RecordingPATH)//for legal path
		
		//RecordedArray[Chosen_row] = MPlayer_SoundSrc + Chosen_row + ".wav"
		
		//MPlayer_SoundSrc = RecordedArray[Chosen_row];
		if (Chosen_row>NumberOfRecorders){
		    NumberOfRecorders=Chosen_row;
		}
		
		try
		{
			//mmc.RecordMode	= MCI_RECORD_OVERWRITE;
			//mmc.FileName	= MPlayer_SoundSrc;
	    
			//mmc.command		= "Open";
			//mmc.command		= "Record";
			//QSR.Play(Chosen_row);
			QSR.StartRecord();
		}
		catch(e)
		{
            alert(pleaserunsyschkmsg);    
 			return
		}
		
		stop = window.setTimeout("SaveRecord("+El+","+Chosen_row+")", RECORD_LENGTH);
	}
	
	else //stop record
	{
		window.clearTimeout(stop);
		SaveRecord(El,Chosen_row);
	}


}

function SaveRecord(El,Chosen_row){
    try{
        QSR.SaveRecord(Chosen_row);
    }catch(e){
        alert(pleaserunsyschkmsg);    
        return
    }
    enable_buttons();
    RecordedArray[Chosen_row]=1;

}

function Play_or_Record_or_Stop_Sound(El)
{
	var obj_IFRAME	= document.frames("ifr_ExerciseInterface")
	
	switch(El.State)
	{
		case "Play":
			El.title = stop_play_value;
			El.State = "Stop_Playing";
			if(!BackgroundImage_for_sound_btn)//replaced values
				El.value = stop_play_value;
			else if(BackgroundImage_for_sound_btn)//replaced images
				El.className = "BUTTON_stop_playing";
			return "play";
			break;
			
		case "Record":
			El.title = stop_record_value;
			El.State = "stop_record";
			if(!BackgroundImage_for_sound_btn)//replaced values
				El.value = stop_record_value;
			else if(BackgroundImage_for_sound_btn)//replaced images
				El.className = "BUTTON_stop_record";
			return "Record";
			break;
			
		case "Stop_Playing":
			El.title     = play_value;
			El.State     = "Play";
			if(!BackgroundImage_for_sound_btn)//replaced values
				El.value   = play_value;
			else if(BackgroundImage_for_sound_btn)//replaced images
				El.className = "BUTTON_play";
			sound		 =  "stop_record"
			simpleStopSound();
			break;

		case "stop_record":
			El.title	 = record_value;
			El.State	 = "Record";
			if(!BackgroundImage_for_sound_btn)//replaced values
				El.value = record_value;
			else if(BackgroundImage_for_sound_btn)//replaced images
				El.className = "BUTTON_record";
			return "stop_record";
			
			break;

		case "Next":
			obj_IFRAME.document.all.Play.title = stop_play_value;
			obj_IFRAME.document.all.Play.State = "Stop_Playing";
			if(!BackgroundImage_for_sound_btn)//replaced values
				obj_IFRAME.document.all.Play.value = stop_play_value;
			else if(BackgroundImage_for_sound_btn)//replaced images
				obj_IFRAME.document.all.Play.className = "BUTTON_stop_playing";
			break;
	}
}

function InitQSR(){

    try{
        QSR = new ActiveXObject("QSound.Recorder");
    }catch(e)
    {}

}


/******************************************************************************************
 Function DisableSoundButtons(El,disable)
	Parameters: El:		 The clicked sound button.
				disable: true -> make it disable; false -> make it enabled	

	Description: This function disable all other sound buttons, besides the chosen one.
*****************************************************************************************/
function DisableSoundButtons(El,disable)
{
	var arr_buttons = new Array()
	arr_buttons = document.frames("ifr_ExerciseInterface").document.getElementsByTagName("BUTTON");
	
	if(El)
		var oSRC = El.className;
	for(i=0; i<arr_buttons.length; i++)
	{
		arr_buttons[i].disabled = disable;//false if disable var is false, or true.
		
		if(DisabledSoundImage)
		{
			if(disable)
				arr_buttons[i].className = "BUTTON_" + arr_buttons[i].State + "_disabled";
			else
				arr_buttons[i].className = "BUTTON_" + arr_buttons[i].State;
		}
	}	
	if(disable)
	{
		El.disabled = false;
		
		if(DisabledSoundImage && El)
			El.className = oSRC;
	}
}

function enable_buttons()
{
	var obj_IFRAME = document.frames("ifr_ExerciseInterface").document.all
	arr_soundButtons = new Array()
	
	//enable buttons
	arr_soundButtons = document.frames("ifr_ExerciseInterface").document.getElementsByTagName("BUTTON")
	
	for(i in arr_soundButtons)
	{
		if(arr_soundButtons[i].id != "Clear" && arr_soundButtons[i].id != "NextDMS" && arr_soundButtons[i].id != "Record")
		{
			arr_soundButtons[i].title = play_value;
			arr_soundButtons[i].State = "Play";
			if(!BackgroundImage_for_sound_btn)//replaced values
				arr_soundButtons[i].value = play_value;
			else if(BackgroundImage_for_sound_btn)//replaced images
				arr_soundButtons[i].className = "BUTTON_play";
		}
	}
	
	if(obj_IFRAME.Play)
	{
		obj_IFRAME.Play.title = play_value;
		obj_IFRAME.Play.State = "Play";
		if(!BackgroundImage_for_sound_btn)//replaced values
			obj_IFRAME.Play.value = play_value;
		else if(BackgroundImage_for_sound_btn)//replaced images
			obj_IFRAME.Play.className = "BUTTON_play";
	}
	
	if(obj_IFRAME.PlayB)
	{
		obj_IFRAME.PlayB.title = play_value;
		obj_IFRAME.PlayB.State = "Play";
		if(!BackgroundImage_for_sound_btn)//replaced values
			obj_IFRAME.PlayB.value = play_value;
		else if(BackgroundImage_for_sound_btn)//replaced images
			obj_IFRAME.PlayB.className = "BUTTON_play";
		playB = true;
	}
	else	playB = false;
	
	if(obj_IFRAME.Record)
	{
		obj_IFRAME.Record.title = record_value;
		obj_IFRAME.Record.State = "Record";
		if(!BackgroundImage_for_sound_btn)//replaced values
			obj_IFRAME.Record.value = record_value;
		else if(BackgroundImage_for_sound_btn)//replaced images
			obj_IFRAME.Record.className = "BUTTON_record";
	}
	
	if(obj_IFRAME.NextDMS)
		obj_IFRAME.NextDMS.disabled = false;
		
	if(playB)
		DisableSoundButtons(obj_IFRAME.PlayB,false)
	else
		DisableSoundButtons(obj_IFRAME.Play,false)
}


/******************************************************************************************
 Function PlaySoundMCI(El,logic)
	Parameters: El: The clicked sound button.
				logic: SR1, DMS	

	Description: This function calls MCI_Control(), after a "Play" sound button was clicked.
	This function is being called from the XSL file.
*****************************************************************************************/
function PlaySoundMCI(El,logic)
{
	var obj_IFRAME  = document.frames("ifr_ExerciseInterface")
	MCI_was_used	= true;
	global_logic	= logic //init global variable
	
	DisableSoundButtons(El,true)
	sound = Play_or_Record_or_Stop_Sound(El)
	
	if(sound == "play")
	{
		if(global_logic == "DMS" && obj_IFRAME.document.all.NextDMS)
			obj_IFRAME.document.all.NextDMS.disabled = true;
		if(global_logic == "SR1" && obj_IFRAME.document.all.Record)
		{
			srcAfterRecorded = false;
			obj_IFRAME.document.all.Record.disabled = true;
			
			if(ChosenRowSound == "")//difault: 1st row
				{	
					ChosenRowSound = obj_IFRAME.document.all.par_1.SoundSrc;
					obj_IFRAME.document.all.par_1.className = "title_show_table";
				}
			Chosen_row	= ChosenRow;	
			
		}	
		MCI_Control(logic)
	}
}



/******************************************************************************************
 Function MCI_Control(logic)
	Description: This function controll all the functions that handle the MCI controll.
*****************************************************************************************/
function MCI_Control(logic)
{
	global_logic = logic //init global variable
	
	InitSoundArray();

	InitQSR();
	
	if(global_logic == "DMS")
		calculate_str_PATH(soundArray[SoundCounter_next])
	else if(global_logic == "SR1")
	{   
	    try{
	        var GetRecordedFile=QSR.GetSoundFile(Chosen_row)
		    if(!RecordedArray[Chosen_row] || GetRecordedFile == "") //play the chosen row's model(original) sound.
			    calculate_str_PATH(ChosenRowSound)
		    else							//play the chosen row's recorded sound.
			    calculate_str_PATH(GetRecordedFile)
	    }catch(e){
            alert(pleaserunsyschkmsg);    
            return
        }
	}
	
	PlaySpecial()
}

function calculate_str_PATH(El_SoundSrc)
{
	MPlayer_SoundSrc = El_SoundSrc;
	
	MPlayer_SoundSrc = parent.PathForStreaming(MPlayer_SoundSrc);
	
	if(MPlayer_SoundSrc.substr(0,5) == "file:")
		MPlayer_SoundSrc = MPlayer_SoundSrc.substring(5)
	
	while(MPlayer_SoundSrc.indexOf("\\") != -1) //while there are spaces in the string. 
		MPlayer_SoundSrc = MPlayer_SoundSrc.replace("\\","/") //remove them
}
function PlaySpecial()
{
	document.all.MediaPlayer1.FileName =  MPlayer_SoundSrc
	//alert(MPlayer_SoundSrc);
	var Temp = window.setTimeout("Play_Or_Die();",100);
}


function Play_Or_Die()
{
	document.all.MediaPlayer1.Play();
}

/******************************************************************************************
 Function InitSoundArray()
	Description: This function saves in an array all the paths from the XML to the sounds 
	that can be heard. DMS.
*****************************************************************************************/
function InitSoundArray()
{
	sound_El_Array = oExerciseXml.documentElement.getElementsByTagName("Dictation")
	for(i=0; i < sound_El_Array.length; i++)
		soundArray[i] = sound_El_Array[i].text; //the first sound was played by the play button.
}


/******************************************************************************************
 Function simpleStopSound()

	Description: This function stops the sound, when the user stops the playback or the recording.
*****************************************************************************************/
function simpleStopSound()
{
	var	DEVICE_PLAY		= 526;
	size = 0;	//to stop all loops in "MMControlFunctions.asp"
	
	if(document.all.MediaPlayer1)
		if(document.all.MediaPlayer1.PlayState == 2)//The MediaPlayer1 player is playing the multimedia file.
		{
			document.all.MediaPlayer1.FileName = ""; //Reset file name to release the file
			document.all.MediaPlayer1.Stop();
		}
	
	//if(document.all.MMControl)
	//	document.all.MMControl.command = "Close";//If the MCI player is playing the multimedia file.
	//try{QSR.StopAll();}catch(e){};
	
	if(sound != "stop_record")//reset variale
		SoundCounter_next = 0;
	
	enable_buttons()
}


/******************************************************************************************
 Function StopSound()

	Description: This function stops the sound, when the user moves to another exercise, by
	stoping the MediaPlayer or the MCI Objects. It also resets variables and call DeleteRecordingFolder.
	This function is being called from all XSLs file.
*****************************************************************************************/
function StopSound(logic)//resets other destructors and changes the current destructor's style. 
{
	var	DEVICE_PLAY		= 526;
	size = 0;	//to stop all loops in "MMControlFunctions.asp"
	
	if(document.all.MediaPlayer1)
		if(document.all.MediaPlayer1.PlayState == 2)//The MediaPlayer1 player is playing the multimedia file.
			document.all.MediaPlayer1.Stop()
	
	//if(document.all.MMControl)
	//	document.all.MMControl.command = "Close";//If the MCI player is playing the multimedia file.
	
	if(sound != "stop_record")//reset variale
		SoundCounter_next = 0;
	
	//if(logic) No need for Cleaner... the Activex handels it
	//	DeleteRecordingFolder(logic)
	//else
	//	DeleteRecordingFolder()
	
	//reset global variables
	if(Item_was)
		Item_was = ""; //holds the last clicked item's id.
	if(Item_was_in)
		Item_was_in = ""; //holds the SPAN that the clicked item was in.
	if(replaceCache)
		replaceCache = "";
	if(replaceCache2)
		replaceCache2 = "";
	
	checked		= false;
	firstTime3  = true;
	
	if(logic && (logic == "SR1" || logic == "DMS"))//more resets
	{
		//if(logic == "SR1")
			//window.clearTimeout(stop); //Not working
			
		MPlayer_SoundSrc_DR1 = ""; //holds the path to next sound to be played
		MPlayer_SoundSrc	 = ""; //holds the path to next sound to be played
		SoundCounter_next	 = 0 ;//a global variable
		ChosenRowSound		 = "";
		ChosenRow			 = 1;
		
		if(logic == "DMS")
		{
			if(sound_El_Array)//DMS
				for(i=0; i < sound_El_Array.length; i++)
					sound_El_Array[i] = ""
		}
		
		if(soundArray)
			for(i=0; i < soundArray.length; i++)
				soundArray[i] = ""
		if(RecordedArray)
			for(i=0; i < RecordedArray.length; i++)
				RecordedArray[i] = ""
		
		Chosen_row			= 0; //holds the chosen row -> SR1
	}
	if(logic == "DR1")
		part = "none";
}

function cleaner(){

    try{
        InitQSR();
        QSR.Cleaner(NumberOfRecorders);
    }catch(e){
        //alert(pleaserunsyschkmsg);    
    }
    

}
this.onunload=cleaner;
//alert("blabla");
/******************************************************************************************
 Function PlaySound(El)
	Parameters: El: The clicked sound button.

	Description: This function plays the sound, after a sound button was clicked, by
	changing the bgSound of the body element.
	This function is being called from the XSL file.
	If there is a posibility to change the button's caption:
	Change the Cases in the function, when the caption of the buttons change(like when changing levels).
*****************************************************************************************/
function PlaySound(El)
{
    
	global_logic = "";//so other logics will not be effected from DR1, DMS, SR1
	
	sound = Play_or_Record_or_Stop_Sound(El)
	if(sound == "play")
	{
		//alert(El.SoundSrc)
		document.all.MediaPlayer1.FileName = parent.PathForStreaming(El.SoundSrc);
		//Disable all other sound buttons
		DisableSoundButtons(El,true)
	}	
}


