/* Author: yangzeming
* Date: 2014-02-10
* Desc: 选择组件
*/

define(function (require, exports, module) {

    var Select = _.Class({
        defaultOp: {
            con: "",
            setValue: function () { }

        },
        initial: function (option) {
            this._eventStack = {};
            this.options = $.extend(true, {}, this.defaultOp, option);
            this.$con = $(option.con);
            this.$select = this.$con.find('[data-role="select"]');
            this.$selectBtn = this.$con.find('[data-role="selectBtn"]');
            this.$optionCon = this.$con.find('[data-role="optionCon"]');
            this.bindEvent();
        },
        bindEvent: function () {
            var self = this;
            this.$selectBtn.click(function () {
                if (self.$optionCon.is(":visible")) {

                    self.hide();
                } else {
                    self.show();
                }

            });
            $(document).bind("click", function (e) {
                var target = $(e.target);
                if (target.closest(self.options.con).length == 0) {
                    self.hide();
                }

            });
            this.$optionCon.delegate('[data-role="option"]', "click", function () {
                var value = $(this).data("value"),
                    name = $(this).data("name");
                self.value = value;
                self.trigger("onchange", value, name);
                self.options.setValue(value,name);
                self.hide();

            });

        },
        getValue: function () {
            return this.value;
        },
        setValue: function (value) {

        },
        show: function () {
            this.$optionCon.show();

        },
        hide: function () {
            this.$optionCon.hide();

        }

    });
    module.exports = Select;
});