var eotts_post_url = "http://210.90.46.147/eoneoservices/talk.asp";
var eotts_webcatcher_url = "http://210.90.46.147/eoneoservices/WebCatcher.dll";
var eotts_enabled = true;
var eotts_read_title = true;

var gTimeoutID = -1;
var gCurrentSpeaker = null;
var gCurrentSpeakingObject = null;

function DoSpeakerCommand(o, cmd, args) {
	var sen;
	var obj;
	var arr;

	switch (cmd) {
	case "please load":
		if (gCurrentSpeaker) {
			DoSpeakerCommand(gCurrentSpeaker, "please stop", "");
		}

		if (o.HasNext()) {
			arr = o.Next();
			sen = arr[0]; obj = arr[1];
			
			gCurrentSpeaker = o;
			gCurrentSpeakingObject = obj;

			o.ShowLoading();

			soundsystem.SetVariable("gUrl", o.gUrl);
			soundsystem.GoToFrame(4);
		}

		break;
	case "please stop":
		if (gTimeoutID != -1) {
			clearTimeout(gTimeoutID);
			gTimeoutID = -1;
		}
		gCurrentSpeaker.ShowReady();
		gCurrentSpeaker.Reset();

		soundsystem.GoToFrame(29);

		gCurrentSpeaker = null;
		break;
	case "sound loaded":
		if (gCurrentSpeaker) {
			gCurrentSpeaker.ShowPlaying();
			if (gCurrentSpeaker.IsFirstTime()) {

				soundsystem.GoToFrame(19);

				gCurrentSpeaker.SetFirstTime(false);
			} else {
				if (gTimeoutID != -1) {
					clearTimeout(gTimeoutID);
					gTimeoutID = -1;
				}
				gTimeoutID = setTimeout("soundsystem.GoToFrame(19);", 700);
			}
		}
		break;
	case "restart from":
		gCurrentSpeaker = o;

		gCurrentSpeaker.ShowReady();
		gCurrentSpeaker.Reset();

		soundsystem.GoToFrame(29);

		gCurrentSpeaker.SetCurrentPos(args);

		if (o.HasNext()) {
			arr = o.Next();
			sen = arr[0]; obj = arr[1];

			gCurrentSpeaker = o;
			gCurrentSpeakingObject = obj;

			o.ShowLoading();

			soundsystem.SetVariable("gUrl", o.gUrl);
			soundsystem.GoToFrame(4);
		}
		break;
	case "sound complete":
		if (gCurrentSpeakingObject) {
			if (gCurrentSpeakingObject.tagName == "SPAN" ||
			    gCurrentSpeakingObject.tagName == "TD") {
				document.selection.empty();
			}
		}

		if (gCurrentSpeaker.HasNext()) {
			arr = gCurrentSpeaker.Next();
			sen = arr[0]; obj = arr[1];

			gCurrentSpeakingObject = obj;
			gCurrentSpeaker.ShowLoading();

			soundsystem.SetVariable("gUrl", o.gUrl);
			soundsystem.GoToFrame(4);
		} else {
			if (typeof(gCurrentSpeaker.onComplete) != "undefined" && gCurrentSpeaker.onComplete) {
				gCurrentSpeaker.onComplete(gCurrentSpeaker);
			}
			DoSpeakerCommand(gCurrentSpeaker, "please stop", "");
		}
		break;
	}

	if (gCurrentSpeaker && gCurrentSpeaker.associatedControl != null) {
		gCurrentSpeaker.associatedControl.focus();
	}
}

function ResetSoundSystem() {
	if (gTimeoutID != -1) {
		clearTimeout(gTimeoutID);
		gTimeoutID = -1;
	}

	soundsystem.GoToFrame(29);
	globalspeaker.Reset();

	globalspeaker.ShowReady();
}

function eotts_enable() {
	eotts_enabled = true;
}

function eotts_disable() {
	eotts_enabled = false;
}

function eotts_on_load() {
	soundsystem.SetVariable("gPostUrl", eotts_post_url);

	if (!eotts_read_title || !eotts_enabled)
		return;
		
	var text = document.title;
	var l = text.length;
	if (l <= 0)
		return;
		
	eotts_read_text(text);
}

function eotts_src_index(elt) {
	for (var i=0; i<document.all.length; i++) {
		if (document.all[i] == elt) {
			return i;
		}
	}
	
	return -1;
}

function eotts_prev_focusable(elt) {
	var srcIndex = eotts_src_index(elt);
	if (srcIndex == -1) {
		return null;
	}
	
	// search from first elements to src element
	for (var i=srcIndex - 1; i>=0; i--) {
		if (document.all[i].onfocus) {
			return document.all[i];
		}
	}
	
	// hmm, seems that there are no focusable elements before,
	// return null
	return null;
}

function eotts_next_focusable(elt) {
	var srcIndex = eotts_src_index(elt);
	if (srcIndex == -1) {
		return null;
	}
	
	// search from current elements
	for (var i=srcIndex + 1; i<document.all.length; i++) {
		if (document.all[i].onfocus) {
			return document.all[i];
		}
	}
	
	// hmm, there's no more focusable elements,
	// return null;
	return null;
}

function eotts_read_text(text) {
	if (eotts_enabled) {
		l = text.length;
		if (l <= 0)
			return;

		encodedText = "text=";
		encodedText += document.all.webcatcher.EncodeString(text);

		try {	
			result = document.all.webcatcher.Post("210.90.46.147", "/eoneoservices/talk.asp", 80, encodedText);
		} catch (err) {
			return;
		}

		globalspeaker.CreateFromText(text);
		globalspeaker.gUrl = result.substr(4);
		DoSpeakerCommand(globalspeaker, "please load", "");
	}
}

function eotts_read(event) {
	if (!eotts_enabled)
		return;
	
	var re1 = /^\s+/;
	var re2 = /\s$/;

	tagName = event.srcElement.tagName;
	if (tagName == "A") {
		text = event.srcElement.innerText;
	} else if (tagName == "IMG") {
		text = event.srcElement.alt;
	} else {
		text = event.srcElement.innerText;
	}
	
	text = text.replace(re1, "");
	text = text.replace(re2, "");
	
	if (text.length <= 0) {
		var shifted = event.shiftKey;
		var next_focusable = null;
		
		if (shifted) {
			next_focusable = eotts_prev_focusable(event.srcElement);
		} else {
			next_focusable = eotts_next_focusable(event.srcElement);
		}
		
		if (next_focusable) {
			next_focusable.focus();
		}
	} else {
		eotts_read_text(text);
	}
}

function eotts_add_control(default_enabled, read_title) {
	eotts_enabled = default_enabled;
	eotts_read_title = read_title;

html = "<div id='globalspeaker' style='behavior:url(/tts/speaker.htc); display: none;'></div>";
html += "<object name='webcatcher'";
html += "classid='clsid:C1A56639-4C5B-47DB-9351-FF0C29F854CC'";
html += "codebase='" + eotts_webcatcher_url + "' ";
html += " style='display:none;'>";
html += "</object>";
html += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'";
html += "		codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0'";
html += "		width='0' height='0'";
html += "		id='soundsystem' tabindex='-1'>";
html += "	<param name='movie' value='/tts/sound.swf'>";
html += "	<param name='quality' value='dhigh'>";
html += "	<param name='bgcolor' value='#FFFFFF'>";
html += "</object>";
html += "<script defer>";
html += "	soundsystemfscmd = function(cmd, args) {";
html += "		switch(cmd) {";
html += "		case 'sound loaded':";
html += "			DoSpeakerCommand(null, 'sound loaded', '');";
html += "			break;";
html += "		case 'sound complete':";
html += "			DoSpeakerCommand(null, 'sound complete', '');";
html += "			break;";
html += "		default:";
html += "			break;";
html += "		}";
html += "	};";
html +=	"soundsystem.attachEvent('FSCommand', soundsystemfscmd);";
html += "</script>";

	document.write(html);
}
