﻿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">' + val.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++)
    	varout = varout + "<a href='javascript:ta(\"" + vars[y] + "\")'>" + vars[y] + "</a> ";
  return varout + ' - ' + val.v + '</div>';
}
function LoadSession() {
    $("#ddlsref").bind("change",function(e){$.cookie("dict", this.selectedIndex);});
    if ($.cookie("dict") != null && $.cookie("dict") != '') $("#ddlsref")[0].selectedIndex = $.cookie("dict");
    $("#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") != '') $("#ddlChoices")[0].selectedIndex = $.cookie("choices");
    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);
    $('#quizdivq').data("i",di.i);
    
    $('#quizdivwords').empty();
    jQuery.each(qArray, function() {$('<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(val){
    val = val.replace(/\?/gi, "").replace(/\!/gi, "").replace(/\,/gi, "").replace(/\./gi, "").replace(/\)/gi, "").replace(/\(/gi, "");
	if (val == '') return 
	var red= $("#ddlsref 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(red,targ2,targ1)
}
