//下拉搜索框 date: 2015.4.16


(function ($) {
    var defaults = { url: '', parms: [], msgtip: '' };
    function createLi(data, count) {
        $callbackData[count].count.splice(0, $callbackData[count].count.length);
        var ull = $newElement[count].find('ul');
        $($newElement[count]).find('.chosen-container').width($($element[count]).width());
        $(ull).empty();
        for (var i = 0; i < data.length; i++) {
            $callbackData[count].count.push(data[i]);
            var $lie = $('<li class="active-result"></li>');
            $lie.text(data[i].title);
            $lie.attr('data_ind', i);
            $lie.hover(function () {
                $(this).addClass('highlighted');
            }, function () {
                $(this).removeClass('highlighted');
            });
            ull.hover(function () {
                $hover = 1;
            }, function () {
                $hover = 0;
            });
            $lie.unbind('click');
            $lie.bind('click', function () {
                //selVal = $callbackData[count].count[$(this).attr('data_index')];
                $element[count].val($(this).text());
                //$newElement[count].find('ul').hide();

                $element[count].triggerHandler("change.tc", $callbackData[count].count[$(this).attr('data_ind')]);
                $newElement[count].hide();
            });
            ull.append($lie);
        }
        $newElement[count].show();
        if (data.length <= 0) {
            var $lie = $('<li class="active-result"></li>');
            $lie.text($msg[count]);
            ull.append($lie);
            $newElement[count].show();
        }
        else {
            //$newElement[count].hide();
        }
    }


    function ajaxcallback(d, count) {
        if ($element[count].attr('id') == 'uzhiwu') {

        }
        else {
            if (dallValue[count].length <= 0) {
                return;
            }
        }
       
        var p = "";
        for (var i = 0; i < urlParms[count].count.length; i++) {
            p += "&" + urlParms[count].count[i].prmname + "=" + urlParms[count].count[i].pvalue;
        }
        var urls = dallValue[count] + d;
        if (p.length > 0) {
            urls += p;
        }
        $.ajax({ url: urls, cache: false, async: false, type: 'post', dateType: 'json',
            success: function (data) {
                createLi(eval(data), count);
            }, error: function () {
                alert('加载数据错误!');
            }
        });
    }


    function createDropdown(count) {
        var searchbox = '<div class="chosen-search ' + $element[count].attr('class') + '"><input type="text" name="tcsearcher" class="form-control ' + $element[count].attr('class') + '" /><div>';
        var dropul = '<ul class="chosen-results" style="margin:0"></ul>';
        var drop = '<div class="chosen-container" style="padding-right:0;z-index:9999;position:absolute;background: #fff;width: ' + $element[count].width() + 'px;border:1px solid #ccc">' + dropul + '</div>';
        return $(drop);
    }

    function createView(count) {
        //$element[count].hide();
        $newElement.push(createDropdown(count));
        $newElement[count].hide();
        $element[count].after($newElement[count]);
        $searchbox.push($newElement[count].find('input'));
        $element[count].bind('focus', function () {
            if ($(this).attr('id') == "uzhiwu") {
                ajaxcallback($(this).val(), count);
            }
            else {
                if ($element[count].val().length > 0) {
                    ajaxcallback($(this).val(), count);
                    //$newElement[count].show();
                    //$newElement[count].find('ul').show();
                }
                else {
                    //$newElement[count].find('ul').hide();
                    $newElement[count].hide();
                }
            }

        });

        $element[count].on('input propertychange', function () {
            if ($element[count].val().length > 0) {
                ajaxcallback($(this).val(), count);
                //$newElement[count].find('ul').show();
                //$newElement[count].show();
            }
            else {
                //$newElement[count].find('ul').hide();
                $newElement[count].hide();
            }
        });
        $element[count].bind('blur', function () {
            if ($hover == 1) {
                return;
            }
            $newElement[count].hide();
        });

    }
    function init(count) {
        createView(count);
    }
    var $element = [];
    var $newElement = [];
    var $lis = [];
    var $searchbox = [];
    var $callbackData = [];
    var $msg = [];
    var selVal = [];
    var dallValue = [];
    var allValue = [];
    var count = 0;
    var urlParms = [];
    var $hover = 0;
    //数据格式{'ctrname':'控件名称','parms':[{'prmname':'参数1','pvalue':'参数值1'},{'prmname':'参数2','pvalue':'参数值2'}]}
    $.fn.setParms = function (options) {
        var ll = -1;
        for (var i = 0; i < $element.length; i++) {
            if ($element[i].attr('id') == options.ctrname) {
                ll = i;
            }
        }
        if (ll == -1) {
            alert('未初始化');
            return;
        }
        urlParms[ll].count.splice(0, urlParms[ll].count.length);
        for (var i = 0; i < options.parms.length; i++) {
            urlParms[ll].count.push(options.parms[i]);
        }
        init(ll);
    }


    $.fn.selectedValue = function () {
        return selVal;
    }
    $.fn.TCSearcher = function (options) {
        $element.push(this);
        var l = $.extend(defaults, options);
        dallValue.push(l.url);
        var ddd = dallValue;
        urlParms.push({ count: [] });
        $callbackData.push({ count: [] });
        $msg.push(options.msgtip);
        init(count);
        count++;
    }
})(window.jQuery);