charactors = [
				{'sceneID':'245591', 'voiceID':'5', 'languageID':'1', 'engineID':'1'},
				{'sceneID':'245592', 'voiceID':'6', 'languageID':'1', 'engineID':'1'}
			 ];

charactorID=null;
voiceID=null;
languageID=null;
engineID=null;

playAudio = [];
loadedAudio = [];
charactorSpeaking = false;
avatarStatus = 'on';

isLoading = false;

function debug(text)
{
	if (output = document.getElementById('debug'))
	{
		output.innerHTML+=text+"<br />";
	}
}

function say(audio_text,playWhenDone,alreadyQueued)
{
	audio_id = hex_md4(audio_text);

	// Loading text before a previous load has finished seems to abort the previous load
	// Check for this and queue the next load if necessary
	if (!isLoading)
	{
		isLoading = true;
		debug("Loading Text: "+audio_id+" playWhenDone: "+playWhenDone);
		playAudio[audio_id]=playWhenDone;

		// loadText doesn't seem to work if you load the same text more than once
		if (loadedAudio[audio_id])
		{
			debug("Already loaded text: "+audio_id);
			vh_ttsLoaded(audio_text);
		}
		else
		{
			loadText(audio_text,voiceID,languageID,engineID);
		}
	}
	else
	{
		if (!alreadyQueued)
		{
			debug("Queued Text: "+audio_id+" playWhenDone: "+playWhenDone);
		}
		
		window.setTimeout('say(\''+audio_text+'\','+playWhenDone+',true)',1000);
	}
}

function vh_ttsLoaded(audio_text)
{
	isLoading = false;

	audio_id = hex_md4(audio_text);
	debug("Finished loading text: "+audio_id+" (len: "+audio_text.length+")");

	loadedAudio[audio_id]=true;

	if (playAudio[audio_id])
	{
		debug("Playing: "+audio_id);
		sayText(audio_text,voiceID,languageID,engineID);
	}
	else
	{
		debug("Not Playing: "+audio_id);
	}
}

function vh_sceneLoaded()
{
	setPlayerVolume(10);
	
	if (avatarStatus == 'on')
	{
		if (introductionText.length > 40)
		{
			say(introductionText,true);
		}
		
		
		if (bodyText[0])
		{
			say(bodyText[0],false);
		}

	}
}

function changeCharactor()
{
	charactorID = (charactorID == 1) ? 0 : 1;
	setCookie('forus_charactorID',charactorID,null,'/',null,null);

	debug("Switching to charactorID "+charactorID);

	// Have to reload the page as the embed command inserts the html using document.write so it's embeded where the function is called.
	document.location.reload();
}

function setCharactor()
{
	charactorID=getCookie('forus_charactorID');
	if ((charactorID!=0 && charactorID!=1))
	{
		charactorID=Math.round(Math.random()*(charactors.length-1));
		setCookie('forus_charactorID',charactorID,null,'/',null,null);
	}

	voiceID = charactors[charactorID].voiceID;
	languageID = charactors[charactorID].languageID;
	engineID = charactors[charactorID].engineID;
	
	AC_VHost_Embed_16269(130,130,'ffffff',1,1,charactors[charactorID].sceneID,0,0,0,'1e0e5265bb2be6e22f5ee8ff6516a896',6);
}

function speakText()
{
	if (!charactorSpeaking) // Ignore event if already speaking text
	{
		charactorSpeaking = true;
		for (i in bodyText)
		{
			say(bodyText[i],true);
		}
		charactorSpeaking = false;
	}
}

// Set a cookie
function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

// Read value from a cookie
function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}

	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}


	value = unescape(dc.substring(begin + prefix.length, end));
	
	return value;
}

/* Handle Persistent Stylesheet - Based on http://www.alistapart.com/articles/alternate/ */

function setActiveStyleSheet(title)
{
	// Switch back to default if alternate stylesheet is already active
	if (getActiveStyleSheet() == 'basic')
	{
		title='standard';
	}

	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			a.disabled = true;
			if(a.getAttribute("title") == title)
			{
				a.disabled = false;
			}
		}
	}
}

function getActiveStyleSheet()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
		{
			return a.getAttribute("title");
		}
	}
	return null;
}

function getPreferredStyleSheet()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title"))
		{
			return a.getAttribute("title");
		}
	}
	return null;
}

function toggleAvatarMute()
{
	avatarStatus = (avatarStatus == 'on') ? 'off' : 'on';
	setCookie("avatar_status",avatarStatus,null,'/',null,null);

	if (avatarStatus == 'off')
	{
		stopSpeech();
		document.getElementById('avatar_mute_button').className = 'avatar_mute_on';
	}
	else
	{
		document.getElementById('avatar_mute_button').className = 'avatar_mute_off';
		vh_sceneLoaded();
	}
}

window.onunload = function(e)
{
	var title = getActiveStyleSheet();
	setCookie("style", title,null,'/',null,null);
}

window.onload = function(e)
{
	avatarStatus = getCookie("avatar_status");
	
	if (!avatarStatus)
	{
		avatarStatus='on';
	}

	document.getElementById('avatar_mute_button').className = (avatarStatus == 'off') ? 'avatar_mute_on' : 'avatar_mute_off';
}

var cookie = getCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
