﻿
if (typeof oe == 'undefined') oe = {};


oe.gap_onclick = function(gapElem) {
    var gap = oe[gapElem.id];
    var ClozeUid = gap.Cloze;
    var ActiveGapUid = oe[ClozeUid].ActiveGapUid;
    var Cloze = oe[ClozeUid];
    if (Cloze.Checked) {
        // do nothing
    } else {
        if (ActiveGapUid != null) {
            oe.setStyle(ActiveGapUid, 'backgroundColor', oe.get_color(Cloze.ColorSectionName, 'default', 'GapOnlineBG'));
            oe.setStyle(oe[ActiveGapUid].ChoiceList, 'display', 'none');
        }
        ActiveGapUid = gapElem.id;
        Cloze.ActiveGapUid = ActiveGapUid;
        oe.setStyle(ActiveGapUid, 'backgroundColor', oe.get_color(Cloze.ColorSectionName, 'default', 'GapSelectedBG'));

        oe.update_choices(ActiveGapUid, gap);

        var instruction = gap.Instruction ? gap.Instruction : Cloze.Instruction;
        var hint = gap.Hint ? gap.Hint : (Cloze.Hint ? Cloze.Hint : "&nbsp;");
        document.getElementById(ClozeUid + "_instruction").innerHTML = instruction;
        document.getElementById(ClozeUid + "_hint").innerHTML = hint;
    }
};
oe.gap_onmouseover = function(gapElem) {
	// don't display solution
	/*
    var gap = oe[gapElem.id];
    var Cloze = oe[gap.Cloze];
    if (Cloze.Checked) {
        if (!gapElem.WasRight) {
            overlib("Eingabe: " + gapElem.value + "<br />" + "Richtige Lösung: " + gap.Solutions[0]);
        }
    }*/
}
oe.gap_onmouseout = function(gapElem) {
    var gap = oe[gapElem.id];
    var Cloze = oe[gap.Cloze];
    if (Cloze.Checked) {
        nd();
    }
}

oe.update_choices = function(ActiveGapUid, ActiveGap) {
    if (!oe.choice_one_per_gap_used(ActiveGapUid)) {
        oe.setStyle(oe[ActiveGapUid].ChoiceList, 'display', 'block');
    }

    var ChoiceList = oe[ActiveGap.ChoiceList];
    if (ChoiceList && ChoiceList.ChoiceExpend != 'none') {
        for (var n in ChoiceList.Choices) {
            document.getElementById(ActiveGap.ChoiceList + "_ch_" + n).style.display = oe.choice_is_used(ChoiceList, n) ? "none" : "inline";
        }
    }
};
oe.reset_onclick = function(resetElem, ClozeUid) {
    var Cloze = oe[ClozeUid];
    for (var n in Cloze.ChoiceLists) {
        var ChoiceList = oe[Cloze.ChoiceLists[n]];
        if (ChoiceList.DictChoiceGap) {
            for (var i in ChoiceList.DictChoiceGap) {
                if (ChoiceList.DictChoiceGap[i] == Cloze.ActiveGapUid) {
                    ChoiceList.DictChoiceGap[i] = null;
                }
            }
        }
    }
    oe.set_gap_default([Cloze.ActiveGapUid]);
    oe.update_choices(Cloze.ActiveGapUid, oe[Cloze.ActiveGapUid]);
};
oe.setStyle = function(id, attr, value) {
    if (typeof id == 'undefined') return;
    var elem = document.getElementById(id);
    if (elem == null) return;
    elem.style[attr] = value;
}
oe.choice_onclick = function(choiceElem, ChoiceListUid, i) {
    var ChoiceList = oe[ChoiceListUid];
    var ActiveGapUid = oe[ChoiceList.Cloze].ActiveGapUid;
    var ActiveGapElem = document.getElementById(ActiveGapUid);
    ActiveGapElem.value += oe.addSpace(ActiveGapElem.value) + ChoiceList.Choices[i];
    if (ChoiceList.ChoiceExpend == 'one_per_gap') {
        document.getElementById(ChoiceListUid).style.display = 'none';
    } else if (ChoiceList.ChoiceExpend == 'normal') {
        choiceElem.style.display = 'none';
    }

    if (ChoiceList.ChoiceExpend != 'none') {
        oe.use_choice(ChoiceList, ChoiceListUid, i, ActiveGapUid);
    }
};
oe.use_choice = function(ChoiceList, ChoiceListUid, n, GapUid) {
    if (typeof ChoiceList.DictChoiceGap == 'undefined') {
        ChoiceList.DictChoiceGap = {};
    }
    ChoiceList.DictChoiceGap[n] = GapUid;
};
oe.choice_is_used = function(ChoiceList, n) {
    if (typeof ChoiceList.DictChoiceGap == 'undefined') return false;
    if (ChoiceList.DictChoiceGap[n]) return true;
    return false;
}
oe.choice_one_per_gap_used = function(GapUid) {
    if (!oe[GapUid].ChoiceList) return false;
    var ChoiceList = oe[oe[GapUid].ChoiceList];
    if (!ChoiceList) return false;
    if (!ChoiceList.DictChoiceGap) return false;
    if (ChoiceList.ChoiceExpend != 'one_per_gap') return false;
    for (var n in ChoiceList.DictChoiceGap) {
        if (ChoiceList.DictChoiceGap[n] == GapUid) return true;
    }
    return false;
};
oe.reset_choice_use = function() {
    //
};

oe.set_gap_default = function(GapUIDs) {
    for (var n in GapUIDs) {
        var GapUID = GapUIDs[n];
        document.getElementById(GapUID).value = typeof oe[GapUID].Default == 'undefined' ? '' : oe[GapUID].Default;
    }
}
oe.init_gaps = function(GapUIDs) {
    oe.set_gap_default(GapUIDs);
    for (var n in GapUIDs) {
        if (!oe[GapUIDs[n]].Editable) {
            document.getElementById(GapUIDs[n]).setAttribute("readonly", "readonly");
        }
    }
}

oe.addSpace = function(s) {
    if (s.length == 0) return "";
    return s[s.length - 1] == " " ? "" : " ";
};
oe.check_onclick = function(checkElem, ClozeUid) {
    var Cloze = oe[ClozeUid];
    var result = true;
    var rigthCount = 0;
    for (var i in Cloze.Gaps) {
        if (oe.check_gap(Cloze.Gaps[i])) {
            rigthCount++;
        } else {
            result = false;
        }
    }
    var wrongCount = Cloze.Gaps.length - rigthCount;
    alert(result ? "Prima gemacht!" : "Leider nicht richtig!");
    Cloze.Checked = true;

    var ActiveGapUid = oe[ClozeUid].ActiveGapUid;
    var Cloze = oe[ClozeUid];

    if (ActiveGapUid != null) {
        oe.setStyle(oe[ActiveGapUid].ChoiceList, 'display', 'none');
    }

    oe.setStyle(ClozeUid + "_cloze_footer", "display", "none");
    //oe.setStyle(ClozeUid + "_check", "display", "none");
    
    oe.setStyle(ClozeUid + "_result", "display", "block");
    oe.setStyle(ClozeUid + "_result", "backgroundColor", oe.get_color(Cloze.ColorSectionName, "default", result ? "GapRightBG" : "GapWrongBG"));
    var html = "Richtig: " + rigthCount + " Falsch: " + wrongCount;
    
    var resultElem = document.getElementById(ClozeUid + "_result");

    if (result) {
    	resultElem.className += ' result_right';
    } else {
    	resultElem.className += ' result_wrong';
    	//html += "<br />Bewegen Sie die Maus auf die rot markierten Lücken, um die Lösungen zu sehen.";
    }
	html += '<div style="width: 600px; margin: 10px 0 10px 0px; background-color:#FFF;">';
	html += '<a style="width: 600px; margin: 0px;" class="dl_c3_link c3_link_large" href="'+download_url+'">Lernprogramm herunterladen</a>';
    html += '</div>';
	
    resultElem.innerHTML = html;
    
    if (typeof oe.onCheck == 'function') {
        oe.onCheck(ClozeUid);
    }
};
oe.check_gap = function(gapUid) {
    var gap = oe[gapUid];
    var result = false;
    var gapElem = document.getElementById(gapUid);
    for (var i in gap.Solutions) {
        if (gapElem.value == gap.Solutions[i]) {
            result = true;
            break;
        }
    }
    gapElem.WasRight = result;
    gapElem.style.backgroundColor = oe.get_color('cloze', 'default', result ? 'GapRightBG' : 'GapWrongBG');
    gapElem.setAttribute("readonly", "readonly");
    return result;
};
oe.get_color = function(section, scheme, color) {
    section = section.toLowerCase();
    scheme = scheme.toLowerCase();
    color = color.toLowerCase();
    if (typeof oe.ColorSettings[section] == 'undefined') section = "default";
    if (typeof oe.ColorSettings[section][scheme] == 'undefined') scheme = "default";
    return '#' + oe.ColorSettings[section][scheme][color];
};

