/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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/2/9.
 */
define(["adoreapp/widget/Composite", "adoreapp/widget/WidgetManager"], function (BaseWidget, Manager) {
    "use strict";
    return BaseWidget.extend(function (base) {
        var _collapseButton;
        var attachNodeOfFather;
        return {
            _initWidget: function (attrs) {
                var id = this.id;
                this.children = new Array();
                this.jqueryNode = $(attrs.template).attr("id", this.id);
                this.top = 0;
                this.bottom = 0;
                var jqueryNode = this.jqueryNode;
                var own = this;
                $(window).bind("resize." + this.id, function () {
                    if (attachNodeOfFather.css("width") !== own.attachNodeW) {
                        _collapseButton.jqueryNode.sideNav("setOptions", {menuWidth: attachNodeOfFather.css("width").replace("px", "")});
                    }
                    // 大屏环境，不能有蒙板
                    if (window.innerWidth > 992) {
                        $("#sidenav-overlay").remove();
                        $("#sidenav-overlay").remove();
                    }
                });
                $(document).bind("click.sidenav-overlay", function (e) {
                    /**
                     * 由于偶尔会出现两个相同ID的sidenav蒙板，保险起见，删除两次
                     */
                    $("#sidenav-overlay").remove();
                    $("#sidenav-overlay").remove();
                });
            },

            _setAlignment: function (attrs) {
                if (attrs.alignment === 0) {
                } else {
                }
            },

            _dispose: function () {
                base._dispose.call(this);
                $("div.drag-target").remove();
                $(document).unbind("click.sidenav-overlay");
                $(window).unbind("resize." + this.id);
            },

            _setTop: function (attrs) {
                this.top = attrs.top;
            },

            _setBottom: function (attrs) {
                this.bottom = attrs.bottom;
            },

            setCollapseButton: function (collapseButton) {
                if (!_collapseButton) {
                    _collapseButton = collapseButton;
                }
            },

            _layout: function () {
                var layout = this.father ? this.father.layout : undefined;
                if (layout) {
                    if (layout.toString() === "SashLayout") {
                        var side = this.layoutData.getSide();
                        var smallColumn = this.layoutData.getSmallColumn();
                        var middleColumn = this.layoutData.getMiddleColumn();
                        var largeColumn = this.layoutData.getLargeColumn();
                        var style;
                        if (side === 0) {
                            //left
                            style = "col s" + smallColumn + " m" + middleColumn + " l" + largeColumn;
                            attachNodeOfFather = layout.getLeftSide()
                                .css({position: "absolute", top: 0, bottom: 0})
                                .addClass(style)
                                .resize(function () {
                                    _collapseButton.jqueryNode.sideNav({menuWidth: this.css("width")});
                                });
                        } else {
                            style = "col s" + smallColumn +
                                " m" + middleColumn +
                                " l" + largeColumn +
                                " offset-s" + (12 - smallColumn) +
                                " offset-m" + (12 - middleColumn) +
                                " offset-l" + (12 - largeColumn);
                            attachNodeOfFather = layout.getRightSide()
                                .css({position: "absolute", top: 0, bottom: 0})
                                .addClass(style)
                                .resize(function () {
                                    _collapseButton.jqueryNode.sideNav({menuWidth: this.css("width")});
                                });
                        }
                        this.attachNode = this.jqueryNode;
                        this.jqueryNode.appendTo(attachNodeOfFather);
                        this.jqueryNode.addClass(style);
                        _collapseButton = Manager.byId(this.id + "_collapseButton");
                        if (_collapseButton) {
                            _collapseButton.setWidth(parseInt(this.jqueryNode.parent().css("width").replace("px", "")));
                            if (!_collapseButton.alignment) {
                                _collapseButton.jqueryNode.sideNav({edge: 'left', menuWidth: _collapseButton.sideNavW});
                            } else {
                                _collapseButton.jqueryNode.sideNav({
                                    edge: _collapseButton.alignment,
                                    menuWidth: _collapseButton.sideNavW
                                });
                            }
                            if (side === 0) {
                                _collapseButton.jqueryNode.sideNav({edge: 'left'});
                            } else {
                                _collapseButton.jqueryNode.sideNav({edge: 'right'});
                            }
                            this.attachNodeW = _collapseButton.sideNavW;
                            _collapseButton.jqueryNode.collapsible();
                        }
                    }
                    $(window).resize();
                    this.jqueryNode.removeAttr("width");
                }
            }
        };
    });
});