var IESound 			= navigator.plugins && document.all; // Checks whether sound is enabled.
var audioEnabled 		= IESound;
var speech_state 		= "notinited"; //Used to maintain the state of the Audion Controls Start, Stop, Pause, Resume, Disabled
var keyboard_enabled	= false; //Used to check whether Keyboard options are enabled.

/*************************************************************************/
// enablePlay(enable_object) function is used to give voice when the user
// mouse overs the audio controls [start,stop.mpause,disable,enable] and
// when the page loads first it speaks as [Press F2 to enable].
// You can modify the voices, which is located in audio folder.
/*************************************************************************/

function enablePlay(enable_object){

}

/*************************************************************************/
// getApplet(this_object, add_text, text_position) function gets the text
// object and put it in the applet frame form and submits that form to reload
// the applet.

// this_object	- this argument holds the object id of the text.
// add_text		- this argument holds the extra text to be added in the text
// text_position- this argument holds the position, where the extra text is
//				  to be added. [-1 adds to prefix, 1 add to suffix]

// This method can be invoked in mouseover, click and load.
// <a onMouseOver="getApplet(this)">Hello</a> - Speaks 'Hello'
// <a onMouseOver="getApplet(this,'Sir',1)">Hello</a> - Speaks 'Hello Sir'
// <a onMouseOver="getApplet(null,'Sir')">Hello</a> - Speaks 'Sir'
// <body onLoad="getApplet(document.all.dynamic_content)"> - Speaks content
//	of dynamic_content.
//  dynamic_content	- may be table id, td id, and other text holding tags
//  Instead of passing 'dynamic_content' you can pass your own id, but that
//  have to be specified in the nativeFrame() function in speech.js
/*************************************************************************/

function getApplet(this_object, add_text, text_position){

	if(parent == null)
		return false;

	if(parent.speech_panel == null)
		return false;

	if(parent.speech_panel.document == null)
		return false;

	if(parent.speech_panel.document.speech_control == null)
		return false;

	parent.speech_panel.document.speech_control.action 		= "/scripts/appletspeech/applets/speech/startspeech.jsp";
	parent.speech_panel.document.speech_control.speech_type.value	= "text";

	if(this_object == null){
		if(add_text != null){
			parent.speech_panel.document.speech_control.speech_text.value	= add_text;
		}
		else{
			parent.speech_panel.document.speech_control.speech_text.value	= "";
		}
	}
	else{

		if(add_text != null){

			if(text_position != null && text_position == -1){
				if(this_object.innerText != null && this_object.innerText != "")
					parent.speech_panel.document.speech_control.speech_text.value	= add_text+",  "+this_object.innerText;
				else if(this_object.title != null && this_object.title != "")
					parent.speech_panel.document.speech_control.speech_text.value	= add_text+",  "+this_object.title;
				else if(this_object.alt != null && this_object.alt != "")
					parent.speech_panel.document.speech_control.speech_text.value	= add_text+",  "+this_object.alt;
			}
			else {
				if(this_object.innerText != null && this_object.innerText != "")
					parent.speech_panel.document.speech_control.speech_text.value	= this_object.innerText+" "+add_text;
				else if(this_object.title != null && this_object.title != "")
					parent.speech_panel.document.speech_control.speech_text.value	= this_object.title+" "+add_text;
				else if(this_object.alt != null && this_object.alt != "")
					parent.speech_panel.document.speech_control.speech_text.value	= this_object.alt+" "+add_text;
			}

		}
		else{
			if(this_object.innerText != null && this_object.innerText != "")
				parent.speech_panel.document.speech_control.speech_text.value	= this_object.innerText;
			else if(this_object.title != null && this_object.title != "")
				parent.speech_panel.document.speech_control.speech_text.value	= this_object.title;
			else if(this_object.alt != null && this_object.alt != "")
				parent.speech_panel.document.speech_control.speech_text.value	= this_object.alt;
		}
	}

	if(text_position == 100){
		//parent.speech_panel.perform(-1);
		speech_state = "started";
		keyboard_enabled	= true;

		parent.speech_panel.document.speech_control.current_downloaded_url.value = parent.content_panel.document.location.href;
		parent.speech_panel.document.speech_control.submit();
	}
	else if(text_position == 101){
		//parent.speech_panel.perform(-1);
		speech_state = "started";
		keyboard_enabled	= true;

		parent.speech_panel.document.speech_control.current_downloaded_url.value = parent.content_panel.document.location.href;
		parent.speech_panel.document.speech_control.submit();
	}
	else if(speech_state == "notinited" || speech_state == "stopped" || speech_state == "nocontent"){
		//parent.speech_panel.perform(-1);
		parent.speech_panel.document.speech_control.current_downloaded_url.value = parent.content_panel.document.location.href;
		parent.speech_panel.document.speech_control.submit();
	}
	else{
		return false;
	}
}

/*************************************************************************/
// sendWithCurrentPage() function is used to initialize and enable voice.
// This is a toggle function in which if frames [applets] are not created
// it submits the file to initialize applet.
// If it is already enabled then this function shows the audio controls.
// The search1.by_pass_url value holds the current URL displayed to
// the client, so that it after submiting it is shown in the second frame.
/*************************************************************************/

function sendWithCurrentPage(){

	if(parent.frames.length > 1){

		var clientX = 5;
		var clientY = 5;

		if(document.all.cursor_position != null){
			clientX = document.all.cursor_position.offsetLeft;
			clientY = document.all.cursor_position.offsetTop;
		}

		if(navigator.appName == "Microsoft Internet Explorer"){
			document.all.control_table.style.visibility = "visible";
			if(clientY != null){
				getApplet(null,"Speech have been enabled , please press F7 key to start speech or press control for help.");
				var open_panel_width 	= 5;
				var open_panel_height	= 5;

				document.all.control_table.style.visibility	= "visible";
				document.all.control_table.style.top 		= open_panel_height;
				document.all.control_table.style.left 		= open_panel_width;

			}
		}
	}
	else if(parent.frames.length == 0){
		document.search1.by_pass_url.value = document.location.href;
		document.search1.target = "_parent";
		document.search1.action	= "/scripts/appletspeech/vision_index.jsp";
		document.search1.submit();
	}
}

/*************************************************************************/
// controlMe() function is used set the audio control X.Y position.
/*************************************************************************/

function controlMe(table_object){

	if(parent.frames.length > 1){
		var open_panel_width 	= 5;
		var open_panel_height	= 5;

		table_object.style.visibility	= "visible";
		table_object.style.top 		= open_panel_height;
		table_object.style.left 	= open_panel_width;

		keyboard_enabled	= true;
	}
}

/*************************************************************************/
// nativeFrame(control_mode) function is used to call its co-frame page
// JavaScript function perform() [located in startspeech.jsp], which in turn
// activates the applet.

// control_mode - 0, starts to speak. Submits the applet frame form
// control_mode - 1, pause/resume. Invokes directly applet's pause  method.
// control_mode - 2, stop speech. Invokes directly applet's stop method.
// control_mode - 3, disables speech. Joins two frame into one frame.
// control_mode - 4, closes audio control.
/*************************************************************************/

function nativeFrame(control_mode){

	if(parent.frames.length > 1){}
	else{return false;}

	document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play.gif";
	document.all.audio_control_stop.src= "/scripts/appletspeech/applets/speech/images/controls/stop.gif";
	document.all.audio_control_pause.src= "/scripts/appletspeech/applets/speech/images/controls/pause.gif";

	if(control_mode == 0){
		if(document.all.dynamic_content != null){
				keyboard_enabled	= true;


			if(speech_state == "paused"){
				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				//getApplet(null,"Speech is in pause mode, Please click it again to start speech.");
				return false;
			}
			else if(speech_state == "started"){
				if(document.all.control_table.style.visibility == "hidden")
					document.all.control_table.style.visibility = "visible";

				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				//getApplet(null,"Speech is already started.");
				//getApplet(document.all.dynamic_content,null,100);
			}
			else{
				speech_state = "started";
				if(document.all.control_table.style.visibility == "hidden")
					document.all.control_table.style.visibility = "visible";

				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				getApplet(document.all.dynamic_content,null,100);
			}
		}
		else if(document.all.reference_object != null){
		keyboard_enabled	= true;

			if(speech_state == "paused"){
				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				//getApplet(null,"Speech is in pause mode, Please click it again to start speech.");
				return false;
			}
			else if(speech_state == "started"){
				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				if(document.all.control_table.style.visibility == "hidden")
					document.all.control_table.style.visibility = "visible";
				//getApplet(document.all.reference_object,null,100);
				//getApplet(null,"Speech is already started.");
			}
			else{
				speech_state = "started";
				if(document.all.control_table.style.visibility == "hidden")
					document.all.control_table.style.visibility = "visible";

				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				getApplet(document.all.reference_object,null,100);
			}
		}
		else if(document.all.music_id != null){
				keyboard_enabled	= true;

			if(speech_state == "paused"){
				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				//getApplet(null,"Speech is in pause mode, Please click it again to start speech.");
				return false;
			}
			else if(speech_state == "started"){
				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				if(document.all.control_table.style.visibility == "hidden")
					document.all.control_table.style.visibility = "visible";

				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				//getApplet(document.all.music_id,null,100);

				//getApplet(null,"Speech is already started.");
				return false;
			}
			else{
				speech_state = "started";
				if(document.all.control_table.style.visibility == "hidden")
					document.all.control_table.style.visibility = "visible";

				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				getApplet(document.all.music_id,null,100);
			}
		}
		else{
			speech_state = "nocontent";
			getApplet(null,"Dynamic contents not found in this page.");
			parent.speech_panel.document.speech_control.speech_type.value	= "file";
			parent.speech_panel.document.speech_control.current_downloaded_url.value = parent.content_panel.document.location.href;
			parent.speech_panel.document.speech_control.submit();
			return false;
		}
	}
	else{
		var action_to_perform	= false;

		if(control_mode == 1){
			if(speech_state == "started"){
				speech_state = "paused";
				document.all.audio_control_pause.src= "/scripts/appletspeech/applets/speech/images/controls/pause_over.gif";
				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				action_to_perform	= true;
			}
			else if(speech_state == "paused"){
				speech_state = "started";
				document.all.audio_control_play.src= "/scripts/appletspeech/applets/speech/images/controls/play_over.gif";
				action_to_perform	= true;
			}
			else{
				getApplet(null,"Speech is not started for this page.");
			}

		}
		else if(control_mode == 2){
			if(speech_state == "started" || speech_state == "paused"){
				action_to_perform	= true;
				document.all.audio_control_stop.src= "/scripts/appletspeech/applets/speech/images/controls/stop_over.gif";
				speech_state = "stopped";
			}
			else if(speech_state == "paused"){
				//getApplet(null,"Speech is in pause mode, Please click it again to start speech");
			}
			else
				getApplet(null,"Speech is not started for this page.");
		}
		else if(control_mode == 3){
			action_to_perform = true;
			speech_state = "notinited";
			getApplet(null,"Speech is currently disabled, to enable it again press F2 key.");
		}
		else if(control_mode == 4){
			nativeFrame(1);
			nativeFrame(2);
			speech_state = "notinited";
			document.all.control_table.style.visibility = "hidden";
			getApplet(null,"Speech is currently stopped, to start press F7 key.");
		}

		if(action_to_perform)
			parent.speech_panel.perform(control_mode);
		else
			return false;
	}
}

/*************************************************************************/
// connectKeyBoard() function is used to listen with keyboard actions so
// that according to its keycode actions are invoked.

// 113 	- enabling the speech [F2]
// 118 	- starts speech [F7]
// 19 	- pauses and resumes speech [Pause/Break]
// 119 	- stops speech [F8]
// 120 	- disables speech [F9]
// 27 	- closes speech control [Esc]
// 17 	- help. [Control]
/*************************************************************************/

function connectKeyBoard(){

	if(window.event.keyCode == 113){
		keyboard_enabled	= true;
		sendWithCurrentPage();//enabling the speech [F2]
	}

	if(keyboard_enabled){
		if(window.event.keyCode == 118){
			nativeFrame(0);//starts speech [F7]
		}
		if(window.event.keyCode == 19){
			nativeFrame(1);//pauses and resumes speech [Pause/Break]
		}
		if(window.event.keyCode == 119){
			nativeFrame(2);//stops speech [F8]
		}
		if(window.event.keyCode == 120){
			keyboard_enabled	= false;
			nativeFrame(3);//disables speech [F9]
		}
		if(window.event.keyCode == 27){
			nativeFrame(4);//closes speech control [Esc]
		}
		if(window.event.keyCode == 17){
			getApplet(null,"Virtual Vision. Keyboard Options. Press F2. to enable speech. Press F7. to start speech. Press F8. to stop speech. Press F9. to disable speech. Press Escape. to Closes speech control. Press Pause and Break. to Pause and Restart speech. Press Control. to Help you.");
		}
	}
	else{
		if(window.event.keyCode == 17){
			getApplet(null,"Virtual Vision. Keyboard Options. Press F2. to enable speech. Press F7. to start speech. Press F8. to stop speech. Press F9. to disable speech. Press Escape. to Closes speech control. Press Pause and Break. to Pause and Restart speech. Press Control. to Help you.");
		}
		else{
			getApplet(null,"Please press F2 key to enable speech. For help press control key.");
		}
		return false;
	}
}

/*************************************************************************/
// resetSpeech() function is used pause and stop the speech if the page is
// submitted or reloaded. This is used to overcome overlapping of Voices.
/*************************************************************************/

function resetSpeech(){

	if(speech_state == "started"){
		nativeFrame(1);
		nativeFrame(2);
	}
}

/*************************************************************************/
// skipError() function is used hide the errors that are all occuring in
// virtual vision.
/*************************************************************************/

function skipError(){
	return true;
}

function makeContentFocus(){
}


/*************************************************************************/
// onerror is captured when ever an error occurs in scripts/appletspeech
// onkeyup is captured when ever the user performs the voice using keyboard.
/*************************************************************************/

window.onerror 			= skipError;
document.onkeyup		= connectKeyBoard;

