/*****************************************************
 * 准备条件
 *   1. 要求flash对js开放的接口如下:
 *      (1) vox_voice_engine_recordStart()
 *      (2) vox_voice_engine_recordEnd(retcode)
 *      (3) vox_voice_engine_playStop()
******************************************************/

/** prepare variables defination **/
var _vox_voice_engine_map = {}; // 声音引擎对象映射(key = flash's id)
var _vox_voice_engine_record_start = "vox_voice_engine_recordStart";
var _vox_voice_engine_record_end = "vox_voice_engine_recordEnd";
var _vox_voice_engine_play_stop = "vox_voice_engine_playStop";

/** constant defination **/
var vox_voice_engine_RECORD_END_REASON_OK = 0;
var vox_voice_engine_RECORD_END_REASON_LOUD = 1;
var vox_voice_engine_RECORD_END_REASON_MUTE = 2;
var vox_voice_engine_RECORD_END_REASON_BLOW = 3;
var vox_voice_engine_RECORD_END_REASON_SYSERROR = 4;

function writeLog(type, logInfo) {
	try {
		addLog(type, logInfo);
	} catch(e){}
}

/**
 * 检查ActiveX是否已经安装
 * @Return
 *     true : ActiveX已经安装
 *    false : ActiveX没有安装
 */
function vox_check_activex_install(){
	try {
		var detectorObj= new ActiveXObject("LangKoo.Detector");
		var localVersion=checkActiveObj.Version;
		if (localVersion == 0) return false;
		else return true;
	} catch(e){
		return false;
	}	
}

/**
 * 引擎初始化
 * @Return
 *     0 : 初始化成功
 *    -1 : 初始化失败
 */
function vox_voice_engine_init(flash_id){
	try {
		
		_vox_voice_engine_map[flash_id] = new ActiveXObject("VoxLearning.Record.1");
		_vox_voice_engine_map[flash_id].CallbackPlayStop = function(){
			setTimeout(function(){
				writeLog("warn", "js:activeX返回播放停止事件");
				document.getElementById(flash_id)[_vox_voice_engine_play_stop]();
				writeLog("warn", "js:播放停止事件被传递到flash");
			}, 100);
		}
		_vox_voice_engine_map[flash_id].CallbackRecordStarted = function(){
			setTimeout(function(){
				writeLog("warn", "js:activeX返回录音开始事件 ");
				document.getElementById(flash_id)[_vox_voice_engine_record_start]();
				writeLog("warn", "js:录音开始事件被传递到flash");
			}, 100);
		}
		_vox_voice_engine_map[flash_id].CallbackFinish = function(){
			setTimeout(function(){
				writeLog("warn", "js:activeX返回录音结束事件:正常结束");
				document.getElementById(flash_id)[_vox_voice_engine_record_end](vox_voice_engine_RECORD_END_REASON_OK);
				writeLog("warn", "js:录音结束事件被传递到flash");
			}, 100);
		}
		_vox_voice_engine_map[flash_id].CallbackLoud = function(){
			setTimeout(function(){
				writeLog("warn", "js:activeX返回录音结束事件:声音太大");
				document.getElementById(flash_id)[_vox_voice_engine_record_end](vox_voice_engine_RECORD_END_REASON_LOUD);
				writeLog("warn", "js:录音结束事件被传递到flash");
			}, 100);
		}
		_vox_voice_engine_map[flash_id].CallbackMute = function(){
			setTimeout(function(){
				writeLog("warn", "js:activeX返回录音结束事件:声音太小");
				document.getElementById(flash_id)[_vox_voice_engine_record_end](vox_voice_engine_RECORD_END_REASON_MUTE);
				writeLog("warn", "js:录音结束事件被传递到flash");
			}, 100);
		}
		_vox_voice_engine_map[flash_id].CallbackBlow = function(){
			setTimeout(function(){
				writeLog("warn", "js:activeX返回录音结束事件:吹麦克风");
				document.getElementById(flash_id)[_vox_voice_engine_record_end](vox_voice_engine_RECORD_END_REASON_BLOW);
				writeLog("warn", "js:录音结束事件被传递到flash");
			}, 100);
		}
		_vox_voice_engine_map[flash_id].CallbackSystemError = function(){
			setTimeout(function(){
				writeLog("warn", "js:activeX返回录音结束事件:系统错误");
				document.getElementById(flash_id)[_vox_voice_engine_record_end](vox_voice_engine_RECORD_END_REASON_SYSERROR);
				writeLog("warn", "js:录音结束事件被传递到flash");
			}, 100);
		}
		_vox_voice_engine_map[flash_id] = _vox_voice_engine_map[flash_id];
		return 0;
	} catch(e){
		return -1;
	}
}

/** get/set userName **/
function vox_voice_engine_setUserName(flash_id, userName){_vox_voice_engine_map[flash_id].CurrentUser = userName;}
function vox_voice_engine_getUserName(flash_id){return _vox_voice_engine_map[flash_id].CurrentUser;}
/** get/set lessonId **/
function vox_voice_engine_setLessonId(flash_id, lessonId){_vox_voice_engine_map[flash_id].CurrentLesson = lessonId;}
function vox_voice_engine_getLessonId(flash_id){return _vox_voice_engine_map[flash_id].CurrentLesson;}
/** get/set record mode **/
function vox_voice_engine_setRecordMode(flash_id, recordMode){_vox_voice_engine_map[flash_id].RecordMode = recordMode;}
function vox_voice_engine_getRecordMode(flash_id){return _vox_voice_engine_map[flash_id].RecordMode;}
/** get/set prompt url **/
function vox_voice_engine_setPromptUrl(flash_id, promptUrl){_vox_voice_engine_map[flash_id].PromptUrl = promptUrl;}
function vox_voice_engine_getPromptUrl(flash_id){return _vox_voice_engine_map[flash_id].PromptUrl;}
/** get/set level **/
function vox_voice_engine_setLevel(flash_id, level){_vox_voice_engine_map[flash_id].CurrentDifficultyLevel = level;}
function vox_voice_engine_getLevel(flash_id){return _vox_voice_engine_map[flash_id].CurrentDifficultyLevel;}
/** get user wave url **/
function vox_voice_engine_getUserWavUrl(flash_id){return _vox_voice_engine_map[flash_id].RecordFilePath;}
/** get/set micVolume **/
function vox_voice_engine_setMicVolume(flash_id, micVolume){
    try {
        _vox_voice_engine_map[flash_id].MicVolume = micVolume;
    } catch(e){}
}
function vox_voice_engine_getMicVolume(flash_id){
	try {
		return _vox_voice_engine_map[flash_id].MicVolume;
	} catch(e){
		return -1;
	}
}
/** get/set volume **/
function vox_voice_engine_setSpeakerVolume(flash_id, speakerVolume){
    _vox_voice_engine_map[flash_id].Volume = speakerVolume;
}
function vox_voice_engine_getSpeakerVolume(flash_id){
		return _vox_voice_engine_map[flash_id].Volume;
}
/** get/set sentence id **/
function vox_voice_engine_setSentsId(flash_id, sentsId){_vox_voice_engine_map[flash_id].CurrentSentence = sentsId;}
function vox_voice_engine_getSentsId(flash_id){return _vox_voice_engine_map[flash_id].CurrentSentence;};
/** get/set meta data **/
function vox_voice_engine_setMetaData(flash_id, metaData){_vox_voice_engine_map[flash_id].MetaData = metaData;}
function vox_voice_engine_getMetaData(flash_id){return _vox_voice_engine_map[flash_id].MetaData;}
/** get/set WaveUrl **/
function vox_voice_engine_setWaveUrl(flash_id, waveUrl){_vox_voice_engine_map[flash_id].WaveUrl = waveUrl;}
function vox_voice_engine_getWaveUrl(flash_id){return _vox_voice_engine_map[flash_id].WaveUrl;}
/** get/set timeout second **/
function vox_voice_engine_setTimeoutSec(flash_id, timeoutsec) {_vox_voice_engine_map[flash_id].TimeoutSec = timeoutsec;}
function vox_voice_engine_getTimeoutSec(flash_id) {return _vox_voice_engine_map[flash_id].TimeoutSec;}
/** get/set IntervalSilence **/
function vox_voice_engine_setIntervalSilenceWaitSec(flash_id, time) {_vox_voice_engine_map[flash_id].IntervalSilenceWaitTime = time;}
function vox_voice_engine_getIntervalSilenceWaitSec(flash_id) {return _vox_voice_engine_map[flash_id].IntervalSilenceWaitTime;}

function vox_voice_engine_playSpecUrl(flash_id, wavUrl){
	writeLog("warn", "js:调用activeX的PlayFile("+wavUrl+")");
	return _vox_voice_engine_map[flash_id].PlayFile(wavUrl);
}

function vox_voice_engine_playSents(flash_id, type){
	writeLog("warn", "js:调用activeX的playSents("+type+")");
	return _vox_voice_engine_map[flash_id].PlayCurrentSentence(type);
}

function vox_voice_engine_playSpecSents(flash_id, index){
	writeLog("warn", "js:调用activeX的playSpecSents("+index+")");
	return _vox_voice_engine_map[flash_id].PlaySentence(index);
}

function vox_voice_engine_startRecord(flash_id, recordtime){
	writeLog("warn", "js:调用activeX的startRecord("+recordtime+")");
	return _vox_voice_engine_map[flash_id].StartRecord(recordtime);
}

function vox_voice_engine_stopPlay(flash_id, isFireout){
	try {
		writeLog("warn", "js:调用activeX的stopPlay("+isFireout+")");
		_vox_voice_engine_map[flash_id].StopPlay(isFireout?1:0);
	} catch(e){}
}

function vox_voice_engine_stopRecord(flash_id, isFireout){
	try {
		writeLog("warn", "js:调用activeX的stopRecord("+isFireout+")");
		_vox_voice_engine_map[flash_id].StopRecord(isFireout?1:0);
	} catch(e){}
}

function vox_voice_engine_preWavLoad(flash_id, sentsId, wavUrl){
	_vox_voice_engine_map[flash_id].DownloadWave(sentsId, wavUrl);
}

function vox_voice_engine_generateMp3(flash_id, lessonId, sentsIds) {
	writeLog("warn", "js:调用activeX的generateMp3("+sentsIds+")");
	var retCode = _vox_voice_engine_map[flash_id].GenerateMp3(lessonId, sentsIds);
	writeLog("warn", "js:调用activeX的generateMp3()完成");
	return retCode;
}

function vox_voice_engine_playEvaluableSound(flash_id, index){
	switch(index){
	  case 0:
	  	return _vox_voice_engine_map[flash_id].PlayWaveTryAgain();
	  case 1:
	  	return _vox_voice_engine_map[flash_id].PlayWaveGood();
	  case 2:
	  	return _vox_voice_engine_map[flash_id].PlayWaveVeryGood();
	}
}

function vox_voice_engine_getTestResult(flash_id){
	return _vox_voice_engine_map[flash_id].MicTestResult;
}

function vox_voice_engine_getFirstMidResult(flash_id){
	return _vox_voice_engine_map[flash_id].Base64Data;
}

function vox_voice_engine_getFinalResult(flash_id, secondMidResult){
	writeLog("warn", "js:调用activeX的getFinalResult()");
	_vox_voice_engine_map[flash_id].GenerateResultEncoded(secondMidResult);
	writeLog("warn", "js:调用activeX的getFinalResult()完成");
	return _vox_voice_engine_map[flash_id].XML;
}

function vox_voice_engine_getVersion(flash_id){
	return _vox_voice_engine_map[flash_id].Version;
}

function vox_voice_engine_clearCurLessonVoices(flash_id){
	_vox_voice_engine_map[flash_id].ClearCurrentLessonUserVoice();
}

function vox_voice_engine_clearCurUserVoices(flash_id){
	_vox_voice_engine_map[flash_id].ClearCurrentUser();
}

function vox_voice_engine_clearFeedbackFiles(flash_id){
	_vox_voice_engine_map[flash_id].ClearFeedbackFiles();
}

function vox_voice_engine_dispose(flash_id){
	_vox_voice_engine_map[flash_id].CallbackPlayStop = {};
	_vox_voice_engine_map[flash_id].CallbackFinish = {};
	_vox_voice_engine_map[flash_id].CallbackLoud = {};
	_vox_voice_engine_map[flash_id].CallbackMute = {};
	_vox_voice_engine_map[flash_id].CallbackBlow = {};
	_vox_voice_engine_map[flash_id].CallbackSystemError = {};
	_vox_voice_engine_map[flash_id].CallbackRecordStarted = {};
	_vox_voice_engine_map[flash_id].Close();
	delete _vox_voice_engine_map[flash_id];
}