/*
 Title: SNSList.js
 Skype Mini widget
 */
alert('SceneSNSList(Skype).js loaded');
/*
 Class: SceneSNSList
 Control Skype Mini widget
 */
var WIDGET = {
	id : "Skype",
	name : "SNSList"
};
var SKYPE_DIV = "feed_content_list_Skype";
var Flag_Hidden_State = false;
var SKYPE_MINI_PATH = "/mtd_down/widgets/user/Skype/app/mini/";

function SceneSNSList() {
	this.isFocused = false;
	this.isLogin = false;
	this.isInit = false;
	this.moveFocusToSkype= false;
	this.tabFocus = true;
	this.helpbarType = 0;
	$this = this;
}

SceneSNSList.prototype.initialize = function() {
	alert("##mini##SceneSNSList.initialize()");
	// this function will be called only once when the scene manager show this scene first time
	// initialize the scene controls and styles, and initialize your variables here
	// scene HTML and CSS will be loaded before this function is called

	$("#"+SKYPE_DIV).css("top","0px");
	$("#"+SKYPE_DIV).css("height","440px");
	$("#"+SKYPE_DIV).css("overflow","hidden");
	
	if(document.getElementById("miniOneButtonPopup_id")==null)
		$("#Default").append("<div id='miniOneButtonPopup_id'>");
	if(document.getElementById("miniCallEnding_id")==null)	
		$("#Default").append("<div id='miniCallEnding_id'></div>");
	if(document.getElementById("miniCallConfirmPopup_id")==null)
		$("#Default").append("<div id='miniCallConfirmPopup_id'></div>");
	if(document.getElementById("miniVolume_id")==null)
		$("#Default").append("<div id='miniVolume_id'></div>");
				
	$("div.mouse_down_arrow").hide();
	$("div.mouse_up_arrow").hide();
	this.initSkype();
	

};

SceneSNSList.prototype.handleShow = function() {
	alert("##mini##SceneSNSList.handleShow()");
	// this function will be called when the scene manager show this scene
	//if(this.isInit)
	//	StateMgr.Start_Current_State();
}

SceneSNSList.prototype.handleHide = function() {
	alert("##mini##SceneSNSList.handleHide()");
	// this function will be called when the scene manager hide this scene
	//if(this.isInit)
	//	StateMgr.Hide_Current_State();
}

SceneSNSList.prototype.handleFocus = function() {
	alert("##mini##SceneSNSList.handleFocus() : moveFocusToSkype = "+this.moveFocusToSkype);
	
	$("div.mouse_down_arrow").hide();
	$("div.mouse_up_arrow").hide();
	this.ShowKeyHelpbar();
	// this function will be called when the scene manager focus this scene
	if(this.moveFocusToSkype==false){		
		if(Flag_Hidden_State ==true)
		{
			this.onShow();	
			return;
		}
		
		this.moveFocusToSkype=true;
		if(this.tabFocus == false)
		{				
			utilitySet.changeTabBGStatus(WIDGET.id,'focus',true);
			StateMgr.Focus_Current_State();
		}
		else
		{
			utilitySet.changeTabBGStatus(WIDGET.id,'focus',false);	
		}
		
		RecogMgr.StartVoiceRecog();
	}
	else{
		utilitySet.changeTabBGStatus(WIDGET.id,'focus',false);	
		this.tabFocus = true;
	}
	/*add by minseok : mouse click*/
	var Main_Scene = $.sfScene.get({id:"miniMainContact", scope:WIDGET.id});
	if(Main_Scene){
		if(Main_Scene.list.Flag_Select==true){
			Main_Scene.list.blur();
		}
	}
	StateMgr.StopSubState();		//for mouse click
	// $.sfScene.focus({id:"miniWelcome", scope:WIDGET.id});
	
}

SceneSNSList.prototype.handleBlur = function() {
	alert("##mini##SceneSNSList.handleBlur()");
	//this.isFocused = false;
	// this function will be called when the scene manager move focus to another scene from this scene
}
/*
 Function: handleKeyDown
 Key handler

 Parameters:
 keyCode - Key code
 */
SceneSNSList.prototype.handleKeyDown = function(keyCode) {
	alert("##mini##SceneSNSList.handleKeyDown(" + keyCode + ")");
	if(TVCOMM_SERVICE_MODE == TVCHAT_MODE_HIDDEN && !(keyCode==$.sfKey.RETURN ||keyCode==$.sfKey.EXIT||keyCode==$.sfKey.BLUE)){
		return;
	}
	switch (keyCode) {
		case $.sfKey.DOWN:
			/**
			 * * @param service_id - SNS service id
		 		* @param state - normal or focus
		 		* @param hasfocusedItem - true or false
			 */
			this.processDownKey();
			
			break;
		case $.sfKey.LEFT:
			this.processLeftKey();

			break;
		case $.sfKey.RIGHT:
			this.processRightKey();
			break;
		case $.sfKey.RED:
			this.processRedKey();
			$.sfKey.block(event);
			break;
		case $.sfKey.TOOLS:
			this.processToolsKey();
			break;
		case $.sfKey.BLUE:
			this.processBlueKey();
			break;
		case $.sfKey.RETURN:
		case $.sfKey.EXIT:
			this.processExitKey();
			break;
	}
}

SceneSNSList.prototype.processDownKey = function() {
	alert("##mini##***** SceneSNSList.processDownKey() "+ this.moveFocusToSkype);
	if(StateMgr.GetCurrentState()=="miniWidgetLoading")
		return ;
		
	if(this.moveFocusToSkype == true)
	{
		this.tabFocus = false;
		utilitySet.changeTabBGStatus(WIDGET.id,'focus',true);
		StateMgr.Focus_Current_State()
	}
	/*else
	{
		this.moveFocusToSkype = true;
	}*/
}

SceneSNSList.prototype.processLeftKey = function() {
	
	alert("##mini##***** SceneSNSList.processLeftKey()");
	
	if(StateMgr.GetCurrentState()=="miniWidgetLoading"){
	alert("##mini##[SkypeMini] Skype Loading....");
		return;
	}

	RecogMgr.StopVoiceRecog();
	
	$.sfScene.get("Home").prevService();
	this.moveFocusToSkype = false;
}

SceneSNSList.prototype.processRightKey = function() {
	
	alert("##mini##***** SceneSNSList.processRightKey()");
	
	if(StateMgr.GetCurrentState()=="miniWidgetLoading"){
	alert("##mini##[SkypeMini] Skype Loading....");
		return;
	}
	 
	if(utilitySet.hasNextTab() != true )
	{
		return;	
	}	
	 
	RecogMgr.StopVoiceRecog();
	$.sfScene.get("Home").nextService();
	this.moveFocusToSkype = false;
}
SceneSNSList.prototype.processRedKey = function()
{
	alert("##mini##***** SceneSNSList.processRedKey()");
	
	alert("-------------------------------this.helpbarType = "+this.helpbarType);
	if(this.helpbarType == 0)
		return;
	else if(this.helpbarType==1){
		$.sfScene.get({id:"miniWelcome", scope:WIDGET.id}).processEnterKey();
		
	}else if(this.helpbarType==2){
		StateMgr.Start_Signout_State();
		//signout
	}
}
SceneSNSList.prototype.processBlueKey = function()
{
	alert("##mini##***** SceneSNSList.processBlueKey()");
	TVComm.changeMode();
	this.CheckHiddenMode();
	$.sfScene.get("Home").focusCurService();
}
SceneSNSList.prototype.processToolsKey = function()
{
	alert("##mini##***** SceneSNSList.processToolsKey()");
	
	var instanceServiceSetting = $.sfScene.get("ServiceSetting");
	instanceServiceSetting.reset();
	instanceServiceSetting.set(function(pEvent) {
		if(pEvent == EVENT_SOCIAL_KEY) {
			$this.returnToCategory();
		} else {
			$.sfScene.get("Home").focusCurService();
		}
	});
	$.sfScene.show("ServiceSetting");
	$.sfScene.focus("ServiceSetting");
	this.moveFocusToSkype = false;
}

SceneSNSList.prototype.processExitKey = function()
{
	alert("##mini##***** SceneSNSList.processExitKey()");
	$.sfKey.block(event);
	TVComm.returnEvent();
}

SceneSNSList.prototype.CheckHiddenMode = function() {
	alert("##mini##***** SceneSNSList.CheckHiddenMode()");
	if(TVCOMM_SERVICE_MODE == TVCHAT_MODE_HIDDEN){
		RecogMgr.StopVoiceRecog();
		$("#"+SKYPE_DIV).hide();
		Flag_Hidden_State = true;
		PluginAPIMgr.SetSkypeWidgetState(0);
	}else{
		RecogMgr.StartVoiceRecog();
		$("#"+SKYPE_DIV).show();
		Flag_Hidden_State = false;
		PluginAPIMgr.SetSkypeWidgetState(2);
	}
}
SceneSNSList.prototype.initSkype = function() {
	alert("##mini##***** SceneSNSList.initSkype()");

	//**********register event*************
	ProcMgr.registerKey();
	$("#HomeHelp").sfKeyHelp({
		'blue' : TV_SID_CHANGE_VIEW,
		'tools' : sfLang.SID_TOOLS,
		'return' : SID_RETURN
	});
	
	var currentLanguage = parseInt($.sf.plugin('TV').GetLanguage());
	$.sf.loadJS(SKYPE_MINI_PATH+"lang/"+TV_LANGUAGE_CODE[currentLanguage]+".js", function(){
		StateMgr.Start_Init_State();
		$.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).isInit = true;
	});
	
	/*if($.sfScene.get("Home").getCurServiceID()==WIDGET.id&&this.moveFocusToSkype==false){
		$.sfScene.focus({id:WIDGET.name, scope:WIDGET.id});
	}*/
	this.notyLoadedSkype();

	// EventMgr.initEvent();
}

SceneSNSList.prototype.notyLoadedSkype = function() {
	alert("##mini##***** SceneSNSList.notyLoadedSkype()");
	
	commonFlagSet.is_skype_loaded = true; 
	commonFlagSet.is_voice_unsubscribed = true;
	
	alert("$.sfScene.get('Home').getCurServiceID():"+$.sfScene.get("Home").getCurServiceID());
	if($.sfScene.get("Home").getCurServiceID()==WIDGET.id)
	{
		serviceFocusMoveHandler.giveFocusToMe(SKYPE_ID);
	}
}

SceneSNSList.prototype.ShowKeyHelpbar = function(){
	switch(this.helpbarType){
		case 0: // red nothing
			$("#HomeHelp").sfKeyHelp({
				'blue' : TV_SID_CHANGE_VIEW,
				'tools' : sfLang.SID_TOOLS,
				'return' : SID_RETURN
			});
			break;
		case 1: //login
			$("#HomeHelp").sfKeyHelp({
				'red' : SID_LOGIN,
				'blue' : TV_SID_CHANGE_VIEW,
				'tools' : sfLang.SID_TOOLS,
				'return' : SID_RETURN
			});
			break;
		case 2:	//logout 
			$("#HomeHelp").sfKeyHelp({
				'red' : SID_LOGOUT,
				'blue' : TV_SID_CHANGE_VIEW,
				'tools' : sfLang.SID_TOOLS,
				'return' : SID_RETURN
			});
			break;
	}
}
SceneSNSList.prototype.isReady = function() {
	alert('*****************************SceneSNSList.isReady : ' + $this.isInit);
	return $this.isInit;
}
SceneSNSList.prototype.focusFirstItem = function() {
	alert('*************SceneSNSList.focusFirstItem : isFocused = '+this.isFocused);
	/*if(this.isFocused==false){
	this.isFocused = true;
		return;
	}*/
	// this.isFocused = true;
	if(this.isFocused==false)
	{
		this.tabFocus = false;
		return;	
	}
	
	if(this.isInit)
		StateMgr.Focus_Current_State();
}


//for event!!!!
//from oter tab to skype 
SceneSNSList.prototype.FocusSkype = function()
{		
	if(Flag_Hidden_State==true)
		return false;
	if(this.tabFocus==false)
		return true;
	alert('*****************************SceneSNSList.FocusSkype');	
	utilitySet.changeTabBGStatus(WIDGET.id,'focus',true);
	this.tabFocus = false;
	return true ;
}


SceneSNSList.prototype.onShow = function(){
	alert("##mini##SceneSNSList.onShow() SceneSNSList Show...");
	ProcMgr.registerKey(); 
	if(ProcMgr.isCalling==true)
	{
		ProcMgr.registerCallKey(); 
	}
	if(Flag_Hidden_State==true)
	{
		Flag_Hidden_State = false;
		
		/*if(this.moveFocusToSkype==true)
		{
			if(this.tabFocus == false)
			{
				utilitySet.changeTabBGStatus(WIDGET.id,'focus',true);
				StateMgr.Focus_Current_State();
			}			
		}
		else
		{
			this.moveFocusToSkype =true;
			if(this.tabFocus == false)
			{
				utilitySet.changeTabBGStatus(WIDGET.id,'focus',true);
			StateMgr.Focus_Current_State();
		}	
		}*/	
		if(TVCOMM_SERVICE_MODE == TVCHAT_MODE_HIDDEN){
			PluginAPIMgr.SetSkypeWidgetState(0);
		}else{
			PluginAPIMgr.SetSkypeWidgetState(2);	//widget main start : 1, widget social start : 2, widget destroy : 0
		}	
		
	}
	
	RecogMgr.StartVoiceRecog();
	$("#"+SKYPE_DIV).show();
	$("div.mouse_down_arrow").hide();
	$("div.mouse_up_arrow").hide();
	
	
}
SceneSNSList.prototype.onHide = function(){
	
	alert("##mini##SceneSNSList.onHide() SceneSNSList hide...");
	if(Flag_Hidden_State==true)
		return;
	Flag_Hidden_State = true;
	
	RecogMgr.StopVoiceRecog();

	//2013 add by minseok : mouse click
	StateMgr.StopSubState();		//for mouse click
	//StateMgr.ReturnToMainState();
	
	if(ProcMgr.isCalling == true) // already receive inprogress
    {
   		 ProcMgr.DisconnectCall(true);
    }
	else
	{
		if(PluginAPIMgr.IsInCall()==1 && TVCOMM_SERVICE_MODE != TVCHAT_MODE_HIDDEN)  // calling 	
		{
			ProcMgr.CancelCall();
		}	
	}

	if(ProcMgr.isVoiceMailSending == true)
	{
		 ProcMgr.CancelVoiceMail();
	}
	
	//ProcMgr.isCalling = false;
	ProcMgr.unregisterKey();
	ProcMgr.unregisterCallKey();
	PluginAPIMgr.SetSkypeWidgetState(0);
	
	$("#"+SKYPE_DIV).hide();
	$("div.mouse_down_arrow").show();
	$("div.mouse_up_arrow").show();
}

SceneSNSList.prototype.onDestroy = function(){
	alert("##mini##SceneSNSList.onDestroy() SceneSNSList Destroy...");
	Flag_Hidden_State = false;
	
	RecogMgr.StopVoiceRecog();
	
	if(ProcMgr.isCalling == true) // already receive inprogress
    {
       		 ProcMgr.DisconnectCall(true);
    	}
	else
	{
		if(PluginAPIMgr.IsInCall()==1&& TVCOMM_SERVICE_MODE != TVCHAT_MODE_HIDDEN)  // calling 
		{
			ProcMgr.CancelCall();
		}	
	}
	ProcMgr.isCalling = false;
	ProcMgr.unregisterCallKey();
	ProcMgr.unregisterKey();
	
    PluginAPIMgr.SetSkypeWidgetState(0);
    
    PluginAPIMgr.closePluginAPI();
	$("div.mouse_down_arrow").show();
	$("div.mouse_up_arrow").show();
	$("#"+SKYPE_DIV).hide();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

