﻿(function ($) {
    $.fn.populateDropDown = function (options) {
        var opts = $.extend({}, $.fn.populateDropDown.defaults, options);
        var arrVals = [];
        var selectedOptGroup;
        var CurrentCategoryId;
        return this.each(function () {
            CurrentDropDown = this;
            if (opts.IsOptGroup) {
                var $groups = $('optgroup', $(this));
                var values = '<option selected=\"selected\" value=\"' + opts.DefaultVal + '\">' + opts.DefaultText + '</option>';
                $groups.each(function () {
                    PushSubQuerysOpt(this, $(this).attr("id"));
                    if ($(this).attr("id") != 0)
                        values += '<option ' + (selectedOptGroup == $(this).attr("id") ? 'selected="selected"' : '') + ' value="' + $(this).attr("id") + '">' + $(this).attr("label") + '</option>';
                    if (selectedOptGroup == $(this).attr("id"))
                        fillSubList($(this).attr("id"));
                });
                $(this).html(values);
            }
            else {
                PushSubQuerys(this, 0);
                $(this).find("option[value='" + selectedOptGroup + "']").attr("selected", "selected");
                if (selectedOptGroup == null) selectedOptGroup = $(this).find("option:selected").attr("value");
                fillSubList(selectedOptGroup);
            }

            $(this).change(function () {
                $(opts.SubDropDown).find('option').remove();
                fillSubList($(this).val());
                $("label[for=" + opts.SubDropDown.replace("#", "").replace(".", "") + "]").show();
            });
        });

        function fillSubList(parentID) {
            var values = "";
            $(opts.SubDropDown).show();
            if (opts.DefaultTextSub != null) values = "<option value=\"" + opts.DefaultValSub + "\">" + opts.DefaultTextSub + "</option>";
            for (var i = 0; i < arrVals.length; i++)
                if (arrVals[i].categoryId == parentID)
                    values += '<option ' + (arrVals[i].selected == true && (arrVals[i - 1] != null && arrVals[i - 1].selected != true) ? 'selected="selected"' : '') + ' value="' + arrVals[i].val + '">' + arrVals[i].text + '</option>';
            $(CurrentDropDown).closest("form").find(opts.SubDropDown).html(values);
            /*if ($(CurrentDropDown).siblings(opts.SubDropDown).size() > 0)
            $(CurrentDropDown).siblings(opts.SubDropDown).html(values);
            else
            $(opts.SubDropDown).html(values);*/
        }

        function PushSubQuerysOpt(item, CurrentCategoryId) {
            var tmpCategoryID = CurrentCategoryId;
            var $options = $('option', $(item));
            if (opts.Multiple)
                arrVals = [];

            $options.each(function () {
                if ($(this).text().substring(0, opts.Delimiter.length) == opts.Delimiter) {
                    arrVals.push({
                        val: $(this).val(),
                        text: $(this).text().replace(opts.Delimiter, ''),
                        selected: $(this).attr("selected"),
                        categoryId: tmpCategoryID
                    });

                    if ($(this).attr("selected") === true && selectedOptGroup == null)
                        selectedOptGroup = $(item).attr("id");

                    $(this).remove();
                }
                else {
                    tmpCategoryID = $(this).val();
                }
            });
        }

        function PushSubQuerys(item, CurrentCategoryId) {
            var tmpCategoryID = CurrentCategoryId;
            var $options = $('option', $(item));
            if (opts.Multiple)
                arrVals = [];
            $options.each(function () {
                if ($(this).text().substring(0, opts.Delimiter.length) == opts.Delimiter) {
                    arrVals.push({
                        val: $(this).val(),
                        text: $(this).text().replace(opts.Delimiter, ''),
                        selected: $(this).attr("selected"),
                        categoryId: tmpCategoryID
                    });

                    if ($(this).attr("selected") == true)
                        selectedOptGroup = tmpCategoryID;

                    $(this).remove();
                }
                else {
                    tmpCategoryID = $(this).val();
                }
            });
        }
    }
    $.fn.populateDropDown.defaults = { DefaultTextSub: null, DefaultValSub: 0, script: "/Json/SubCategories", SubDropDown: "#SubCategory", DefaultText: "Välj underkategori", IsOptGroup: true, Delimiter: "--", DefaultVal: "Alla", Multiple: false };
})(jQuery);


