function TTSService()
{
	_this = this;
	this.DEBUG = false;
	this.sentences = [];
	this.EOS = "\001";
	this.WebServiceUrl = "/VWTTSServiceProxy.php";
	this.pitch = -1;
	this.speed = -1;
	this.volume = 100;
	this.delay = 500;
	this.lang = "";
	this.maxChars = 512;

	this.mobile = false;
}

TTSService.prototype = {
	debug:function(s)
	{
		if (this.DEBUG)
			window.status = s;
	},
	listen:function(o,lang)
	{
		if (!o)
			return;
		
		var str = o.innerText;
		
		this.lang = (lang == undefined)? "cn" : lang;
		this.sentences = [];

		if (str.trim())
			this.rawSentence = str.trim();
		else
		{
			alert("문장이 없습니다.");
			return;
		}

		var regex = this.getHanRegex();		
		str = str.replace(regex,'');
		
		// 미디어 플레이어
		this.hasPlayer = this.attachPlayer();
		if (!this.hasPlayer)
		{
			alert("미디어 플레이어를 찾을 수 없습니다.");
			return;
		}

		// 문장 마침 부호를 찾아서 뒤에 특수문자를 붙여둔다
		var regex = /([.。,?？])/gi;

		// 문장 마침 부호+특수문자를 기준으로 배열로 생성		
		var sen = this.rawSentence.replace(regex,"$1"+this.EOS);
		this.cleanSentences(sen.split(this.EOS));		
	},
	getHanRegex:function()
	{
		var o = document.createElement("div");
		o.style.display = "none";
		o.innerHTML = "&#44032;-&#55203;";
		return eval("/[" + o.innerHTML + "]+/g");
	},
	initMobile:function(textareaId, lang)
	{
		this.mobile = true;

		this.init(textareaId, lang);
	},
	init:function(textareaId, lang)
	{
		this.lang = (lang == undefined)? "cn" : lang;
		this.sentences = [];
		var oTextarea = document.getElementById(textareaId);

		if (!oTextarea)
		{
			alert("TTS로 들으실 내용이 없습니다.");
			return;
		}

		var str = oTextarea.value;

		if (str.trim())
		{
			this.rawSentence = str.trim();
		}
		else
		{
			alert("문장이 없습니다.");
			return;
		}
		
		if (this.rawSentence.length > this.maxChars)
		{
			alert("글자수는 " + this.maxChars + "개까지만 가능합니다.");
			return;			
		}

		// 한글 포함 여부(가-힣)
		var regex = this.getHanRegex();
		if (this.lang != 'ko' && regex.test(str))
		{
			alert("한글이 포함되어 있습니다.");
			return;			
		}

		if (!this.mobile)
			mp3EventSet(this.onFSCommand);

		// 문장 마침 부호를 찾아서 뒤에 특수문자를 붙여둔다
		var regex = /([.。,?？\n])/gi;

		// 문장 마침 부호+특수문자를 기준으로 배열로 생성		
		var sen = this.rawSentence.replace(regex,"$1"+this.EOS);

		this.cleanSentences(sen.split(this.EOS));
	},
	cleanSentences:function(sen) // 문장을 정리
	{
		for (var i in sen)
		{
			if (typeof sen[i] != "string")
				continue;

			if (sen[i].toString().trim())
				this.sentences.push(sen[i].trim());
		}
		this.sentenceCount = this.sentences.length;
		this.sendSentence(0);
	},
	attachPlayer:function()
	{
		if (this.hasPlayer)
			return true;

		if (this.mobile)
			return true;
				
		var oPlayer = document.getElementById('mp3streamer');
		this.player = oPlayer || null;
		
		// 미디어플레이어의 이벤트를 override
		if (this.player)
		{
			this.player.attachEvent("FSCommand",_this.onFSCommand);
		}
		return oPlayer == null ? false : true;
	},
	onFSCommand:function(cmd,args)
	{
		switch (cmd.toUpperCase())
		{
			case "NEWSTREAM":
				break;
			case "ENDOFSTREAM":
				_this.endOfStream(0);
				break;
		}
	},
	sendSentence:function(nSentence)
	{
		this.sentenceNo = nSentence;

		if (this.sentences[nSentence])
		{
			var oDisplayer = document.getElementById("tts-current-sentence");

			if (oDisplayer)
			{
				oDisplayer.style.textAlign = "left";
				oDisplayer.style.font = "12pt dotum";
				oDisplayer.style.padding = "8px";
				oDisplayer.style.backgroundColor = "#ECF6EE";
				oDisplayer.style.width = "600px";
				oDisplayer.style.marginBottom = "10px";
				oDisplayer.lang = "zh-cn";

				oDisplayer.innerText = this.sentences[nSentence];
			}

			var soundUrl = gURL_root + "/api/GET/tts.play." + this.lang;

			soundUrl += "?text=" + escape(this.sentences[nSentence]);
			soundUrl += "&pitch=" + this.pitch;
			soundUrl += "&speed=" + this.speed;
			soundUrl += "&volume=" + this.volume;

			playSound(soundUrl);
		}
		else
		{
			alert("문장이 없습니다.");
		}
	},
	endOfStream:function(nResult)
	{
		if (nResult == 0) // 재생이 끝났으면
		{
			_this.debug("재생 완료 : " + _this.currentSoundUrl);
			if (_this.hasNextSentence())
			{
				_this.debug("다음 문장으로..");
				window.setTimeout(_this.sendNextSentence,_this.delay);
			}
			else
			{
				var oDisplayer = document.getElementById("tts-current-sentence");
				if (oDisplayer)
					oDisplayer.innerText = "재생이 완료되었습니다.";
			}
		}
	},
	sendNextSentence:function()
	{
		_this.sentenceNo = _this.sentenceNo + 1;
		_this.sendSentence(_this.sentenceNo);
	},
	hasNextSentence:function()
	{
		return this.sentenceNo < this.sentenceCount - 1;
	}
}

var TTSService = new TTSService();
