var alterfrom;
var alterto;
var destilliertfrom;
var destilliertto;
var abgefuelltfrom;
var abgefuelltto;
var alkohol;
var optionvalues = new Array();
var optionnames = new Array();
var categoryId;

$(function () {
    $("select#msalterfrom").change(function () {
        changeSearch();
    });
    $("select#msalterto").change(function () {
        changeSearch();
    });
    $("select#msdestilliertfrom").change(function () {
        changeSearch();
    });
    $("select#msdestilliertto").change(function () {
        changeSearch();
    });
    $("select#msabgefuelltfrom").change(function () {
        changeSearch();
    });
    $("select#msabgefuelltto").change(function () {
        changeSearch();
    });
    $("select#msalkohol").change(function () {
        changeSearch();
    });
    for (var i = 0; i < MSOptions.length; i++) {
        $("select#msoptions" + MSOptions[i]).change(function () {
            changeSearch();
        });
    }
    $("select#destillerien").change(function () {
        document.location = $(this).val()
    });
    $('select#pfrom, select#pto').change(function() {
        changeSearch();
    });
});

function countResults() {
    var params = new Object();
    params.alterfrom = alterfrom;
    params.alterto = alterto;
    params.destilliertfrom = destilliertfrom;
    params.destilliertto = destilliertto;
    params.abgefuelltfrom = abgefuelltfrom;
    params.abgefuelltto = abgefuelltto;
    params.alkohol = alkohol;
    params.option5 = optionvalues[0];
    params.option6 = optionvalues[1];
    params.option7 = optionvalues[2];
    params.option9 = optionvalues[3];
    params.option10 = optionvalues[4];
    params.option11 = optionvalues[5];
    params.option12 = optionvalues[6];
    params.option13 = optionvalues[7];
    params.option14 = optionvalues[8];
    params.option15 = optionvalues[9];
    params.pfrom = $("#pfrom").val();
    params.pto = $("#pto").val();
    params.keywords = $("#maltsquery").val() != 'Suchbegriff' ? $("#maltsquery").val() : '';
    params.categories_id = categoryId;
    $.getJSON("/msCount.php", params, function (result) {
        $('#maltssubmit').text('Treffer anzeigen: ' + result[0].count);
    })
}

function changeSearch() {
    alterfrom = $("select#msalterfrom").val();
    alterto = $("select#msalterto").val();
    destilliertfrom = $("select#msdestilliertfrom").val();
    destilliertto = $("select#msdestilliertto").val();
    abgefuelltfrom = $("select#msabgefuelltfrom").val();
    abgefuelltto = $("select#msabgefuelltto").val();
    alkohol = $("select#msalkohol").val();
    categoryId = $("#hiddenCategoryId").val();
    for (var i = 0; i < MSOptions.length; i++) {
        optionvalues[i] = $("select#msoptions" + MSOptions[i]).val();
    }
    changeSelect("alterfrom", alterfrom, 'von Alter', "alterto", alterto);
    changeSelect("destilliertfrom", destilliertfrom, 'Destilliert von', "destilliertto", destilliertto);
    changeSelect("abgefuelltfrom", abgefuelltfrom, 'Abgefüllt', 'abgefuelltto', abgefuelltto);
    changeSelect("alkohol", alkohol, 'Alkoholgehalt', '', '');
    for (var i = 0; i < MSOptions.length; i++) {
        changeSelect("options" + MSOptions[i], optionvalues[i], optionnames[i], '', '');
    }
    countResults();
}

function changeSelect(name, wert, text, twin, twinVal) {

    var params = new Object();
    params.get = name;
    params.alterfrom = alterfrom;
    params.alterto = alterto;
    params.destilliertfrom = destilliertfrom;
    params.destilliertto = destilliertto;
    params.abgefuelltfrom = abgefuelltfrom;
    params.abgefuelltto = abgefuelltto;
    params.alkohol = alkohol;
    params.options5 = optionvalues[0];
    params.options6 = optionvalues[1];
    params.options7 = optionvalues[2];
    params.options9 = optionvalues[3];
    params.options10 = optionvalues[4];
    params.options11 = optionvalues[5];
    params.options12 = optionvalues[6];
    params.options13 = optionvalues[7];
    params.options14 = optionvalues[8];
    params.options15 = optionvalues[9];
    params.optionsIds = MSOptions;
    params.categories_id = categoryId;
    params.pfrom = $("#pfrom").val();
    params.pto = $("#pto").val();
    params.keywords = $("#maltsquery").val() != 'Suchbegriff' ? $("#maltsquery").val() : '';

    $("#ms" + name).empty();
    $("#ms" + name).html('<option value="">wird aktualisiert...</option>');
    $("#ms" + name).trigger("liszt:updated");
    if (twin != null && twin != '') {
        $("#ms" + twin).empty();
        $("#ms" + twin).html('<option value="">wird aktualisiert...</option>');
        $("#ms" + twin).trigger("liszt:updated");
    }

    $.getJSON("/msget.php", params, function (j) {
        var emptyOptions = addEmptyOption(name, wert);
        var options;
        for (var i = 0; i < j.length; i++) options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        $("#ms" + name).empty();
        $("#ms" + name).html(emptyOptions + options);
        $("#ms" + name + " option[value='" + wert + "']").attr('selected', 'selected');
        if ($("#ms" + name + " option[value='" + wert + "']").length == 0 && j.length > 0){
            $("#ms" + name + " option[value!='']").first().attr('selected', 'selected');
        }
        $("#ms" + name).trigger("liszt:updated");
        if (twin != null && twin != '') {
            emptyOptions = addEmptyOption(twin, twinVal);
            $("#ms" + twin).empty();
            $("#ms" + twin).html(emptyOptions + options);
            $("#ms" + twin + " option[value='" + twinVal + "']").attr('selected', 'selected');
            $("#ms" + twin).trigger("liszt:updated");
        }
        checkRestButton();
    })
}

function addEmptyOption(name, wert) {
    var options = '<option value=""></option>';
    if (isTouchDevice()) {
        if (wert != null && wert != '') {
            options = '<option value="">Beliebig</option>';
        } else {
            options = '<option value="">' + $("#ms" + name).attr('data-placeholder') + '</option>';
        }
    } else if (wert != null && wert != '') {
        options += '<option value="">Beliebig</option>';
    }
    return options;
}

function resetSearch() {
    alterfrom = "";
    alterto = "";
    destilliertfrom = "";
    destilliertto = "";
    abgefuelltfrom = "";
    abgefuelltto = "";
    alkohol = "";
    categoryId = $("#hiddenCategoryId").val();
    $("select#msalterfrom option[value='']").attr('selected', 'selected');
    $("select#msalterto option[value='']").attr('selected', 'selected');
    $("select#msdestilliertfrom option[value='']").attr('selected', 'selected');
    $("select#msdestilliertto option[value='']").attr('selected', 'selected');
    $("select#msabgefuelltfrom option[value='']").attr('selected', 'selected');
    $("select#msabgefuelltto option[value='']").attr('selected', 'selected');
    $("select#msalkohol option[value='']").attr('selected', 'selected');
    $("select#pfrom").children().removeAttr('selected');
    $("select#pto").children().removeAttr('selected');
    $("select#pfrom option[value='']").attr('selected', 'selected').trigger("liszt:updated");
    $("select#pto option[value='']").attr('selected', 'selected').trigger("liszt:updated");
    for (var i = 0; i < MSOptions.length; i++) {
        optionvalues[i] = "";
        $("select#msoptions" + MSOptions[i] + " option[value='']").attr('selected', 'selected');
    }
    $("#maltsquery").attr('value', 'Suchbegriff');

    changeSelect("alterfrom", "", "von Alter", "alterto", "");
    changeSelect("destilliertfrom", "", "Destilliert", 'destilliertto', "");
    changeSelect("abgefuelltfrom", "", "Abgefüllt", 'abgefuelltto', "");
    changeSelect("alkohol", "", "Alkoholgehalt", '', "");
    for (var i = 0; i < MSOptions.length; i++) {
        changeSelect("options" + MSOptions[i], "", optionnames[i], '', "");
    }

    checkRestButton();
    $('#previewSearchResults').empty();

}

function resetCategory() {
    $("#hiddenCategoryId").val('');
    changeSearch();
    return false;
}

function checkRestButton() {
    var optionCheck = 0;
    for (var i = 0; i < MSOptions.length; i++) {
        if ($("select#msoptions" + MSOptions[i]).val() != "") optionCheck = 1;
    }

    var visible = false;
    $('select.attributeSelect').each(function() {
        if ($(this).val() != '') {
            visible = true;
            return false;
        }
    });
    visible = visible || ($('#maltsquery').val() != '' && $('#maltsquery').val() != 'Suchbegriff');

    $('#searchreset').toggle(visible);

//    if ($("select#msalterfrom").val() != "" || $("select#msalterto").val() != ""
//            || $("select#msdestilliertfrom").val() != "" || $("select#msdestilliertto").val() != ""
//            || $("select#msabgefuelltfrom").val() != "" || $("select#msabgefuelltto").val() != ""
//            || $("select#msalkohol").val() != "" || optionCheck) {
//
//        $('#searchreset').show();
//    }
//    else {
//        $('#searchreset').hide();
//    }
}

function checkSuchbegriff() {
    if ($("#maltsquery").val() == "Suchbegriff") $("#maltsquery").val('');
//    if ($("#maltsvon").val() == "Preis von") $("#maltsvon").val('');
//    if ($("#maltsbis").val() == "Preis bis") $("#maltsbis").val('');
}
