﻿// 외국어 발음 학습/평가를 위한 오디오 마법사(Audio Wizard)

function audioWizard()
{

	_this = this;
	
	this.cabFilePath = null;
	this.cabVersion = "1,0,0,4";
	this.currentStep = 1;

	this.activex = null;
	this.volume = null;

	this.refURL = null;
	this.skipChin = false;
	this.popupMode = false;
	this.popupWidth = 0;
	this.popupHeight = 0;

	// 듣기 볼륩값 초기값
	this.currentSpkVolumeLevel = 100;
	this.currentMicVolumeLevel = 100;

	this.currentSpkMute = false;
	this.currentMicMute = false;

	this.currentMicMode = 1;
	this.currentEnv = 0;

	this.currentUserSex = 1;


	this.stepImageArray = ["title_step1.png","title_step2.png","title_step3.png","title_step4.png",
							"title_step5.png","title_step6.png","title_step7.png","title_step8.png"];
	this.labelImageArray = ["label_step1.png","label_step3.png","label_step4.png",
							"label_step5.png","label_step6.png","label_step7.png","label_step8.png"];
	this.symbolImageArray = ["symbol_img1.png","symbol_img3.png","symbol_img4.png",
							"symbol_img5.png","symbol_img6.png","symbol_img7.png","symbol_img8.png"];

	this.isInstall = false;

}

audioWizard.prototype = {
	init : function(sid)
	{

		this.printObjTag(sid);
		this.ocxCheck(sid);

		this.activex = document.getElementById(sid);

		this.volume = document.getElementById('volume');

		this.activex.attachEvent("OnRecordingStopped",this.onRecordStopped);
		this.activex.attachEvent("OnRecordingComplete",this.onRecordComplete);
		this.activex.attachEvent("OnRecordingError",this.onRecordError);
		this.activex.attachEvent("OnVolumeChanged",this.onVolumeChanged);
		this.activex.attachEvent("OnRetryRecording",this.onRetryRecording);

		this.activex.SetPauseMS(300);
		this.activex.SetVolume(true);
		this.activex.SetTiming(2);
		
		this.urlParse();

		this.skipChin = true; // 중국어 체크 pass
	},
	printObjTag:function(sid)
	{
		this.cabFilePath = opener.gCabFilePath;
		objTag = "<OBJECT ID=\""+sid+"\" width=\"0\" height=\"0\" border=\"0\" style=\"position:absolute;left:-100px;top:-100px\"";
		objTag += "CLASSID=\"CLSID:251C3790-C2D0-42BA-A42A-E08BE6DD3600\"";
		objTag += "codebase=\""+this.cabFilePath+"_activex/AudioWizard_eoneo.cab#Version="+this.cabVersion+"\"></OBJECT>";
		document.write(objTag);
	},
	ocxCheck:function(sid)
	{

		if (typeof(document.all(sid))!="undefined" && document.all(sid).object!=null)
		{
			this.isInstall = true;
		}
		else
		{
			this.isInstall = false;
		}
	},
	urlParse:function()
	{
		var temp = new String(document.location);
		var tempPos = temp.indexOf('?');
		if (tempPos < 1)
		{
			alert("테스트 완료 후, 이동할 URL이 전달되지 않았습니다.");
			self.close();
		}
		var refPMStr = '&refurl=';

		this.refURL = temp.substr(temp.indexOf(refPMStr) + refPMStr.length);
//		alert(this.refURL);

		var tempAmp = temp.substr(tempPos + 1).split("&",3);
		var tempEqu = new Array();

		for (var j = 0; j < tempAmp.length ; j++ )
		{
			tempEqu = tempAmp[j].split("=");
			if (tempEqu[0] == "skipchin" && tempEqu[1] == "true")
			{
				this.skipChin = true;
			}
			else if (tempEqu[0] == "popopt" && tempEqu[1] == "true")
			{
				this.popupMode = true;
				this.popupWidth = opener.gPopupWidth;
				this.popupHeight = opener.gPopupHeight;
			}
		}

	},
	// 테스트 이후, 이동
	gotoRefURL:function()
	{
		opener.document.location.reload();
		self.close();
/*
		if (this.popupMode == true)
		{
			document.write("Loading...");
			var y = screen.width/2-this.popupWidth/2;
			var x = screen.height/2-this.popupHeight/2; 
			window.resizeTo(this.popupWidth,this.popupHeight);

			window.moveTo(x,y);
			location.replace(this.refURL);
		}
		else
		{
			opener.document.location.href = this.refURL;
			self.close();
		}
*/
	},
	changeTitleImage:function()
	{
		var cStep = this.currentStep-1;

		if (this.skipChin == true && this.currentStep == 7)
		{
			document.getElementById('step_img').src = "_img/" + this.stepImageArray[5];
		}
		else
		{
			document.getElementById('step_img').src = "_img/" + this.stepImageArray[cStep];
		}

		document.getElementById('label_img').src = "_img/" + this.labelImageArray[this.currentStep-1];
		document.getElementById('symbol_img').src = "_img/" + this.symbolImageArray[this.currentStep-1];

		for (i=1; i<=7; i++)
		{
			if (i == this.currentStep)
			{
				document.getElementById('location_' + this.currentStep ).style.display = "inline";
				document.getElementById('location_' + this.currentStep + '_disabled').style.display = "none";
			}
			else
			{
				document.getElementById('location_' + i).style.display = "none";
				document.getElementById('location_' + i + '_disabled').style.display = "inline";
			}
		}

		if (this.skipChin == true)
		{
			document.getElementById('location_7').style.display = "none";
			document.getElementById('location_7_disabled').style.display = "none";
			if (this.currentStep == 7)
			{
				document.getElementById('location_6').style.display = "inline";
				document.getElementById('location_6_disabled').style.display = "none";
			}
		}

	},
	moveStep:function(stepNo)
	{
		this.actionStopSndPlay();

		if (stepNo == 1)
			document.getElementById("bt_pass").style.display = "inline";
		else
			document.getElementById("bt_pass").style.display = "none";

		if (stepNo > 7) this.gotoRefURL();

		else
		{

			if (stepNo == 1)
			{
				this.showButton("prev_disabled","next");
			}
			else if (stepNo == 7)
			{
				this.showButton("prev","end");
			}
			else
			{
				this.showButton("prev","next");
			}

			for (var i = 1; i <= 7; i++)
			{
				var awStepDiv = eval("awStep" + i);

				if (i == stepNo)
					awStepDiv.style.display = "inline";
				else
					awStepDiv.style.display = "none";
			}

			this.currentStep = stepNo;
			
			// 타이틀 이미지 변경
			this.changeTitleImage();

			// 스텝별 초기화
			onLoadStep();

		}
	},
	movePrevStep:function()
	{
		if (this.currentStep == 7 && this.skipChin == true)
		{
			this.moveStep(5);
		}
		else
		{
			this.moveStep(this.currentStep - 1);
		}
	},
	moveNextStep:function()
	{
		if (this.currentStep == 2)
		{
			var frm = document.forms.awStep3Form;
			if (frm.sex_male.checked == false && frm.sex_female.checked == false)
			{
				alert("성별을 선택해주세요.");
				return true;
			}
		}

		if (this.currentStep == 5 && this.skipChin == true)
		{
			this.moveStep(7);
		}
		else
		{
			this.moveStep(this.currentStep + 1);
		}
	},
	checkUseSex:function(mode)
	{
		var frm = document.forms.awStep3Form;

		if (mode == 'male')
		{
			frm.sex_female.checked = false;
			frm.sex_male.checked = true;
			this.activex.SetSex(true);
		}
		else
		{
			frm.sex_female.checked = true;
			frm.sex_male.checked = false;
			this.activex.SetSex(false);
		}
	},
	// 스피커, 마이크 활성화
	muteActivity:function(mode)
	{
		if (mode == "spk")
		{
			if (this.activex.SetSpkRunning())
			{
				mute_activity_bt_spk.style.display = "none";
				mute_activity_bt_spk_disabled.style.display = "inline";
				mute_state_spk.src = "./_img/circle_blue.png";
			}
		}
		else
		{
			if (this.activex.SetMicStatus())
			{
				if (this.currentMicMode == 1)
				{
					mute_activity_bt_mic.style.display = "none";
					mute_activity_bt_mic_disabled.style.display = "inline";
					mute_state_mic.src = "./_img/circle_blue.png";
					MicRec.style.display = "inline";
					MicRec_disabled.style.display = "none";
					MicStop.style.display = "none";
					MicStop_disabled.style.display = "inline";
					MicPlay.style.display = "none";
					MicPlay_disabled.style.display = "inline";
				}
				else if (this.currentMicMode == 2)
				{
					mute_activity_bt_mic2.style.display = "none";
					mute_activity_bt_mic2_disabled.style.display = "inline";
					mute_state_mic2.src = "./_img/circle_green.png";
					MicRec2.disabled = false;
				}
				else if (this.currentMicMode == 3)
				{
					mute_activity_bt_mic3.style.display = "none";
					mute_activity_bt_mic3_disabled.style.display = "inline";
					mute_state_mic3.src = "./_img/circle_green.png";
					MicRec3.disabled = false;
				}
			}
		}
	},
	showButton:function(button1,button2)
	{
		document.getElementById("bt_prev").style.display = "none";
		document.getElementById("bt_prev_disabled").style.display = "none";
		document.getElementById("bt_next").style.display = "none";
		document.getElementById("bt_next_disabled").style.display = "none";
		document.getElementById("bt_end").style.display = "none";
		document.getElementById("bt_end_disabled").style.display = "none";
		
		document.getElementById("bt_" + button1).style.display = "inline";
		document.getElementById("bt_" + button2).style.display = "inline";
		
	},
	// 이벤트
	onRecordStopped:function()
	{ 
		alert("onRecordStopped");
	},
	onRecordComplete:function(iResult)
	{
		// 녹음중 이동 못하게한 것 해제
		_this.showButton("prev","next");

		if (_this.currentMicMode == 1)
		{
			MicRec.style.display = "inline";
			MicRec_disabled.style.display = "none";
			MicStop.style.display = "none";
			MicStop_disabled.style.display = "inline";
			MicPlay.style.display = "inline";
			MicPlay_disabled.style.display = "none";
		}
		else if (_this.currentMicMode == 2)
		{
			if (iResult == 1)
				this.mute_state_mic2.src = "./_img/circle_blue.png";
			else
				this.mute_state_mic2.src = "./_img/circle_red.png";

			MicRec2.style.display = "inline";
			MicRec2_disabled.style.display = "none";
		}
		else if (_this.currentMicMode == 3)
		{
			if (iResult == 1)
				this.mute_state_mic3.src = "./_img/circle_blue.png";
			else
				this.mute_state_mic3.src = "./_img/circle_red.png";

			MicRec3.style.display = "inline";
			MicRec3_disabled.style.display = "none";
		}
	},
	onRecordError:function()
	{
		alert("onRecordError");
	},
	onVolumeChanged:function(n)
	{
		if (_this.currentMicMode == 1)
		{
			this.volume_pb.style.width = n/100*266;
		}
		else if (_this.currentMicMode == 2)
		{
			this.volume_pb2.style.width = n/100*321;
		}
		else if (_this.currentMicMode == 3)
		{
			this.volume_pb3.style.width = n/100*321;
		}
	},
	onRetryRecording:function(n)
	{
		// 녹음중에는 이동 못하게 한것 해제
		_this.showButton("prev","next");

		this.mute_state_mic3.src = "./_img/circle_red.png";
		mute_activity_bt_mic3.style.display = "none";
		mute_activity_bt_mic3_disabled.style.display = "inline";
	},
	// 3단계 성별
	initGetUserSex:function()
	{
		this.currentUserSex = this.activex.GetSex();
	},
	// 스피커
	initGetSpk:function()
	{
		this.getSpeaker();
		this.currentSpkVolumeLevel = this.currentSpkVolumeLevel / 100;

		// 음소거 여부에 따른 이미지 변경
		if (this.currentSpkMute)
		{
			mute_activity_bt_spk.style.display = "none";
			mute_activity_bt_spk_disabled.style.display = "inline";
			mute_state_spk.src = "./_img/circle_blue.png";
		}
		else 
		{
			mute_activity_bt_spk.style.display = "inline";
			mute_activity_bt_spk_disabled.style.display = "none";
			mute_state_spk.src = "./_img/circle_red.png";
		}
	},
	getSpeaker:function()
	{
		_this.currentSpkVolumeLevel = _this.activex.GetSpkCurrentVolumeLevel();
		_this.currentSpkMute = _this.activex.GetSpkRunning();
	},
	actionSpkPlay:function()
	{
		this.activex.StartSndPlay("");
	},
	actionStopSndPlay:function()
	{
		this.activex.StopSndPlay();
	},
	// 마이크
	initGetMic:function(no)
	{
		this.getMic();
		this.currentMicVolumeLevel = this.currentMicVolumeLevel / 100;

		if (this.currentMicMute)
		{
			if (this.currentMicMode == 1)
			{
				mute_activity_bt_mic.style.display = "none";
				mute_activity_bt_mic_disabled.style.display = "inline";
				mute_state_mic.src = "./_img/circle_blue.png";

				MicRec.style.display = "inline";
				MicRec_disabled.style.display = "none";
				MicStop.style.display = "none";
				MicStop_disabled.style.display = "inline";
				MicPlay.style.display = "none";
				MicPlay_disabled.style.display = "inline";
			}
			else if (this.currentMicMode == 2)
			{
				mute_activity_bt_mic2.style.display = "none";
				mute_activity_bt_mic2_disabled.style.display = "inline";
				mute_state_mic2.src = "./_img/circle_green.png";
			}
			else if (this.currentMicMode == 3)
			{
				mute_activity_bt_mic3.style.display = "none";
				mute_activity_bt_mic3_disabled.style.display = "inline";
				mute_state_mic3.src = "./_img/circle_green.png";
			}
		}
		else 
		{
			if (this.currentMicMode == 1)
			{
				mute_activity_bt_mic.style.display = "inline";
				mute_activity_bt_mic_disabled.style.display = "none";
				mute_state_mic.src = "./_img/circle_red.png";
				MicRec.style.display = "none";
				MicRec_disabled.style.display = "inline";
				MicStop.style.display = "none";
				MicStop_disabled.style.display = "inline";
				MicPlay.style.display = "none";
				MicPlay_disabled.style.display = "inline";
			}
			else if (this.currentMicMode == 2)
			{
				mute_activity_bt_mic2.style.display = "inline";
				mute_activity_bt_mic2_disabled.style.display = "none";
				mute_state_mic2.src = "./_img/circle_red.png";
			}
			else if (this.currentMicMode == 3)
			{
				mute_activity_bt_mic3.style.display = "inline";
				mute_activity_bt_mic3_disabled.style.display = "none";
				mute_state_mic3.src = "./_img/circle_red.png";
			}
		}
	},
	getMic:function()
	{
		this.currentMicVolumeLevel = this.activex.GetMicCurrentVolumeLevel();
		this.currentMicMute = this.activex.GetMicRunning();
	},
	actionMicStop:function()
	{
		var result = this.activex.StopRecord();
	},
	actionMicPlay:function()
	{
		this.activex.StartSndPlay("test.wav");
	},

	actionMicRec:function(mode)
	{
		// 녹음중에는 이동 못하게
		_this.showButton("prev_disabled","next_disabled");

		if (this.currentMicMode == "1")
		{
			MicRec.style.display = "none";
			MicRec_disabled.style.display = "inline";
			MicStop.style.display = "inline";
			MicStop_disabled.style.display = "none";
			MicPlay.style.display = "inline";
			MicPlay_disabled.style.display = "none";

			this.activex.SetRecordType(2);
		}
		else if (this.currentMicMode == "2")
		{
			MicRec2.style.display = "none";
			MicRec2_disabled.style.display = "inline";
			this.activex.SetRecordType(3);
		}
		else if (this.currentMicMode == "3")
		{
			MicRec3.style.display = "none";
			MicRec3_disabled.style.display = "inline";
			this.activex.SetRecordType(1);
		}

		var result = this.activex.StartRecord("test.wav");
	}

}

