﻿(function ($) {

    $.fn.PromoterSearch = function (methods) {
        var caller = this;
        this.attr('autocomplete', 'off');
        this.typeWatch({
            callback: function () {
                if (!$(methods.popup).is(':visible')) {
                    $(methods.popup).show();
                }
                else {
                    $(methods.popup).html('<img src="../Content/Images/eqqS.gif" alt="Equaliser" id="Equaliser" />');
                }

                $.ajax({
                    url: homeURL + methods.controller + "/PerformTextSearch",
                    data: { text: caller.val(), orientation: "Vertical" },
                    dataType: "html",
                    type: "GET",
                    global: false,
                    success: function (data) {
                        $(methods.popup).html(data);



                        $(methods.popup + ' td').click(function () {
                            var id = $(this).find('a').attr('id');
                            var name = $(this).find('a').html();
                            var postcode = $(this).find('a').attr('postcode');

                            methods.click(id, name, postcode);
                            if ($(methods.popup).is(':visible')) {
                                $(methods.popup).hide();
                            }

                            caller.val(name);
                        })
                    },
                    error: function () {
                        alert("An error has occurred");
                    }
                });
            },
            wait: 750,
            captureLength: 2
        });

    };
})(jQuery);





