
/* Author: yangzeming
 * Date: 
 * Desc: 模拟select方法
 */
define(function (require, exports, module) {
   // 模拟select方法
    var tui_select = _.Class({

        initial: function () {
            this.bindEvent();

        },
        bindEvent: function () {

            $(document).delegate("[data-selectinput]", "click", function (event) {
                var self = $(this),
                    inp = self.data("selectinput"),
                    ss = $(document).find('[data-select="' + inp + '"]');

                //alert(self.is(":visible"));
                if (ss.is(":visible")) {
                    // 已经显示了就隐藏

                    ss.hide();
                } else {
                    // 显示下拉菜单   缺少一个点击
                    $(document).find("[data-select]").filter(":visible").hide();
                    ss.show();

                }
                $("#suggestionul").hide();
                event.stopPropagation();

            });
            $(document).delegate("[data-option]", "click", function (event) {
                var self = $(this),
                    option = self.data("option"),
                    selectI = self.closest("[data-select]"),
                    inp = selectI.data("select"),
                    si = $(document).find('[data-selectinput="' + inp + '"]'),
                    ss = $(document).find('[data-svalue="' + inp + '"]'),
                    val = self.data("opval"),
                    callback = selectI.data("callback");
                //alert(self.is(":visible"));
                // 设置值
                ss.html(option);
                ss.data("value", val);

                // 隐藏
                selectI.hide();
                if (callback == "yes") {
                    //setStorage();
                    //searchData();
					selectI.trigger("onchange",[val,option]);//触发外部事件
                }

                event.stopPropagation();
            });
            // 一些hover事件
            $(document).delegate("[data-option]", "mouseenter", function (event) {
                var self = $(this),
                    option = self.data("option");
                self.parent().find("[data-option]").removeClass("active");
                self.addClass("active");
                event.stopPropagation();
            });
            $(document).delegate("[data-option]", "mouseleave", function (event) {
                var self = $(this),
                    option = self.data("option");
                // self.parent().find("[data-option]").removeClass("active");
                self.removeClass("active");
                event.stopPropagation();
            });
            $(document).bind("click.ss", function (e) {
                var target = $(e.target);

                var t = $(document).find("[data-select]").filter(":visible");
                // 获取到当前显示
                if (target.closest(t).length == 0) {
                    t.hide();
                }

            });

        },
        // 获取某个select的值
        getSelect: function (dn) {
            return $(document).find('[data-svalue="' + dn + '"]').data("value");

        },
        // 设置某个select的值
        setSelect: function (dn, value) {
            var ss = $(document).find('[data-svalue="' + dn + '"]'),
               opt = $(document).find('[data-select="' + dn + '"]');
            var op = opt.find('[data-opval="' + value + '"]').data("option");

            ss.html(op);
            ss.data("value", value);

        }

    });

	module.exports = tui_select;

});