/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE 
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer 
 * Software" and other relevant administrative regulations and international treaties. Without 
 * the written permission of the Company, no person may use (including but not limited to the 
 * illegal copy, distribute, display, image, upload, and download) and disclose the above 
 * technical documents to any third party. Otherwise, any infringer shall afford the legal 
 * liability to the company.
 */
/**
 * Created by liushuai on 2015/1/16.
 */
define(["adoreapp/widget/BaseWidget", "adoreapp/widget/EventHub"], function (BaseWidget, EventHub) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.jqueryNode = $(attrs.template).attr("id", this.id);
                var tmpId = this.id;
                var own = this;
                this.jqueryNode.find("input[type=radio]").attr("id", this.id + "_radio").on("change", function (e, options) {
                    own.triggerOtherChangeEvent(this, options);
                    // dispatch onChange event
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                        id: tmpId,
                        checked: this.checked + "",
                        evtType: EventHub.ON_CHANGE,
                        syncAttrName: this.checked
                    });
                });
                this.jqueryNode.find("label").attr("for", this.id + "_radio");
            },

            triggerOtherChangeEvent: function (thisGroup, options) {
                if (options === "setChecked") {
                    return;
                }
                var radioGroup = $("input[name='" + this.groupName + "']");
                for (var i = 0; i < radioGroup.length; i++) {
                    if ($(radioGroup[i]).attr("checked") === "checked") {
                        $(radioGroup[i]).checked = false;
                        $(radioGroup[i]).removeAttr("checked");
                        $(radioGroup[i]).trigger("change", ["setChecked"]);
                        break;
                    }
                }
                // radioGroup.removeAttr("checked");
                $(thisGroup).attr("checked", "checked");
            },

            _setName: function (attrs) {
                var name = attrs.name;
                this.jqueryNode.find("label").text(name);
            },

            _setGroupName: function (attrs) {
                this.groupName = attrs.group;
                this.jqueryNode.find("input").attr("name", this.groupName);
            },

            _setChecked: function (attrs) {
                var isChecked = attrs.isChecked;
                if (isChecked) {
                    this.reset(this.jqueryNode.find("input")[0]);
                }
            },

            reset: function (thisGroup) {
                if (thisGroup.checked) {
                    return;
                }
                var radioGroup = $("input[name='" + this.groupName + "']");
                for (var i = 0; i < radioGroup.length; i++) {
                    if ($(radioGroup[i]).attr("checked") === "checked") {
                        $(radioGroup[i]).checked = false;
                        $(radioGroup[i]).removeAttr("checked");
                        $(radioGroup[i]).trigger("change", ["setChecked"]);
                        break;
                    }
                }
                $(thisGroup).attr("checked", "checked");
                thisGroup.checked = true;
                $(thisGroup).trigger("change", ["setChecked"]);
            },


            _layout: function () {
                base._layout.call(this);
            }
        };
    });
});
//<form action="#">
//    <p>
//        <input name="group1" type="radio" id="test1" />
//        <label for="test1">Red</label>
//    </p>
//    <p>
//        <input name="group1" type="radio" id="test2" />
//        <label for="test2">Yellow</label>
//    </p>
//    <p>
//        <input class="with-gap" name="group1" type="radio" id="test3"  />
//        <label for="test3">Green</label>
//    </p>
//    <p>
//        <input name="group1" type="radio" id="test4" disabled="disabled" />
//        <label for="test4">Brown</label>
//    </p>
//</form>
