﻿var _plugin = null;
var _timer = null;
var _countDown = null;
var _txtTime = null;
var _txtQtNum = null;
var _txtQtn = null;
var _imgMedia = null;
var _meVideo = null;
var _fxBeginQuestion = null;
var _page = null;
var _scale = null;
var _inputEnabled = true;
var _quizTime = 10;
var _currentQtn = 0;
var _totalQtnCount = 3;
var _jsonQuiz = null;
var _curQuizItem = null;
var _downloader = null;
var _totalScore = 0;
var _currentPoints = 10;
var _curQIndex = 0;

function StartQuiz(s,e)
{
	_plugin = s.getHost();
	_page = s.findName("_page");
    _timer = s.findName("_sbTimer");
    _fxBeginQuestion = s.findName("_sbBeginQuestion");
    _fxEndQuestion = s.findName("_sbEndQuestion");
    _countDown = s.findName("_sbCountdown");
    _txtTime = s.findName("_txtTime");
    _txtQtn = s.findName("_txtQtn");
    _txtQtNum = s.findName("_txtQtNum");
    _imgMedia = s.findName("_imgMedia");
    _meVideo = s.findName("_meVideo");
    _scale = s.findName("_scale");
    
    _downloader = _plugin.createObject("Downloader");
    _downloader.addEventListener("downloadProgressChanged", OnDownloadProgressChanged);
    _downloader.addEventListener("completed", OnDownloadCompleted);
    
    _imgMedia["Canvas.Left"] = -500;
    _meVideo["Canvas.Left"] = -500;
    _meVideo.addEventListener("currentStateChanged", OnMediaStateChanged);
    InitializeQuiz();
}

function InitializeQuiz()
{
    // Call the web service to load the quiz questions
    new Ajax.Request('MCQuizService.asmx/GetQuizData',{
        method: 'post', contentType:'application/json; charset=utf-8',
        onSuccess: OnRequestSuccess,
        onFailure: OnRequestFailure
    });
}

function OnRequestSuccess(val)
{
    var obj = val.responseText.evalJSON();
    if (obj==null) 
        alert("Error: Unable to get quiz data");
        
    var s = obj.d.toString();
    s = obj.d.toString();

    _jsonQuiz = GetQuizItems(s);
    _totalQtnCount = _jsonQuiz.length;
    
    // Begin the quiz UI
    LoadNextQuiz();
}

function GetQuizItems(s)
{
    return eval("("+s+")").MediaCorpQuiz.QuizItem;
}

function OnRequestFailure(err)
{
    var val = err.responseText;
    alert(val);
}

function LoadNextQuiz()
{
    //Math.floor(Math.random()*max);
    _currentQtn++;
    var max = _jsonQuiz.length;
    if (max==0) 
    {
        // End of questions
        return;
    }
    if (max==1)
    {
        DisplayQuizItem(1);
    }
    else
    {
        var idx = Math.floor(Math.random()*max+1);
        DisplayQuizItem(idx);
    }
}

function UpdateScore()
{
    // Update the score area
    $("_totalScore").innerHTML = "<b>"+_totalScore.toString()+"</b>";
}

function DisplayQuizItem(idx)
{
    DisableInputs();
    _txtQtNum.text = "Q"+_currentQtn; 
    // Populate the current quiz data
    var q = _jsonQuiz[idx-1];
    _curQuizItem = q;
    _curQIndex = idx-1;
    
    _txtQtn.text = q.question;
    switch (q.qtype)
    {
        case "PIC":
            _imgMedia.visibility = "Visible";
            _imgMedia["Canvas.Left"] = 10;
            _downloader.open("GET", "resources/"+q.mediaUrl);
            _downloader.send();
        break;
        case "VID":
            _meVideo.visibility = "Visible";
            _meVideo["Canvas.Left"] = 10;
            _downloader.open("GET", "resources/"+q.mediaUrl);
            _downloader.send();
        break;
        case "AUD":
        break;
    }
    
    _page.findName("_t@A").text = q.c_A;
    _page.findName("_t@B").text = q.c_B;
    _page.findName("_t@C").text = q.c_C;
    _page.findName("_t@D").text = q.c_D;
}

function DisplayQuizItem2()
{
    DisableInputs();
    _txtQtNum.text = "Q"+_currentQtn; 
    // Populate the current quiz data
    var i = _currentQtn-1;
    var q = _jsonQuiz[i];
    _curQuizItem = q;
    
    _txtQtn.text = q.question;
    switch (q.qtype)
    {
        case "PIC":
            _imgMedia.visibility = "Visible";
            _imgMedia["Canvas.Left"] = 10;
            _downloader.open("GET", "resources/"+q.mediaUrl);
            _downloader.send();
        break;
        case "VID":
            _meVideo.visibility = "Visible";
            _meVideo["Canvas.Left"] = 10;
            _downloader.open("GET", "resources/"+q.mediaUrl);
            _downloader.send();
        break;
        case "AUD":
        break;
    }
    
    _page.findName("_t@A").text = q.c_A;
    _page.findName("_t@B").text = q.c_B;
    _page.findName("_t@C").text = q.c_C;
    _page.findName("_t@D").text = q.c_D;
}

function OnMediaStateChanged(s,e) 
{
}

function OnDownloadProgressChanged(s,e)
{
}

function OnDownloadCompleted(s,e)
{
    switch (_curQuizItem.qtype)
    {
        case "PIC":
            _imgMedia.source = "resources/"+_curQuizItem.mediaUrl;
            // If it is a picture quiz type, start playing immediately
            _fxBeginQuestion.begin();
        break;
    
        case "VID":
            _meVideo.source = "resources/"+_curQuizItem.mediaUrl;
            // If it is a picture quiz type, start playing immediately
            _fxBeginQuestion.begin();
        break;
    }
}

function OnFXBeginQuestionEnd(s,e)
{
    StartQuizTimer();
}

function OnFXEndQuestionEnd(s,e)
{
    _txtQtn.opacity = 0;
    _page.findName("_c@A")["Canvas.Left"]=400;
    _page.findName("_c@B")["Canvas.Left"]=400;
    _page.findName("_c@C")["Canvas.Left"]=400;
    _page.findName("_c@D")["Canvas.Left"]=400;
    
    DisableInputs();
    
    LoadNextQuiz();
}

function StartQuizTimer() 
{
    _quizTime = 10;
    _txtTime.text = "10 sec(s) left";
    _scale.scaleX = 0;
    
    _countDown.begin();
    _timer.begin(); 
    
    EnableInputs();   
}

function OnTimerCompleted(s, e)
{
    _quizTime--;
    if (_quizTime<0) 
    {
        DisableInputs();
        
        ResetQuiz();
    }
    else 
    {
        _currentPoints = _quizTime;
        _txtTime.text = _quizTime.toString() + " sec(s) left";
        _timer.begin();
    }
}

function OnHover(s,e)
{
    if (!_inputEnabled) 
        return;
    
    s.opacity = 1;
}

function OnLeave(s,e)
{
    if (!_inputEnabled) 
        return;
    
    s.opacity = 0.6;
}

function OnChoiceSelected(s,e)
{
    if (!_inputEnabled) 
        return;
        
    var n=s.name;
    var a = n.split("@");
    a = a[1];

    if (a==_curQuizItem.answer) 
    {
        _totalScore += _currentPoints;
    }
    
    DisableInputs();
    ResetQuiz();
    UpdateScore();
}

function ResetQuiz()
{
    _countDown.stop();
    _timer.stop();
    
    _currentPoints = 10;
    // Hide the timer 
    _txtTime.text = "";
    _scale.scaleX = 0;

    // Hide the media elements
    _meVideo.stop();
    _meVideo.source = "";
    _imgMedia.source="";
    _imgMedia["Canvas.Left"] = -500;
    _imgMedia.visibility = "Collapsed";
    _meVideo["Canvas.Left"] = -500;
    _meVideo.visibility = "Collapsed";

    // Remove the shown quiz from the array
    _jsonQuiz.splice(_curQIndex,1);
    // Fade the UI away and reset the user interface interface
    _fxEndQuestion.begin();
}

function EnableInputs()
{
    if (_inputEnabled) 
        return;
    _inputEnabled = true;
    
    _page.findName("_r@A").cursor="Hand";
    _page.findName("_r@A").opacity=0.6;
    _page.findName("_r@B").cursor="Hand";
    _page.findName("_r@B").opacity=0.6;
    _page.findName("_r@C").cursor="Hand";
    _page.findName("_r@C").opacity=0.6;
    _page.findName("_r@D").cursor="Hand";
    _page.findName("_r@D").opacity=0.6;
}
function DisableInputs()
{
    if (!_inputEnabled) 
        return;
    _inputEnabled = false;
    
    _page.findName("_r@A").cursor="Default";
    _page.findName("_r@A").opacity=0.6;
    _page.findName("_r@B").cursor="Default";
    _page.findName("_r@B").opacity=0.6;
    _page.findName("_r@C").cursor="Default";
    _page.findName("_r@C").opacity=0.6;
    _page.findName("_r@D").cursor="Default";
    _page.findName("_r@D").opacity=0.6;
}