﻿(function() {

    function CikkSzuroPanel() {
        this.qs = new QueryString();
    }

    CikkSzuroPanel.prototype.SetFilterDDL = function(key, control, url) {
        if (control) {
            var options = control.options;
            var length = options.length;

            var selected = [];
            var volt_valid = false;
            for (var i = 0; i < length; i++) {
                if (options[i].selected) {
                    selected.push(options[i].value.replace('***', '___'));
                    if (!volt_valid && !options[i].disabled) {
                        volt_valid = true;
                    }
                }
            }

            if (!volt_valid) {
                this.SetAndRefresh(key, undefined, url);
            } else {
                this.SetAndRefresh(key, (selected.length === 0 ? undefined : selected), url);
            }
        }

        return false;
    };

    CikkSzuroPanel.prototype.SetFilterCHB = function(key, control, url) {
        if (control) {
            this.SetAndRefresh(key, (control.checked ? '1' : '0'), url);
        }
        return false;
    };

    CikkSzuroPanel.prototype.SetFilterCHB2 = function(key, control, url) {
        if (control) {
            var selected = control.checked;
            var value = control.value;
            var current_value = this.qs.get(key);

            var t = [];

            if (current_value) {
                if (selected) {
                    current_value.push(value);
                    t = current_value;
                } else {
                    for (var i in current_value) {
                        if (i == value) continue;
                        t.push(i);
                    }
                    if (t.length === 0) {
                        t = undefined;
                    }
                }
            } else {
                t = (selected ? [value] : undefined);
            }

            this.SetAndRefresh(key, t, url);
        }
        return false;
    };

    CikkSzuroPanel.prototype.SetFilterEDI = function(key, control, url) {
        if (control) {
            this.SetAndRefresh(key, control.value, url);
        }
        return false;
    };

    CikkSzuroPanel.prototype.SetFilter = function(key, value, url) {
        this.SetAndRefresh(key, value, url);
        return false;
    };

    CikkSzuroPanel.prototype.SetAndRefresh = function(key, value, url) {
        this.qs.set(key, value);

        if (this.qs.get('p')) {
            this.qs.set('p', '1');
        }

        if (!url) url = window.location.pathname;

        window.location = url + '?' + this.qs.toString();
        return false;
    };

    CikkSzuroPanel.prototype.SetFogalomRendezes = function(index, dir) {
        this.qs.set('cpr', index);
        this.qs.set('cpi', dir);
        if (this.qs.get('rm')) {
            this.qs.set('rm', '');
        }
        if (this.qs.get('ri')) {
            this.qs.set('ri', '');
        }

        window.location = window.location.pathname + '?' + this.qs.toString();
        return false;
    };

    CikkSzuroPanel.prototype.SetMezoRendezes = function(value, dir) {
        this.qs.set('rm', value);
        this.qs.set('ri', dir);

        if (this.qs.get('cpr')) {
            this.qs.set('cpr', '');
        }

        if (this.qs.get('cpi')) {
            this.qs.set('cpi', '');
        }

        window.location = window.location.pathname + '?' + this.qs.toString();
        return false;
    };

    CikkSzuroPanel.prototype.SetRendezesFromDDDL = function(idField, idDir) {
        var field = document.getElementById(idField);
        var direction = document.getElementById(idDir);
        if (field && direction) {
            var f = field.value;
            var d = direction.value;

            this.qs.set('rm', f);
            this.qs.set('ri', d);

            if (this.qs.get('cpr')) {
                this.qs.set('cpr', '');
            }

            if (this.qs.get('cpi')) {
                this.qs.set('cpi', '');
            }

            window.location = window.location.pathname + '?' + this.qs.toString();
        }
        return false;
    };

    CikkSzuroPanel.prototype.SetEgyszeruKereses = function(key, id, url) {
        var control = document.getElementById(id);
        if (control) {
            var value = control.value;
            if (value && value.length > 1) {
                window.location = url + '?' + key + "=" + value;
            }
        }
        return false;
    };

    CikkSzuroPanel.prototype.SetEgyszeruKeresesKifutott = function(key, id, url, id_kifutott) {
        var control = document.getElementById(id);
        if (control) {
            var value = control.value;
            if (value && value.length > 1) {

                var control2 = document.getElementById(id_kifutott);
                if (control2 && control2.checked) {
                    window.location = url + '?' + key + '=' + value + '&kiftt=1';
                } else {
                    window.location = url + '?' + key + "=" + value;
                }
            }
        }
        return false;
    };

    CikkSzuroPanel.prototype.SetFilterMinMaxDDL = function(key, control, minmax, url) {
        if (control) {
            var value = control.options[control.selectedIndex].value;

            if (!value) value = '';
            var queryValue = this.qs.get(key);

            var values = (queryValue ? queryValue[0].split('_', 2) : ['', '']);
            if (values.length != 2) {
                if (values.length === 0) values = ['', ''];
                if (values.length === 1) values = [values[0], ''];
                if (values.length > 2) values = values.splice(0, 2);
            }

            values[minmax] = value;

            queryValue = values[0] + '_' + values[1];
            if (queryValue == '_' || queryValue.length < 2)
                this.SetAndRefresh(key, '', url);
            else
                this.SetAndRefresh(key, [values[0] + '_' + values[1]]);
        }

        return false;
    };

    window.CikkSzuroPanel = new CikkSzuroPanel();

})();


