if(!this.RecogMgr) {
	this.RecogMgr = new Object();
}

//for voice recognition command string
if(!this.g_Voice) {
	this.g_Voice = new Object();
}

RecogMgr ={
	
	isVoiceRecog : false,
	voiceHelpbarInfo : new Object(),
	voiceHelpbarInfo : { helpbarItemsList: new Array() , candidateList : new Array()},
	categoryList : new Array(),	
	contactList : new Array(),
	callList : new Array(),
	incomingCallList : new Array(),
	voiceCallText : "",
	videoCallText : "",
	declineText : "",
	cancelText : "",
	exitText : "",
	answerText : "",
	
	isFaceRegistrationAfterSignin : false, //init
	isSigninAfterPwdCheck : false, //init
	CountOfRecogFail : 0,
	CountOfTrainingFail : 0,
	
	voiceRecogStep : "Init",
	recogContactIndex : 0,
	recogContactName : 0,
	
	snsTabInfo : new Object(),
	changeViewText : "",
}

function OnVoiceRecogEvent(resultInfo)
{
	alert("##mini##****OnVoiceRecogEvent -> " + resultInfo.eventtype);
	
	if (resultInfo.name != 11111000001) {
			return;
	}
	
	switch(resultInfo.eventtype)
	{
		case "EVENT_VOICE_BEGIN_MONITOR":
		case "EVENT_VOICE_BTSOUND_START":
		
			RecogMgr.ProcessVoiceRecogStart();
			
		break;
		
		case "EVENT_VOICE_RECOG_RESULT":
			RecogMgr.ProcessVoiceRecogResult(resultInfo.result); 
		break
		
		default:
		
		break;
		
	}	
}

RecogMgr.ProcessVoiceRecogStart= function()
{
	alert("***RecogMgr.ProcessVoiceRecogStart");
	
	var v_lang = webapis.recognition.GetCurrentVoiceLanguage();
	var voice_lang_file_path = SKYPE_MINI_ROOT_PATH+"app/mini/lang_voice/"+v_lang+".js";
	
	$.sf.loadJS(voice_lang_file_path, function(){
		
		if(ProcMgr.isIncomingCall == true)
		{
			RecogMgr.StartVoiceHelpbar("IncomingCall"); 
		}
		else
		{
			var LoginState = PluginAPIMgr.GetLoginState();
			
			if(LoginState == "0")
			{
				RecogMgr.StartVoiceHelpbar("Welcome"); 
			}
			
			if(LoginState == "2")
			{
				RecogMgr.StartVoiceHelpbar("Main"); 
			}
				
		}
	});
}


RecogMgr.StartVoiceRecog= function()
{
	if(this.isVoiceRecog==false)
	{	
		if(Flag_Hidden_State==true)
		{
			return;
		}
		
		if(utilitySet.getCurrentFocusedSNSId()!=WIDGET.id)
		{
			return;
		}
		
		if(PluginAPIMgr.IsCallInProgress())
		{
			return;	
		}
		
		if(PluginAPIMgr.IsInCall()==1)  // calling 
		{
			if(ProcMgr.isIncomingCall != true)
			{
				return;
			}
		}
		
		alert("##mini##***RecogMgr.StartVoiceRecog");
		
		voiceRecognitionHandlerSet.unsubscribeVoiceEventHandler(); //delete event handler that was subscribed by social TV 
		voiceRecognitionHandlerSet.subscribeVoiceEventHandler(OnVoiceRecogEvent);
		
		RecogMgr.ProcessVoiceRecogStart();
		this.isVoiceRecog = true;
	}
}

RecogMgr.StopVoiceRecog= function(flagHide)
{
	if(this.isVoiceRecog==true)
	{	
		alert("##mini##***RecogMgr.StopVoiceRecog");
		
		if(flagHide)
		{
			RecogMgr.HideVoiceHelpbar();
		}
		
		
		voiceRecognitionHandlerSet.unsubscribeVoiceEventHandler();
		RecogMgr.SetHelpbarType("Init"); 
		this.isVoiceRecog = false;
	}
}

RecogMgr.HideVoiceHelpbar= function()
{
	alert("##mini##****RecogMgr.HideVoiceHelpbar");
	webapis.recognition.HideVoiceHelpbar();
	//PluginAPIMgr.HideVoiceHelpbar();
}

RecogMgr.ChangeVoiceHelpbar = function()
{
	alert("***RecogMgr.ChangeVoiceHelpbar");
	
	if(this.isVoiceRecog==true)
	{
		if(ProcMgr.isIncomingCall == true)
		{
			if(this.voiceRecogStep !="IncomingCall")
			{
				RecogMgr.StartVoiceHelpbar("IncomingCall"); 
			}		
		}
		else
		{
			var LoginState = PluginAPIMgr.GetLoginState();
			
			if(LoginState == "0")
			{
				if(this.voiceRecogStep !="Welcome")
				{
					RecogMgr.StartVoiceHelpbar("Welcome"); 
				}				
			}
			
			if(LoginState == "2")
			{
				if(this.voiceRecogStep !="Main")
				{
					RecogMgr.StartVoiceHelpbar("Main"); 
				}				
			}				
		}
	}	
}

RecogMgr.StartVoiceHelpbar = function(step)
{
	alert("##mini##***RecogMgr.StartVoiceHelpbar");
	
	RecogMgr.SetRecogStep(step);
	
	if(this.voiceRecogStep =="Main")
	{
		this.InitVoiceHelpbarInfo("HELPBAR_TYPE_VOICE_EMBED_GUIDE", g_Voice.TV_SID_GUIDE_CALL_PERSON ,"", true ); 
		
		var i=0;	
		for(i=0; i<this.contactList.length; i++)
		{
			RecogMgr.SetHelpbarItem(this.contactList[i]);
		}
		
		for(i=0; i<this.snsTabInfo.commandText.length; i++)
		{
			var isServiceInstalled = utilitySet.isServiceActivated(this.snsTabInfo.serviceId[i]);
			if(isServiceInstalled ==true)
			{
				RecogMgr.SetHelpbarItem(this.snsTabInfo.commandText[i]);
			}			
		}

		this.SetHelpbarItem(this.changeViewText);
		//this.SetHelpbarItem(this.exitText);
	}
	else if(this.voiceRecogStep =="StartCall")
	{
		this.InitVoiceHelpbarInfo("HELPBAR_TYPE_VOICE_CUSTOMIZE", "" ,"", true ); 

		var i=0;
		for(i=0; i<this.callList.length; i++)
		{
			RecogMgr.SetHelpbarItem(this.callList[i]);
		}
		
		this.SetHelpbarItem(this.cancelText);
		//this.SetHelpbarItem(this.exitText);
	}
	else if(this.voiceRecogStep =="IncomingCall")
	{
		this.InitVoiceHelpbarInfo("HELPBAR_TYPE_VOICE_CUSTOMIZE", "" ,"", true ); 

		var i=0;
		for(i=0; i<this.incomingCallList.length; i++)
		{
			RecogMgr.SetHelpbarItem(this.incomingCallList[i]);
		}	
		
		//this.SetHelpbarItem(this.exitText);
	}
	else if(this.voiceRecogStep =="Welcome")
	{
		this.InitVoiceHelpbarInfo("HELPBAR_TYPE_VOICE_CUSTOMIZE", "" ,"", true ); 
		
		for(i=0; i<this.snsTabInfo.commandText.length; i++)
		{
			var isServiceInstalled = utilitySet.isServiceActivated(this.snsTabInfo.serviceId[i]);
			if(isServiceInstalled ==true)
			{
				RecogMgr.SetHelpbarItem(this.snsTabInfo.commandText[i]);
			}			
		}
		
		this.SetHelpbarItem(this.changeViewText);
		//this.SetHelpbarItem(this.exitText);
	}

	var voiceHelpbarInfo_string = JSON.stringify(this.voiceHelpbarInfo);
	//voiceRecognitionHandlerSet.setSkyeVoiceHelpBar(voiceHelpbarInfo_string);
	webapis.recognition.SetVoiceHelpbarInfo(voiceHelpbarInfo_string);
		
}

RecogMgr.SetRecogStep = function(step)
{
	this.voiceRecogStep = step;
}


RecogMgr.InitVoiceHelpbarInfo = function(helpbarType, guideTest, exampleText, bKeepCurrentInfo)
{
	this.voiceHelpbarInfo = new Object();
	
	this.SetHelpbarType(helpbarType);
	this.SetGuideText(guideTest);
	this.SetExampleText(exampleText);
	this.SetKeepCurrentInfo(bKeepCurrentInfo);
	
	this.voiceHelpbarInfo.candidateList = new Array();
	this.voiceHelpbarInfo.helpbarItemsList = new Array();
	
	RecogMgr.InitVoiceRecogCandidateData();
}

RecogMgr.InitVoiceRecogCandidateData = function()
{
	this.cancelText = g_Voice.SID_CANCEL;
	this.exitText = g_Voice.SID_EXIT ;
	this.voiceCallText = g_Voice.IDWS_MOIP_VOICE_CALL_KR_VOICE;
	this.videoCallText = g_Voice.IDWS_MOIP_VIDEO_CALL_KR_VOICE;
	this.declineText = g_Voice.TV_SID_VOICE_DECLINE;
	this.answerText = g_Voice.TV_VCMD_ANSWER;
	
	//for social TV
	this.changeViewText = g_Voice.TV_SID_CHANGE_VIEW;
	
	this.snsTabInfo.commandText = new Array();
	this.snsTabInfo.serviceId = new Array();
	
	this.snsTabInfo.commandText = [
							g_Voice.SID_VOICE_TWEET_LIVE, 
							g_Voice.TV_SID_TWITTER, 
							g_Voice.TV_SID_FACEBOOK, 
							g_Voice.TV_SID_GOOGLE_TALK
							];
							
	this.snsTabInfo.serviceId = [
							TWITTER_LIVE_SERVICE, 
							TWITTER_ID, 
							FACEBOOK_ID, 
							GTALK_ID
							];
	
	var i=0;						
	for (i=0; i<this.snsTabInfo.serviceId.length ; i++)
	{
		alert("##mini##***sns servise id : "+i+" -> "+this.snsTabInfo.serviceId[i]);
	}
								
	if(this.voiceRecogStep =="Main")
	{						
		var STX = String.fromCharCode(2);
		var ETX = String.fromCharCode(3);
		var splitter = ETX + STX;
			
		var contactList_string = PluginAPIMgr.GetOnlineContacts();
		this.contactList = contactList_string.split(splitter);
		this.contactList.pop(); // delete empty item
	}
	else if(this.voiceRecogStep =="StartCall")
	{
		var pstn_name = PluginAPIMgr.GetPSTNNumberWithIndex(this.recogContactIndex);
		if(pstn_name.length > 0 || this.recogContactName == "echo123")
		{
			this.callList= [
					this.voiceCallText
					];
		}
		else
		{
			var camState = PluginAPIMgr.GetCameraState();
			if(camState ==  ENUM_CAM_STATE.CAM_STATE_READY || camState == ENUM_CAM_STATE.CAM_STATE_PLAYING)
			{
				this.callList= [ 
						this.videoCallText, 
						this.voiceCallText					
						];
			}
			else
			{
				this.callList= [
					this.voiceCallText
					];
			}
		}		
	}
	else if(this.voiceRecogStep =="IncomingCall")
	{
		if(PluginAPIMgr.IsConference() == 0)
		{
			var camState = PluginAPIMgr.GetCameraState();
			if(camState ==  ENUM_CAM_STATE.CAM_STATE_READY || camState == ENUM_CAM_STATE.CAM_STATE_PLAYING)
			{
				this.incomingCallList = [					
						this.videoCallText,
						this.voiceCallText,
						this.declineText
						];
			}
			else
			{
				this.incomingCallList = [					
					this.voiceCallText,
					this.declineText
					];
			}
		}
		else
		{
			this.incomingCallList = [					
					this.answerText,
					this.declineText
					];
		}
	}
}


RecogMgr.SetHelpbarType = function(type)
{
	this.voiceHelpbarInfo.helpbarType = type;
}

RecogMgr.SetGuideText = function(text)
{
	this.voiceHelpbarInfo.guideText = text;
}

RecogMgr.SetExampleText = function(text)
{
	this.voiceHelpbarInfo.exampleText = text;
}

RecogMgr.SetKeepCurrentInfo = function(flag)
{
	this.voiceHelpbarInfo.bKeepCurrentInfo = flag;
}

RecogMgr.SetCandidateList = function(candidate_string)
{
	var index = this.voiceHelpbarInfo.candidateList.length;
	
	this.voiceHelpbarInfo.candidateList[index]= new Object();
	this.voiceHelpbarInfo.candidateList[index].candidate = candidate_string;	
}

RecogMgr.SetHelpbarItemsList= function(item_text , command_string_list)
{
	var index = this.voiceHelpbarInfo.helpbarItemsList.length;
	
	this.voiceHelpbarInfo.helpbarItemsList[index]= new Object();
	this.voiceHelpbarInfo.helpbarItemsList[index].itemText = item_text;
	this.voiceHelpbarInfo.helpbarItemsList[index].commandList = new Array();
	
	var i=0;
	for(i=0; i<command_string_list.length; i++)
	{
		this.voiceHelpbarInfo.helpbarItemsList[index].commandList[i] = new Object();
		this.voiceHelpbarInfo.helpbarItemsList[index].commandList[i].command = command_string_list[i];
	}
}

RecogMgr.SetHelpbarItem= function(command_string)
{
	var index = this.voiceHelpbarInfo.helpbarItemsList.length;
	
	this.voiceHelpbarInfo.helpbarItemsList[index]= new Object();
	this.voiceHelpbarInfo.helpbarItemsList[index].itemText = command_string;
	this.voiceHelpbarInfo.helpbarItemsList[index].commandList = new Array();
	
	this.voiceHelpbarInfo.helpbarItemsList[index].commandList[0] = new Object();
	this.voiceHelpbarInfo.helpbarItemsList[index].commandList[0].command = command_string;
}




RecogMgr.ProcessVoiceRecogResult = function(result)
{
	alert("##mini##***RecogMgr.ProcessVoiceRecogResult -> "+ result);
		
	var i=0;
	var result_length = result.length;
	
	if(this.voiceRecogStep =="Main" || this.voiceRecogStep =="Welcome")
	{
		if(this.exitText == result  || this.changeViewText == result)
		{
			if(this.exitText == result)
			{
				$.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).processExitKey();
			}
			
			if(this.changeViewText == result)
			{
				//voiceActionHandlerSet.executeChangeView();
				backgroundHandler.executeBlueHelpAction();
			}
			
			var LoginState = PluginAPIMgr.GetLoginState();
			if(LoginState == "0")
			{
				this.StartVoiceHelpbar("Welcome");	
			}
			else if(LoginState == "2")
			{
				this.StartVoiceHelpbar("Main");
			}
			
			return;			
		}
		
		for(i=0; i<this.snsTabInfo.commandText.length; i++)
		{
			if(this.snsTabInfo.commandText[i].length == result_length)
			{
				if(this.snsTabInfo.commandText[i]==result)
				{
					var isServiceInstalled = utilitySet.isServiceActivated(this.snsTabInfo.serviceId[i]);
					if(isServiceInstalled==true)
					{
						RecogMgr.StopVoiceRecog();
					
						 var object = {
						 		id : this.snsTabInfo.serviceId[i]+'_tab'
						 };
						 					 
	    				 //mouseActionSet.tabClick(object);
	    				 tabMoveHandler.tabClick(object);
	    				 
	    				 return;
					}
				}
			}
		}
	}
	
	if(this.voiceRecogStep =="Main")
	{
		for(i=0; i<this.contactList.length; i++)
		{
			if(this.contactList[i].length == result_length)
			{
				if(this.contactList[i]==result)
				{
						this.recogContactName = PluginAPIMgr.GetSkypeNameWithDisplayName(result);
						
						if(this.recogContactName.length >0)
						{
							alert("##mini##***RecogMgr.ProcessVoiceRecogResult : Callee name -> "+ this.recogContactName);
						RecogMgr.RecognizeContact(result);
						this.StartVoiceHelpbar("StartCall");
						return;
					}	
				}
			}
		}
		
		this.StartVoiceHelpbar("Main");
	}
	else if(this.voiceRecogStep =="StartCall")	
	{
		if(this.cancelText == result)
		{
			StateMgr.Stop_Mini_Contact_Call_Menu(true);
			this.StartVoiceHelpbar("Main");
			
			return;
		}
		
		for(i=0; i<this.callList.length; i++)
		{
			if(this.callList[i].length == result_length)
			{
				if(this.callList[i] == result)
				{
					if(this.videoCallText ==result)
					{
						ProcMgr.CreateCall(this.recogContactName, 1);
					}
					else
					{
						ProcMgr.CreateCall(this.recogContactName, 0);
					}
								
					return;	
				}					
			}						
		}	
	}
	else if(this.voiceRecogStep =="IncomingCall")
	{
		if(this.videoCallText ==result)
		{
			ProcMgr.AnswerByVideo();
		}
		else if(this.voiceCallText == result || this.answerText== result )
		{
			ProcMgr.AnswerByVoice();
		}
		else if(this.declineText == result)
		{
			if(PluginAPIMgr.IsConference() == 1)
			{
				ProcMgr.DeclineIncomingConferenceCall();
			}
			else
			{
				ProcMgr.DeclineIncomingCall();
			}
		}
	}
}

RecogMgr.RecognizeContact = function(name)
{
	this.recogContactIndex = PluginAPIMgr.GetIndexOfContact(this.recogContactName);
	
	alert("##mini##**RecogMgr.RecognizeContact -> index: "+ this.recogContactIndex +" name: "+this.recogContactName);
	
	//stop SubState
	StateMgr.ReturnToMainState();
	
	//if($.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).moveFocusToSkype == true && $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).tabFocus == true)
	if(utilitySet.getCurrentFocusedSNSId()==WIDGET.id && $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).tabFocus == true)
	{
		$.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).tabFocus = false;
		utilitySet.changeTabBGStatus(WIDGET.id,'focus',true);
		StateMgr.Focus_Current_State()
	}
	
	WndMgr.SelectItemOfContactList(parseInt(this.recogContactIndex ));	
}


