﻿// JScript File
function MediaCmd()
{
    this.cmd = "play";//record;
    this.id = 1;//play list id
    this.playFlag = 0;// 0 is org ,1 is recorded
}
/*
    strWav: 文件在本地，相对路径
    strGif: 绝对路径
*/
function HFollowreading (oContainer,strDataRoot, strDataPath,strAnswer, strChn, strWav, strPhon, strGif, nType)
{
    //debugger;
    this.m_oContainer = oContainer;
    this.m_nType = nType;
    
    this.$ = function(id)
	{
	    return this.m_oContainer.all(id);
	}
	
    this.m_oContainer.bindObj = this;

    //this.m_strDataRoot = strDataRoot;
    //this.m_strDataPath = strDataPath;
 
	var show_img = this.$("showImg");
	show_img.style.display = "none";
    show_img.onload = function()
    {
		show_img.onload = null;
		show_img.style.display = (strGif==""?"none":"block");
    };
    show_img.src = strGif;
    
    this.$("showEng").innerText = strAnswer;
    this.$("showChn").innerHTML = strChn;
    this.m_strAnswer = strAnswer;
    this.m_strChn = strChn;
    this.m_strWav = strDataPath + strWav;    
    this.m_strRecWav = strDataPath + "_" + strWav;
    this.m_strTipWav  = strDataRoot + "rectip.wav";
    this.m_strPhon = strPhon;
    this.m_nScore = 0;
    
    
    
    this.$("processBarParent").innerHTML = "";
    
    this.oPlayProcessBar = this.$("processbarContainer").children[0].cloneNode(true);
    this.processBar_Binder1 = new ProcessBar_Binder(this.oPlayProcessBar);
    this.processBar_Binder1.setRange( 0, 0 ,100,1);
    
    this.oRecProcessBar = this.$("processbarContainer").children[1].cloneNode(true);
    this.processBar_Binder2 = new ProcessBar_Binder(this.oRecProcessBar);
    this.processBar_Binder2.setRange( 0, 0 ,100,1);
    
    this.mediaCmdArray = new Array();
    
    //快照，保存当前状态
    
    this.snap = function()
    {
        /*
        var objClone = this.clone();
        objClone.m_cloneControl = this.m_oContainer.cloneNode(true);
        return objClone;
        */
    }
    
    this.nTimeoutId0 = 0;
    this.nTimeoutId1 = 0;
    this.nTimeoutId1 = 0;

    
    this.CheckMediaState = function(oThis)
    {
        if (oThis.m_oContainer.style.display == "none")
        {
            oThis.nTimeoutId0 = setTimeoutEx( oThis.CheckMediaState, 100 , oThis);
            return;
        }
       
        var oProcessBarParent = oThis.$("processBarParent");
        var oProcessBar = oProcessBarParent.children[0];
        var state = $$("wmp1").playState;
        
        if (IsEnableVCOnline())
        {
            if ($$("VCOnline1").IsRecording() == true)
            {//录音
                var mc = oThis.GetMediaCmd();
                if (typeof(mc) != "undefined")
                {
                    var d = $$("VCOnline1").GetRecordTimeLength();
                    var p = $$("VCOnline1").GetRecordTimePosition();
                    p = parseInt(p * 100 / d);
                    if (typeof(oProcessBar) == "undefined")
                    {  
                        oProcessBarParent.appendChild(oThis.oRecProcessBar);
                    }
                    
                    oThis.processBar_Binder2.setPosition( p );
                }
                
            }
          
            
        }
       
        
        if (state == 3)
        {
            var d = $$("wmp1").currentMedia.duration;
            var p = $$("wmp1").controls.currentPosition;
            if (typeof(d) != "undefined" && typeof(p) != "undefined")
            {
                if (typeof(oProcessBar) == "undefined" )
                {
                    oProcessBarParent.appendChild(oThis.oPlayProcessBar);
                }
                oThis.processBar_Binder1.setPosition( parseInt(p * 100 / d));
            }
        }
      
        oThis.nTimeoutId0 = setTimeoutEx( oThis.CheckMediaState, 100 , oThis);
    }
    
   
    this.RemoveProcessBar = function()
    {
        //debugger;
        var oProcessBarParent = this.$("processBarParent");
        var oProcessBar = oProcessBarParent.children[0];
        if (typeof(oProcessBar) != "undefined")
        {
            oProcessBarParent.removeChild(oProcessBar);
            //this.$("processbarContainer").appendChild(oProcessBar);
        }
    }
    
     
    this.GetMediaCmd = function()
    {
        var mc;
        if (this.mediaCmdArray.length <= 0)
            return mc;
        mc = this.mediaCmdArray[0];
        return mc;
    }
    
    this.DomediaCmdArray = function(oThis)
    {
        var mc = oThis.GetMediaCmd();
        if (typeof(mc) != "undefined")
        {   
            if (mc.cmd == "play")
            {
                if (mc.id == 3)
                {
                    if (oThis.m_nType == 1)
                    {
                        oThis.$("showEng").innerText = oThis.m_strAnswer;
                    }
                    oThis.ShowCompare();
                    if (oThis.onCompareDone)
						oThis.onCompareDone();
                }
                
            
                var strWav;
                if (mc.playFlag == 0)
                {
                    strWav = oThis.m_strWav;
                }
                else
                {
                    //debugger;
                    strWav = oThis.m_strRecWav;
                }    
                if ( $$("VCOnline1").IsFileExist(strWav) == false)
                {//
                    oThis.mediaCmdArray.splice(0,1); 
                    oThis.DomediaCmdArray(oThis);                       
                }
                else
                {
                    //$$("wmp1").URL = GetMediaFile(strWav);
                    //alert(GetMediaFile(strWav));
                    PlayMp3File(GetMediaFile(strWav));
                }
            }
            else if (mc.cmd == "record")
            {
                //debugger;
                if (IsEnableVCOnline())
                {
                    //debugger;
                    var nRecTipTime = $$("VCOnline1").GetLocalWaveTimeLength(oThis.m_strTipWav);
                    
                    $$("VCOnline1").PlayStart(oThis.m_strTipWav);
                    //while ($$("VCOnline1").IsPlaying());
                    
                    oThis.nTimeoutId2 = setTimeoutEx( oThis.OnRecTipEnd, nRecTipTime , oThis);
                    
                    return;
               
                }
                else
                {
                    oThis.mediaCmdArray.splice(0,1); 
                    oThis.DomediaCmdArray(oThis);   
                }
            }
        }
        else
        {
            oThis.HideCompare(); 
        }
         
    }
    
    this.HideCompare = function()
    {
        //this.$("showCompare").style.display = "none";
    }
    
    this.ShowCompare = function()
    {
        //debugger;
        var oCompareImage = this.$("showCompare_Image");
        this.$("showCompare").style.display = "";
        
        var strMp3Sys = this.m_strWav;
        var strMp3User = this.m_strRecWav;
        //得到PCM语音
        var strWavSys = strMp3Sys.replace(/.mp3$/gi,".wav");
        var strWavUser = strMp3User.replace(/.mp3$/gi,".wav");
        
        //if ($$("VCOnline1").IsFileExist(strWavSys) != true)
        {
            $$("VCOnline1").ToWAV(strMp3Sys);
        }
        
        if ($$("VCOnline1").IsFileExist(strWavUser) != true)
        {
            $$("VCOnline1").ToWAV(strMp3User);
        }
        
        this.m_nScore = $$("VCOnline1").VoiceCompare(strWavSys, strWavUser, this.m_strPhon);
        this.$("showCompare_Score").innerText = "成绩：" + this.score();
        oCompareImage.src = $$("VCOnline1").GenerateLastVCImageFile();
    }
    
    this.OnRecTipEnd = function(oThis)
    {
        var nRecTime = GetLocalMp3TimeLength(oThis.m_strWav);
        nRecTime += 3000;
        $$("VCOnline1").RecordStart(oThis.m_strRecWav, nRecTime, true);
        //debugger;
    }
    
    this.nDelayTimeRef = 2;
    this.nDelayTime = 2;
    
    this.Start = function()
    {
        
        this.RemoveProcessBar();
        
        this.nTimeoutId0 = setTimeoutEx( this.CheckMediaState, 100 , this);
        
        this.$("showEng").innerText = strAnswer;
        
        this.$("dTime").style.visibility = "visible";
        this.$("dTime").style.width = this.$("showEng").offsetWidth;
    
        //this.ShowCompare();
        //if (this.$("showCompare_Image").fileSize == "-1")
        {
            this.$("showCompare").style.display = "none";
        }
        
        this.nDelayTimeRef =  this.m_strAnswer.length / 3;
        if (this.nDelayTimeRef < 3)
        {
            this.nDelayTimeRef = 3;
        }
        else if( this.nDelayTimeRef > 7)
        {
            this.nDelayTimeRef = 8;
        }
               
        this.m_nScore = 0;
        var mc;
        
        if (this.m_nType == 0)
        {//回声跟读
            mc = new MediaCmd();
            mc.cmd = "play";
            mc.id = 1;
            mc.playFlag = 0;
            this.mediaCmdArray.push(mc);

            mc = new MediaCmd();
            mc.cmd = "record";
            mc.id = 2;
            this.mediaCmdArray.push(mc);

            mc = new MediaCmd();
            mc.cmd = "play";
            mc.id = 3;
            mc.playFlag = 0;
            this.mediaCmdArray.push(mc);


            mc = new MediaCmd();
            mc.cmd = "play";
            mc.id = 4;
            mc.playFlag = 1;
            this.mediaCmdArray.push(mc);  
            
            
        }
        else if (this.m_nType == 1)
        {//提示说话
           
            mc = new MediaCmd();
            mc.cmd = "record";
            mc.id = 2;
            this.mediaCmdArray.push(mc);

            mc = new MediaCmd();
            mc.cmd = "play";
            mc.id = 3;
            mc.playFlag = 0;
            this.mediaCmdArray.push(mc);


            mc = new MediaCmd();
            mc.cmd = "play";
            mc.id = 4;
            mc.playFlag = 1;
            this.mediaCmdArray.push(mc);
            
            
        }
        else if (this.m_nType == 2)
        {//见词发音
            
            mc = new MediaCmd();
            mc.cmd = "record";
            mc.id = 2;
            this.mediaCmdArray.push(mc);

            mc = new MediaCmd();
            mc.cmd = "play";
            mc.id = 3;
            mc.playFlag = 0;
            this.mediaCmdArray.push(mc);


            mc = new MediaCmd();
            mc.cmd = "play";
            mc.id = 4;
            mc.playFlag = 1;
            this.mediaCmdArray.push(mc);  
            this.nDelayTimeRef = 2;
            
        }
        this.nDelayTime = this.nDelayTimeRef;
        this.nTimeoutId1 = setTimeoutEx( this.DelayStart, 50 , this);
           
    }
    
    this.DelayStart = function(oThis)
    {
        oThis.nDelayTime -= 0.05;
        
        if (oThis.nDelayTime > 0)
        {
            oThis.$("dTime").style.width = oThis.$("showEng").offsetWidth * ( oThis.nDelayTime / oThis.nDelayTimeRef);
            oThis.nTimeoutId1 = setTimeoutEx( oThis.DelayStart, 50 , oThis);
            return;       
        }
        
        oThis.$("dTime").style.visibility = "hidden";
        
        if (oThis.m_nType == 1)
        {
            var strAnswer = "";
            var ss = oThis.m_strAnswer.split(" ");
            var i;
            for ( i = 0; i < ss.length; i++)
            {
                if (i % 2 == 1)
                {
                    ss[i] = ss[i].replace(/./g,"_");
                }
                strAnswer += ss[i] + " ";
                
            }
            //debugger;
            oThis.$("showEng").innerText = strAnswer;
        }
        oThis.DomediaCmdArray(oThis);
    }
    
    this.Stop = function()
    {
        this.mediaCmdArray.splice(0, this.mediaCmdArray.length);
        window.clearTimeout(this.nTimeoutId0);
        window.clearTimeout(this.nTimeoutId1);
        window.clearTimeout(this.nTimeoutId2);
        MediaStop();
    }    
    
    
    
    
     this.score = function(_100, id)
     {
        if (this.m_nScore < 50)
        {
            return 0;
        }
        else if (this.m_nScore < 80)
        {
            return this.m_nScore;
        }
        return 100;    
     }
     
     
     this.Play = function()
     {
        this.ShowAnswer();
     }
     
     this.ShowAnswer = function()
     {
        //$$("wmp1").URL = GetMediaFile(this.m_strWav);
        //alert(GetMediaFile(this.m_strWav));
        PlayMp3File(GetMediaFile(this.m_strWav));
     } 
     
     this.ShowMyWork = function()
     {
        //$$("wmp1").URL = GetMediaFile(this.m_strRecWav);
        //alert(GetMediaFile(this.m_strRecWav));
        PlayMp3File(GetMediaFile(this.m_strRecWav));
     }  
     
     this.onresize = function()
     {
      
     }
     
     this.PlayStateChange = function(nState) 
    {
        //var state = $$("wmp1").playState;
        if ( nState == 1)
        {
            this.RemoveProcessBar();
            this.mediaCmdArray.splice(0,1);
            window.setTimeoutEx(this.DomediaCmdArray,10,this);
        }
        else if (nState == 2)
        {
          
        }
        
        //this.$("info1").innerText = state;
    }

    this.RecordFileSaved = function(id) 
    {
        this.RemoveProcessBar();
        this.mediaCmdArray.splice(0,1);
        window.setTimeoutEx(this.DomediaCmdArray,10,this);
    }
    
    this.onresize = function()
    {
        
        this.$("showCompare_Image").style.width = this.$("hfollowreading_td3").offsetWidth - 30;        

    }

}



