
function ContactList() {

	this.ListPropNum	= 7;
	
	//this.LiveCallMode	= false;
	this.PopupMode		= false;
						
	MainList.call(this);
}

ContactList.prototype = new MainList(); 

ContactList.prototype.Costomizing = function(){

	this.ElimentName = "contact";
	this.ItemClass = ContactItem;
	this.FuncPopupClass = ContactFuncPopup;
	this.NoItemString = TV_SID_YOU_DONT_HAVE_ANY_SKYPE_CONTACTS_YET + "</P>" + TV_SID_SELECT_ADD_A_CONTACT_AND_ENTER_SKYPE_NAME_OR_EMAIL_ADDRESS_OF_THE_FRIEND + "</p>" + TV_SID_FRIEND_CONFIRMED_REQUEST_CAN_CALL_USING_SKYPE;

	this.FuncPopupPropertyType	= FUNC_POPUP_PROPERTY_TYPE.IDENTITY;
	
	this.SceneName = "ListMainContact";
}

//costomize...
ContactList.prototype.GetNumberOfDatas = function(){
	
	var TotalContactNum;
	
	if(this.PopupMode){
		
		TotalContactNum = PluginAPIMgr.GetNumberOfContactsInPopupMode();
	}
	else{
		
		TotalContactNum = PluginAPIMgr.GetNumberOfContacts();
	}
		
	if(ProcMgr.GetIsInCall()){
		
		var CallMemberIdentity = PluginAPIMgr.GetCallMemberID();
		
		if(PluginAPIMgr.IsMyBuddy(CallMemberIdentity)){
			
			return TotalContactNum - 1;	
		}
		else{
			
			return TotalContactNum;
		}
		
	}
	else{
	
		return TotalContactNum;	
	}
	
}

ContactList.prototype.GetIndexOfData = function(DataID){
	
	return PluginAPIMgr.GetIndexOfContact(DataID);
}


//ready for CurPage, but must be correct!!!! over than one contact.
ContactList.prototype.UpdatePage = function(){

	alert("ContactList.UpdatePage"); 

//LIVECALL
	if(ProcMgr.GetIsInCall()){
		this.UpdateLiveCallList();
	}
	else{
		this.UpdateNormalList();
	}

	this.ShowItem();	
	this.ResetPageUI();

	if((this.TotalDataNum < 1) || ((this.TotalDataNum == 1) && (this.ItemArray[0].IdentityName == "echo123")))	{
		this.NotifingLabel.Show();
		this.flagNoItem = true;
	}	else {
		this.NotifingLabel.Hide();
	}

}

ContactList.prototype.UpdateNormalList = function(){

	var startIndexOfPage = (this.CurPage - 1) * MaxNumOfMainItem;
	//alert("first Name = " + ContactName );	
	
	var strContactList;
	
	if(this.PopupMode){
		
		strContactList = PluginAPIMgr.GetContactListInPopupMode(startIndexOfPage);
	}
	else{
		
		var ContactName = PluginAPIMgr.GetContactNameWithIndex(startIndexOfPage);	
		strContactList = PluginAPIMgr.GetContactList(ContactName);	
	}
		
	//alert("String = " + strContactList );

	var SkypeNameArray		= new Array(MaxNumOfMainItem);
	var PSTNNumberArray		= new Array(MaxNumOfMainItem);
	var DisplayNameArray	= new Array(MaxNumOfMainItem);
	var AvailityArray 		= new Array(MaxNumOfMainItem);
	var GenderArray 		= new Array(MaxNumOfMainItem);
	var AvataImgPathArray 	= new Array(MaxNumOfMainItem);
	var MoodMsgArray 		= new Array(MaxNumOfMainItem);	
			
	var NumOfItemInThisPage = 0;						
							
	if(strContactList)
	{				
		var STX = String.fromCharCode(2);
		var ETX = String.fromCharCode(3);
		var splitter = ETX + STX;
	
		var Result = strContactList.split(splitter);	
		NumOfItemInThisPage = parseInt(Result.length / this.ListPropNum);
		alert("NumOfItemInThisPage = " + NumOfItemInThisPage );
		
		var TokenNum = 0;
		
		for (var i=0; i < NumOfItemInThisPage ; i++) {
					
			TokenNum = i*(this.ListPropNum);						
			
			SkypeNameArray[i] = Result[TokenNum];			
			PSTNNumberArray[i] = Result[TokenNum+1];			
			DisplayNameArray[i] = Result[TokenNum+2];						
			AvailityArray[i] = Result[TokenNum+3];						
			GenderArray[i] = Result[TokenNum+4];						
			AvataImgPathArray[i] = Result[TokenNum+5];					
			MoodMsgArray[i] = Result[TokenNum+6];		
			
/*
			alert("SkypeNameArray[" + i +"] = " + SkypeNameArray[i] );	
			alert("PSTNNumberArray[" + i +"] = " + PSTNNumberArray[i] );				
			alert("DisplayNameArray[" + i +"] = " + DisplayNameArray[i] );
			alert("AvailityArray[" + i +"] = " + AvailityArray[i] );
			alert("GenderArray[" + i +"] = " + GenderArray[i] );
			alert("AvataImgPathArray[" + i +"] = " + AvataImgPathArray[i] );
			alert("MoodMsgArray[" + i +"] = " + MoodMsgArray[i] );	
*/						
							
		}
	}
		
	var tempData = new Array(MaxNumOfMainItem);
	var tempContctData;
		
			
	for (var j=0; j < NumOfItemInThisPage; j++) {
	
		//alert("tempData[" + j +"] create");	
		tempData[j] = new ContactData();		
		tempData[j].SetData(SkypeNameArray[j],PSTNNumberArray[j],DisplayNameArray[j],AvailityArray[j],GenderArray[j],AvataImgPathArray[j],MoodMsgArray[j]);
		

		this.ItemArray[j].SetItem(tempData[j]);
		
		//this.ItemArray[j].SetItem(tempData[j]);
				
	}
	
	this.NumOfVisibleItem = NumOfItemInThisPage;
	for(var k = NumOfItemInThisPage; k < MaxNumOfMainItem ; k++) {
		
		//alert("ItemArray[" + k +"] not include data!!");
		if(this.ItemArray[k]){
			
			this.ItemArray[k].SetFlagForExistData(false);
		}		
	}
}


ContactList.prototype.UpdateLiveCallList = function(){

	var CallMemberName = PluginAPIMgr.GetCallMemberID();
	var CallMemberIndex = PluginAPIMgr.GetIndexOfContact(CallMemberName);
	alert("CallMemberIndex = " + CallMemberIndex);
		
	var startIndexOfPage = (this.CurPage - 1) * (LiveNumOfContact);

	if(CallMemberIndex < startIndexOfPage)
	{
		startIndexOfPage++;
	}

	var strContactList;
	
	if(this.PopupMode){
		
		strContactList = PluginAPIMgr.GetLiveContactListInPopupMode(startIndexOfPage);
	}
	else{
		
		strContactList = PluginAPIMgr.GetLiveContactList(startIndexOfPage);	
	}

	var SkypeNameArray		= new Array(LiveNumOfContact);
	var PSTNNumberArray		= new Array(LiveNumOfContact);
	var DisplayNameArray	= new Array(LiveNumOfContact);
	var AvailityArray 		= new Array(LiveNumOfContact);
	var GenderArray 		= new Array(LiveNumOfContact);
	var AvataImgPathArray 	= new Array(LiveNumOfContact);
	var MoodMsgArray 		= new Array(LiveNumOfContact);	
			
	var NumOfItemInThisPage = 0;						
							
	if(strContactList)
	{				
		var STX = String.fromCharCode(2);
		var ETX = String.fromCharCode(3);
		var splitter = ETX + STX;
	
		var Result = strContactList.split(splitter);	
		NumOfItemInThisPage = parseInt(Result.length / this.ListPropNum);
		alert("NumOfItemInThisPage = " + NumOfItemInThisPage );
		
		var TokenNum = 0;
		
		for (var i=0; i < NumOfItemInThisPage ; i++) {
					
			TokenNum = i*(this.ListPropNum);						
			
			SkypeNameArray[i] = Result[TokenNum];			
			PSTNNumberArray[i] = Result[TokenNum+1];			
			DisplayNameArray[i] = Result[TokenNum+2];						
			AvailityArray[i] = Result[TokenNum+3];						
			GenderArray[i] = Result[TokenNum+4];						
			AvataImgPathArray[i] = Result[TokenNum+5];					
			MoodMsgArray[i] = Result[TokenNum+6];		
										
		}
	}
		
	var tempData = new Array(LiveNumOfContact);
	var tempContctData;
	var Idex;
	
	NumOfItemInThisPage++;
	this.ItemArray[0].SetLiveCallItem(CallMemberName);			
			
	for (var j=1; j < NumOfItemInThisPage; j++) {
		
		Idex = j - 1;
		//alert("tempData[" + Idex +"] create");
	
		tempData[Idex] = new ContactData();		
		tempData[Idex].SetData(SkypeNameArray[Idex],PSTNNumberArray[Idex],DisplayNameArray[Idex],AvailityArray[Idex],GenderArray[Idex],AvataImgPathArray[Idex],MoodMsgArray[Idex]);
			
		this.ItemArray[j].SetItem(tempData[Idex]);
				
	}
	
	this.NumOfVisibleItem = NumOfItemInThisPage;
	for(var k = NumOfItemInThisPage; k < MaxNumOfMainItem ; k++) {
		
		//alert("ItemArray[" + k +"] not include data!!");
		if(this.ItemArray[k]){
			
			this.ItemArray[k].SetFlagForExistData(false);
		}		
	}

}

ContactList.prototype.ProgressCallDuration = function(DurationTime){

	if(ProcMgr.GetIsInCall()){
		this.ItemArray[0].SetDuration(DurationTime);
	}
}

//TEMPCODE:
ContactList.prototype.RefreshForCurIdentity = function(){

	alert("ContactList.RefreshFoCurIdentity");
	
	var ItemIdentity = this.GetCurItemIdentity();
	var Index = parseInt(this.GetIndexOfData(ItemIdentity));
	alert("Index = " + Index);
	
	if (Index >= 0) {
	
//		this.InitializeDataOfPage();

		if(ProcMgr.GetIsInCall()){
			
			var CallingIdentity = PluginAPIMgr.GetCallMemberID();
			var CallingIndex = parseInt(this.GetIndexOfData(CallingIdentity));		
			
			if(Index == CallingIndex){
			
				this.RefreshForCurrentPage();
			}
			else if(Index > CallingIndex){
				
				this.InitializeDataOfPage();

				//reset current page
				this.CurPage = parseInt(Index / LiveNumOfContact);
				var CurItemIndex = Index % LiveNumOfContact;
				
				if (CurItemIndex > 0) {
					this.CurPage++;					
				}
				else{
					CurItemIndex = LiveNumOfContact;
				}
				alert("CurPage = " + this.CurPage);
				
				this.CurIndex = Index;
				
				//reset page & scorllbar
				this.UpdatePage();			
				
				this.CurrentItemIndex = -1;
				this.MoveItem(CurItemIndex);								

			}
			else{
			
				this.InitializeDataOfPage();
				//reset current page
				this.CurPage = parseInt(Index / LiveNumOfContact) + 1;
				alert("CurPage = " + this.CurPage);
				
				this.CurIndex = Index;
				
				//reset page & scorllbar
				this.UpdatePage();
				
				var CurItemIndex = (Index % LiveNumOfContact) + 1;
				
								
				this.CurrentItemIndex = -1;
				this.MoveItem(CurItemIndex);
					
			}
						
		}
		else {

			this.InitializeDataOfPage();
			
			//reset current page
			this.CurPage = parseInt(Index / MaxNumOfMainItem) + 1;
			alert("CurPage = " + this.CurPage);
			this.CurIndex = Index;
			
			//reset page & scorllbar
			this.UpdatePage();
			
			var CurItemIndex = Index % MaxNumOfMainItem;
			
			this.CurrentItemIndex = -1;
			this.MoveItem(CurItemIndex);			
		}		
	}
	else {
	
		alert("=====================================");
		alert("Error!! no result of searching index ");
		alert("=====================================");
		
		this.RefreshForCurrentPage();
		
	}
	
}

ContactList.prototype.InitializeDataOfPage = function(){
	
	this.TotalDataNum = parseInt(this.GetNumberOfDatas());
						
	if( this.TotalDataNum > 0){
		
		var NumOfItem;		
		if(ProcMgr.GetIsInCall()){
			NumOfItem = LiveNumOfContact;
		}
		else {
			NumOfItem = MaxNumOfMainItem;
		}
		
		this.TotalPageNum = parseInt(this.TotalDataNum / NumOfItem);

		if((this.TotalDataNum % NumOfItem) > 0){
			this.TotalPageNum++;
		}						
	}
	else {
		this.InitializeListInfo();					
	}		
		
}



ContactList.prototype.RefreshForIndex = function(Index)
{
	alert("ContactList.RefreshForIndex---" + Index);

	if(Index < 0)	
	{
		alert("=====================================");
		alert("Error!! no result of searching index ");
		alert("=====================================");		
		return;		
	}

	// if(ProcMgr.GetIsInCall())	
	// {
		// alert("=====================================");
		// alert("Error!! in Call state.... ");
		// alert("=====================================");		
		// return;		
	// }

	this.InitializeDataOfPage();
	
	//reset current page
	this.CurPage = parseInt(Index / MaxNumOfMainItem) + 1;
	alert("CurPage = " + this.CurPage);
	this.CurIndex = Index;
	
	//reset page & scorllbar
	this.UpdatePage();
	
	var CurItemIndex = Index % MaxNumOfMainItem;
	
	this.CurrentItemIndex = -1;
	this.MoveItem(CurItemIndex);
	
}

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

function pContactList() {
					
	ContactList.call(this);
}

pContactList.prototype = new ContactList(); 

pContactList.prototype.Costomizing = function(){

	this.ElimentName = "pContact";
	this.ItemClass = pContactItem;
	this.FuncPopupClass = pContactFuncPopup;
	this.FuncPopupPropertyType = FUNC_POPUP_PROPERTY_TYPE.IDENTITY;
	this.SceneName = "LiveContactSwitch";
	
	this.PopupMode = true;
}

pContactList.prototype.ProcessReturnKey = function(){

	this.FuncPopup.processReturnKey();
}
