(function(document) {

	var m_fileCheck,
		m_ttsTxtFocus = 0,
		m_ttsTextArea,
		m_curCharDisplay

	$(document).ready(function(){

		if (swfobject.hasFlashPlayerVersion("9.0.0")) {  
			var att = { data:"/swf/audio/tts/ttsAudio.swf", width:"25", height:"25" };
			var par = { quality:"high", wmode:"transparent"};
			var myObject = swfobject.createSWF(att, par, "ttsAudioPlayer");
		}else{
			// Need To Show Alert To No Flash Here
		
		}
		
		m_ttsTextArea = $("#ttsBanner textarea");
		m_curCharDisplay = $("#ttsBanner textarea + div span:first");
		
		$('#ttsBanner textarea').focus(function(){
			
			if(typeof(objMarquee) == "object"){
				objMarquee.stopTransition();
			}
			
			m_ttsTxtFocus = setInterval(chkTxtInput, 200);
		});
		
		$('#ttsBanner textarea').blur(function(){
			clearInterval(m_ttsTxtFocus);
		});

		$(".languageOptions a").each(function(p_index, p_elm){
			$(this).click(function(){
				if($('.languageOptions span.selected').length > 0){
					$('.languageOptions .selected').removeClass('selected');
				}
				$(this).parent().addClass('selected');		

			});
		});

		$("#btnSendTxt").click(function(){
			init();
		});
		
		$("#btnStopAudio").click(function(){
			callAS('stopAudio');
		});
	});

	function init(){
		
		var msg = $("#ttsTxt").val();
		var voice = $("select.voiceLanguage").val();
		
		msg = msg.replace(/\"/g, "");

		if(msg.length > 1){
		
			if(msg.length > 250){
 				var answer = confirm("You have entered more than 250 characters. Your text will be limited to 250 characters. Would you like to continue?");
				if (answer){
					msg = msg.substring(0, msg.lastIndexOf(' '));
					document.getElementById('ttsTxt').value = msg;
					document.getElementById('curChar').innerHTML = msg.length;
				}else{
					return false;
				} 
			}
		
			//$("#btnSendTxt").attr('src', '/img/products/tts/btnListen-disabled.png');
			$("#requestPending").css('display', 'block');
			
			m_fileCheck = 0;
		
			$.ajax({
				url: '/products/tts/processTTS.ashx',
				data: 'msg=' + msg + '&voice=' + $("select.voiceLanguage").val(),
				type: 'POST',
				success: function (p_data, textStatus) {
					checkFile($(p_data).find('status').find('path').text());
				},
				error: function (data) {
					alert('Our service is temporarily down. We apologize for any inconvenience.');
				}
			});
		}else{
			alert('Please enter text to try our text-to-speech tecnology');
		}
	}

	function checkFile(p_fileName){
	
		$.ajax({
			url: '/products/tts/fileReady.ashx',
			data: 'fileName=' + p_fileName,
			type: 'POST',
			success: function (p_data, textStatus) {
				if($(p_data).find('status').find('exists').text() == '1'){
					// Have Audio : Load
					setTimeout(function(){
						callAS('loadAudio', '/products/tts/audio/' + p_fileName);
					}, 1000);
				}else{
					// No Audio yet : Set Que
					// Check For File Again
					if(m_fileCheck < 30){
						setTimeout(function(){checkFile(p_fileName)}, 500);
					}else{
						errorAlert();
					}
				}
			},
			error: function (data) {
				// Close timeout
				errorAlert();
			}
		});

		m_fileCheck++;	
	}

	function errorAlert(){
		$("#btnSendTxt").attr('src', '/img/products/tts/btnListen.png');
		alert('Our service is temporarily down. We apologize for any inconvenience.');
	}
	
	function chkTxtInput(){

		if(m_ttsTextArea.val().length <= 250){		
			m_curCharDisplay.text(m_ttsTextArea.val().length);
		}else{
			var msg = m_ttsTextArea.val().substring(0, 250);
			m_ttsTextArea.val(msg.substring(0, msg.lastIndexOf(' ')));
			
			m_curCharDisplay.val(msg.length);
			
			alert('The entered text is longer than 250 characters. Your entry has been limited to 250 characters.')
		}
	}

	function callAS(p_func, p_value) {
		if(arguments.length == 1){
			document["ttsAudioPlayer"][p_func]();
		}else{
			document["ttsAudioPlayer"][p_func](p_value);
		}
		return false;
	}
})(document);

function audioAction(p_action){

	if(p_action == 'play'){
		$("#requestPending").css('display', 'none');
		$("#btnSendTxt").css('display', 'none');
		$("#btnStopAudio").css('display', 'block');
	}else{
		$("#btnSendTxt").css('display', 'block');
		$("#btnStopAudio").css('display', 'none');
		$("#btnSendTxt").attr('src', '/img/products/tts/btnListen.png');
	}
}


