﻿var oQ;
var diAnswer = 1;
var nQuestions = 6;
cpass="#AFEEEE"
cfail="yellow"
                    
 $(document).ready(function(){LoadSession()});
 
 
function ShowWords() {
   $('#listdivwords').html('');
    jQuery.each(oQ.DataListItems, function() { $(PrepWords(this)).appendTo('#listdivwords');});
    $(oQ.showResult()).prependTo('#listdivwords');
    boxHeight(".listdivword")
} 
function boxHeight(val)
{
     var k=0;
    jQuery.each($(val), function (){ if (k < $(this).height()) k=$(this).height(); });
    jQuery.each($(val), function (){$(this).height(k);});
}

function PrepWords(val)
{
    vars = (val.k).split(' ');
  varout = '<div class="listdivword"><font color="gray"><i>' + val.i + '</i> </font>';
  if (val.f > 0) varout = '<div class="listdivword" style="background-color:yellow"><font color="gray">' + val.i + '. </font><font color="red">Incorrect responses: ' + val.f + '</font><br>';
  for (y = 0; y < vars.length; y++) 
      if (vars[y].charAt(0) != '(')
          varout = varout + "<a href='javascript:ta(1, \"" + vars[y] + "\")'><b>" + vars[y] + "</b></a> ";
      else
          varout = varout + vars[y];
  varout=  varout + ' - ';
  vars = (val.v).split(";");
  for (y = 0; y < vars.length; y++)
      varout = varout + "<a href='javascript:ta(2, \"" + vars[y] + "\")'>" + vars[y] + "</a> ";
  return varout + '</div>';
}
function LoadSession() {
    $("#ddlsref").bind("change",function(e){$.cookie("dict", this.selectedIndex);});
    if ($.cookie("dict") != null && $.cookie("dict") != '') {  try{$("#ddlsref")[0].selectedIndex = $.cookie("dict");} catch(err){}}
    $("#ddlsref2").bind("change", function(e) { $.cookie("dict2", this.selectedIndex); });
    if ($.cookie("dict2") != null && $.cookie("dict2") != '') { try { $("#ddlsref2")[0].selectedIndex = $.cookie("dict2"); } catch (err) { } }
    $("#ddlChoices").bind("change",function(e){$.cookie("choices", $("#ddlChoices")[0].selectedIndex); nQuestions=$("#ddlChoices option:selected").val(); $("#quizdiv").fadeOut("slow"); setTimeout('QuizQuestion()', 500);});
    if ($.cookie("choices") != null && $.cookie("choices") != '') { try { $("#ddlChoices")[0].selectedIndex = $.cookie("choices"); } catch (err) { } }
    nQuestions=$("#ddlChoices option:selected").val();
    oQ = eval('(' + $('#jsnDataItems').html() + ')');
    oQ.QuizPoints=function() {return oQ.DataListItems.length * diAnswer;}
    oQ.UserPoints=function() {val=0; jQuery.each(oQ.DataListItems, function() {val=val+this.s;}); return val;}
    oQ.LostPoints=function() {val=0; jQuery.each(oQ.DataListItems, function() {val=val+this.f;}); return val;}

    oQ.showResult = function() {
        /////////////////////////////////////////////////////////
        if (this.UserPoints()==0) return '<div></div>';
        var  EarnedPoints = this.UserPoints()-this.LostPoints(); var PercentPoints = Math.round(EarnedPoints/this.QuizPoints()*100);
        var strOut = "<div "; var PointPercent = ' - ' + EarnedPoints + ' out of ' + this.QuizPoints() + ' - ' + PercentPoints  +'%'
        if (PercentPoints==100) strOut =strOut + ' class="gr5">Excellent' + PointPercent + '</div>';
        else if (PercentPoints>89) strOut =strOut + ' class="gr4">Very Good' + PointPercent + '</div>';
        else if (PercentPoints>79) strOut =strOut + ' class="gr3">Good' + PointPercent + '</div>';
        else if (PercentPoints>59) strOut =strOut + ' class="gr2">Poor' + PointPercent + '</div>';
        else if (PercentPoints>19) strOut =strOut + ' class="gr1">Fail' + PointPercent + '</div>';
        else strOut =strOut + ' class="gr1">Fail - too many errors. Please retake this quiz again.</div>';
        return strOut;
    };
     QuizSet();
     $('#btnQuiz').bind("click", function(e){ Quiz();});
     $('#btnCancel').bind("click", function(e){ QuizCancel();});
     ShowWords();
}

function QuizSet() {  
    jQuery.each(oQ.DataListItems, function() {this.s=0;this.f=0;});
    $('#QuizPoints').html(oQ.QuizPoints());
    $('#UserPoints').html('0');
    $('#LostPoints').html('0');
}
function Quiz() {
    $('#listdiv').hide();  
    QuizSet();
    QuizQuestion();
}
function QuizCancel() {
    $('#quizdiv').hide();
    $('#listdiv').fadeIn("slow"); 
}
function QuizQuestion() {
    var arr = jQuery.grep(oQ.DataListItems, function(di) { return di.s < diAnswer; });
    if (arr.length==0){

        $('#quizdiv').hide();
        $('#listdiv').fadeIn("slow");
        ShowWords();
        return;
    }
    $('#quizdiv').fadeIn("fast");
    var di = arr[Math.floor(Math.random() * arr.length)]
    
    var cDataListItems = oQ.DataListItems.slice();
    cDataListItems.sort(function() {return  0.5 - Math.random()});
    var qArray = cDataListItems.slice(0,nQuestions-1);
    
    
    qArray.splice(Math.round(Math.random() * qArray.length), 0, di); 
    qArray = jQuery.unique(qArray);  
    
    $('#quizdivq').html(((di.k).split('('))[0]);
    $('#quizdivq').data("i",di.i);

    $('#quizdivwords').empty();
    //////////////////////////////////////////////////////
    jQuery.each(qArray, function() {
        if ((this.v == di.v) && (this.i != di.i)) {
        } else {
            $('<div class="quizdivword">' + this.v + '</div>').data("i", this.i).appendTo('#quizdivwords');
        }
    });
    $(".quizdivword").bind("mouseenter mouseleave", function(e){
        $(this).toggleClass("over");
    });
    $(".quizdivword").bind("click", function(e){
       if ($(this).data("i") != $('#quizdivq').data("i")){
            if ($(this).html() == "Wrong answer. Try again.") return;
            jQuery.each(oQ.DataListItems, function() {if (this.i == $('#quizdivq').data("i")) {this.s=this.s-1;this.f=this.f+1}});
            $(this).css({ backgroundColor:cfail, color:"red", cursor:"default" }).html("Wrong answer. Try again.");
       }else{
            jQuery.each(oQ.DataListItems, function() {if (this.i == $('#quizdivq').data("i")) this.s=this.s+1;});
            $(this).css({ backgroundColor:cpass, fontWeight:"bold" }).html("Correct!");
            $("#quizdiv").fadeOut("slow");
            setTimeout('QuizQuestion()', 500)
       }  
       $('#UserPoints').html("" + oQ.UserPoints());
       $('#LostPoints').html("" + oQ.LostPoints());  
    });
   
}
function ta(ch, val){
    val = val.replace(/\?/gi, "").replace(/\!/gi, "").replace(/\,/gi, "").replace(/\./gi, "").replace(/\)/gi, "").replace(/\(/gi, "");
    if (val == '') return
    var vUrl;
	if (ch==1)
	    vUrl = $("#ddlsref option:selected").val().replace(/XXX/gi, val);
	else
	    vUrl = $("#ddlsref2 option:selected").val().replace(/XXX/gi, val);
	var targ1 = 'width=900,height=700,toolbar=no,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes'
	now= new Date();
	var targ2 = now.getSeconds()+'_'+now.getMinutes()
	window.open(vUrl, targ2, targ1)
}
