﻿(function($)
{
    $.fn.extend({
        autocomplete: function(listdata, inittext, initvalue, idfield, textfield, ismodify)
        {
            return this.each(function()
            {
                new $.Autocompleter(this, listdata, inittext, initvalue, idfield, textfield, ismodify);
            });
        },
        result: function(handler)
        {
            return this.bind("result", handler);
        }
    });

    $.Autocompleter = function(ac, listdata, inittext, initvalue, idfield, textfield, ismodify)
    {
        $(ac).unbind();
        $(ac).html(construct_autocomplete(null, inittext, initvalue, null, ismodify));

        $(ac).bind("click", function(event)
        {
            $('.ac_list').hide();
            var irow = 0;
            var html = '<ul>';

            if (listdata == null || listdata.length < 1)
                return;
            for (var i = 0; i < listdata.length; i++)
            {
                var rowclass = irow % 2 == 0 ? 'odd' : 'even';
                html += '<li acid=' + listdata[i][idfield] + ' class=' + rowclass + '>' + listdata[i][textfield] + '</li>';
                irow++;
            }

            html += '</ul>';

            $('#' + (this.id) + ' .ac_wrapper .ac_list').html(html);
            $('#' + (this.id) + ' .ac_wrapper .ac_list').show();
            $('#' + (this.id) + ' .ac_wrapper .ac_list ul li').bind("click", function(event)
            {

                select_ac_item(event);

            });

        });

        function select_ac_item(e)
        {

            var ele = e.target || window.event.srcElement;

            $('#' + (ac.id) + ' .ac_wrapper .ac_upper input').val($(ele).text());

            var selected = $(ele).text();
            var selectedid = $(ele).attr('acid');
            $(ac).trigger("result", [selectedid, selected]);
            $(ac).html(construct_autocomplete(null, '', selected, null));
            //ac = null;

            $('.ac_list').live("click", function()
            {

                $('.ac_list').hide();
            });

            return true;
        }
    };
})(jQuery);

function construct_autocomplete(id, text, value, onenterfunction, ismodify)
{
    var inputvalue = value != "" ? value : text;
    var textcolor = value != "" ? "black" : "gray";
    var idparam = id != "" ? ' id = "' + id + '" ' : '';
    var readonly = ismodify ? '' : 'readonly';
    var readonlycursor = ismodify ? 'default' : 'pointer';

    var html = '<div class="ac_wrapper">' +
                    '<div class="ac_upper">' +
                           '<div style=" background: url(/images/ac_arrowdown.gif) no-repeat right; width:100%;"><input ' + readonly + ' type="text" ' + idparam + ' style="cursor:' + readonlycursor + ';margin:0; border: none 0px; color:' + textcolor + '; " value="' + inputvalue + '" onkeydown="' + onenterfunction + '"/></div>' +
                   '</div>' +
                   '<div class="ac_list">' +
                   '</div>' +
               '</div>';
    $('.ac_list').hide();

    return html;
}