var IDX_ITEM_TYPE = {
	ID : 0,
	MOBILE : 1,
	OFFICE : 2,
	HOME : 3,
	OTHER : 4,
};

var LIST_CANCEL_BTN_TOP = [63, 101, 139, 177, 215, 253, 291];
var LIST_HEIGHT_SIZE = [121, 159, 197, 235, 273, 311, 349];
var LIST_TOP_FIXED_SIZE = [0,283,245,207,169,131,93];	// 494- height size
function SceneminiPhoneNumberList(options) {
	this.options = options;

	this.miniPhoneNumberListView = null;

	this.ListItemBtn = new Array();

	this.ListDataArray = new Array();
	this.ListTypeArray = new Array();

	this.HomeNumberArray = new Array();
	this.OfficeNumberArray = new Array();
	this.MobileNumberArray = new Array();
	this.OtherNumberArray = new Array();
	this.HomeNumberCnt = 0;
	this.OfficeNumberCnt = 0;
	this.MobileNumberCnt = 0;
	this.OtherNumberCnt = 0;

	this.CancelBtn = null;
	this.CloseTimer = false;

}

SceneminiPhoneNumberList.prototype.initialize = function() {
	alert("SceneminiPhoneNumberList.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

	this.create();
}

SceneminiPhoneNumberList.prototype.handleShow = function() {
	alert("SceneminiPhoneNumberList.handleShow()");
	// this function will be called when the scene manager show this scene
	this.DataInfoInit();

	this.startTimer();
	this.ShowKeyHelpbar();
	$("#skype_popup_background").show();
}

SceneminiPhoneNumberList.prototype.handleHide = function() {
	alert("SceneminiPhoneNumberList.handleHide()");
	// this function will be called when the scene manager hide this scene

	if(this.CloseTimer == true) {
		this.stopTimer();
	}
	this.highlight(null);
	$("#skype_popup_background").hide();
}

SceneminiPhoneNumberList.prototype.handleFocus = function() {
	alert("SceneminiPhoneNumberList.handleFocus()");
	// this function will be called when the scene manager focus this scene
	var obj = this.miniPhoneNumberListView.getCurrentHighlight();
	if(obj) {
		this.highlight(obj);
	} else {
		this.highlight(this.ListItemBtn[0]);
	}
	if(this.GetTotalItemCount()==1)
		this.processEnterKey();
	
}

SceneminiPhoneNumberList.prototype.handleBlur = function() {
	alert("SceneminiPhoneNumberList.handleBlur()");
	// this function will be called when the scene manager move focus to another scene from this scene
	var obj = this.miniPhoneNumberListView.getCurrentHighlight();
	if(obj) {
		obj.blur();
	}
}

SceneminiPhoneNumberList.prototype.handleKeyDown = function(keyCode) {
	alert("SceneminiPhoneNumberList.handleKeyDown(" + keyCode + ")");
	// TODO : write an key event handler when this scene get focued
	PluginAPIMgr.PlayAudio(keyCode);
	switch (keyCode) {
		case $.sfKey.LEFT:
		case $.sfKey.RIGHT:

			break;
		case $.sfKey.UP:
			this.resetTimer();
			this.processUpKey();
			break;
		case $.sfKey.DOWN:
			this.resetTimer();
			this.processDownKey();
			break;

		case $.sfKey.ENTER:
			this.processEnterKey();
			$.sfKey.block(event);
			break;
		case $.sfKey.RETURN:
		case $.sfKey.EXIT:
			this.processReturnKey();
			$.sfKey.block(event);
			break;

		case 147:
		//infolink
		case 261:
		//contents
		case 115:
			//wiselink
			ProcMgr.processInfolinkKey();
			break;
	}
	if(Flag_Hidden_State==false)
		event.stopPropagation();
}
SceneminiPhoneNumberList.prototype.ShowKeyHelpbar = function(){
	if(WIDGET.mouse==true){
		helpBarHandler.hideHelpIcon('red',"");
		helpBarHandler.hideHelpIcon('green',"");
		helpBarHandler.hideHelpIcon('yellow',"");
		helpBarHandler.hideHelpIcon('blue',TV_SID_CHANGE_VIEW);
		helpBarHandler.hideHelpIcon('tools',sfLang.SID_TOOL);
		helpBarHandler.hideHelpIcon('return',SID_TOOLS);
	}else{
		$("#HomeHelp").sfKeyHelp({
		//	'blue' : TV_SID_CHANGE_VIEW,
		//	'tools' : sfLang.SID_TOOLS,
			'return' : SID_RETURN
		});	
	}
}
SceneminiPhoneNumberList.prototype.highlight = function(obj) {
	this.miniPhoneNumberListView.setCurrenHighlight(obj);

}
SceneminiPhoneNumberList.prototype.GetCurrentHighlightIdx = function(obj) {
	for(var i = 0; i < this.GetTotalItemCount(); i++) {
		if(obj == this.ListItemBtn[i]) {
			return i;
		}
	}
	return -1;
}
SceneminiPhoneNumberList.prototype.processUpKey = function() {
	alert("SceneminiPhoneNumberList.processUpKey() ");

	var obj = this.miniPhoneNumberListView.getCurrentHighlight();
	var idx = this.GetCurrentHighlightIdx(obj);
	if(idx == 0) {
		this.highlight(this.CancelBtn);
	} else if(idx == -1) {
		this.highlight(this.ListItemBtn[this.GetTotalItemCount() - 1]);
	} else {
		this.highlight(this.ListItemBtn[idx - 1]);
	}
}

SceneminiPhoneNumberList.prototype.processDownKey = function() {
	alert("SceneminiPhoneNumberList.processDownKey() ");

	var obj = this.miniPhoneNumberListView.getCurrentHighlight();
	var idx = this.GetCurrentHighlightIdx(obj);
	if(idx == this.GetTotalItemCount() - 1) {
		this.highlight(this.CancelBtn);
	} else if(idx == -1) {
		this.highlight(this.ListItemBtn[0]);
	} else {
		this.highlight(this.ListItemBtn[idx + 1]);
	}
}

SceneminiPhoneNumberList.prototype.processEnterKey = function() {
	alert("SceneminiPhoneNumberList.processEnterKey() ");

	var obj = this.miniPhoneNumberListView.getCurrentHighlight();
	var idx = this.GetCurrentHighlightIdx(obj);
	var callOutData = null;
	if(idx == -1) {//cancelbtn		
		StateMgr.Stop_Mini_Phone_Number_List(true);
		return;
	} else {
		callOutData = this.ListDataArray[idx];
	}
	
	StateMgr.Stop_Mini_Phone_Number_List(false);
	
	if(callOutData != null) {
		alert("callout data =====================================" + callOutData);
		ProcMgr.CreateCall(callOutData, 0);
	}	
}

SceneminiPhoneNumberList.prototype.processReturnKey = function() {
	alert("SceneminiPhoneNumberList.processReturnKey() ");
	StateMgr.Stop_Mini_Phone_Number_List(true);

}

SceneminiPhoneNumberList.prototype.startTimer = function() {
	alert("SceneminiPhoneNumberList.startTimer() ");

	this.CloseTimer = true;
	var this_ = this;
	PluginAPIMgr.StartOSDTimer("60000", function() {
		this_.popupTimerCallback();
	}, ENUM_TIMER_TYPE.POPUP_WND);
}

SceneminiPhoneNumberList.prototype.resetTimer = function() {
	alert("SceneminiPhoneNumberList.resetTimer() ");

	if(this.CloseTimer == true) {
		PluginAPIMgr.RestartOSDTimer("60000", ENUM_TIMER_TYPE.POPUP_WND);
	}
}

SceneminiPhoneNumberList.prototype.stopTimer = function() {
	alert("SceneminiPhoneNumberList.stopTimer() ");

	this.CloseTimer = false;
	PluginAPIMgr.StopOSDTimer(ENUM_TIMER_TYPE.POPUP_WND);
}

SceneminiPhoneNumberList.prototype.popupTimerCallback = function() {
	alert("SceneminiPhoneNumberList.popupTimerCallback() ");
	if(this.CloseTimer == false) {
		return
	}
	this.CloseTimer = false;
	/////////////////////
	StateMgr.Stop_Mini_Phone_Number_List(true);
}
SceneminiPhoneNumberList.prototype.processMouseOver = function(obj) {
    alert("SceneminiPhoneNumberList.processMouseOver() ");

    var currentHighlight = this.miniPhoneNumberListView.getCurrentHighlight();
    if(currentHighlight != obj) {
        this.highlight(obj);
    }
    this.resetTimer();
}
SceneminiPhoneNumberList.prototype.processClickEvent = function(obj) {
	alert("SceneminiPhoneNumberList.processClickEvent() ");

	var currentHighlight = this.miniPhoneNumberListView.getCurrentHighlight();
	if(currentHighlight != obj) {
		this.highlight(obj);
	}

	this.processEnterKey();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

SceneminiPhoneNumberList.prototype.create = function() {
	alert("SceneminiPhoneNumberList.create()");
	this.miniPhoneNumberListView = new View("miniPhoneNumberList");

	var this_ = this;
	var clickCF = function(obj) {
		this_.processClickEvent(obj);
	};
	for(var i = 0; i < 7; i++) {
		this.ListItemBtn[i] = new Button();
		this.ListItemBtn[i].SetImageObjId("phonenumberlist_item" + i);
		this.ListItemBtn[i].SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, null);
		this.ListItemBtn[i].SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_PHONE_NUMBER_LIST_SELECT_CALL_FOCUS);
		this.ListItemBtn[i].SetTextObjId("phonenumberlist_item" + i + "_text");
		this.ListItemBtn[i].SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE, "phonenumberlist_text_style");
		this.ListItemBtn[i].SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE, "phonenumberlist_text_focus_style");
		this.ListItemBtn[i].SetIconObjId("phonenumberlist_item" + i + "_icon");
		this.ListItemBtn[i].SetOverflowMarquee("phonenumberlist_text_marquee_style");
		this.ListItemBtn[i].SetReverseDisable(true);
		this.ListItemBtn[i].SetOnClickCallbackFunc(clickCF);
		this.ListItemBtn[i].SetOnMouseOverCallbackFunc(function(obj){
        this_.processMouseOver(obj);
    });
	}
	this.CancelBtn = new Button();
	this.CancelBtn.SetImageObjId("phonenumberlist_cancel_btn", 20);
	this.CancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_NEW_BUTTON_NORMAL);
	this.CancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_NEW_BUTTON_HIGHLIGHT);

	this.CancelBtn.SetTextObjId("phonenumberlist_cancel_btn_text");
	this.CancelBtn.SetText(SID_CANCEL);
	this.CancelBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE, "list_btn_text_normal_style");
	this.CancelBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE, "list_btn_text_highlight_style");
	this.CancelBtn.SetOnClickCallbackFunc(clickCF);
	this.CancelBtn.SetOnMouseOverCallbackFunc(function(obj){
        this_.processMouseOver(obj);
    });
}
SceneminiPhoneNumberList.prototype.DataInfoInit = function() {
	this.InfoInit(this.ListDataArray) ;
	this.InfoInit(this.ListTypeArray) ;
	this.InfoInit(this.HomeNumberArray);
	this.InfoInit(this.OfficeNumberArray);
	this.InfoInit(this.MobileNumberArray);
	this.InfoInit(this.OtherNumberArray);
	
	this.HomeNumberCnt = 0;
	this.OfficeNumberCnt = 0;
	this.MobileNumberCnt = 0;
	this.OtherNumberCnt = 0;
}
SceneminiPhoneNumberList.prototype.InfoInit = function(obj) {
	alert("init array num : "+obj.length);
	for(var i=0; i<obj.length; i++){
		obj[i]=null;
	}
}
SceneminiPhoneNumberList.prototype.SetInit = function(itemIndex, skypeId) {
	alert("SceneminiPhoneNumberList.SetInit");
	this.SkypeId = skypeId;
	this.GetPhoneInfo();
	this.SetDataArrayInfo();
	
	var SizeIdx = this.GetTotalItemCount() - 1;
	alert("MaxItemNum = " + SizeIdx);
	for(var i = 0; i <= SizeIdx; i++) {
		if(this.ListTypeArray[i] == IDX_ITEM_TYPE.ID) {
			this.ListItemBtn[i].SetBgIcon(0, g_Image.IMAGE_PHONE_NUMBER_ICON_SKYPEID);
		} else if(this.ListTypeArray[i] == IDX_ITEM_TYPE.MOBILE) {
			this.ListItemBtn[i].SetBgIcon(0, g_Image.IMAGE_PHONE_NUMBER_ICON_MOBILE);
		} else if(this.ListTypeArray[i] == IDX_ITEM_TYPE.OFFICE) {
			this.ListItemBtn[i].SetBgIcon(0, g_Image.IMAGE_PHONE_NUMBER_ICON_OFFICE);
		} else if(this.ListTypeArray[i] == IDX_ITEM_TYPE.HOME) {
			this.ListItemBtn[i].SetBgIcon(0, g_Image.IMAGE_PHONE_NUMBER_ICON_HOME);
		} else if(this.ListTypeArray[i] == IDX_ITEM_TYPE.OTHER) {
			this.ListItemBtn[i].SetBgIcon(0, g_Image.IMAGE_PHONE_NUMBER_ICON_OTHER);
		}
			this.ListItemBtn[i].SetText(this.ListDataArray[i]);
		this.ListItemBtn[i].setTextKPILog(this.ListDataArray[i])
		this.ListItemBtn[i].visibleObj();
	}
	for(var i=SizeIdx+1; i<7;i++){
		this.ListItemBtn[i].hiddenObj();
	}
	this.itemIndex = itemIndex;
	this.ModifyUI();
}

SceneminiPhoneNumberList.prototype.ModifyUI = function() {
	alert("SceneminiPhoneNumberList.ModifyUI");
	var itemSize = 74;
	var baseTop = 100;
	var SizeIdx = this.GetTotalItemCount() - 1;
	var mainContact = $.sfScene.get({id:"miniMainContact",scope:WIDGET.id});
	if(this.itemIndex!=mainContact.list.CurrentItemIndex)
		this.itemIndex = mainContact.list.CurrentItemIndex;
	var posTop = baseTop + itemSize * this.itemIndex;
	if(posTop>=LIST_TOP_FIXED_SIZE[SizeIdx])
		posTop = LIST_TOP_FIXED_SIZE[SizeIdx];
	alert("posTop====" + posTop);
	$("#miniPhoneNumberList").css("top", posTop + "px");
	this.miniPhoneNumberListView.SetBgImage(g_Image.IMAGE_PHONE_NUMBER_LIST_BG + SizeIdx + ".png");
	$("#phonenumberlist_cancel_btn").css("top", LIST_CANCEL_BTN_TOP[SizeIdx] + "px");
	$("#miniPhoneNumberList").css("height", LIST_HEIGHT_SIZE[SizeIdx] + "px");
}
SceneminiPhoneNumberList.prototype.SetDataArrayInfo = function() {
	alert("SceneminiPhoneNumberList.SetDataArrayInfo");
	var idx = 0;
	this.ListTypeArray[idx] = IDX_ITEM_TYPE.ID;
	this.ListDataArray[idx++] = this.SkypeId;
	for(var i = 0; i < this.MobileNumberCnt; i++) {
		this.ListTypeArray[idx] = IDX_ITEM_TYPE.MOBILE;
		this.ListDataArray[idx++] = this.MobileNumberArray[i];
	}
	for(var i = 0; i < this.OfficeNumberCnt; i++) {
		this.ListTypeArray[idx] = IDX_ITEM_TYPE.OFFICE;
		this.ListDataArray[idx++] = this.OfficeNumberArray[i];
	}
	for(var i = 0; i < this.HomeNumberCnt; i++) {
		this.ListTypeArray[idx] = IDX_ITEM_TYPE.HOME;
		this.ListDataArray[idx++] = this.HomeNumberArray[i];
	}
	for(var i = 0; i < this.OtherNumberCnt; i++) {
		this.ListTypeArray[idx] = IDX_ITEM_TYPE.OTHER;
		this.ListDataArray[idx++] = this.OtherNumberArray[i];
	}
}
SceneminiPhoneNumberList.prototype.GetTotalItemCount = function() {
	return this.OfficeNumberCnt + this.OtherNumberCnt + this.MobileNumberCnt + this.HomeNumberCnt + 1;
}
SceneminiPhoneNumberList.prototype.GetPhoneInfo = function() {
	alert("SceneminiPhoneNumberList.GetPhoneInfo");
	var ContactName = this.SkypeId;
	var tmp= null;
	tmp = PluginAPIMgr.GetHomePhoneNumberWithName(ContactName);
	if(tmp&&tmp.length>0)
		this.HomeNumberArray[this.HomeNumberCnt++] = tmp;
	tmp = null;
	
	tmp = PluginAPIMgr.GetOfficePhoneNumberWithName(ContactName);
	if(tmp&&tmp.length>0)
		this.OfficeNumberArray[this.OfficeNumberCnt++] = tmp;
	tmp = null;
	
	tmp = PluginAPIMgr.GetMobilePhoneNumberWithName(ContactName);
	if(tmp&&tmp.length>0)	
		this.MobileNumberArray[this.MobileNumberCnt++] = tmp;

	var assignedNumber = null;
	var label = null;

	for(var i = 2; i <= 6; i = i + 2) {
		label = PluginAPIMgr.GetAssignedPhoneNumberWithName(ContactName, i);
		if(label != null && label != "none") {
			assignedNumber = PluginAPIMgr.GetAssignedPhoneNumberWithName(ContactName, i - 1);
			switch(String(label)) {
				case "mobile":
					this.MobileNumberArray[this.MobileNumberCnt++] = assignedNumber;
					break;
				case "office":
					this.OfficeNumberArray[this.OfficeNumberCnt++] = assignedNumber;
					break;
				case "home":
					this.HomeNumberArray[this.HomeNumberCnt++] = assignedNumber;
					break;
				case "other":
					this.OtherNumberArray[this.OtherNumberCnt++] = assignedNumber;
					break;
			}
		}
	}
}
