var FLAG_LIST_TYPE = {      
	    INIT						:   0,
	    LIST_MAIN_CALL				:   1, 
		ADD_CONTACT_PHONENUMBER		:	2,
		PROFILE_HOME_PHONE			:	3,
		PROFILE_OFFICE_PHONE		:	4,
		PROFILE_MOBILE_PHONE		:	5,      
        OPTIONS_CALL_FORWARD_FIRST  :   6,
        OPTIONS_CALL_FORWARD_SECOND :   7,
        OPTIONS_CALL_FORWARD_THIRD  :   8,
        ADD_ASSIGNED_PHONENUMBER	:   9,
        CHANGE_PHONENUMBER			:   10,
        PROFILE_COUNTRY_REGION		: 	11
}

var FlagListStyle = [
		"CountryFlagList_Init_position",
		"CountryFlagList_ListMainCall_position",
		"CountryFlagList_AddPhoneNumber_position",	
		"CountryFlagList_ListMainCall_position",	
		"CountryFlagList_ListMainCall_position",
		"CountryFlagList_ListMainCall_position",
		"CountryFlagList_Options_Callforward_First_position",			
		"CountryFlagList_Options_Callforward_Second_position",			
		"CountryFlagList_Options_Callforward_Third_position",
		"CountryFlagList_Add_Assigned_Phonenumber_position"	,
		"CountryFlagList_ListMainCall_position"	
	];

	
	
function SceneCountryFlagList(options) {

	this.options			= options;	
	
	this.FlagType	= FLAG_LIST_TYPE.INIT; 
	this.FlagList	= null;
	this.NeedToInit	= true;
	this.CurCountryIndex	= -1;
	this.PrevCheckImageIndex = -1;
	this.CurCheckImageIndex = -1;


	//2013 add by minseok
	this.CountryFlagListDim = null;
	this.CountryFlagListBG = null;
	this.CountryFlagCancelBtn = null;
	this.CancelBtnflag = false;
	this.ContentsLabel = null;
	this.FlagListBGLabel = null;
	
	this.CallerScene = null;
	this.EnterCallbackFunc = null;
	
	this.testCallback = null;		
	
	this.WheelOperation = false;
	
	this.DoublePopupFlag = false;
	
}

SceneCountryFlagList.prototype.initialize = function () {
	alert("SceneCountryFlagList.initialize()");	
	this.create();
}

SceneCountryFlagList.prototype.handleShow = function () {
	alert("SceneCountryFlagList.handleShow()");

	if(this.NeedToInit){
	// if(true){
		alert("FlagList Init.......");
		this.CountryFlagListBG = new View("CountryFlagListBG");
		this.CountryFlagListBG.CreatePopupBG(0,"CountryFlagList_popup_Bg");
		this.CountryFlagListBG.SetTitleTextofPopupBG(IDWS_MOIP_SETTING_COUNTRY);
		this.CountryFlagListBG.CreateInnerBox("CountryFlagList_ListMainCall_innerbox");
		
		
		
		//this.NeedToInit = false;

		var BgLayer = this.FlagList.GetBGLayer();
		// BgLayer.SetImage(FlagListBGImage[this.FlagType]);
		BgLayer.SetStyle(FlagListStyle[this.FlagType]);	

		//for testing on sdk
		this.FlagList.SetStyle(this.FlagType);
				
		this.NeedToInit = false;	
	}
		
	//reset highlight position
	//2013 add by minseok
	alert("remove check image!! index  : " + this.PrevCheckImageIndex);
	this.FlagList.RemoveCheckForDataIndex(this.PrevCheckImageIndex);
	this.FlagList.SetCheckForDataIndex(this.CurCheckImageIndex, true);
	//2013 addby minseok : for reset arrow state
	this.FlagList.onMouseOutFunc();
	this.FlagList.Show();
	if(this.DoublePopupFlag){
		this.CountryFlagListDim.hide();
	}else{
	this.CountryFlagListDim.show();
	}
	this.CancelBtnflag = false;
	this.CountryFlagCancelBtn.blur();
		
}

SceneCountryFlagList.prototype.handleHide = function () {
	alert("SceneCountryFlagList.handleHide()");
	
	this.FlagList.Hide();

}

SceneCountryFlagList.prototype.handleFocus = function () {
	alert("SceneCountryFlagList.handleFocus()");

	if(this.FlagList.GetCurrentDataIndex() != this.CurCountryIndex){
		this.FlagList.SetHighlightForDataIndex(this.CurCountryIndex);	
	}
	else {		
		this.FlagList.highlight();
	}	
}

SceneCountryFlagList.prototype.handleBlur = function () {
	alert("SceneCountryFlagList.handleBlur()");
	
	this.FlagList.blur();
}

SceneCountryFlagList.prototype.handleKeyDown = function (keyCode) {
	
	alert("SceneCountryFlagList.handleKeyDown(" + keyCode + ")");
	PluginAPIMgr.PlayAudio(keyCode);
	if(ProcMgr.checkWidgetHighlight()==false){
		return;
	}
	var ridgeDirection = Math.round((keyCode - (5*10000000)) / 100000);
	
	if(ridgeDirection == 1) { // Ridge flick : Up
		if(ProcMgr.GetRidgeFlick() == false) {
			this.FlagList.PreviousPage();
			ProcMgr.StartRidgeTimer();
		}
	}
	else if(ridgeDirection == 2) { // Ridge flick : Down
		if(ProcMgr.GetRidgeFlick() == false) {
			this.FlagList.NextPage();
			ProcMgr.StartRidgeTimer();
		}
	}
	else {
		switch(keyCode){
	    
			case sf.key.ENTER:
				this.ResetCallerSceneTimer();
				if(this.CancelBtnflag){
					this.ProcessKeyReturn();
				}else{
					this.ProcessKeyEnter();	
				}
				break;			
	                   
			case sf.key.RETURN:
				alert("KEY_RETURN");
				this.ResetCallerSceneTimer();
				this.ProcessKeyReturn();
				//2013 modify by minseok
				sf.key.preventDefault();
				break;
				
			case sf.key.EXIT:
				//2013 modify by minseok
				sf.key.preventDefault();
				break;	
				
	        case 147:  //infolink
	        case 261:  //contents
	        case 115:  //wiselink
	            ProcMgr.processInfolinkKey(); 
				break;
			case sf.key.LEFT:
				this.EnterList();
				break;
			case sf.key.RIGHT:	
				this.LeaveList();
				break;
			case sf.key.UP:
			case sf.key.DOWN:
				this.UpdownKeyProcess(keyCode);
				this.ResetCallerSceneTimer();
				break;
			default :
				if(keyCode == sf.key.DOWN || keyCode == sf.key.UP)
				{
					this.ResetCallerSceneTimer();	
				}
				
				this.FlagList.KeyProcess(keyCode);	
				sf.key.preventDefault();	
				break;	
			                   
	    }
	}
}

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

SceneCountryFlagList.prototype.create = function(){
    alert("SceneCountryFlagList.create() ");

	this.BGObj = new CompBase(); 
	this.BGObj.CreateBG("CountryFlagList");
	// this.BGObj.SetOnClickCallbackFunc(this.testCallback);

	this.CountryFlagListDim = new DimBg("CountryFlagListDim","CountryFlagList");



	var this_ = sf.scene.get('CountryFlagList');
	var clickCF = function(obj) {
		this_.processClickEvent(obj);
	};
	var overCF = function(obj) {
		this_.processMouseOverEvent(obj);
	};
	var outCF = function(obj) {
		this_.processMouseOut();
	};

	this.CountryFlagCancelBtn = new Button();
	this.CountryFlagCancelBtn.SetImageObjId("CFlagListCancelBtn", 20);
    this.CountryFlagCancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_POPUP_BTN_NORMAL);
    this.CountryFlagCancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_POPUP_BTN_NORMALFOCUS);
	this.CountryFlagCancelBtn.SetBgImage(BtnImgIndex.BTN_DIM_IMG, g_Image.IMAGE_POPUP_BTN_DIM);	
    this.CountryFlagCancelBtn.SetTextObjId("CFlagListCancelBtn_text");
    this.CountryFlagCancelBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE,"cPopup_btn_text");
    this.CountryFlagCancelBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE, "cPopup_btn_text_high");
    this.CountryFlagCancelBtn.SetText(SID_CANCEL);
    this.CountryFlagCancelBtn.SetOnClickCallbackFunc(clickCF);
    this.CountryFlagCancelBtn.SetOnMouseOverCallbackFunc(overCF);
	
	
	
	this.FlagList = new CountryList();
	this.FlagList.Create();
	
    this.TempDiv = document.getElementById("CountryFlagList");
   	this.TempDiv.onmousewheel = function()
   	{
   		if(this_.WheelOperation == false)
   		{
   			alert("SceneListMainContact.onmousewheel");
	   		this_.WheelOperation = true;
	   		PluginAPIMgr.StartOSDTimer("700" , function() { this_.wheelTimerCallback();}, ENUM_TIMER_TYPE.POPUP_WND);
	   		
			if(event.wheelDelta > 0)
			{
				//alert("Up Up");
				this_.FlagList.PreviousPage();
			}
			else
			{
				//alert("Down Down");
				this_.FlagList.NextPage();
			}
	    }		
    };
    	
    
}

//2013 add by minseok
SceneCountryFlagList.prototype.LeaveList = function(){
	alert("SceneCountryFlagList.LeaveList()");
	this.ResetCallerSceneTimer();
	this.CancelBtnflag = true;
	this.CountryFlagCancelBtn.highlight();
	var index = this.FlagList.GetCurrentItem();
	this.FlagList.ItemArray[index].Normal();
}
SceneCountryFlagList.prototype.EnterList = function(){
	alert("SceneCountryFlagList.EnterList()");
	this.ResetCallerSceneTimer();
	this.CancelBtnflag = false;
	this.CountryFlagCancelBtn.blur();
	var index = this.FlagList.GetCurrentItem();
	this.FlagList.ItemArray[index].Highlight();
}

SceneCountryFlagList.prototype.Init = function(nType,CallerScene_,EnterCallbackFunc,ReturnCallbackFunc){
	
	// this.NeedToInit	= true;
	if(nType==FLAG_LIST_TYPE.ADD_CONTACT_PHONENUMBER){
		this.FlagType	= FLAG_LIST_TYPE.LIST_MAIN_CALL;
		this.DoublePopupFlag = true;
	}else{
		this.DoublePopupFlag = false;
		this.FlagType	= nType;	
	}
	
	this.CallerScene = CallerScene_;
	this.EnterCallbackFunc = EnterCallbackFunc;
	this.ReturnCallbackFunc = ReturnCallbackFunc;
	
}
SceneCountryFlagList.prototype.UpdownKeyProcess = function(keyCode){
	alert("SceneCountryFlagList.UpdownKeyProcess() ");
	if(this.CancelBtnflag==false){
		this.FlagList.KeyProcess(keyCode);	
	}
}


SceneCountryFlagList.prototype.ProcessKeyReturn = function(){

	alert("SceneCountryFlagList.ProcessKeyReturn() ");

	if(this.ReturnCallbackFunc){
		this.ReturnCallbackFunc();
	}

	sf.scene.focus(this.CallerScene);
	sf.scene.hide('CountryFlagList');
}


SceneCountryFlagList.prototype.ProcessKeyEnter = function(){
	alert("SceneCountryFlagList.ProcessKeyEnter() ");
	// alert("remove check image!! index  : " + this.CurCheckImageIndex);
	// this.FlagList.RemoveCheckForDataIndex(this.CurCheckImageIndex);
			
	if(this.EnterCallbackFunc){
		this.EnterCallbackFunc();
	}
	
	sf.scene.hide('CountryFlagList');	
	sf.scene.focus(this.CallerScene);
}


SceneCountryFlagList.prototype.GetCountryFlag = function(){
	
	var Index = this.FlagList.GetCurrentDataIndex();
	return g_Image.base_flag_image_path + g_Data.DATA_COUNTRY_CODE_ARRAY[Index].toUpperCase() + ".png"
}

SceneCountryFlagList.prototype.GetPrefix = function(){
	
	var Index = this.FlagList.GetCurrentDataIndex();	
	return g_Data.DATA_COUNTRY_PREFIX_ARRAY[Index];

}

SceneCountryFlagList.prototype.GetCountryName = function(){

	var Index = this.FlagList.GetCurrentDataIndex();	
	return g_Data.DATA_COUNTRY_NAME_ARRAY[Index];

}

SceneCountryFlagList.prototype.GetCountryCode= function(){

	var Index = this.FlagList.GetCurrentDataIndex();	
	return g_Data.DATA_COUNTRY_CODE_ARRAY[Index];

}

SceneCountryFlagList.prototype.SetCurIndex= function(Index){
	
	alert("SceneCountryFlagList.SetCurIndex");
	// alert("remove check image!! index  : " + this.CurCountryIndex);
	// this.FlagList.RemoveCheckForDataIndex(this.CurCountryIndex);
	this.PrevCheckImageIndex = this.CurCountryIndex;
	
	if(Index < 0){		
		this.CurCountryIndex = g_Data.GetMyCountryIndex();
		this.CurCheckImageIndex = -1;		
	}
	else{
		this.CurCountryIndex = Index;	
		this.CurCheckImageIndex = Index;	
	}
	
	alert("Index = " + this.CurCountryIndex);
	alert("CheckImageIndex = " + this.CurCheckImageIndex);

}

SceneCountryFlagList.prototype.ResetCallerSceneTimer = function(){
	alert("SceneCountryFlagList.ResetCallerSceneTimer() - flagType : "+this.FlagType);
	switch(this.FlagType)
	{
		case FLAG_LIST_TYPE.LIST_MAIN_CALL:
		case FLAG_LIST_TYPE.ADD_CONTACT_PHONENUMBER:
			// sf.scene.get("AddContactPhoneNumber").resetTimer();
			WndMgr.resetPopupHideTimer();
		break;
		case FLAG_LIST_TYPE.PROFILE_HOME_PHONE:
		case FLAG_LIST_TYPE.PROFILE_OFFICE_PHONE:
		case FLAG_LIST_TYPE.PROFILE_MOBILE_PHONE:
		 	// sf.scene.get("ProfilePhoneNumberSet").resetTimer();
		 	WndMgr.resetPopupHideTimer();
		break;
		// case FLAG_LIST_TYPE.PROFILE_COUNTRY_REGION:
	//		break;
		case FLAG_LIST_TYPE.OPTIONS_CALL_FORWARD_FIRST  :
        case FLAG_LIST_TYPE.OPTIONS_CALL_FORWARD_SECOND :
        case FLAG_LIST_TYPE.OPTIONS_CALL_FORWARD_THIRD  :
			
		 break;
		 
		case FLAG_LIST_TYPE.ADD_ASSIGNED_PHONENUMBER:
		case FLAG_LIST_TYPE.CHANGE_PHONENUMBER:
			WndMgr.resetPopupHideTimer();
		//	sf.scene.get("AddAssignedPhoneNumber").resetTimer();
		//	sf.scene.get("ChangePhoneNumber").resetTimer();
		break;
	}
}

SceneCountryFlagList.prototype.processClickEvent = function(obj){
	alert("SceneCountryFlagList.processClickEvent() ");
	// this.LeaveList();
	this.ProcessKeyReturn();
}
SceneCountryFlagList.prototype.processMouseOverEvent = function(obj){
	alert("SceneCountryFlagList.processMouseOverEvent() ");
	this.LeaveList();
	// this.ProcessKeyReturn();
}
SceneCountryFlagList.prototype.ProcessClick = function(ItemIndex)
{
	alert("SceneCountryFlagList.ProcessClick() -- " + ItemIndex);		

	// this.FlagList.MoveItem(ItemIndex);

	// alert("remove check image!! index  : " + this.CurCheckImageIndex);
	// this.FlagList.RemoveCheckForDataIndex(this.CurCheckImageIndex);
	
	if(this.EnterCallbackFunc){
		this.EnterCallbackFunc();		
	}
	
	sf.scene.focus(this.CallerScene);
	sf.scene.hide('CountryFlagList');	

}
SceneCountryFlagList.prototype.processMouseOver = function(index){
	alert("SceneCountryFlagList.processMouseOver() index : "+index);
	this.EnterList();
	this.FlagList.MoveItem(index);
}
SceneCountryFlagList.prototype.wheelTimerCallback=function()
{
	this.WheelOperation = false;
}
