alert("init.js loaded");

var Flag_Hidden_State = false;
var flag_face_recog_cam = false;
var flag_face_recog_subscribe = false;
var Flag_Preload = false;
var Flag_Init = false;
var Flag_Shown_Complete = false;
//140527 add for KPILOG '14 camera
var KPI_LOG = null;

var widgetAPI = new Common.API.Widget();

function onStart() {
	// TODO : Add your Initilize code here
	// NOTE : In order to start your app, call "sf.start()" at the end of this function!!	
	alert("*****init.js onStart()");
	
	//window.addEventListener('onshow', onShow);
	//window.addEventListener('onhide', onHide);
	window.onShow = onShow;
	window.onHide = onHide;
	window.onResume = onShow;
	window.onPause = onHide;

	Flag_Shown_Complete = false;

	g_Data.InitWidgetParam();
	
	//140527 add for KPILOG '14 camera
	KPI_LOG = LogCollector.getInstance();
	
	var Widget_param = GetWedgetParameter(window.location.search);
		$.ajax({
			type:"GET",
			url : "/mtd_down/common/LogPolicyConfig.xml",
			success : function(data, textStatus, xhr){
				alert("ajax success : "+textStatus);
				var obj = sf.util.xml2obj(xhr.responseXML);
				KPI_LOG.init(Widget_param,obj,"13_facerecog");
				
			//for test
			//KPI_LOG.addLog(KPI_EVENT.SIGNIN_WITH_PWD);
			},
			error : function(xhr, textStatus, errorThrown){
				alert("ajax error : "+textStatus);
			}
		});
	
	//KPI_LOG.addLog(KPI_EVENT.CAM_CHECK,"true");
	
	ProcMgr.registerKey(); 
	
	var NNaviPlugin = sf.core.plugin('NNavi');
	NNaviPlugin.SetBannerState(1);
	
	if(Flag_Preload==false)
	{
		Flag_Init = true;
		StateMgr.Start_Init_State();
	}
	else
	{
		alert("***** preload == true -> Start_Init_State() will be called after onShow()");
	}
    
		
	//Caution!!!!! 
	//Temp file     by Lee Donghee
	//VirtualEventMgr.vtSkypeLoadedEvent();

	//focus!!
    // window.addEventListener('click', $.sf.returnFocus);
    // $(document).bind('contextmenu',function(){
		// $.sf.returnFocus();
	// });
}

function onDestroy() {
	// stop your XHR or Ajax operation and put codes to distroy your application here
	
	alert("*****init.js onDestroy()");
	
	Flag_Hidden_State = false;
	Flag_Preload = false;
	Flag_Init = false;
		
	if(StateMgr.GetCurrentState()== "VideoSnapshot" || StateMgr.GetCurrentState()== "CameraCheckConnected")
	{
		PluginAPIMgr.StopPreview();	
	}

	if(ProcMgr.isVoiceMailSending == true)
	{
		 ProcMgr.CancelVoiceMail();
	}
	
    if(ProcMgr.isCalling == true) // already receive inprogress
    {
    	if(PluginAPIMgr.IsSkypeSupportMultitasking()==0)
    	{
    		ProcMgr.DisconnectCall(true);
    	}       
    }
	else
	{
		if(PluginAPIMgr.IsInCall()==1)  // calling 
		{
			ProcMgr.CancelCall();
		}	
	}
    
	ProcMgr.isVoiceMailSending = false;
    ProcMgr.isCalling = false;
    
     if(ProcMgr.isChatting ==true)
    {
        ProcMgr.DisconnectChat(); 
    }
	
	if(g_Data.GetIndexOfPlayingVoicemail()>=0)
	{
		ProcMgr.StopHistoryVoicemail(g_Data.GetIndexOfPlayingVoicemail());
	}
	
    
	ProcMgr.unregisterCallKey();
	ProcMgr.unregisterKey();
	
	if(g_NumObj != null)
	{
		g_AppCommon.UnsubscribeEvent(APPCOMMON_MESSAGE_QWERTY_KEY_INPUT);
	}
	
    PluginAPIMgr.SetSkypeWidgetState(0);
    
    ProcMgr.ClosePluginAboutNTV();
    PluginAPIMgr.closePluginAPI();

	//click
    //window.removeEventListener('click', $.sf.returnFocus);
	
}

function onShow(event)
{
	alert("*****init.js onShow() flag_hidden -> "+Flag_Hidden_State);
	Flag_Hidden_State = false;
	
	var NNaviPlugin = sf.core.plugin('NNavi');
	NNaviPlugin.SetBannerState(1);

	ProcMgr.registerKey(); 
	if(ProcMgr.isCalling==true)
	{
		ProcMgr.registerCallKey(); 
	}
	
	if(EventMgr.isHighlight==false){
		EventMgr.isHighlight = true;
		WndMgr.HideWidgetDimBG();
	}
	
	if(Flag_Init==false)
	{
		Flag_Preload = false; //init
			
		//for app jump 
		GetWedgetParameter(event.data);
		Flag_Init = true;
		StateMgr.Start_Init_State();
			
	}
	else
	{		
		Flag_Preload = false; //init
				
		//for app jump 
		GetWedgetParameter(event.data);
				
		//must call [SetRect] function before [SetSkypeWidgetState].
		//reset call video rect
		PluginAPIMgr.GetPanelResolution();
		
		ProcMgr.SetVideoCallRect(false);
		
		PluginAPIMgr.WidgetHandleIncomingCall(1); //to display Incomgin Call scene of widget
		PluginAPIMgr.SetSkypeWidgetState(1);	//widget start : 1, widget destroy : 0
		EventMgr.isHighlight = true; //init
				 
		if(PluginAPIMgr.GetLoginState()=="2")
		{			
			var currentState =StateMgr.GetCurrentState();
			
			if(g_Data.WIDGET_PARAM.socail == true)
	        {
				if(g_Data.WIDGET_PARAM.mode == 'Voicemail')
	            {
	            	//onshow skype & play voicemail in skype from social panel 
			        ProcMgr.PlayVoicemailforJump(g_Data.WIDGET_PARAM.timestamp);
	            }
				else if(g_Data.WIDGET_PARAM.mode == 'Calling')
				{					
					//onshow skype & call buddy in skype from social panel
					ProcMgr.CreateCallforJump(g_Data.WIDGET_PARAM.skypename,g_Data.WIDGET_PARAM.calltype);
	        	}
	        	else if(g_Data.WIDGET_PARAM.mode =='MissedCallHistory')
	        	{
	        		//ProcMgr.CheckMissedCallList();
	        		StateMgr.Start_Jump_To_Main_State(1,0);
	        	}
	    	}
			else if(currentState== "VideoSnapshot")
			{
				ProcMgr.StartPreviewOfVideoSnapshot();
			}
			else if(currentState== "CameraCheckConnected")
			{
				ProcMgr.StartPreviewOfCameraCheckConnected();
			}
			else if(currentState== "CallCameraControl")
			{
				sf.scene.get('CallCameraControl').update();
			}			
			else if(currentState=="FaceRecogRegistration"){
				if(flag_face_recog_cam==true){
					PluginAPIMgr.StartCameraForFaceRecognition(11,330,1280,720);
					flag_face_recog_cam = false;
				}
				if(flag_face_recog_subscribe==true){
					PluginAPIMgr.SubscribeFaceRecogEvent();
					ProcMgr.RetryFaceDetect();
					flag_face_recog_subscribe = false;
				}
			}
			//updaet video call(only skype call)
			else if(currentState=="VideoCall")
			{
				WndMgr.UpdateVideoCallMyVideo();
			}
			else if(ProcMgr.isIncomingCall == true)
			{
				if(!PluginAPIMgr.IsCallInProgress()) //incoming call
				{
					/*PluginAPIMgr.StopRing();
         			PluginAPIMgr.StartIncomingRing();*/
         			var NNaviPlugin = sf.core.plugin('NNavi');
					NNaviPlugin.SetBannerState(1);
         			PluginAPIMgr.MuteSpeaker(false);
				}
			}
		}
		else
		{
			//add by minseok
			// PluginAPIMgr.OpenRecognitionPlugin();	
			var currentState =StateMgr.GetCurrentState();
			if(currentState=="FaceRecogSignin"){
				if(flag_face_recog_cam==true){
					PluginAPIMgr.StartCameraForFaceRecognition(11,330,1280,720);
					flag_face_recog_cam = false;
				}
				if(flag_face_recog_subscribe==true){
					PluginAPIMgr.SubscribeFaceRecogEvent();
					flag_face_recog_subscribe = false;
				}
				
			}
		}
		
		curWidget.onWidgetEvent = OnWidgetEngineEvent;	
		RecogMgr.StartVoiceRecog();	
	}
	
	g_Data.CheckWidgetParamFormApp();
	Flag_Shown_Complete = true;
	
	//add by minseok : mouse show/hide
	if(PluginAPIMgr.GetMouseVisibility()==1)
	{
	 	alert("GET_EVENT_MOUSE_STATE_SHOW");
		WndMgr.SetFlagMouseCusorShow(true);
		WndMgr.ShowReturnIcon();
	}
	else
	{
	 	alert("GET_EVENT_MOUSE_STATE_HIDE");
		WndMgr.SetFlagMouseCusorShow(false);
		WndMgr.HideReturnIcon();
		if(StateMgr.MainStateList[MAIN_STATE.MAIN].flagLoad){
		var scene = sf.scene.get("ListMain");
		if(scene){
			scene.InitMouseOver();
		} 
		}
		WndMgr.HideTabOverImage();
	}
	
	
	//140528 add minseok for 14 camera check
	ProcMgr.Check14CameraForKPILog();
}


function onHide()
{
	alert("*****init.js onHide()");
	Flag_Hidden_State = true;
	Flag_Shown_Complete = false;
	
	var currentState = StateMgr.GetCurrentState();

	if(PluginAPIMgr.IsSkypeSupportMultitasking()==1)
	{
		ProcMgr.ChangeVoiceCall();
	}
	else
	{
		if(ProcMgr.isCalling == true)
		{
			ProcMgr.DisconnectCall(true);
		}		
	}
    
	//change position 
	RecogMgr.StopVoiceRecog();
	//2013 add by minseok : close ime
	if(WndMgr.isShowTextEditor == true){
		WndMgr.CancelTextEditor();
	}else if(WndMgr.isShowAddContactSkypeName == true){
		WndMgr.BackStepAddContact();
	}else if(WndMgr.isShowRename == true){
		StateMgr.Stop_Rename_State(true);
	}else if(WndMgr.isShowCheckPwd == true){
		ProcMgr.CancelCheckPwd();
	}	

	//2013 add by minseok
	if(PluginAPIMgr.GetLoginState()=="2"){
		if(currentState=="Signin" || currentState=="CreateAccount"){
			// ProcMgr.StartLoggedInState();
			StateMgr.Start_Main_State();
		}
	}
	if(currentState=="FaceRecogSignin"){
		if(ProcMgr.isFaceRecog==true){		//starting facerecogall....
			alert("Face Recog all........")
			var scene = sf.scene.get('FaceRecogSignin');
			scene.processEnterKey();		//stop
		}
		if(PluginAPIMgr.isStartSubscribe==true){
			PluginAPIMgr.UnsubscribeFaceRecogEvent();
			flag_face_recog_subscribe = true;
		}
		if(PluginAPIMgr.isStartCamForRecog==true){
			PluginAPIMgr.StopCameraForFaceRecognition();
			flag_face_recog_cam = true;
		}
	}
	if(currentState=="FaceRecogRegistration"){
		if(ProcMgr.isFaceRecog==true){
			alert("pause face detect!!!");
			ProcMgr.PauseFaceDetect();
		}
		if(PluginAPIMgr.isStartSubscribe==true){
			PluginAPIMgr.UnsubscribeFaceRecogEvent();
			flag_face_recog_subscribe = true;
		}
		if(PluginAPIMgr.isStartCamForRecog==true){
			PluginAPIMgr.StopCameraForFaceRecognition();
			flag_face_recog_cam = true;
		}	
	}
	
	if(ProcMgr.isCalling==true && currentState == "Main")
	{
		ProcMgr.StartCall(); 
	}

	if(currentState== "VideoSnapshot" || currentState == "CameraCheckConnected");
	{
		PluginAPIMgr.StopPreview();	
	}
	if(currentState == "CallCameraControl")
	{
		sf.scene.get('CallCameraControl').handleHide();
	}
	
	if(ProcMgr.isVoiceMailSending == true)
	{
		 ProcMgr.CancelVoiceMail();
	}
	
	if(ProcMgr.isCalling != true) 
	{
		if(PluginAPIMgr.IsInCall()==1)  // calling 
		{
			ProcMgr.CancelCall();
			ProcMgr.unregisterCallKey();
		}	
	}
	  
	if(g_Data.GetIndexOfPlayingVoicemail()>=0)
	{
		ProcMgr.StopHistoryVoicemail(g_Data.GetIndexOfPlayingVoicemail());
	}
	  
	if(StateMgr.GetCurrentSubState()=="Exit") 
	{
		StateMgr.Stop_Exit_State(true);
	} 
	
	g_Data.InitWidgetParam();
	ProcMgr.unregisterKey();
	PluginAPIMgr.SetSkypeWidgetState(0);
	
}
function GetWedgetParameter(s_param){
    var strParams = s_param;
    var obj = {};
    
    alert('GetWedgetParameter : strParams = ' + strParams);
        
    if (strParams.charAt(0)  == "?") 
    {
        strParams = strParams.slice(1, strParams.length);
    }

    var arrParams = strParams.split("&");    
    for (var idx=0; idx < arrParams.length; idx++) 
    {
        var paramPair = arrParams[idx].split("=");
        if (paramPair.length == 2 && paramPair[0] == "data") 
        {
        	alert("*************Skype Widget is started with parameter : " + paramPair[1]);
        	g_Data.SetWidgetParam(paramPair[1]);
        	//140527 make parameter object
        	// return;
        }
        
        //2013 add by minseok : for app jump!
        if (paramPair.length == 2 && paramPair[0] == "keyword") 
        {
        	if(paramPair[1]=="jumpFromMini"){
        		alert("*************jump from SocialTV!!!!!!!!!!");
        		g_Data.SetWidgetParamJumpFlag(true);
        		return;
        	}
        }
        
        if (paramPair.length == 2 && paramPair[0] == "preload") 
        {
        	if(paramPair[1]=="true")
        	{
        		alert("*************preload : ture");
        		Flag_Preload = true;
        	}
        	else
        	{
        		alert("*************preload : false");
        		Flag_Preload = false;
        	}
        }
        if(paramPair[1]!==undefined){
        	//alert("push parameter ("+paramPair[0]+", "+paramPair[1]+")");
        	obj[paramPair[0]] = paramPair[1];
        } 
    }
    
    return obj;
}
