/**
* 
*/

require(["solutions/qt/js/jquery-1.9.1.js", "dojo/domReady!"], function () { });
require(["solutions/qt/js/jquery-ui-1.10.3.custom.js", "dojo/domReady!"], function () { });
require(["solutions/qt/js/jquery.countdown.js", "dojo/domReady!"], function () { });
require(["solutions/qt/js/jquery.countdown-ru.js", "dojo/domReady!"], function () { });


function prepareDnD() {

    // получаем ответы из модели
    var model = document.getElementById("4_mainModel");
    var instance = model.getInstanceDocument("4_mainInstance");
    var answers = instance.getElementsByTagName("answer");
    for (var i = 0; i < answers.length; ++i) {
        var answer = answers[i];  // Calling myNodeList.item(i) isn't necessary in JavaScript
        $('div.answers').append("<div class='answer'>" + answer.attributes.getNamedItem("text").value + '</div>');
    }

    var destructors = instance.getElementsByTagName("option");

    for (var j = 0; j < destructors.length; ++j) {
        var destructor = destructors[j];
        $("#tblQuestion").append('<tr id="' + destructor.attributes.getNamedItem("id").value + '" class="destructor"><td style="padding: 10px; border: 1px solid #DDDDDD"><div class="values" style="width: 15px;">' + destructor.attributes.getNamedItem("letter").value + '</div></td><td style="width: 20%; padding: 10px; border: 1px solid #DDDDDD">              <div class="values" style="float: left; white-space: normal !important">' + destructor.textContent + '</div></td><td class="acceptAnswers" style="border: 1px solid #DDDDDD; margin:10px;">' + (destructor.attributes.getNamedItem("answer").value != '' ? '<div class="answer">' + destructor.attributes.getNamedItem("answer").value + '</div>' : '') + '</td></tr>');
    }

    var status = instance.getElementsByTagName("testingStatus")[0].textContent;
    if (status == '0') {

        $('div.answer').draggable({
            helper: 'clone',
            opacity: 0.5
        });

        $('td.acceptAnswers').droppable({
            tolerance: 'pointer',
            accept: 'div.answer',
            drop: function (event, ui) {
                $('div.answers').append($(this).children());
                $(this).append(ui.draggable);
            }
        });

        $('div.answers').droppable({
            tolerance: 'pointer',
            accept: 'div.answer',
            drop: function (event, ui) {
                $(this).append(ui.draggable);
            }
        });
    }
}


function prepareSort() {

    // получаем ответы из модели
    var answer2destructor = {};
    var model = document.getElementById("4_mainModel");
    var instance = model.getInstanceDocument("4_mainInstance");
    var destructors = instance.getElementsByTagName("option");
    for (var i = 0; i < destructors.length; ++i) {
        var destructor = destructors[i];  // Calling myNodeList.item(i) isn't necessary in 
        if (destructor.attributes.getNamedItem("answer").value == '') {
            $('div.answers').append('<div id="' + destructor.attributes.getNamedItem("id").value + '" class="answer">' + destructor.textContent + '</div>');
        } else {
            answer2destructor[destructor.attributes.getNamedItem("answer").value] = destructor;
        }
    }

    var answers = instance.getElementsByTagName("answer");

    for (var j = 0; j < answers.length; ++j) {
        var answer = answers[j];
        // найти дестрактор с заданным ответом
        destructor = answer2destructor[answer.attributes.getNamedItem("text").value];
        $("#tblQuestion").append('<tr class="destructor"><td style="width: 20%; padding: 10px; border: 1px solid #DDDDDD"><div class="values" style="float: left; white-space: normal !important">' + answer.attributes.getNamedItem("text").value + '</div></td><td class="acceptAnswers" style="border: 1px solid #DDDDDD; margin:10px;">' + (destructor ? '<div id="' + destructor.attributes.getNamedItem("id").value + '" class="answer">' + destructor.textContent + '</div>' : '') + '</td></tr>');
    }

    var status = instance.getElementsByTagName("testingStatus")[0].textContent;
    if (status == '0') {

        $('div.answer').draggable({
            helper: 'clone',
            opacity: 0.5
        });

        $('td.acceptAnswers').droppable({
            tolerance: 'pointer',
            accept: 'div.answer',
            drop: function (event, ui) {
                $('div.answers').append($(this).children());
                $(this).append(ui.draggable);
            }
        });

        $('div.answers').droppable({
            tolerance: 'pointer',
            accept: 'div.answer',
            drop: function (event, ui) {
                $(this).append(ui.draggable);
            }
        });
    }
}



// сохранение данных html-формы в xml-объект модели XForm
function saveAnswerToModel() {

    var model = document.getElementById("4_mainModel");
    var instance = model.getInstanceDocument("4_mainInstance");
    var nodeAnswers = instance.getElementsByTagName("result")[0];
    $("tr.destructor").each(function (index, tr) {
        var id = tr.id;
        if ($(tr).find('div.answer')[0] != undefined) {
            var answer = $(tr).find('div.answer')[0].innerText;
            var nodeAnswer = nodeAnswers.appendChild(nodeAnswers.ownerDocument.createElement('option2'))
            nodeAnswer.setAttribute("id", id);
            nodeAnswer.setAttribute("answer", answer);
        }
    });
    model.rebuild();
    model.recalculate();
    model.refresh();
}


// сохранение данных html-формы в xml-объект модели XForm
function saveSortAnswerToModel() {

    var model = document.getElementById("4_mainModel");
    var instance = model.getInstanceDocument("4_mainInstance");
    var nodeAnswers = instance.getElementsByTagName("result")[0];
    $("tr.destructor").each(function (index, tr) {
        if ($(tr).find('div.answer')[0] != undefined) {
            var id = $(tr).find('div.answer')[0].id;
            var answer = $(tr).find('div.values')[0].innerText;
            var nodeAnswer = nodeAnswers.appendChild(nodeAnswers.ownerDocument.createElement('option2'))
            nodeAnswer.setAttribute("id", id);
            nodeAnswer.setAttribute("answer", answer);
        }
    });
    model.rebuild();
    model.recalculate();
    model.refresh();
}


function injectCounter(toDate) {
    $('#timerDiv').countdown({ until: toDate, format: 'MS' });
}

function clearWordStyles() {
    $('.MsoNormal').removeAttr('style');
    $('.MsoNormal > *').css('font-size', '12pt');
}

function showcaseShowMessage(mainContext, addContext, filterContext) {
    window.alert('Выделена строка: ' + addContext);
}

function showcaseShowAddContext(mainContext, addContext, filterContext) {
    window.alert('add_context = ' + addContext);
}

function showcaseShowFilterContext(mainContext, addContext, filterContext) {
    window.alert('filter_context = ' + filterContext);
}

function showcaseShowMainContext(mainContext, addContext, filterContext) {
    window.alert('main_context = ' + mainContext);
}

function showcaseShowAllContexts(mainContext, addContext, filterContext) {
    window.alert('main_context = ' + mainContext + '\n' + 'add_context = ' +
			addContext + '\n' + 'filter_context = ' + filterContext);
}