﻿/// <reference path="../jquery1.4.2.js" />
(function ($) {
    $.fn.selsearch = function (settings) {
        settings = $.extend({
            custom: false, //是否允许自定义输入，true不对录入验证，false录入的值必须是下拉列表有的
            direction: "bottom", //bottom向下,top:向上
            scroll: null, //滚动项选择表达式
            __none: 1
        }, settings || {});
        var that = this;
        this.each(function () {
            var sel_id = this.id;
            var sel = $(this); // $("#" + sel_id);
            var custom = false;
            function get_ul_html() {
                var liHtml = "";
                var ops = sel[0].options; // sel.find("option");
                for (var i = 0; i < ops.length; i++) {
                    //                  liHtml += String.format("<li id='{0}_{1}' opvalue='{1}'>{2}</li>", sel_id, this.value + "", this.text);
                    liHtml += "<li id='" + sel_id + "_" + ops[i].value + "' opvalue='" + ops[i].value + "'>" + ops[i].text + "</li>";
                }
                //                sel.find("option").each(function () {
                //                    liHtml += String.format("<li id='{0}_{1}' opvalue='{1}'>{2}</li>", sel_id, this.value + "", this.text);
                //                });

                return String.format("<ul>{0}</ul>", liHtml);
            }
            function create_div() {
                var div = $(String.format("<div id='ss_div_{0}' class='sel_div dn_'></div>", sel_id));
                var ulHtml = get_ul_html();
                div.append(ulHtml);
                return $(div);
            }
            function create_div_bg() {
                var div = $(String.format("<div id='ss_div_{0}_bg' class='sel_div_bg dn_'></div>", sel_id));
                return $(div);
            }
            function create_input() {
                var input = $(String.format("<input type='text' id='ss_input_{0}'  class='txt' value='{1}'/>", sel_id, sel.find(":selected").html()));
                return $(input);
            }
            function create_ct() {
                var ct = $(String.format("<span id='ss_ct_{0}' class='sel_ct'></span>", sel_id));
                return $(ct);
            }
            sel.change(function () {

            });
            var div = create_div();
            var bg = create_div_bg();
            var li = $(div).find("li");
            var input = create_input();
            var ct = create_ct();
            var active = false;
            function init() {
                div.css({
                    width: input.width(),
                    top: input.position().top + input.height() + 1,
                    left: input.position().left
                });
                div.bgIframe();
                bg.bgIframe();




                ct.append(input);
                sel.before(ct);
                sel.focus(function () {
                    input.focus();
                    input.select();
                });
                input.width(sel.width());
                ct.append(bg);
                ct.append(div);
                if (settings.scroll != null) {
                    settings.scroll.scroll(function () {
                        
                        hide();
                    });
                }
                sel.hide();
                init_event();
            }

            //选择下啦列表框
            var selectDll = function (obj, v) {
                var v0 = "＾" + v + "＾";
                var dlist = $(obj).find("option");
                for (var i = 0; i < dlist.length; i++) {
                    var vv = $(dlist[i]).attr("value");
                    if (vv && vv.length > 0) {
                        vv = "＾" + vv + "＾";
                        if (vv.indexOf(v0) > -1) {
                            $(dlist[i]).get(0).selected = true;
                        }
                    }
                }
            }

            function set_depart(value) {
                if (value == null) {
                    if (input.val() != "") {
                        var exist = false;
                        div.find("li").each(function () {
                            if (input.val() == $(this).html()) {
                                exist = true;
                                if (sel[0].value != $(this).attr("opvalue")) {
                                    sel[0].value = $(this).attr("opvalue");
                                    sel.change();
                                }
                                return false;
                            }
                        });
                        if (!exist && !settings.custom) {
                            alert("未找到符合条件的记录");
                            input.val(sel.find(":selected").html());
                        }
                    }
                } else {
                    //sel[0].value = value;
                    selectDll(sel[0], value);
                    input.val(sel.find(":selected").html());
                }
            }
            this.set_depart = set_depart;

            function set_value(value) {
                if (value == null) {
                    if (input.val() != "") {
                        //var selli = div.find(String.format("li:contains({0})", input.val()));
                        var exist = false;
                        div.find("li").each(function () {
                            if (input.val() == $(this).html()) {
                                exist = true;
                                if (sel[0].value != $(this).attr("opvalue")) {
                                    sel[0].value = $(this).attr("opvalue");
                                    sel.change();
                                }
                                return false;
                            }
                        });
                        if (!exist && !settings.custom) {
                            alert("未找到符合条件的记录");
                            input.val(sel.find(":selected").html());
                        }
                    }
                } else {
                    sel[0].value = value;
                    input.val(sel.find(":selected").html());
                }
            }
            this.set_value = set_value;
            function init_event() {
                input.click(function () {
                    show();
                    div.css({
                        width: input.width(),
                        top: input.offset().top + input.height() + 2,
                        left: input.offset().left
                    });
                    div.css("index-z", 100);
                    bg.css({
                        width: input.width() + 30,
                        top: input.offset().top + input.height() + 2 - 10,
                        left: input.offset().left - 10
                    });
                    bg.css("index-z", 99);
                    if (settings.direction == "top") {
                        div.css({ top: input.offset().top - div.height() - 2 });
                        bg.css({ top: input.offset().top - div.height() - 2 - 10 });
                    }
                    bg.height(div.height() + 5);
                    li.show();
                    active = true;
                }).blur(function () {
                    active = false;
                    setTimeout(function () {
                        if (!active) {
                            hide();
                            set_value(null);
                        }
                    }, 200);
                }).keyup(function () {
                    if (this.last_value == this.value) {
                        return;
                    }
                    else
                        this.last_value = this.value;
                    if (this.value == "") {
                        li.show();
                    } else {
                        li.hide();
                        var show_li = div.find(String.format("li:contains({0})", this.value));
                        this.show_length = show_li.length;
                        show_li.show();
                    }
                });
                div.click(function () {
                    active = true;
                }).scroll(function () {
                    active = true;
                }).mousemove(function () {
                    active = true;
                }).blur(function () {
                    active = false;
                    setTimeout(function () {
                        if (!active) {
                            hide();
                            set_value(null);
                        }
                    }, 100);
                }); ;
                div.find("li").click(function () {

                    input.val($(this).html());
                    hide();
                    li.show();
                    set_value(null);
                }).hover(function () {
                    $(this).css({ background: '#E7F7DD' });
                }, function () {
                    $(this).css({ background: 'none' });
                });
            }
            function show() {
//                ct.append(bg);
//                ct.append(div);
                bg.show();
                div.show();
            }
            function hide() {
//                div.remove();
//                bg.remove();
                div.hide();
                bg.hide();
            }
            init();
        });

    };
})(jQuery);
 

