﻿// Copyright Vcast 2006 
// By Felix Li
function SecToHHMMSS(sec)
{
    var n = Math.ceil(sec);
    var h = Math.floor(n / 3600);
    var m = Math.floor(n / 60) - (h * 60);
    var s = Math.floor((h*3600 + m*60) - n );
    s = Math.abs(s);
    if (h<10)
        h = '0' + h;
    if (m<10)
        m = '0' + m;
    if (s<10)
        s = '0' + s;
    return h + ":" + m + ":" + s; 
}

function SlideSynchronizer(_me,_targetImg,_player,_playerTimeDiv,_slideArray,_chapterArray,_mode)
{
    this.me = _me;
    this.player = _player;
    this.targetImg = _targetImg;
    this.slides = _slideArray;
    this.playerTimeDiv = _playerTimeDiv;
    this.chapterArray = _chapterArray;
    this.mode = _mode;
    this.currentSlidePath = "";
    this.qaInstance = "";
    
    this.StartSynchronization = function()
    {
        // Get the current postion of the seminar in second
        // oscarh 20070730
        //var curPos = Math.ceil(document.getElementById(window[this.me].player).Controls.CurrentPosition);
        var curPos = Math.floor(document.getElementById(window[this.me].player).Controls.CurrentPosition);
        var time_text = SecToHHMMSS(document.getElementById(window[this.me].player).Controls.CurrentPosition) + " of " + SecToHHMMSS(this.chapterArray['chapter_duration']);        
      
        // Display the current postion
        document.getElementById(window[this.me].playerTimeDiv).innerHTML = time_text;
        
        if (curPos >= (window[this.me].chapterArray['chapter_duration']))
        {           
            if ((window[this.me].qaInstance != "")&&(window[window[this.me].qaInstance].pause))
            {        
            
            }
            else
            {
                document.getElementById(window[this.me].player).Controls.Pause();
                if (window[this.me].demo == true)
                    return;
                if (window[this.me].mode=='cpt')
                {
                    var url = 'JTABridge.aspx?cmd=chapter_checkout&chapterID='+ window[this.me].chapterArray['chapterID'] + '&userID=' + window[this.me].chapterArray['userID'];
                    //JTABridge(url);
                        window.location = url;           
                   //window.location.href = 'index.aspx?url=cpt_checkout.aspx\"sem_code=' + window[this.me].chapterArray['sem_code'] + '\'mode=cpt';
                    
                    // oscarh 20080505
                    //var ss = alert('This chapter is finished');  
                    //setTimeout(ss,500);
                   	window.setTimeout('alert("This chapter is finished")',500);
                }
                else if(window[this.me].mode=='gen')
                {
                    window.location.href = 'index.aspx?url=Chapters.aspx\"sem_code=' + window[this.me].chapterArray['sem_code'] + '\'mode=gen';
                }               
            }
        }
        window[this.me].timeCheck(curPos);        
        var cmd = "window['" + window[this.me].me + "'].StartSynchronization();";        
        // oscarh 20070725 
        //setTimeout(cmd,200);       
        setTimeout(cmd,50);
    }    
    this.timeCheck = function(curPos)
    {
        for (var i=0;i<window[this.me].slides.length;i++)
        {
            if ((window[this.me].slides[i]['time'] <= curPos) && (window[this.me].currentSlidePath != window[this.me].slides[i]['slide_path']))
            {
                window[this.me].currentSlidePath = window[this.me].slides[i]['slide_path'] ;     
            }                    
        }        
        var fn1 = document.getElementById(window[this.me].targetImg).src;
        var pos = fn1.lastIndexOf('/')+1;
        fn1 = fn1.substring(pos,(fn1.length));
        
        var fn2 = window[this.me].currentSlidePath;
        pos = fn2.lastIndexOf('/')+1;
        fn2 = fn2.substring(pos,(fn2.length));
        
        if (fn1 != fn2)
            document.getElementById(window[this.me].targetImg).src = window[this.me].currentSlidePath;
    }
}

function QuestionAndAnswerScychronizer(_me,_targetDivs,_player,_qaArray,userID)
{
    this.me = _me;
    this.player = _player;
    this.targetDivs = _targetDivs;
    this.qaArray = _qaArray;
    this.xmlDoc = null;
    this.pause = false;
    this.userID = userID;
    this.ans_selected = false;
    this.answer = 'null';
    
    this.StartSynchronization = function(skip)
    {
        if (skip != 'true')  	    
            window[this.me].timeCheck();
        var cmd = "window['" + window[this.me].me + "'].StartSynchronization('false');";
        if (!this.pause)
            // oscarh 20070725 
           //setTimeout(cmd,200); 
            setTimeout(cmd,50);
    }    
    this.timeCheck = function()
    {
        for (var i=0;i<window[this.me].qaArray.length;i++)
        {
            // 20070725 oscarh - dont use Math.floor() in order to get the real value of position
            //var curPos = Math.floor(document.getElementById(window[this.me].player).Controls.CurrentPosition);            
            var curPos = document.getElementById(window[this.me].player).Controls.CurrentPosition; 
            if ((window[this.me].qaArray[i]['time'] <= curPos) && (window[this.me].qaArray[i]['played']==false))             
            {
                window[this.me].pause = true;
                document.getElementById(window[this.me].player).Controls.Pause();
                                  
                // Show questions and answers 
                window[this.me].showQuestion(qaArray[i],window[this.me].targetDivs);
                this.qaArray[i]['played'] = true;
            }
        }
    }
    this.showQuestion = function(qaArray,targetDivs)
    {        
       /* var html = '<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">'+
                      '<tr>'+
                        '<td colspan="2" class="ft11Lh18"><br><br><@question#><br></td>'+
                      '</tr>'+
                      '<tr>'+
                       ' <td colspan="2" class="ft11Lh18"><@choice#></td>'+
                      '</tr>'+
                      '<tr>'+
                       ' <td width="330" class="styleBlack11bold">Your Answer:<div id="userChoice">(Please select your answer from above).</div></td>'+
                       ' <td width="70" class="ft11Lh18">'+
                        '  <@submit#>'+
                       ' </td>'+
                     ' </tr>'+
                     '<tr>'+
                     '<td colspan="2"><td>'+
                     '</tr>' +
                    '</table>';
        html = html.replace('<@question#>',qaArray['question']);
        var choice = "<br>";*/
        
        document.getElementById(targetDivs['question']).innerHTML = "<img src=\"images/radiobutton.gif\" />" + qaArray['question'] + '<br>';
        for (var i=1;i<6;i++)
        {
            if (qaArray['answer_' + i] != 'null')
            {      
                document.getElementById(window[this.me].targetDivs['ans'+i]).style.visibility = 'visible';       
                document.getElementById(targetDivs['ans'+i]).innerHTML = qaArray['answer_' + i] ;
                //document.getElementById(targetDivs['ans'+i]).onmouseover = 'document.getElementById(\''+targetDivs['ans'+1] + '\').style.backgroundcolor = \'#C60446\'';
                //document.getElementById(targetDivs['ans'+i]).onclick = 'document.getElementById(\'' +targetDivs['uans']+ '\').innerHTML = ' + qaArray['answer_' + i];
            }
           else
                 document.getElementById(window[this.me].targetDivs['ans'+i]).style.visibility = 'hidden';
            
//                choice += window[this.me].createOption('choice'+i,qaArray['answer_'+i],'userChoice');
        }
        document.getElementById(targetDivs['uans']).innerHTML = '(select your answer from above)' ;
        //html = html.replace('<@choice#>',choice + '<br>');
        
        var submitstr = window[this.me].createSubmit('qaSubmit',window[this.me].me,window[this.me].userID,qaArray['questionID']);
        document.getElementById(targetDivs['submit']).innerHTML = submitstr;
        //html = html.replace('<@submit#>',submitstr);
        //document.getElementById(window[this.me].targetDiv).innerHTML = html;        
        document.getElementById(window[this.me].targetDivs['body']).style.visibility = 'visible';
    }
    this.createOption = function(name,value,targetDivs)
    {
        var s = '<table border="1" bordercolor="#FFFFFF"><tr><td><div id="' + name +'" style="width:300px;cursor:pointer;background-color=#EFEFEF;"'+
        ' onclick="javascript:document.getElementById(\'' + targetDiv + '\').innerHTML=\'' + value +
        "'; window['"+window[this.me].me+"'].ans_selected = true;\" onmouseover=\"javascript:document.getElementById('choice1').style.backgroundcolor = '#C60446';\" /><img src=\"images/radiobutton.gif\" /> "+ value + '</div></td></tr></table>';
        return s;
    }
    this.createSubmit = function(name,parent,userID,questionID)
    {
        // oscarh 20080505 - Reset chose ans to none
        //var s = '<div id="' + name +'" style="cursor:pointer;" onclick="javascript:window[\'qaSyn\'].submitAns(\''+userID+'\',\''+questionID+'\');"><IMG src="images/btn_submit.gif" /></div>';
        var s = '<div id="' + name +'" style="cursor:pointer;" onclick="javascript:window[\'qaSyn\'].submitAns(\''+userID+'\',\''+questionID+'\');anslayer=null;"><IMG src="images/btn_submit.gif" /></div>';
        return s;
    }
    this.submitAns = function (userID, questionID) {
        if (window[this.me].ans_selected == false) {
            alert('Please select your answer first!');
            return;
        }
        var url = 'JTABridge.aspx?cmd=submit_answer&userID=' + userID + '&ans=' + escape(window[this.me].answer) + '&questionID=' + questionID + '&cid=' + window[this.me].qaArray['chapterID'];
        JTABridge(url);
        //document.getElementById(window[this.me].player).Controls.CurrentPosition = Math.ceil(document.getElementById(window[this.me].player).Controls.CurrentPosition) + 1;
        document.getElementById(window[this.me].player).Controls.Play();
        document.getElementById(window[this.me].targetDivs['body']).style.visibility = 'hidden';

        // oscarh 20080505 - Reset all answer style when question is shown
        document.getElementById('ans1').style.color = '#333333';
        document.getElementById('ans2').style.color = '#333333';
        document.getElementById('ans3').style.color = '#333333';
        document.getElementById('ans4').style.color = '#333333';
        document.getElementById('ans5').style.color = '#333333';
        document.getElementById('ans1').style.fontWeight = 'normal';
        document.getElementById('ans2').style.fontWeight = 'normal';
        document.getElementById('ans3').style.fontWeight = 'normal';
        document.getElementById('ans4').style.fontWeight = 'normal';
        document.getElementById('ans5').style.fontWeight = 'normal';
        window[this.me].pause = false;
        window[this.me].ans_selected = false;
        window[this.me].StartSynchronization('true');

    }
    
}
function PauseCounter(_me,_countTime,_counterDiv,expiredFunc)
{
    this.me = _me;
    this.oriCountTime = _countTime;
    this.countTime = _countTime;
    this.enable = false;
    this.counterDiv = _counterDiv;
    this.onexpired = expiredFunc;
    
    this.Start = function()
    {
        this.countTime = this.oriCountTime;
        this.enable = true;
        window[this.me].timeCheck();
    }
    
    this.timeCheck = function()
    {
        window[this.me].countTime = window[this.me].countTime - 1;
        document.getElementById(window[this.me].counterDiv).innerHTML = window[this.me].countTime;
        var cmd= 'window[\'' + window[this.me].me + '\'].timeCheck();';
        if (window[this.me].enable)
        {
            if (window[this.me].countTime <= 0)
            {
                this.call(window[this.me].onexpired());
             }
             else
             {
                setTimeout(cmd,1000);
             }
        }
    }
    
    this.Stop = function()
    {
        this.enable = false;
    }
}


JTABridge = function(url)
{
    //alert(url);
    /*var head = document.getElementsByTagName('head').item(0);
    var old  = document.getElementById('lastLoadedCmds5');
    if (old) head.removeChild(old);
	
    script = document.createElement('script');
    var scriptUrl = 'ShowAnswer.aspx?userID='+userID+'&ans=' + document.getElementById('userChoice').innerHTML + '&questionID=' + window[this.me].qaArray['questionID'];
    
    script.src = scriptUrl;
    script.type = 'text/javascript';
    script.defer = true;
    script.id = 'lastLoadedCmds5';
    void(head.appendChild(script));*/	    
    var xmlDoc  = null;
    if (window.ActiveXObject)
    {         
        //alert('XMLHTTP support');
         xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        alert('Your browser can\'t handle this script');
        return;
    }
    //alert('XMLHTTP created');
    xmlDoc.open("GET",url,false);      
    //alert('GET URL');
    xmlDoc.send(null);       
    //var response = xmlDoc.responseText;
    //alert(response);
    //alert('SENT REQUEST');

}




