var MAX_CALLING_ANI_IMAGE_NUM = 5;

function SceneCalling(options) {
	this.options = options;
    
    this.CallingView=null;
	
	this.CallingPageView = null;
	this.CallingFailPageView =null;

	this.LogoLabel = null;
	
    this.CallMemberThumbnail = null;
    this.CallMembertNameLabel = null;

	this.MyThumbnail = null;
	
    this.CallMessageLabel = null;

    this.CancelBtn = null;
    
    this.CallingAnimation =null;
    this.CallingAnimationImageList = new Array(MAX_CALLING_ANI_IMAGE_NUM);
    
    this.FlagAnimation = false;
    this.CallingTimer = null;
    this.iCurrAniImage=0;

    this.flag_shown_preview = false;
    this.flag_shown_my_avatar = false;
    
    this.currentVolumeValue = 0;

}

SceneCalling.prototype.initialize = function () {
	alert("SceneCalling.initialize()");

 
   this.create();
}

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

	this.update();
   
	this.FlagAnimation=true;
	this.startTimer();
	
	ProcMgr.ProcessCall();
}

SceneCalling.prototype.handleHide = function () {
	alert("SceneCalling.handleHide()");

	this.hiddenPreview();
    
    this.FlagAnimation = false;
	if(this.CallingTimer != null)
	{
		this.stopTimer();
	}
}

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

	this.highlight(this.CancelBtn);
}

SceneCalling.prototype.handleBlur = function () {
	alert("SceneCalling.handleBlur()");

    this.highlight(null);
}

SceneCalling.prototype.handleKeyDown = function (keyCode) {
	alert("SceneCalling.handleKeyDown(" + keyCode + ")");
	if(ProcMgr.checkWidgetHighlight()==false){
		return;
	}
	PluginAPIMgr.PlayAudio(keyCode);
	switch (keyCode) {
		case sf.key.LEFT:
		case sf.key.RIGHT:
		case sf.key.UP:
		case sf.key.DOWN:
			break;

		case sf.key.ENTER:
        case sf.key.RETURN:
            this.processEnterKey();
            sf.key.preventDefault();
			break;        
        case sf.key.EXIT:
            this.processExitKey();
            sf.key.preventDefault();
			break;

		case sf.key.VOL_DOWN:
        case sf.key.PANEL_VOL_DOWN:
		case sf.key.LEFT:
			this.processVolume(false);
			break;
			
		case sf.key.VOL_UP:
        case sf.key.PANEL_VOL_UP:
		case sf.key.RIGHT:			
			this.processVolume(true);
			break;

		case 602:	//INFOLINK
		case 606:	//CONTENTS
		case 608:	//WISELINK
            ProcMgr.processInfolinkKey(); 
			break;
	}
}

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

SceneCalling.prototype.create = function(){
    alert("SceneCalling.create()");
    
    this.CallingView = new View("Calling");
    //this.CallingView.SetBgImage(g_Image.IMAGE_TEST_VIEW_BG);
	
	this.CallingPageView = new View("calling_page");
	
	this.LogoLabel = new Label("calling_skype_logo", null);
    this.LogoLabel.SetBgImage(g_Image.IMAGE_CALL_SKYPE_LOGO);
	
    this.CallMembertNameLabel = new Label(null,"calling_call_member_name");
    this.CallMembertNameLabel.SetTextStyle("calling_call_member_name_style");
    this.CallMembertNameLabel.SetReverseDisable(true);
    this.CallMembertNameLabel.SetIsPlainText(true);

	this.CallMemberThumbnail = new CompBase(); 
	this.CallMemberThumbnail.CreateBG("calling_call_member_thumbnail");
	this.CallMemberThumbnail.AddJPGImageLayer("calling_call_member_thumbnail_avatar");

	this.MyThumbnail = new CompBase(); 
	this.MyThumbnail.CreateBG("calling_my_thumbnail_thumbnail");
	this.MyThumbnail.AddJPGImageLayer("calling_my_thumbnail_thumbnail_avatar");
	
    this.CallMessageLabel= new Label(null, "calling_message");
    this.CallMessageLabel.SetTextStyle("calling_message_text_style");
       
    var this_ = sf.scene.get("Calling");
    var clickCF =  function(obj){this_.processClickEvent(obj);};

    this.CancelBtn = new Button();
    this.CancelBtn.SetImageObjId("calling_cancel_btn");
    this.CancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_CALL_END_BTN_NORMAL);
    this.CancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_CALL_END_BTN_NORMALFOCUS);
	this.CancelBtn.SetOnClickCallbackFunc(clickCF);
    this.CancelBtn.SetOnClickCallbackFunc(clickCF);	
	
    this.CallingAnimation = new Label("calling_animation", null);
    
    var i=0;
    for( i=0; i < MAX_CALLING_ANI_IMAGE_NUM; i++)
    {
    	this.CallingAnimationImageList[i] = g_Image.base_loading_image_path + 'call_loading_0' + (i+1) + '.png';
    }
}

SceneCalling.prototype.highlight=function(obj)
{
    this.CallingView.setCurrenHighlight(obj);

}

SceneCalling.prototype.update = function()
{
	alert("SceneCalling.update()");
    this.CallMembertNameLabel.SetText(g_Data.DATA_CREATE_CALL_INPUT_INFO.callee_name); 
    this.CallMemberThumbnail.SetImageLayer(1,PluginAPIMgr.GetContactAvataImage(g_Data.DATA_CREATE_CALL_INPUT_INFO.callee_name));
    
    this.MyThumbnail.SetImageLayer(1,PluginAPIMgr.GetMyAvataImage());
        
    if(WndMgr.isCallingFail ==  true)
    {
        this.CallMessageLabel.SetText(IDWS_MOIP_SKYPE_FAILED);
    }
    else
    {
        this.CallMessageLabel.SetText(TV_SID_CALLING);
    }
    
    WndMgr.isCallingFail =  false; //init
    
    
    this.iCurrAniImage = 0;
    this.CallingAnimation.SetBgImage(this.CallingAnimationImageList[this.iCurrAniImage]);
    
	//this.CallingPageView.visibleObj();
	
	this.currentVolumeValue = PluginAPIMgr.GetSpeakerVolume();
	
	this.showAvartar();	
}

SceneCalling.prototype.updateCallingStatus = function(call_status)
{
    this.CallMessageLabel.SetText(this.GetCallStatusString(call_status));
    
    if(call_status == ENUM_CALL_STATUS.CALL_BEING_FORWARDED)
    {
    	this.hiddenPreview();
    	this.showAvartar();
    }
}

SceneCalling.prototype.updateCallingMessage = function(call_status)
{
	this.CallMessageLabel.SetText(this.GetCallStatusString(call_status));
}

SceneCalling.prototype.updateCallingButton= function(flag_dim)
{
	if(flag_dim == true)
	{
		this.highlight(null);
	}
}

SceneCalling.prototype.startTimer = function()
{  
	alert("SceneCalling.startTimer() ");
	
	var scene = sf.scene.get('Calling');
	
	this.CallingTimer = setInterval("timerCF()", 500);
	timerCF = function() { scene.CallingTimerCallback();}
}

SceneCalling.prototype.stopTimer = function()
{
    alert("SceneCalling.stopTimer() ");
    
	clearInterval( this.CallingTimer);
    this.CallingTimer = null;
    this.iCurrAniImage = 0;
}

SceneCalling.prototype.CallingTimerCallback = function()
{
     this.iCurrAniImage++;
     
    if(this.iCurrAniImage >= this.CallingAnimationImageList.length)
    {
        this.iCurrAniImage = 0;
    }   
    
    this.CallingAnimation.SetBgImage(this.CallingAnimationImageList[this.iCurrAniImage]);
    
	if(ProcMgr.callEndWaitingTimer ==true)
	{
		this.stopTimer();
	}

}

SceneCalling.prototype.GetCallStatusString = function(call_status)
{
	switch(call_status)
    {
	    case ENUM_CALL_STATUS.UNPLACED : 
	        return "UNPLACED";
	        break;
	    case ENUM_CALL_STATUS.ROUTING : 
	        return "ROUTING";
	        break;
	    case ENUM_CALL_STATUS.CANCELLED : 
	        return IDWS_MOIP_CANCELLED;
	        break;
	    case ENUM_CALL_STATUS.FAILED :
	        return IDWS_MOIP_SKYPE_FAILED ;
	        break;
	    case ENUM_CALL_STATUS.EARLYMEDIA :
	        return "EARLYMEDIA";
	        break;
	    case ENUM_CALL_STATUS.RINGING_OUT :
	        return  IDWS_MOIP_SKYPE_RINGING;
	        break;
	    case ENUM_CALL_STATUS.RINGING_IN : 
	        return  IDWS_MOIP_SKYPE_RINGING;
	        break;
	    case ENUM_CALL_STATUS.INPROGRESS : 
	        return "INPROGRESS";
	        break;
	    case ENUM_CALL_STATUS.LOCALHOLD : 
	        return "LOCALHOLD";
	        break;
	    case ENUM_CALL_STATUS.REMOTEHOLD : 
	        return "REMOTEHOLD";
	        break;
	    case ENUM_CALL_STATUS.FINISHED : 
	        return IDWS_MOIP_SKYPE_FINISHED;
	        break;
	    case ENUM_CALL_STATUS.MISSED : 
	        return "MISSED";
	        break;
	    case ENUM_CALL_STATUS.REFUSED : 
	        return IDWS_MOIP_SKYPE_BUSY;
	        break;
	    case ENUM_CALL_STATUS.BUSY : 
	        return IDWS_MOIP_SKYPE_BUSY;
	        break;
	    case ENUM_CALL_STATUS.DROPPED : 
	        return "DROPPED";
	        break;
	    case ENUM_CALL_STATUS.VM_BUFFERING_GREETING :
	        return "VM_BUFFERING_GREETING";
	        break;
	    case ENUM_CALL_STATUS.VM_PLAYING_GREETING :
	        return "VM_PLAYING_GREETING";
	        break;
	    case ENUM_CALL_STATUS.VM_RECORDING :
	        return "VM_RECORDING";
	        break;
	    case ENUM_CALL_STATUS.VM_UPLOADING : 
	        return "VM_UPLOADING";
	        break;
	    case ENUM_CALL_STATUS.VM_SENT : 
	        return "VM_SENT";
	        break;
	    case ENUM_CALL_STATUS.VM_CANCELLED : 
	        return "VM_CANCELLED";
	        break;
	    case ENUM_CALL_STATUS.VM_FAILED : 
	        return "VM_FAILED";
	        break;
	    case ENUM_CALL_STATUS.TRANSFERRING : 
	        return "TRANSFERRING";
	        break;
	    case ENUM_CALL_STATUS.TRANSFERRED : 
	        return "TRANSFERRED";
	        break;
	        
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_1 :
	            return IDWS_MOIP_SKYPEOUT_FAIL24;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_2:
	                return IDWS_MOIP_SKYPEOUT_FAIL4;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_3 :
	                return IDWS_MOIP_SKYPEOUT_FAIL7;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_4 :
	                return IDWS_MOIP_SKYPEOUT_FAIL8;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_5 :
	                return IDWS_MOIP_SKYPEOUT_FAIL9;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_6 :
	         return IDWS_MOIP_SKYPEOUT_FAIL24;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_7 :
	                return IDWS_MOIP_SKYPEOUT_FAIL12;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_8 :
	                return IDWS_MOIP_SKYPEOUT_FAIL13;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_9 :
	                return IDWS_MOIP_SKYPEOUT_FAIL17;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_10 :
	                return IDWS_MOIP_SKYPEOUT_FAIL2;
	        break;
	    case ENUM_CALL_STATUS.SKYPEOUT_FAIL_REASON_11 :
	                return SID_EMERGENCY_CALL;
	        break;
	        
        case ENUM_CALL_STATUS.CALL_BEING_FORWARDED:
	                return IDWS_MOIP_CALL_BEING_FORWARDED;
	        break;
    }
}


SceneCalling.prototype.processEnterKey = function(){
    alert("SceneCalling.processEnterKey() ");
	
	this.hiddenPreview();
    ProcMgr.CancelCall();     
}

SceneCalling.prototype.processExitKey = function(){
    alert("SceneCalling.processExitKey() ");
    
   	//must cancel call because call will be changed to voicemail or callforwording.
	this.hiddenPreview();
    ProcMgr.CancelCall();
    
	if(ProcMgr.ProductInfo.support_multitasking == true)
	{
		ProcMgr.ExitWidget();
	}
	else
	{
		ProcMgr.flag_show_exit_popup = true;
	}
}

SceneCalling.prototype.processReturnKey = function(){
    alert("SceneCalling.processReturnKey() ");
}

SceneCalling.prototype.processClickEvent = function(obj){
	alert("SceneCalling.processClickEvent() ");
	
	this.processEnterKey();
}

	
SceneCalling.prototype.visiblePreview = function(){
	alert("SceneCalling.visiblePreview() ");
	
	if(g_Data.DATA_CREATE_CALL_INPUT_INFO.call_type == 0)
	{
		return;	
	}

	this.hideAvartar();
		
	if(this.flag_shown_preview == false)
	{		
		$('#window_my_preview').css('opacity','1.0');
		$('#window_my_preview').css('display','block');
		this.flag_shown_preview = true;		
	}
	
}

SceneCalling.prototype.hiddenPreview = function(){
	alert("SceneCalling.hiddenPreview() ");
	
	if(this.flag_shown_preview == true)
	{
		$('#window_my_preview').css('opacity','0.0');
		$('#window_my_preview').css('display','none');
		this.flag_shown_preview = false;	
	}

		
}

SceneCalling.prototype.showAvartar = function(){

	if(this.flag_shown_my_avatar == false)
	{
		alert("showAvartar");
		this.MyThumbnail.Show();
		this.flag_shown_my_avatar = true;
	}
}

SceneCalling.prototype.hideAvartar = function(){

	if(this.flag_shown_my_avatar == true)
	{
		alert("hideAvartar");
		this.MyThumbnail.Hide();
		this.flag_shown_my_avatar = false;
	}
}

SceneCalling.prototype.processVolume = function(Increase){
    alert("SceneCalling.processVolume() ");
    
    if(Increase == true)
    {
    	this.currentVolumeValue++
    	if(this.currentVolumeValue > 100)
    	{
    		this.currentVolumeValue = 100;
    	}
    }
    else
    {
    	this.currentVolumeValue--
    	if(this.currentVolumeValue < 0 )
    	{
    		this.currentVolumeValue = 0;
    	}
    }
    
   	PluginAPIMgr.SetSpeakerVolume(this.currentVolumeValue);
   	
}



