var MAX_CALLING_ANI_IMAGE_NUM = 5;

function SceneminiCalling(options) {
	this.options = options;
    
    this.CallingView=null;
    
    this.CallDurationTimeLabel = null;
	
	this.CallingPageView = null;
	this.CallingFailPageView =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;

	//for voice mail
	this.flag_VM_mode = false;
	this.VM_STATE = null;
	
}

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

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

	this.update();
   
	this.FlagAnimation=true;
	this.startTimer(); 
        this.ShowKeyHelpbar();
	if(Flag_Hidden_State==false)
       $("#skype_popup_background").show();
}

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

	this.hiddenPreview();
	this.hideDuration();
		    
	this.VM_STATE = null;

    this.FlagAnimation = false;
	if(this.CallingTimer != null)
	{
		this.stopTimer();
	}
	
	this.flag_VM_mode = false;
	if(Flag_Hidden_State==false)
       $("#skype_popup_background").hide();

}

SceneminiCalling.prototype.handleFocus = function () {
	alert("SceneminiCalling.handleFocus()");
 this.ShowKeyHelpbar();  
	this.highlight(this.CancelBtn);
	

}

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

    this.highlight(null);
}

SceneminiCalling.prototype.handleKeyDown = function (keyCode) {
	alert("SceneminiCalling.handleKeyDown(" + keyCode + ")");

	switch (keyCode) {
		case $.sfKey.LEFT:
		case $.sfKey.RIGHT:		
			break;
		case $.sfKey.UP:
			break;
			
		case $.sfKey.DOWN:
			break;

		case $.sfKey.ENTER:
             this.processEnterKey();
			break;
            
        case $.sfKey.RETURN:
	        this.processEnterKey();
	        $.sfKey.block(event); 
        	break;
        case $.sfKey.EXIT:
        	ProcMgr.flag_show_exit_popup = true;
            this.processEnterKey();
            $.sfKey.block(event);
			break;

		case 147:  //infolink
		case 261:  //contents
		case 115:  //wiselink
            ProcMgr.processInfolinkKey(); 
			break;
	}
	if(Flag_Hidden_State==false)
	event.stopPropagation();
}

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

SceneminiCalling.prototype.create = function(){
    alert("SceneminiCalling.create()");
	
    this.CallingView = new View("Calling");
	
	this.CallingPageView = new View("calling_page");
        
	this.CallDurationTimeLabel = new Label(null,"calling_call_duration_text");	
	this.CallDurationTimeLabel.SetText(IDWS_MOIP_SKYPE_CALL_DURATION + " 00:00:00");     
    
    this.CallMembertNameLabel = new Label(null,"calling_call_member_name");
    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");
   
    var this_ = $.sfScene.get({id:"miniCalling",scope:WIDGET.id});
    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.SetOnMouseOverCallbackFunc(function(obj){
        this_.highlight(obj);
    });
	
    this.CallingAnimation= new Label("calling_animation", null);
    
    var i=0;
    for( i=0; i < MAX_CALLING_ANI_IMAGE_NUM; i++)
    {
    	//check image path
    	this.CallingAnimationImageList[i] = g_Image.base_image_path + 'img_skype_loading_0' + (i+1) + '.png';
    }
    
    this.hideDuration();
}

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

}
SceneminiCalling.prototype.update = function()
{
	alert("SceneminiCalling.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());

	$.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).PrintKPILog(g_Data.DATA_CREATE_CALL_INPUT_INFO.callee_name);
    
    if(WndMgr.isCallingFail ==  true)
    {
        this.CallMessageLabel.SetText(IDWS_MOIP_SKYPE_FAILED);
        $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).PrintKPILog(IDWS_MOIP_SKYPE_FAILED);
    }
    else
    {
        this.CallMessageLabel.SetText(TV_SID_CALLING);
        $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).PrintKPILog(TV_SID_CALLING);
    }
    
    WndMgr.isCallingFail =  false; //init
    
    
    this.iCurrAniImage=0;
    this.CallingAnimation.SetBgImage(this.CallingAnimationImageList[this.iCurrAniImage]);
    
	this.CallingPageView.visibleObj();

	this.visiblePreview();
}

SceneminiCalling.prototype.updateCallingStatus = function(call_status)
{
	alert('SceneminiCalling.updateCallingStatus');
	
    this.CallMessageLabel.SetText(this.GetCallStatusString(call_status));

    if(call_status == ENUM_CALL_STATUS.CALL_BEING_FORWARDED)
	{
    	this.hiddenPreview();
    	this.showAvartar();
    }
    
    $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).PrintKPILog(this.GetCallStatusString(call_status));
}
    
SceneminiCalling.prototype.updateCallingMessage = function(call_status)
{
	alert('SceneminiCalling.updateCallingMessage');
	
    this.CallMessageLabel.SetText(this.GetCallStatusString(call_status));
    
    $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).PrintKPILog(this.GetCallStatusString(call_status));
}

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

SceneminiCalling.prototype.startTimer = function()
{  
	alert("SceneCalling.startTimer() ");
	
	var scene = $.sfScene.get({id:"miniCalling",scope:WIDGET.id});
	
	this.CallingTimer=setInterval("timerCF()", 500);
	timerCF = function() { scene.CallingTimerCallback();}
	
}

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

SceneminiCalling.prototype.CallingTimerCallback = function()
{
	this.iCurrAniImage++;
     
    if(this.iCurrAniImage >= this.CallingAnimationImageList.length)
    {
        this.iCurrAniImage = 0;
    }
    
	this.CallingAnimation.visibleObj();
    
    this.CallingAnimation.SetBgImage(this.CallingAnimationImageList[this.iCurrAniImage]);

	
	if(ProcMgr.callEndWaitingTimer ==true)
	{
		this.stopTimer();
	}

}

SceneminiCalling.prototype.GetCallStatusString = function(call_status)
{
	switch(call_status)
    {
	    case ENUM_CALL_STATUS.UNPLACED : 
	        return "UNPLACED";

	    case ENUM_CALL_STATUS.ROUTING : 
	        return "ROUTING";

	    case ENUM_CALL_STATUS.CANCELLED : 
	        return IDWS_MOIP_CANCELLED;

	    case ENUM_CALL_STATUS.FAILED :
	        return IDWS_MOIP_SKYPE_FAILED ;

	    case ENUM_CALL_STATUS.EARLYMEDIA :
	        return "EARLYMEDIA";

	    case ENUM_CALL_STATUS.RINGING_OUT :
	        return IDWS_MOIP_SKYPE_RINGING;

	    case ENUM_CALL_STATUS.RINGING_IN : 
	        return IDWS_MOIP_SKYPE_RINGING;

	    case ENUM_CALL_STATUS.INPROGRESS : 
	        return "INPROGRESS";

	    case ENUM_CALL_STATUS.LOCALHOLD : 
	        return "LOCALHOLD";

	    case ENUM_CALL_STATUS.REMOTEHOLD : 
	        return "REMOTEHOLD";

	    case ENUM_CALL_STATUS.FINISHED : 
	        return "FINISHED"; // IDWS_MOIP_SKYPE_FINISHED 20130108

	    case ENUM_CALL_STATUS.MISSED : 
	        return "MISSED";

	    case ENUM_CALL_STATUS.REFUSED : 
	        return IDWS_MOIP_SKYPE_BUSY;

	    case ENUM_CALL_STATUS.BUSY : 
	        return IDWS_MOIP_SKYPE_BUSY;

	    case ENUM_CALL_STATUS.DROPPED : 
	        return "DROPPED";

	    case ENUM_CALL_STATUS.VM_BUFFERING_GREETING :
			return IDWS_MOIP_SKYPE_BUFFERING;

	    case ENUM_CALL_STATUS.VM_PLAYING_GREETING :
			return IDWS_MOIP_PLAYING_VOICEMAIL_GREETING;

	    case ENUM_CALL_STATUS.VM_RECORDING :
			return IDWS_MOIP_VOICEMAIL_RECORDING;

	    case ENUM_CALL_STATUS.VM_UPLOADING : 
			return IDWS_MOIP_VOICEMAIL_UPLOADING;
	        
	    case ENUM_CALL_STATUS.VM_SENT : 
			return IDWS_MOIP_VOICEMAIL_SENT;

	    case ENUM_CALL_STATUS.VM_CANCELLED : 
			return IDWS_MOIP_VOICEMAIL_CANCELLED;

	    case ENUM_CALL_STATUS.VM_FAILED : 
			return IDWS_MOIP_VOICEMAIL_FAILED;

	    case ENUM_CALL_STATUS.TRANSFERRING : 
	        return "TRANSFERRING";

	    case ENUM_CALL_STATUS.TRANSFERRED : 
	        return "TRANSFERRED";

		case ENUM_CALL_STATUS. VM_SEND_FAILED:
			return IDWS_MOIP_VOICEMAIL_FAILED;
			
	    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 "SKYPEOUT_FAIL_REASON_6"; // IDWS_MOIP_SKYPEOUT_FAIL10 20130108
	        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;
    }
}


SceneminiCalling.prototype.processEnterKey = function(){
    alert("SceneminiCalling.processEnterKey() ");
    
    if(this.flag_VM_mode == true)
    {
    	if(this.VM_STATE == ENUM_CALL_STATUS.VM_RECORDING)
    	{
    		ProcMgr.UploadVoiceMail();
    	}
    	else
    	{
    		ProcMgr.CancelVoiceMail();
    	}
    }
    else
    {
		this.hiddenPreview();
	    ProcMgr.CancelCall();
    }
}

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


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

	
SceneminiCalling.prototype.visiblePreview = function(obj){
	alert("SceneminiCalling.visiblePreview() ");
	
	if(g_Data.DATA_CREATE_CALL_INPUT_INFO.call_type == 0)
    {           
		this.showAvartar();
		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;		
	}
			
}

SceneminiCalling.prototype.hiddenPreview = function(){
	alert("SceneminiCalling.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;	
	}
}
	
SceneminiCalling.prototype.showAvartar = function(){

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

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

SceneminiCalling.prototype.changeMode = function(bVMmode){
	
	if(bVMmode == true)
	{
		this.flag_VM_mode = true;

	    this.showDuration();
	    this.hiddenPreview();
	    
	    this.VM_STATE = ENUM_CALL_STATUS.RINGING_OUT;

		
	}
	else{
		this.flag_VM_mode = false;

	    this.hideDuration();
	    
	    this.VM_STATE = ENUM_CALL_STATUS.RINGING_OUT;

		
	}
}

SceneminiCalling.prototype.updateCallDuration = function(time)
{
	this.CallDurationTimeLabel.SetText(IDWS_MOIP_SKYPE_CALL_DURATION + "  "  + time);
}


SceneminiCalling.prototype.updateVoiceMailState = function(call_status)
{
	this.VM_STATE = call_status;
	
    //1. check Message 
    this.CallMessageLabel.SetText(this.GetCallStatusString(call_status));
    $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).PrintKPILog(this.GetCallStatusString(call_status));
    
}

SceneminiCalling.prototype.showDuration = function(){
	alert("SceneminiCalling.showDuration() ");

	this.CallDurationTimeLabel.visibleObj();
	this.CallDurationTimeLabel.SetText("");
	
	$.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).PrintKPILog(IDWS_MOIP_SKYPE_CALL_DURATION + "  00:00:00");
}

SceneminiCalling.prototype.hideDuration = function(){
	alert("SceneminiCalling.hideDuration() ");

	this.CallDurationTimeLabel.hiddenObj();
	this.CallDurationTimeLabel.SetText("");
}

SceneminiCalling.prototype.ShowKeyHelpbar = function(){
	if(WIDGET.mouse==true){
		helpBarHandler.hideHelpIcon('red',"");
		helpBarHandler.hideHelpIcon('green',"");
		helpBarHandler.hideHelpIcon('yellow',"");
		helpBarHandler.hideHelpIcon('blue',"");
		helpBarHandler.hideHelpIcon('tools',"");
		helpBarHandler.showHelpIcon('return',SID_RETURN);
		helpBarHandler.setHelpIconClickEvent('help_return',function(){
                $.sfScene.get({id:WIDGET.name, scope:WIDGET.id}).processExitKey();
        });
	}else{
		$("#HomeHelp").sfKeyHelp({
		//	'blue' : TV_SID_CHANGE_VIEW,
		//	'tools' : sfLang.SID_TOOLS,
			'return' : SID_RETURN
		});	
	}
}

