﻿(function ($) {
    $.fn.CustomFieldDefaults = function (actionPath, settings) {

        if (typeof actionPath == 'undefined') {
            throw "An action path is requried";
        }

        var config = $.extend({}, $.fn.CascadingDropDown.defaults, settings);

        return this.each(function () {
            var $this = $(this);

            (function () {
                var methods = {
                    initialize: function () {

                    },
                    post: function () {
                        $.isFunction(config.onLoading) && config.onLoading.call($this);
                        $.ajax({
                            url: actionPath,
                            type: 'POST',
                            dataType: 'json',
                            data: ((typeof config.postData == "function") ? config.postData() : config.postData) || $this.serialize(),
                            success: function (data) {
                                $.each(data, function () {
                                    var sourceElement = $("#" + this.sourceID);
                                    if (sourceElement != null && this.forceOverride || sourceElement.val() == "") {
                                        if (this.sourceType.toUpperCase() == "TEXT" || this.sourceType.toUpperCase() == "DATETIME") {
                                            sourceElement.trigger('focus');
                                            sourceElement.val(this.value);
                                            sourceElement.trigger('blur');
                                        }
                                        else if (this.sourceType.toUpperCase() == "LIST") {
                                            $("#" + this.sourceID + " option[value=" + this.value + "]").attr("selected", "selected");
                                            sourceElement.trigger('change');
                                        }
                                        else if (this.sourceType.toUpperCase() == "MULTILIST") {
                                            if (sourceElement.get(0).tagName.toUpperCase() == "INPUT") {
                                                var values = this.value.split(',');
                                                sourceElement.parent().find("input[name^='checkbox']").each(function () { $(this).attr('checked', ($.inArray($(this).attr('value'), values) >= 0)); });
                                                sourceElement.val(this.value);
                                                sourceElement.trigger('change');
                                            }
                                            else {
                                                sourceElement.val(this.value);
                                                sourceElement.trigger('change');
                                            }
                                        }
                                    }
                                });
                                $.isFunction(config.onLoaded) && config.onLoaded.call($this);
                            },
                            error: function () {
                                alert('error occurred');
                            }
                        });
                    }
                };

                var previousValue = '';

                var focusHandler = function () {
                    previousValue = $(this).val();
                }

                var blurHandler = function () {
                    if ($(this).val() != previousValue) {
                        methods.post();
                    }
                };

                $this.each(function () {
                    if (this.tagName.toUpperCase() == "INPUT") {
                        $(this).bind('focus', focusHandler);
                        $(this).bind('blur', blurHandler);
                    }
                    else {
                        $(this).change(function () {
                            methods.post();
                        });
                    }
                });


                methods.initialize();

            })();
        });
    }

    $.fn.CustomFieldDefaults.defaults = {
        postData: null,
        onLoading: null,
        onLoaded: null
    }
})(jQuery);
