﻿function CurrencySelector(viewall, name, datasource, itemSelected) {
    this.viewall = viewall;
    this.name = name;
    this.datasource = datasource;
    this.itemSelected = itemSelected;
    this.fieldId = "#" + this.name;
    this.buttonId = "#button_" + this.name;
    var counter = 0;
    var instance = this;

    $j(this.fieldId).autocomplete(
        { source: function (request, response) {
            counter = 0;
            var matcher = new RegExp(request.term.replace(" ", ".*"), "i");
            response($j.map(instance.datasource,
                                    function (item) {
                                        if (!viewall) {
                                            if (counter < 41) {
                                                if (matcher.test(item.text + item.value + item.synonyms)) {
                                                    counter++;
                                                    return { label: item.text, value: item.value };
                                                }
                                            } else if (counter == 40) {
                                                if ((matcher.test(item.text + item.value + item.synonyms))) {
                                                    counter++;
                                                }
                                            }
                                        } else {
                                            if (matcher.test(item.text + item.value + item.synonyms)) {
                                                return { label: item.text, value: item.value };
                                            }
                                        }
                                    }) // map
                    ); // response
        },  // source
            minLength: 0,
            delay: 0,
            select: function (event, ui) {
                this.selected = true;
                $j(instance.fieldId).autocomplete("option", "minLength", 0);
                if (typeof instance.itemSelected === "function") {
                    instance.itemSelected.call(this, ui.item);
                }
            },
            open: function () {
                $j(this).removeClass("ui-corner-all").addClass("ui-corner-top");
            },
            close: function () {
                $j(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
        }   //  select
    );   //   autocomplete

    $j(this.fieldId).data('autocomplete')._renderItem = function (ul, item) {
        var li = $j("<li></li>");
        li.data("item.autocomplete", item);
        li.append("<a><span class='ui-menu-pair'>" + item.label.substring(0, item.label.indexOf("|")) + "</span> <span class='ui-menu-name'>" + item.label.substring(item.label.indexOf("|") + 1) + "</span></a>");
        return li.appendTo(ul);
    };

    $j(this.fieldId).data('autocomplete')._suggest = function (items) {
        var ul = this.menu.element.empty().zIndex(this.element.zIndex() + 1), menuWidth, textWidth;
        this._renderMenu(ul, items);
        // TODO refresh should check if the active item is still in the dom, removing the need for a manual deactivate
        this.menu.deactivate();
        this.menu.refresh();
        //
        //  Original jQueryUI
        //
//		    this.menu.element.show().position( $.extend({
//			    of: this.element
//		    }, this.options.position ));
        if (items.length > 8) {
            ul.height(200);
        }
        else {
            ul.height('auto');
        }
        this.menu.element.show().position({
            my: 'left top',
            at: 'left bottom',
            of: this.element,
            collision: 'none'
        });
        //
        //  Original jQueryUI
        //
        menuWidth = ul.width("").outerWidth();
        textWidth = this.element.outerWidth();
        ul.outerWidth(Math.max(menuWidth, textWidth));
    };
    
    $j(this.buttonId).click(function (event) {
        event.stopImmediatePropagation();
        fieldId = instance.fieldId;
        if ($j(fieldId).autocomplete("widget").is(":visible")) {
            $j(fieldId).autocomplete("close");
            return;
        }

        $j(fieldId).val('Type to search...');
        $j(fieldId).select();
        $j(fieldId).autocomplete("option", "minLength", 0);
        $j(fieldId).autocomplete("search", "");
    });

    $j(this.fieldId).click(function (event) {
        event.stopImmediatePropagation();
        fieldId = instance.fieldId;

        $j(fieldId).val('Type to search...');
        $j(fieldId).select();
        $j(fieldId).autocomplete("option", "minLength", 0);
        $j(fieldId).autocomplete("search", "");
    });
}
