
var CHAT_BUTTON_HIGHLIGHT = { 

	NO_ONE				:	0,
	CHAT_BUTTON			:	1,
	POPUP_AND_SUB_LIST	:	2,
	MAX					:	3

}

var CHAT_BUTTON_MODE = { 

	NOTHING_TYPE			:	0,
	NORMAL_TYPE				:	1

}


function ChatHistoryFuncButtons () {

	this.Frame			    = null,
		
	this.ChatButton		    = null,
	
	this.CurPosition		= -1;
	this.CurChatIndex = -1;
	
	this.LastHighlight			= CHAT_BUTTON_HIGHLIGHT.NO_ONE;	
	this.LastHighlightButton	= null;	

	this.PrevButtonMode     = CHAT_BUTTON_MODE.NOTHING_TYPE;
	this.ButtonMode			= CHAT_BUTTON_MODE.NOTHING_TYPE;	
	
	//flag
	this.flag_Focus			= false;
	this.flag_PrevNewChat	= false;	
	this.flag_CurrNewChat	= false;
}

ChatHistoryFuncButtons.prototype.Create = function() {

	alert("ChatHistoryFuncButtons.Create()");
	
    this.Frame = new CompBase();
    this.Frame.CreateBG("chatButtons");
	
    //create chat button
	this.ChatButton = new ButtonBase();
    this.ChatButton.Create("chatChatButton",BUTTON_BASE_TYPE.BG_FG_TEXT);
    this.ChatButton.SetBgImage(BUTTON_BASE_STATE.NORMAL, g_Image.IMAGE_LIST_MAIN_CONTACT_LIST_ITEM_VIDEO_BTN_NORMAL_BG);
    this.ChatButton.SetBgImage(BUTTON_BASE_STATE.HIGHLIGHT, g_Image.IMAGE_LIST_MAIN_CONTACT_LIST_ITEM_VIDEO_BTN_HIGHLIGHT_BG);	
    this.ChatButton.SetBgImage(BUTTON_BASE_STATE.SELECT, g_Image.IMAGE_LIST_MAIN_CONTACT_LIST_ITEM_VIDEO_BTN_SELECT_BG);
    this.ChatButton.SetText(TV_SID_CHAT_M_DO_CHAT);

	this.ChatButton.SetTextStyle(BUTTON_BASE_STATE.NORMAL,"cFuncButton_Text_Blur");
	this.ChatButton.SetTextStyle(BUTTON_BASE_STATE.HIGHLIGHT,"cFuncButton_Text_Highlight");
	this.ChatButton.SetFgImage(BUTTON_BASE_STATE.NORMAL,g_Image.IMAGE_LIST_MAIN_CONTACT_LIST_ITEM_CHAT_ICON);
	this.ChatButton.SetFgImage(BUTTON_BASE_STATE.HIGHLIGHT,g_Image.IMAGE_LIST_MAIN_CONTACT_LIST_ITEM_CHAT_ICON);
    this.ChatButton.SetID(CHAT_BUTTON_HIGHLIGHT.CHAT_BUTTON);

    this.ChatButton.SetOnClickCallbackFunc(this.ClickChatButton,true);

	this.LastHighlight = CHAT_BUTTON_HIGHLIGHT.NO_ONE;
						

}

ChatHistoryFuncButtons.prototype.Show = function() {
	
    this.Frame.Show();
} 

ChatHistoryFuncButtons.prototype.Hide = function() {

    this.Frame.Hide();   
    
}

ChatHistoryFuncButtons.prototype.SetCurPosition = function(nItemIndex) {

	alert("ChatHistoryFuncButtons.SetCurPosition(" + nItemIndex + ")");
	
	if(nItemIndex < 0 || nItemIndex > MaxNumOfMainItem){
		
		alert("Error nItemIndex is not valid!!!");
		this.CurPosition = 0;
	}
	else {
		this.CurPosition = nItemIndex;
	}
    
	this.MovePosition();

}

ChatHistoryFuncButtons.prototype.SetCurProperty = function(nCurChatIndex) {

	alert("ChatHistoryFuncButtons.SetCurProperty");

	this.CurChatIndex = nCurChatIndex;

	var NumOfNewChat = PluginAPIMgr.GetChatPropertyWithIndex(nCurChatIndex,4);
	alert("NumOfNewChat = " + NumOfNewChat);
	
	if(NumOfNewChat > 0)
	{
		this.flag_CurrNewChat = true;
	}
	else{
		this.flag_CurrNewChat = false;
	}
	
	var CurButtonType;

	//temp
	
	CurButtonType = CHAT_BUTTON_MODE.NORMAL_TYPE;
	
    this.PrevButtonMode = this.ButtonMode;
	this.ButtonMode = CurButtonType;
	
	this.InitButtons();

}

ChatHistoryFuncButtons.prototype.MovePosition = function() {

	var tempY = (this.CurPosition * NORMAL_ITEM_H);	
	var TopP = tempY + 'px';
	alert("TopP = " + TopP);
	
	$("#"+ this.Frame.GetBGLayer().Obj).css('top', TopP );
		
}

ChatHistoryFuncButtons.prototype.InitButtons = function(CurButtonType) {
    
	alert("ChatHistoryFuncButtons.InitButtons");

	if(this.flag_CurrNewChat != this.flag_PrevNewChat)
	{
		
		this.ChatButton.SetText(TV_CHAT_SCREEN);
		
		var CurLangID = ResMgr.GetCurLanguage();
		
		if(this.flag_CurrNewChat == true)
		{
			this.ChatButton.SetText(TV_CHAT_SCREEN);
		 	
		 	if(CurLangID == ENUM_TV_LANGUAGE.FRA || CurLangID == ENUM_TV_LANGUAGE.SLK || CurLangID == ENUM_TV_LANGUAGE.XHO)
		 	{					
				$("#chatChatButtonText").css('font-size', "15px" );
			}
		}			
		else
		{
			this.ChatButton.SetText(TV_SID_CHAT_M_DO_CHAT);
		
		 	if(CurLangID == ENUM_TV_LANGUAGE.FRA || CurLangID == ENUM_TV_LANGUAGE.SLK || CurLangID == ENUM_TV_LANGUAGE.XHO)
		 	{					
				$("#chatChatButtonText").css('font-size', "17px" );
			}
			
		}
	}

	this.flag_PrevNewChat = this.flag_CurrNewChat;
		
	if(this.PrevButtonMode == this.ButtonMode) {
		alert("Not Changed");
		return;
	}
		
	//nothing
	if(this.ButtonMode == CHAT_BUTTON_MODE.NORMAL_TYPE)
	{
		this.ChatButton.Show();
	}
				
}

ChatHistoryFuncButtons.prototype.Highlight = function(){

	alert("ChatHistoryFuncButtons.Highlight");
	
	//unselect button
    if(this.LastHighlightButton && this.LastHighlightButton.GetState() == BUTTON_BASE_STATE.SELECT)
    {
		//check CONTACT_BUTTON_HIGHLIGHT.SELECT_POPUP_AND_SUB_LIST
		alert("unSelect!!!");
        this.LastHighlightButton.highlight();
        this.LastHighlight = this.LastHighlightButton.GetID();
    }
	//move contact with Focus
	else if(this.flag_Focus){
		
		//initialize highlight
		if(this.CheckInitHighlight()){
			
			this.InitializeHighlight();	
		}
		
	}
    else if(this.LastHighlight == CHAT_BUTTON_HIGHLIGHT.NO_ONE){
		
          this.InitializeHighlight();     
    } 
	else{
		
		//return focus to previous button
		if(this.LastHighlightButton){
			this.LastHighlightButton.highlight();
			this.LastHighlight = this.LastHighlightButton.GetID();
		}		
	}
	
	this.flag_Focus = true;
}

ChatHistoryFuncButtons.prototype.Blur = function(){	
	alert("ChatHistoryFuncButtons.Blur");	
	
	if(this.LastHighlight == CHAT_BUTTON_HIGHLIGHT.NO_ONE){
		//highlight button change to blur
		if(this.LastHighlightButton){			
		    this.LastHighlightButton.blur();
			this.LastHighlightButton = null;			
		}
	}
	//case: Popup, to return here!!! 	
	else{		
		//highlight button change to blur
		if(this.LastHighlightButton){			
		    this.LastHighlightButton.blur();			
		}	
	}
	this.flag_Focus = false;
	
}

ChatHistoryFuncButtons.prototype.CheckInitHighlight = function(){
	alert("ChatHistoryFuncButtons.CheckInitHighlight = " + this.ButtonMode);
	//1. 
	if(this.LastHighlight == CHAT_BUTTON_HIGHLIGHT.NO_ONE)
	{
		return true;
	}
	//2.
	if(this.ButtonMode == this.PrevButtonMode){		
		return false;
	}
	//3.
	
	return false;
	
}


ChatHistoryFuncButtons.prototype.InitializeHighlight = function(){

	//09.01
	//prev highlight blur....only highlight remove
	if(this.LastHighlightButton && this.LastHighlightButton.GetState() == BUTTON_BASE_STATE.HIGHLIGHT){
		this.LastHighlightButton.blur();
	}		
			
	alert("InitializeHighlight");

    if(this.ButtonMode == CHAT_BUTTON_MODE.NORMAL_TYPE)
    {
		
        this.ChatButton.highlight();
        this.LastHighlightButton = this.ChatButton;
        this.LastHighlight = CHAT_BUTTON_HIGHLIGHT.CHAT_BUTTON;             
        
    }		

}

ChatHistoryFuncButtons.prototype.KeyProcess = function (keyCode) {
	
	alert("ChatHistoryFuncButtons.KeyProcess");
	switch (keyCode) {

		case sf.key.LEFT:
			alert("KEY_LEFT");
			this.processLeftKey();
			break;	
			
		case sf.key.RIGHT:
			alert("KEY_RIGHT");
			this.processRightKey();
			break;
			
		case sf.key.ENTER:
			alert("KEY_ENTER");
			this.processEnterKey();
			break;
		
		case sf.key.TOOLS:
			alert("KEY_TOOLS");
			this.processToolKey();
			break;
			
		case sf.key.RETURN:
			alert("KEY_RETURN");
			this.processReturnKey();
			sf.key.preventDefault();
			break;	

		case sf.key.EXIT:
			alert("KEY_EXIT");			
			StateMgr.Start_Exit_State(false);
			sf.key.preventDefault();
			break;	
            
         case 147:  //infolink
         case 261:  //contents
         case 115:  //wiselink
            ProcMgr.processInfolinkKey(); 
			break;
            
         default:
            ProcMgr.processBlockKey(keyCode);
            break;  

	}
	
}

ChatHistoryFuncButtons.prototype.processLeftKey = function() {
    
	if(this.LastHighlight == CHAT_BUTTON_HIGHLIGHT.CHAT_BUTTON) {

		this.LastHighlight = CHAT_BUTTON_HIGHLIGHT.NO_ONE;		        
		WndMgr.ReturnSubToMain();
			
	}

}

ChatHistoryFuncButtons.prototype.processRightKey = function() {
    
	//
}


ChatHistoryFuncButtons.prototype.processReturnKey = function() {
    
	alert("ChatHistoryFuncButtons.processReturnKey");

	this.LastHighlight = CHAT_BUTTON_HIGHLIGHT.NO_ONE;		        
	WndMgr.ReturnSubToMain();
	
    //sf.scene.focus('ListMain');

	
}

ChatHistoryFuncButtons.prototype.processToolKey = function() {

	var IsInCall = ProcMgr.GetIsInCall();
	
	if(IsInCall){
		WndMgr.StartOneBottonPopup(OneBtnPopupIndex.NOT_AVAILABLE_DURING_CALL_IN_CHAT_LIST);
	}
	else{

		this.LastHighlight = CHAT_BUTTON_HIGHLIGHT.POPUP_AND_SUB_LIST;	

		StateMgr.Start_List_Tools_State(TOOL_MENU_LIST_TYPE.CHAT_LIST);	
	}	   

}

ChatHistoryFuncButtons.prototype.processEnterKey = function() {
    	
	if(this.LastHighlight == CHAT_BUTTON_HIGHLIGHT.CHAT_BUTTON) {

        this.LastHighlight = CHAT_BUTTON_HIGHLIGHT.NO_ONE;

		//TEMPCODE:
		var scene = sf.scene.get("ListMainProfile");
		var ChatName = scene.list.GetCurChatName();
			
		ProcMgr.OpenChat(ChatName);			
	}
		
}

ChatHistoryFuncButtons.prototype.HideSubScene = function(){
	
	//this.flag_Camera_State = PluginAPIMgr.GetCameraState();
}

ChatHistoryFuncButtons.prototype.ReSetHighlight = function(){
	
}


ChatHistoryFuncButtons.prototype.ClickChatButton = function(){
	alert("ChatHistoryFuncButtons.ClickChatButton");	

	var ReturnScene = sf.scene.get('ListMainProfile');
	var This_ = ReturnScene.list.FuncPopup;
	var IsInCall = ProcMgr.GetIsInCall();

	if(event.button == 2 && IsInCall == true)
	{
		alert("Do Nothing!!!!");	
		return;
	}

    if(This_.flag_Focus == false)
	{   
		sf.scene.get('ListMain').selectOnCurrHighlight();
		This_.flag_Focus = true;
		This_.CurSubScene = null;			
	}

	if(This_.LastHighlightButton)
	{
		This_.LastHighlightButton.blur();
	}
		
	This_.LastHighlightButton = This_.ChatButton;
	This_.ChatButton.highlight();
	
	if(event.button == 2)
	{
		This_.LastHighlight = CHAT_BUTTON_HIGHLIGHT.POPUP_AND_SUB_LIST;	

		StateMgr.Start_List_Tools_State(TOOL_MENU_LIST_TYPE.CHAT_LIST);	
	}
	else
	{
		This_.LastHighlight = CHAT_BUTTON_HIGHLIGHT.NO_ONE;
			
		var ChatName = ReturnScene.list.GetCurChatName();			
		ProcMgr.OpenChat(ChatName);	
	}
	
}
