﻿var color = 0;
var surface = 0;
var type = 0;
var price_from = "";
var price_to = "";

var rbt_color_light_id;
var rbt_color_gold_id;
var rbt_color_red_brown_id;
var rbt_color_dark_id;
var btn_color_reset_id;

var tbx_price_from_id;
var tbx_price_to_id;
var btn_price_reset_id;

var rbt_surface_lacquer_id;
var rbt_surface_oil_id;
var btn_surface_reset_id;

var rbt_type_one_id;
var rbt_type_two_id;
var rbt_type_three_id;
var rbt_type_four_id;
var btn_type_reset_id;

var btn_reset_all_id;

//Устанавливает стиль active выбранному элементу фильтра
function changeGroupCss(key, value) {
    if (key == "color") {
        color = value;
        
        var rbt_color_light = document.getElementById(rbt_color_light_id);
        var rbt_color_gold = document.getElementById(rbt_color_gold_id);
        var rbt_color_red_brown = document.getElementById(rbt_color_red_brown_id);
        var rbt_color_dark = document.getElementById(rbt_color_dark_id);
        
        rbt_color_light.className = "";
        rbt_color_gold.className = "";
        rbt_color_red_brown.className = "";
        rbt_color_dark.className = "";

        switch (value) {
            case 1:
                rbt_color_light.className = "active";
                break;
            case 2:
                rbt_color_gold.className = "active";
                break;
            case 3:
                rbt_color_red_brown.className = "active";
                break;
            case 4:
                rbt_color_dark.className = "active";
                break;
        }

        btn_color_reset = document.getElementById(btn_color_reset_id);
        btn_color_reset.style.visibility = "visible";
        
    }
    if (key == "surface") {
        surface = value;

        var rbt_surface_lacquer = document.getElementById(rbt_surface_lacquer_id);
        var rbt_surface_oil = document.getElementById(rbt_surface_oil_id);

        rbt_surface_lacquer.className = "";
        rbt_surface_oil.className = "";

        switch (value) {
            case 2:
                rbt_surface_lacquer.className = "active";
                break;
            case 3:
                rbt_surface_oil.className = "active";
                break;
        }

        btn_surface_reset = document.getElementById(btn_surface_reset_id);
        btn_surface_reset.style.visibility = "visible";
        
    }
    if (key == "type") {
        type = value;

        var rbt_type_one = document.getElementById(rbt_type_one_id);
        var rbt_type_two = document.getElementById(rbt_type_two_id);
        var rbt_type_three = document.getElementById(rbt_type_three_id);
        var rbt_type_four = document.getElementById(rbt_type_four_id);

        rbt_type_one.className = "";
        rbt_type_two.className = "";
        rbt_type_three.className = "";
        rbt_type_four.className = "";

        switch (value) {
            case 2:
                rbt_type_one.className = "active";
                break;
            case 3:
                rbt_type_two.className = "active";
                break;
            case 4:
                rbt_type_three.className = "active";
                break;
            case 5:
                rbt_type_four.className = "active";
                break;
        }

        btn_type_reset = document.getElementById(btn_type_reset_id);
        btn_type_reset.style.visibility = "visible";
    }

    var btn_reset_all = document.getElementById(btn_reset_all_id);
    btn_reset_all.style.visibility = "visible";
}

//Сбрасывает показатели фильтра
function resetGroup(key) {
    if (key == "color") {
        color = 0;

        var rbt_color_light = document.getElementById(rbt_color_light_id);
        var rbt_color_gold = document.getElementById(rbt_color_gold_id);
        var rbt_color_red_brown = document.getElementById(rbt_color_red_brown_id);
        var rbt_color_dark = document.getElementById(rbt_color_dark_id);

        rbt_color_light.className = "";
        rbt_color_gold.className = "";
        rbt_color_red_brown.className = "";
        rbt_color_dark.className = "";

        btn_color_reset = document.getElementById(btn_color_reset_id);
        btn_color_reset.style.visibility = "hidden";
    }
    if (key == "surface") {
        surface = 0;

        var rbt_surface_lacquer = document.getElementById(rbt_surface_lacquer_id);
        var rbt_surface_oil = document.getElementById(rbt_surface_oil_id);

        rbt_surface_lacquer.className = "";
        rbt_surface_oil.className = "";

        btn_surface_reset = document.getElementById(btn_surface_reset_id);
        btn_surface_reset.style.visibility = "hidden";
    }
    if (key == "type") {
        type = 0;

        var rbt_type_one = document.getElementById(rbt_type_one_id);
        var rbt_type_two = document.getElementById(rbt_type_two_id);
        var rbt_type_three = document.getElementById(rbt_type_three_id);
        var rbt_type_four = document.getElementById(rbt_type_four_id);

        rbt_type_one.className = "";
        rbt_type_two.className = "";
        rbt_type_three.className = "";
        rbt_type_four.className = "";

        btn_type_reset = document.getElementById(btn_type_reset_id);
        btn_type_reset.style.visibility = "hidden";
    }

    if (key == "price") {
        price_from = "";
        price_to = "";

        var tbx_price_from = document.getElementById(tbx_price_from_id);
        var tbx_price_to = document.getElementById(tbx_price_to_id);

        tbx_price_from.value = "";
        tbx_price_to.value = "";

        var btn_price_reset = document.getElementById(btn_price_reset_id);
        btn_price_reset.style.visibility = "hidden";
    }

    checkResetallNecessity();
}

//Проверяет необходимость наличия кнопки ResetAll (проверяет, если есть хоть один выбранный фильтр)
function checkResetallNecessity() {
    var btn_color_reset = document.getElementById(btn_color_reset_id);
    var btn_price_reset = document.getElementById(btn_price_reset_id);
    var btn_surface_reset = document.getElementById(btn_surface_reset_id);
    var btn_type_reset = document.getElementById(btn_type_reset_id);

    if (btn_color_reset.style.visibility == "hidden" && btn_price_reset.style.visibility == "hidden" &&
        btn_surface_reset.style.visibility == "hidden" && btn_type_reset.style.visibility == "hidden") {
        var btn_reset_all = document.getElementById(btn_reset_all_id);
        btn_reset_all.style.visibility = "hidden";
    }
}

//Запоминает изменения текстбоксов, отвечающих за цену
function priceChanged(key) {
    var ok = false;
    if (key == "price_from") {
        var tbx_price_from = document.getElementById(tbx_price_from_id);
        var value = tbx_price_from.value;
        if (value.replace('.', ',') * 1 != value || value <= 0) {
            if (value == "") {
                price_from = "";
            }
            else {
                alert("Введите положительное целое число");
                tbx_price_from.value = "";
            }
        }
        else {
            price_from = value;
            ok = true;
        }
    }
    if (key == "price_to") {
        var tbx_price_to = document.getElementById(tbx_price_to_id);
        var value = tbx_price_to.value;
        if (value.replace('.', ',') * 1 != value || value <= 0) {
            if (value == "") {
                price_to = "";
            }
            else {
                alert("Введите положительное целое число");
                tbx_price_to.value = "";
            }
        }
        else {
            price_to = value;
            ok = true;
        }
    }

    if (ok) {
        var btn_price_reset = document.getElementById(btn_price_reset_id);
        btn_price_reset.style.visibility = "visible";

        var btn_reset_all = document.getElementById(btn_reset_all_id);
        btn_reset_all.style.visibility = "visible";
    }

    if (document.getElementById(tbx_price_from_id).value == "" && document.getElementById(tbx_price_to_id).value == "") {
        var btn_price_reset = document.getElementById(btn_price_reset_id);
        btn_price_reset.style.visibility = "hidden";
    }

    checkResetallNecessity();
}

//Сбрасывает все фильтры
function resetAll() {
    resetGroup("color");
    resetGroup("price");
    resetGroup("surface");
    resetGroup("type");
    var btn_reset_all = document.getElementById(btn_reset_all_id);
    btn_reset_all.style.visibility = "hidden";

    var query_string = window.location.search.substring(1);
    var qs_components = query_string.split("=");
    var cat_id_dirty = qs_components[1];
    var cat_id_dirty_components = cat_id_dirty.split("&");
    var cat_id = cat_id_dirty_components[0];

    var str = location.toString();
    var init_location = str.split("=");
    var redirect_string = init_location[0] + "=" + cat_id;

    window.location = redirect_string;
}

//Переходим на страницу с учетом выбранных фильтров
function makeChose() {


    document.getElementById(tbx_price_from_id).blur();
    document.getElementById(tbx_price_to_id).blur();

    var query_string = window.location.search.substring(1);
    var qs_components = query_string.split("=");
    var cat_id_dirty = qs_components[1];
    var cat_id_dirty_components = cat_id_dirty.split("&");
    var cat_id = cat_id_dirty_components[0];

    var str = location.toString();
    var init_location = str.split("=");
    var redirect_string = init_location[0] + "=" + cat_id;

    if (color != 0) {
        redirect_string = redirect_string + "&Color=" + color.toString();
    }
    if (price_from != "") {
        redirect_string = redirect_string + "&From=" + price_from.toString();
    }
    if (price_to != "") {
        redirect_string = redirect_string + "&To=" + price_to.toString();
    }
    if (surface != 0) {
        redirect_string = redirect_string + "&Surface=" + surface.toString();
    }
    if (type != 0) {
        redirect_string = redirect_string + "&Type=" + type.toString();
    }

    window.location = redirect_string;
}

//Инициализация переменных
function init(
    color_light_id,
    color_gold_id,
    color_red_brown_id,
    color_dark_id,
    color_reset_id,
    price_from_id,
    price_to_id,
    price_reset_id,
    surface_lacquer_id,
    surface_oil_id,
    surface_reset_id,
    type_one_id,
    type_two_id,
    type_three_id,
    type_four_id,
    type_reset_id,
    reset_all_id
    ) {
    rbt_color_light_id = color_light_id;
    rbt_color_gold_id = color_gold_id;
    rbt_color_red_brown_id = color_red_brown_id;
    rbt_color_dark_id = color_dark_id;
    btn_color_reset_id = color_reset_id;

    tbx_price_from_id = price_from_id;
    tbx_price_to_id = price_to_id;
    btn_price_reset_id = price_reset_id;

    rbt_surface_lacquer_id = surface_lacquer_id;
    rbt_surface_oil_id = surface_oil_id;
    btn_surface_reset_id = surface_reset_id;

    rbt_type_one_id = type_one_id;
    rbt_type_two_id = type_two_id;
    rbt_type_three_id = type_three_id;
    rbt_type_four_id = type_four_id;
    btn_type_reset_id = type_reset_id;

    btn_reset_all_id = reset_all_id;

    initFilters();
}

function initFilters() {
    //URL-parsing - сохраняем позиции фильтров с прошлого раза
    var str = location.toString();
    var components = str.split('&');
    for (var i = 0; i < components.length; i++) {
        var component = components[i];
        var parts = component.split('=');
        if (parts[0] == "Color") {
            color = parts[1];
        }
        if (parts[0] == "From") {
            price_from = parts[1].toString();
        }
        if (parts[0] == "To") {
            price_to = parts[1].toString();
        }
        if (parts[0] == "Surface") {
            surface = parts[1];
        }
        if (parts[0] == "Type") {
            type = parts[1];
        }
    }
}