var _currentFill = "fill:#d4213d";
var p_red = 'fill:#d4213d';
var p_yellow = 'fill:#ffe131';
var p_green = 'fill:#80ce1b';
var p_violet = 'fill:#6d2bed';
var p_blue = 'fill:#339eff';
var p_orange = 'fill:#f28d20';
var p_brown = 'fill:#843617';
var p_pink = 'fill:#ff5cae';
var p_white = 'fill:#e9e8e7';

// svg_MODUL_nrZadania
$("#svg_MW_I_1_3").click(function (event) {
    var task = 'MW_I_1_3'; // MODUL_nrZadania

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('style') != 'fill:#000000') $(event.target).attr('style', _currentFill);

    //var elements = $("svg_" + task).children;
    var elements = $(event.target).parent().children();

    var yellow = false;
    var green = false;
    var violet = false;
    var other = true;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if (fill == p_yellow) yellow = true;
        else if (fill == p_green) green = true;
        else if (fill == p_violet) violet = true;
    }

    if (yellow && green && violet) {
        document.getElementById('vs_' + task).innerHTML = "1";
    }
})


$("#svg_MW_I_5_2").click(function (event) {
    var task = 'MW_I_5_2';

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('style') != 'fill:#000000') $(event.target).attr('style', _currentFill);

    var elements = $(event.target).parent().children();

    var red = false;
    var blue = false;
    var yellow = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if (fill == p_red) red = true;
        else if (fill == p_blue) blue = true;
        else if (fill == p_yellow) yellow = true;
    }

    if (red && blue && yellow) {
        document.getElementById('vs_' + task).innerHTML = "1";
    }
})

$("#svg_MW_I_9_4").click(function (event) {
    var task = 'MW_I_9_4';

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var elements = $(event.target).parent().children();

    var red = false;
    var orange = false;
    var brown = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'dab' && fill == p_red) red = true;
        if ($(elements[i]).attr('id') == 'kasztanowiec' && fill == p_orange) orange = true;
        if ($(elements[i]).attr('id') == 'klon' && fill == p_brown) brown = true;
    }

    if (red && orange && brown) {
        document.getElementById('vs_' + task).innerHTML = "1";
    }

})

// OK
$("#svg_MW_I_10_2").click(function (event) {
    var task = 'MW_I_10_2';

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var elements = $(event.target).parent().children();
    
    var tak = 0;
    var nie = 0;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'tak' && fill != "fill:#ffffff") tak++;

        if ($(elements[i]).attr('id') == 'nie' && fill == "fill:#ffffff") nie++;   /// poprawka #ffffff ?

    }


    if (tak == 5 && nie == 1) {
        document.getElementById('vs_' + task).innerHTML = "1";
    }

})





// OK
$("#svg_OS_I_1_8").click(function (event) { 
    var task = 'OS_I_1_8';

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var elements = $(event.target).parent().children();

    var rect = 0;
    var circle = 0;
    var triangle = 0;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'trojkat' && fill == p_red) triangle++;
        if ($(elements[i]).attr('id') == 'trojkat' && fill != p_red) triangle--;

        if ($(elements[i]).attr('id') == 'kwadrat' && fill == p_blue) rect++;
        if ($(elements[i]).attr('id') == 'kwadrat' && fill != p_blue) rect--;

        if ($(elements[i]).attr('id') == 'kolo' && fill == p_yellow) circle++;
        if ($(elements[i]).attr('id') == 'kolo' && fill != p_yellow) circle--;

    }


    if (rect == 4 && circle == 6 && triangle == 5) {
        document.getElementById('vs_' + task).innerHTML = "1";
    }


})

// OK
$("#svg_OS_I_3_1").click(function (event) { 
    var task = 'OS_I_3_1';

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }


    var polska = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'polska' && fill != '') polska = true;
        if ($(elements[i]).attr('id') == 'polska' && fill == '') polska = false;
    }


    if (polska) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})


// OK
$("#svg_MW_I_3_1").click(function (event) {
    var task = 'MW_I_3_1';

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var polska = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'polska' && fill != '') polska = true;
        if ($(elements[i]).attr('id') == 'polska' && fill == '') polska = false;
    }

    if (polska) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})

// OK
$("#svg_OS_I_3_2").click(function (event) {
    var task = 'OS_I_3_2';

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var elements = $(event.target).parent().children();

    var yellow = 0;
    var red = 0;
    var fill = '';

    

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';
        
        if ($(elements[i]).attr('id') == 'path7' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path9' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path11' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path13' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path15' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path17' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path19' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path21' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path23' && fill == p_yellow) yellow++;
        if ($(elements[i]).attr('id') == 'path25' && fill == p_yellow) yellow++;

        if ($(elements[i]).attr('id') == 'rect36' && fill == p_red) red++;
        if ($(elements[i]).attr('id') == 'path5' && fill == p_red) red++;

    }

    if (yellow >= 10 && red >= 2) {
        document.getElementById('vs_' + task).innerHTML = "1";
    }

})


// OK
var europe = false, asia = false, africa = false, australia = false, north_america = false, south_america = false, antarctica = false;
$("#svg_OS_I_3_7").click(function (event) {
    var task = 'OS_I_3_7';

    var elements = $(event.target).parent();

    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';
        
        if ($(event.target).parent().attr('id') == 'europe' && _currentFill == p_red) europe = true;
        if ($(event.target).parent().attr('id') == 'europe' && _currentFill != p_red) europe = false;

        if ($(event.target).parent().attr('id') == 'asia' && _currentFill == p_yellow) asia = true;
        if ($(event.target).parent().attr('id') == 'asia' && _currentFill != p_yellow) asia = false;

        if ($(event.target).parent().attr('id') == 'africa' && _currentFill == p_green) africa = true;
        if ($(event.target).parent().attr('id') == 'africa' && _currentFill != p_green) africa = false;

        if ($(event.target).parent().attr('id') == 'australia' && _currentFill == p_brown) australia = true;
        if ($(event.target).parent().attr('id') == 'australia' && _currentFill != p_brown) australia = false;

        if ($(event.target).parent().attr('id') == 'north_america' && _currentFill == p_orange) north_america = true;
        if ($(event.target).parent().attr('id') == 'north_america' && _currentFill != p_orange) north_america = false;

        if ($(event.target).parent().attr('id') == 'south_america' && _currentFill == p_pink) south_america = true;
        if ($(event.target).parent().attr('id') == 'south_america' && _currentFill != p_pink) south_america = false;

        if ($(event.target).parent().attr('id') == 'antarctica' && _currentFill == p_white) antarctica = true;
        if ($(event.target).parent().attr('id') == 'antarctica' && _currentFill != p_white) antarctica = false;


        // Coloring Group Hack
        var inner = $(event.target).parent().children();
        for (var j = 0; j < inner.length; j++) {
            $(inner[j]).attr('style', _currentFill);
        }
       
    }

    if (europe && asia && africa && australia && north_america && south_america && antarctica) {
        document.getElementById('vs_' + task).innerHTML = "1";
    }
    else document.getElementById('vs_' + task).innerHTML = "0";


})

// OK
$("#svg_OS_I_4_2").click(function (event) {
    var task = 'OS_I_4_2';

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var wielka_brytania = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'wielka_brytania' && fill != '') wielka_brytania = true;
        if ($(elements[i]).attr('id') == 'wielka_brytania' && fill == '') wielka_brytania = false;
    }

    if (wielka_brytania) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})

// OK
$("#svg_OS_I_5_1").click(function (event) {
    var task = 'OS_I_5_1';

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var hiszpania = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'hiszpania' && fill != '') hiszpania = true;
        if ($(elements[i]).attr('id') == 'hiszpania' && fill == '') hiszpania = false;
    }

    if (hiszpania) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})


// OK
$("#svg_OS_I_6_3").click(function (event) {
    var task = 'OS_I_6_3';

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var grecja = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'grecja' && fill != '') grecja = true;
        if ($(elements[i]).attr('id') == 'grecja' && fill == '') grecja = false;
    }

    if (grecja) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})


// OK
$("#svg_OS_I_8_7").click(function (event) {
    var task = 'OS_I_8_7';

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var rumunia = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'rumunia' && fill != '') rumunia = true;
        if ($(elements[i]).attr('id') == 'rumunia' && fill == '') rumunia = false;
    }

    if (rumunia) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})


// OK
$("#svg_OS_I_9_3").click(function (event) {
    var task = 'OS_I_9_3'; 

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }

    var francja = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'francja' && fill != '') francja = true;
        if ($(elements[i]).attr('id') == 'francja' && fill == '') francja = false;
    }

    if (francja) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})

$("#svg_ZIP_I_9_8").click(function (event) {
    var task = 'ZIP_I_9_8';

    var elements = $(event.target).parent().children();

    for (var i = 0; i < elements.length; i++) $(elements[i]).removeAttr("style");

    if ($(event.target).attr('id') != 'svg_' + task && $(event.target).attr('id') != undefined) {
        $(event.target).attr('style', _currentFill);
    }


    var polska = false;
    var fill = '';

    for (var i = 0; i < elements.length; i++) {

        if ($(elements[i]).attr('style') !== undefined) fill = $(elements[i]).attr('style').replace(" ", "").replace(";", ""); // IE hack
        else fill = '';

        if ($(elements[i]).attr('id') == 'polska' && fill != '') polska = true;
        if ($(elements[i]).attr('id') == 'polska' && fill == '') polska = false;
    }


    if (polska) document.getElementById('vs_' + task).innerHTML = "1";
    else document.getElementById('vs_' + task).innerHTML = "0";

})

var $swatches = $(".swatches");
$swatches.click(function (event) {
    $swatch = $(event.target);
    loc = [parseInt($swatch.attr('x'), 10), parseInt($swatch.attr('y'), 10)]
    if (loc[0] > -1) $("#selection", $swatches).attr('x', loc[0]);
    if (loc[1] > -1) $("#selection", $swatches).attr('y', loc[1]);
    _currentFill = $swatch.attr('style');
})