/*****************************************************
 * 准备条件
 *   1. flash对象的Object的Id默认为"vox_engine_flash_id"
 *   2. 要求flash对js开放的接口如下:
 *      (1) recordStart()
 *      (2) recordEnd(retcode)
 *      (3) playStop()
******************************************************/

/** prepare variables defination **/
var _vox_engine_obj = null;
var _vox_engine_flash_id = "vox_engine_flash_id";
var _vox_engine_record_start = "vox_engine_recordStart";
var _vox_engine_record_end = "vox_engine_recordEnd";
var _vox_engine_play_stop = "vox_engine_playStop";

/** constant defination **/
var VOX_ENGINE_RECORD_END_REASON_OK = 0;
var VOX_ENGINE_RECORD_END_REASON_LOUD = 1;
var VOX_ENGINE_RECORD_END_REASON_MUTE = 2;
var VOX_ENGINE_RECORD_END_REASON_BLOW = 3;
var VOX_ENGINE_RECORD_END_REASON_SYSERROR = 4;

/**
 * 引擎初始化
 * @Return
 *     0 : 初始化成功
 *    -1 : 初始化失败
 */
function vox_engine_init(){
	try {
		_vox_engine_obj = new ActiveXObject("VoxLearning.Record.1");
		_vox_engine_obj.CallbackPlayStop = function(){
			setTimeout(function(){
				try { if(vox_engine_event_playstop) vox_engine_event_playstop();}catch(e){}	
				try { document.getElementById(_vox_engine_flash_id)[_vox_engine_play_stop]();}catch(e){}	
			}, 100);
		}
		_vox_engine_obj.CallbackRecordStarted = function(){
			setTimeout(function(){
				document.getElementById(_vox_engine_flash_id)[_vox_engine_record_start]();
			}, 100);
		}
		_vox_engine_obj.CallbackFinish = function(){
			setTimeout(function(){
				document.getElementById(_vox_engine_flash_id)[_vox_engine_record_end](VOX_ENGINE_RECORD_END_REASON_OK);
			}, 100);
		}
		_vox_engine_obj.CallbackLoud = function(){
			setTimeout(function(){
				document.getElementById(_vox_engine_flash_id)[_vox_engine_record_end](VOX_ENGINE_RECORD_END_REASON_LOUD);
			}, 100);
		}
		_vox_engine_obj.CallbackMute = function(){
			setTimeout(function(){
				document.getElementById(_vox_engine_flash_id)[_vox_engine_record_end](VOX_ENGINE_RECORD_END_REASON_MUTE);
			}, 100);
		}
		_vox_engine_obj.CallbackBlow = function(){
			setTimeout(function(){
				document.getElementById(_vox_engine_flash_id)[_vox_engine_record_end](VOX_ENGINE_RECORD_END_REASON_BLOW);
			}, 100);
		}
		_vox_engine_obj.CallbackSystemError = function(){
			setTimeout(function(){
				document.getElementById(_vox_engine_flash_id)[_vox_engine_record_end](VOX_ENGINE_RECORD_END_REASON_SYSERROR);
			}, 100);
		}
	} catch(e){
		return -1;
	}
}

/** get/set userName **/
function vox_engine_setUserName(userName){_vox_engine_obj.CurrentUser = userName;}
function vox_engine_getUserName(){return _vox_engine_obj.CurrentUser;}
/** get/set lessonId **/
function vox_engine_setLessonId(lessonId){_vox_engine_obj.CurrentLesson = lessonId;}
function vox_engine_getLessonId(){return _vox_engine_obj.CurrentLesson;}
/** get/set record mode **/
function vox_engine_setRecordMode(recordMode){_vox_engine_obj.RecordMode = recordMode;}
function vox_engine_getRecordMode(){return _vox_engine_obj.RecordMode;}
/** get/set prompt url **/
function vox_engine_setPromptUrl(promptUrl){_vox_engine_obj.PromptUrl = promptUrl;}
function vox_engine_getPromptUrl(){return _vox_engine_obj.PromptUrl;}
/** get/set level **/
function vox_engine_setLevel(level){_vox_engine_obj.CurrentDifficultyLevel = level;}
function vox_engine_getLevel(){return _vox_engine_obj.CurrentDifficultyLevel;}
/** get user wave url **/
function vox_engine_getUserWavUrl(){return _vox_engine_obj.RecordFilePath;}
/** get/set micVolume **/
function vox_engine_setMicVolume(micVolume){
    try {
        _vox_engine_obj.MicVolume = micVolume;
    } catch(e){}
}
function vox_engine_getMicVolume(){
	try {
		return _vox_engine_obj.MicVolume;
	} catch(e){
		return -1;
	}
}
/** get/set sentence id **/
function vox_engine_setSentsId(sentsId){_vox_engine_obj.CurrentSentence = sentsId;}
function vox_engine_getSentsId(){return _vox_engine_obj.CurrentSentence;};
/** get/set meta data **/
function vox_engine_setMetaData(metaData){_vox_engine_obj.MetaData = metaData;}
function vox_engine_getMetaData(){return _vox_engine_obj.MetaData;}
/** get/set WaveUrl **/
function vox_engine_setWaveUrl(waveUrl){_vox_engine_obj.WaveUrl = waveUrl;}
function vox_engine_getWaveUrl(){return _vox_engine_obj.WaveUrl;}


function vox_engine_playSpecUrl(wavUrl){
	var audio_obj = document.getElementById("audio");
	//ajax request
	var xhr;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
	      xhr = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) { // IE 
	      try {
			 xhr = new ActiveXObject("MSXML2.XMLHTTP");
			} catch(e) {
			 xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
	}
	xhr.open("POST", wavUrl, true);
	xhr.setRequestHeader("Content-Type", "Application/x-www-form-urlencoded");
    xhr.onreadystatechange=function(){
    	if (xhr.readyState == 4) {
    		if (xhr.status == 200) {
    			var realWavUrl=xhr.getResponseHeader("wavFilePath");
				//audio_obj.URL = realWavUrl;
				//audio_obj.Settings.PlayCount = 1;
				//audio_obj.Settings.rate = 1;
				//audio_obj.SendPlayStateChangeEvents = true;
				//audio_obj.play();
				_vox_engine_obj.PlayFile(realWavUrl);				
 			} 
    		delete xhr.onreadystatechange;
    		xhr = null;
    	}
    }
    xhr.send("");
	
	var b_playsound_mode = document.getElementById("sound_mode").checked;
	
	if ( b_playsound_mode)
		return 0;
	return 1;
	//return _vox_engine_obj.PlayFile(wavUrl);
}

function vox_engine_playSents(type){
	_vox_engine_obj.PlayCurrentSentence(type);
}

function vox_engine_startRecord(recordtime){
	_vox_engine_obj.StartRecord(recordtime);
}

function vox_engine_stopPlay(isFireout){
	try {
		_vox_engine_obj.StopPlay(isFireout?1:0);
	} catch(e){}
}

function vox_engine_stopRecord(isFireout){
	try {
		_vox_engine_obj.StopRecord(isFireout?1:0);
	} catch(e){}
}

function vox_engine_preWavLoad(sentsId, wavUrl){
	_vox_engine_obj.DownloadWave(sentsId, wavUrl);
}

function vox_engine_playEvaluableSound(index){
	switch(index){
	  case 0:
	  	_vox_engine_obj.PlayWaveTryAgain();break;
	  case 1:
	  	_vox_engine_obj.PlayWaveGood();break;
	  case 2:
	  	_vox_engine_obj.PlayWaveVeryGood();break;
	}
}

function vox_engine_getTestResult(){
	return _vox_engine_obj.MicTestResult;
}

function vox_engine_getFirstMidResult(){
	return _vox_engine_obj.Base64Data;
}

function vox_engine_getFinalResult(secondMidResult){
	_vox_engine_obj.GenerateResultEncoded(secondMidResult);
	return _vox_engine_obj.XML;
}

function vox_engine_getVersion(){
	return _vox_engine_obj.Version;
}

function vox_engine_clearCurLessonVoices(){
	_vox_engine_obj.ClearCurrentLessonUserVoice();
}

function vox_engine_clearCurUserVoices(){
	_vox_engine_obj.ClearCurrentUser();
}

function vox_engine_dispose(){
	_vox_engine_obj.CallbackPlayStop = {};
	_vox_engine_obj.CallbackFinish = {};
	_vox_engine_obj.CallbackLoud = {};
	_vox_engine_obj.CallbackMute = {};
	_vox_engine_obj.CallbackBlow = {};
	_vox_engine_obj.CallbackSystemError = {};
	_vox_engine_obj.CallbackRecordStarted = {};
	_vox_engine_obj.Close();
}