jQuery(function($) {
    
    $("#ds_name").on("keyup",function() {
        var $this = $(this);
        var not = $this.val().match(/[^a-zA-Z0-9_-]+/g);
        if (not) {
            not.forEach(function(text) {
                $this.val($this.val().replace(text, ""));
            });
        }
    });
    
    $("#ds_options_use_values").on("change", function() {
        var $this = $(this);
        if($this.is(":checked")) {
            $(".wpadverts-multiselect-option-value").show();
        } else {
            $(".wpadverts-multiselect-option-value").hide();
        }
    });
    
    $("#ds_type").on("change", function(e) {
        var type = $(this).val();
        
        var trCallback = $("#ds_callback").closest("tr");
        var trTaxonomy = $("#ds_taxonomy").closest("tr");
        var trArgs = $("#ds_args").closest("tr");
        var trOptions = $("#ds_options").closest("tr");
        
        // hide all
        trCallback.hide();
        trTaxonomy.hide();
        trArgs.hide();
        trOptions.hide();
        
        if( type === "options" ) {
            trOptions.show();
            $("#ds_options_use_values").change();
        } else if( type === "callback" ) {
            trCallback.show();
            trArgs.show();
        } else if( type === "taxonomy" ) {
            trTaxonomy.show();
            trArgs.show();
        }
        
    });
    
    $("#ds_type").change();
    
    $("#ds_options_list_add").on("click", function(e) {
        e.preventDefault();

        wpacf_insert_option({
           value:  "",
           text: "",
           depth: "",
           disabled: 0
        });
        
        return false;
    });
    

    
    $("#ds_options_list").sortable({
        handle: ".wpadverts-multiselect-option-drag"
    });
    
    if(ds_options !== 'undefined') {
        jQuery.each(ds_options, function(index, item) {
            wpacf_insert_option(item);
        });
    }
    
});

var wpacf_uniqueid_val = 0;

wpacf_uniqueid = function() {
    return wpacf_uniqueid_val++;
};

wpacf_insert_option = function(data) {
    
    var total = wpacf_uniqueid();
    var template = jQuery(".tmpl-cf-data-source-item").clone();

    template.data("index", total);
    template.attr("index", total);
    template.find(".wpadverts-multiselect-option-value").attr("name", "ds_option["+total+"][value]").val(data.value);
    template.find(".wpadverts-multiselect-option-text").attr("name", "ds_option["+total+"][text]").val(data.text);
    template.find(".wpadverts-multiselect-option-depth").attr("name", "ds_option["+total+"][depth]").val(data.depth);
    template.find(".wpadverts-multiselect-option-disabled").attr("name", "ds_option["+total+"][disabled]");

    if( typeof data.disabled !== 'undefined' && data.disabled == "1" ) {
        template.find(".wpadverts-multiselect-option-disabled").prop("checked", true);
    }

    template.removeClass("tmpl-cf-data-source-item");
    template.find(".wpadverts-multiselect-option-trash").on("click", function(e) {
        e.preventDefault();
        jQuery(this).closest("li").remove();
    });
    template.find(".wpadverts-multiselect-option-text,.wpadverts-multiselect-option-text,.wpadverts-multiselect-option-depth").on("keypress", function(e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if(keycode == '13'){
            jQuery("#ds_options_list_add").click();
            e.preventDefault();
            return false;
        }
    });

    var options = jQuery("#ds_options_list");

    options.append(template);

    if(options.hasClass("wpacf-multiselect-uses-values")) {
        template.find(".wpadverts-multiselect-option-value").focus();
    } else {
        template.find(".wpadverts-multiselect-option-text").focus();
    }

    options.scrollTop(options.prop("scrollHeight"));
};