var MAX_VM_ANI_IMAGE_NUM = 5;


function SceneVoiceMail(options) {
	this.options = options;
    
    this.VoiceMailView = null;
    
    this.YourThumbnail = null;
    this.YourNameLabel = null;
    
    this.MyThumbnail = null;
    
	this.LogoLabel = null;
	    
	this.MessageLabel = null;

    this.CallDurationBgLabel	= null;
    this.CallDurationTextLabel	= null;
    this.CallDurationTimeLabel	= null;    
    this.shown_duration = true;
    
	this.CancelBtn = null;
    //this.UploadBtn = null;
    this.EndCallBtn = null;
	
	this.VM_STATE = null;
	
    this.current_volume_value = 0;

    this.iCurrAniImage = 0;
    this.VMAnimation = null;
    this.VMAnimationImageList = new Array(MAX_VM_ANI_IMAGE_NUM);
    
    this.VMTimer = null;
}

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

	this.create();
}

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

	this.update();

	this.startTimer();
	
}

SceneVoiceMail.prototype.handleHide = function () {
	alert("SceneVoiceMail.handleHide()");
	
	if(this.VMTimer != null)
	{
		this.stopTimer();
	}
}

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

	//check
    var currentHighlight =  this.VoiceMailView.getCurrentHighlight();
    if(currentHighlight == null)
    {
        this.highlight(this.EndCallBtn);
    }
    else
    {
        this.highlight(currentHighlight);
    }
   
}

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

	//check
	var currentHighlight =  this.VoiceMailView.getCurrentHighlight();
     if(currentHighlight != null)
    {
        currentHighlight.blur();
    }

}

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

	PluginAPIMgr.PlayAudio(keyCode);
	if(ProcMgr.checkWidgetHighlight()==false){
		return;
	}
	switch (keyCode) {
		
		case sf.key.VOL_DOWN:
        case sf.key.PANEL_VOL_DOWN:
			//this.processVolumeDownKey();
			break;
		case sf.key.VOL_UP:
        case sf.key.PANEL_VOL_UP:
			//this.processVolumeUpKey();
			break;
			
		case sf.key.LEFT:
             //this.processLeftKey();
             break;
             
		case sf.key.RIGHT:
             //this.processRightKey();
			break;
			
		case sf.key.UP:
			break;
		case sf.key.DOWN:
			break;
			
		case sf.key.ENTER:
             this.processEnterKey();
			break;
            
        case sf.key.RETURN:
		case sf.key.EXIT:
            sf.key.preventDefault();
			break;
            
        case 147:  //infolink
         case 261:  //contents
         case 115:  //wiselink
            sf.key.preventDefault();
			break;
	}
}

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

SceneVoiceMail.prototype.create = function(){
    alert("SceneVoiceMail.create()");
    this.VoiceMailView = new View("VoiceMail");
  
    this.YourNameLabel = new Label(null,"voice_mail_member_name");
    this.YourNameLabel.SetIsPlainText(true);

	this.YourThumbnail = new CompBase(); 
	this.YourThumbnail.CreateBG("voice_mail_member_thumbnail");
	this.YourThumbnail.AddJPGImageLayer("voice_mail_member_thumbnail_avatar");
    
	this.MyThumbnail = new CompBase(); 
	this.MyThumbnail.CreateBG("voice_mail_my_thumbnail_thumbnail");
	this.MyThumbnail.AddJPGImageLayer("voice_mail_my_thumbnail_thumbnail_avatar");    
    
	this.LogoLabel = new Label("voice_mail_skype_logo", null);
    this.LogoLabel.SetBgImage(g_Image.IMAGE_CALL_SKYPE_LOGO);    
    
    this.MessageLabel = new Label(null, "voice_mail_message");
   
    this.CallDurationBgLabel = new Label("voice_mail_duration_bg",null);
	this.CallDurationBgLabel.SetBgImage(g_Image.IMAGE_CALL_DURATION_BG);
	
	this.CallDurationTextLabel = new Label(null,"voice_mail_duration_text");
	this.CallDurationTextLabel.SetText(IDWS_MOIP_SKYPE_CALL_DURATION);
	
    this.CallDurationTimeLabel = new Label(null,"voice_mail_duration_time");

	var CurLangID = ResMgr.GetCurLanguage();
	if(CurLangID == ENUM_TV_LANGUAGE.VIE || CurLangID == ENUM_TV_LANGUAGE.IND || CurLangID == ENUM_TV_LANGUAGE.KAZ || CurLangID == ENUM_TV_LANGUAGE.LIT ||
		CurLangID == ENUM_TV_LANGUAGE.MKD || CurLangID == ENUM_TV_LANGUAGE.MAY || CurLangID == ENUM_TV_LANGUAGE.POL || CurLangID == ENUM_TV_LANGUAGE.ALB ||
		CurLangID == ENUM_TV_LANGUAGE.TAM || CurLangID == ENUM_TV_LANGUAGE.XHO || CurLangID == ENUM_TV_LANGUAGE.ZUL)
	{
		$('#voice_mail_duration_text').css('left','18px');
		$('#voice_mail_duration_text').css('width','220px');
		
		$('#voice_mail_duration_time').css('left','210px');
	}

    this.VMAnimation = new Label("voice_mail_animation", null);
    
    var i=0;
    for( i=0; i < MAX_VM_ANI_IMAGE_NUM; i++)
    {
    	this.VMAnimationImageList[i] = g_Image.base_loading_image_path + 'call_loading_0' + (i+1) + '.png';
    }

    var this_ = sf.scene.get("VoiceMail");
    var clickCF =  function(obj){this_.processClickEvent(obj);};
	
    this.EndCallBtn = new Button();
    this.EndCallBtn.SetImageObjId("voice_mail_end_call_btn");
    this.EndCallBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_CALL_END_BTN_NORMAL);
    this.EndCallBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_CALL_END_BTN_NORMALFOCUS);
	this.EndCallBtn.SetOnClickCallbackFunc(clickCF);
}

SceneVoiceMail.prototype.highlight=function(obj)
{
    this.VoiceMailView.setCurrenHighlight(obj);
}

SceneVoiceMail.prototype.update = function()
{
    alert("SceneVoiceMail.update()");
    		
    this.YourThumbnail.SetImageLayer(1,PluginAPIMgr.GetContactAvataImage(g_Data.DATA_VOICE_MAIL_INPUT_INFO.callee_name));    
    this.YourNameLabel.SetText( g_Data.DATA_VOICE_MAIL_INPUT_INFO.callee_name);     

    this.MessageLabel.SetText(IDWS_MOIP_SKYPE_RINGING);

    this.MyThumbnail.SetImageLayer(1,PluginAPIMgr.GetMyAvataImage());
    
	//this.BtnEnable();
	
	//this.CancelBtn.hiddenObj();
	//this.UploadBtn.hiddenObj();

    this.iCurrAniImage = 0;
    this.VMAnimation.SetBgImage(this.VMAnimationImageList[this.iCurrAniImage]);
	
    this.hideDuration();
    
    this.VM_STATE = ENUM_CALL_STATUS.RINGING_OUT;
}

SceneVoiceMail.prototype.updateCallDuration = function(time)
{
     this.CallDurationTimeLabel.SetText(time);
}


SceneVoiceMail.prototype.updateVoiceMailState = function(call_status)
{
	this.VM_STATE = call_status;
	
    //1. check Message 
    this.MessageLabel.SetText(this.GetCallStatusString(call_status));
    
    //2. check duration 
    if(call_status == ENUM_CALL_STATUS.VM_RECORDING)
    {
		this.showDuration();
    }
    else
    {
		this.hideDuration();
    }
    
    //3. check btn
    // if(call_status == ENUM_CALL_STATUS.VM_RECORDING)
    // {
    	// this.CancelBtn.visibleObj();
    // }
    // else
    // {
    	// this.CancelBtn.hiddenObj();
    // }
    
}


SceneVoiceMail.prototype.GetCallStatusString = function(call_status)
{
	switch(call_status)
	{
		case ENUM_CALL_STATUS.VM_BUFFERING_GREETING :
			return IDWS_MOIP_BUFFERING_VOICEMAIL_GREETING;

		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;
    }
}

SceneVoiceMail.prototype.processLeftKey = function(){
    alert("SceneVoiceMail.processLeftKey() ");
    
    // var currentHighlight =  this.VoiceMailView.getCurrentHighlight();
//     
    // if(currentHighlight == this.EndCallBtn && this.CancelBtn.GetFlagVisible() == true)
    // {
    	// this.highlight(this.CancelBtn);
    // }
}

SceneVoiceMail.prototype.processRightKey = function(){
    alert("SceneVoiceMail.processRightKey() ");
    
    // var currentHighlight =  this.VoiceMailView.getCurrentHighlight();
//     
    // if(currentHighlight == this.CancelBtn)
    // {
    	// this.highlight(this.EndCallBtn);
    // }
}


//check
SceneVoiceMail.prototype.processEnterKey = function(){
    alert("SceneVoiceMail.processEnterKey() ");
    
    var currentHighlight =  this.VoiceMailView.getCurrentHighlight();

	if(currentHighlight == this.EndCallBtn)
	{
    	if(this.VM_STATE == ENUM_CALL_STATUS.VM_RECORDING)
    	{
    		ProcMgr.UploadVoiceMail();
    	}
    	else
    	{
    		ProcMgr.CancelVoiceMail();
    	}
	}
    
    // switch(currentHighlight)
    // {       
	    // case this.CancelBtn:
			// ProcMgr.CancelVoiceMail();
            // break;
// 			    
        // case this.EndCallBtn:
        	// if(this.VM_STATE == ENUM_CALL_STATUS.VM_RECORDING)
        	// {
        		// ProcMgr.UploadVoiceMail();
        	// }
        	// else
        	// {
        		// ProcMgr.CancelVoiceMail();
        	// }
            // break;
// 
    // }
}

SceneVoiceMail.prototype.BtnDisable = function()
{
	this.highlight(null);
	// this.CancelBtn.SetFlagDim(true);
	// this.UploadBtn.SetFlagDim(true);
	// this.EndCallBtn.SetFlagDim(true);
	
}

SceneVoiceMail.prototype.BtnEnable = function()
{
	// this.CancelBtn.SetFlagDim(false);
	// this.UploadBtn.SetFlagDim(false);
	// this.EndCallBtn.SetFlagDim(false);
	
}

SceneVoiceMail.prototype.processClickEvent = function(obj){
	alert("SceneVoiceMail.processClickEvent() ");
	
	var currentHighlight =  this.VoiceMailView.getCurrentHighlight();
    if(currentHighlight != obj)
    {
    	this.highlight(obj);
    }
	
	this.processEnterKey();
}

/*

SceneVoiceMail.prototype.processVolumeDownKey = function(){
    alert("SceneVoiceMail.processVolumeDownKey() ");
   this.current_volume_value = PluginAPIMgr.GetSpeakerVolume();
   
    var min_volume =0;
    this.current_volume_value --;
    
    if(this.current_volume_value < min_volume)
    {
        this.current_volume_value = min_volume;
    }
    
    PluginAPIMgr.SetSpeakerVolume(this.current_volume_value );  
}

SceneVoiceMail.prototype.processVolumeUpKey = function(){
    alert("SceneVoiceMail.processVolumeUpKey() ");
    this.current_volume_value = PluginAPIMgr.GetSpeakerVolume();
   
    var max_volume =100;
    this.current_volume_value ++;
    
    if(this.current_volume_value >  max_volume)
    {
        this.current_volume_value = max_volume;
    }
    
    PluginAPIMgr.SetSpeakerVolume(this.current_volume_value );  
}

*/

SceneVoiceMail.prototype.showDuration = function(){
	alert("SceneVoiceMail.showDuration() ");
	
	if(this.shown_duration == false)
	{
		this.CallDurationBgLabel.visibleObj();
    	this.CallDurationTimeLabel.SetText("");
    	this.shown_duration = true;
	}
}

SceneVoiceMail.prototype.hideDuration = function(){
	alert("SceneVoiceMail.hideDuration() ");
	
	if(this.shown_duration == true)
	{
		this.CallDurationBgLabel.hiddenObj();
    	this.CallDurationTimeLabel.SetText("");
    	this.shown_duration = false;
	}
}


SceneVoiceMail.prototype.startTimer = function()
{  
	alert("SceneVoiceMail.startTimer() ");
	
	var scene = sf.scene.get('VoiceMail');
	
	this.VMTimer = setInterval("VoiceMailTimerCF()", 500);
	VoiceMailTimerCF = function() { scene.VMTimerCallback();}
}

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

SceneVoiceMail.prototype.VMTimerCallback = function()
{
    alert("SceneVoiceMail.VMTimerCallback() ");
    	
     this.iCurrAniImage++;
     
    if(this.iCurrAniImage >= this.VMAnimationImageList.length)
    {
        this.iCurrAniImage = 0;
    }   
    
    this.VMAnimation.SetBgImage(this.VMAnimationImageList[this.iCurrAniImage]);
    
	if(ProcMgr.callEndWaitingTimer ==true)
	{
		this.stopTimer();
	}

}
