// ------------------------- API INTIATION AND SERVICE
mouseMode=1;
var vhssObj;
var hostPath=null;
var vhostLoaded=false;
var vhostSpeaking=false;

var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

var hostPath = '/slide/curPuppet/';

if(InternetExplorer){
	sep1 = sep2 = ".";
}else{
	sep1 = ":";
	sep2 = "/";
}

function getHostPath(){
	if(InternetExplorer){
		mc = VHSS;
		hostPath = mc.GetVariable(hostPath);
		curHost = hostPath + sep2 + "host" + sep2 + "engine";
	}else{
		mc = document.VHSS;
		hostPath = mc.TGetProperty(hostPath,11);
		curHost = hostPath + sep2 + "host" + sep2 + "engine";
	}
}
// -------------------------

// ------------------------- EVENTS functions
// FSCommand messages
function VHSS_DoFSCommand(eventName, args) {
	var VHSSObj = InternetExplorer ? VHSS : document.VHSS;
	if (eventName=="onSlideBegin") {
		// playing scene index = sceneNumber
		//the scene begins playing, add actions here
		vhostLoaded=true;
	}
	else if (eventName=="hostLoaded") {
		//Host loaded, add actions here
		//path to sender = args
	}
	else if (eventName=="talkStarted") {
		//path to sender = args
		//talk begins, add actions here
	}
	else if (eventName=="talkEnded") {
		//path to sender = args
		//talk ended, add actions here
		vhostSpeaking=false;
	}
	else if (eventName=="audioLoaded"){
		//loaded audio track name =  args
		//loaded audio is ready
	}
	else if (eventName=="ttsLoaded"){
		//loaded text to sppech audio =  args
		//loaded text to speech is ready
	}
}

// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
	  navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub VHSS_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call VHSS_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}

function showTranscript(acc_id,ss_id,audioID,slidenum){
	transWindow = window.open( "http://vhss.oddcast.com/transcript_frame.php?acc="+acc_id+"&ss="+ss_id+"&au="+audioID+"&sn="+slidenum,"transWindow","width=500,height=500,toolbar=no,resizable=no");
	transWindow.focus();
}
// -------------------------

// ------------------------- Host Speech and Facial API FUNCTIONS
function stopSpeech(){
	getHostPath();
	mc.TCallLabel(curHost, "stopSpeech");
}

function replaySpeech(){
	getHostPath();
    mc.TCallLabel(curHost, "replaySpeech");
}

//function variables:
//newMouseMode: 0 - turns mouse tracking OFF; 1 - turns mouse tracking ON
function followcursor(newMouseMode){
	getHostPath();
	mouseModePath= hostPath + sep2 + "host" + sep1 + "mouseMode" ;
	mc.SetVariable(mouseModePath,newMouseMode);
	mc.TCallLabel(curHost, "followcursor");
}

function recenter(){
	getHostPath();
	mc.TCallLabel(curHost, "recenter");
}

function setGaze(deg, dur, rad){
	getHostPath();
	tmp= hostPath + sep2 + "host" + sep1 + "lookDeg";
	mc.SetVariable(tmp, deg);
	tmp= hostPath + sep2 + "host" + sep1 + "lookRad";
	mc.SetVariable(tmp, rad);
	tmp= hostPath + sep2 + "host" + sep1 + "lookDur";
	mc.SetVariable(tmp, dur);
	mc.TCallLabel(curHost, "setLook");
}

function freezeToggle(){
   vhssObj = InternetExplorer ? VHSS : window.document.VHSS;
   vhssObj.TCallLabel("/", "freezeToggle");
}

function sayText(ttsText,voice,lang,engine,noInterrupt){
	vhostSpeaking=true;
	getHostPath();
	mc.SetVariable("tts.lang",lang);
	mc.SetVariable("tts.voice",voice);
	mc.SetVariable("tts.text",ttsText);
	mc.SetVariable("tts.engine",engine);
	mc.TCallLabel("/tts", "sayTTS");
}

function sayAudio(audioName,noInterrupt){
	getHostPath();
	mc.SetVariable("sayMC.audioName",audioName);
	mc.TCallLabel("/sayMC", "sayByName");
}

function loadText(ttsText,voice,lang,engine){
	getHostPath();
	mc.SetVariable("tts.cacheIt",1);
	sayText(ttsText,voice,lang,engine);
}

function loadAudio(audioName){
	getHostPath();
	mc.SetVariable("sayMC.cacheIt", 1);
	sayAudio(audioName);
}

// ------------------------- Flow API FUNCTION

function replay(){
   vhssObj = InternetExplorer ? VHSS : window.document.VHSS;
   vhssObj.TCallLabel("/", "replay");
}

// ------------------------- Link API FUNCTION

function SetLink (href){
   vhssObj = InternetExplorer ? VHSS : window.document.VHSS;
   vhssObj.SetVariable("slide.BUTTON.HREF",href);
}

function SetTarget (targetName){
   vhssObj = InternetExplorer ? VHSS : window.document.VHSS;
   vhssObj.SetVariable("slide.BUTTON.WINDOW",targetName);
}

// ------------------------- backend tts API FUNCTION 

function say(fn){
	getHostPath();
	var baseURL="";
	voice= hostPath + sep1 + "voice" ;
	mc.SetVariable(voice,baseURL+fn);
    mc.TCallLabel(curHost, "talk");
}
