define(['modules/sections/section', 'modules/sections/addsection'], function (Section, AddSection) {
    var gs = {};
    gs.title = GetTextByKey("P_GLOBALSECTIONS", 'Global Sections');
    gs.description = GetTextByKey("P_GLOBALSECTIONS", 'Global Sections');
    gs.version = '1.0';

    var datacontent = null;
    gs.createContent = function (sections) {
        templatereadonly = false;
        editable = true;
        sectiontype = 0;//sectiontype:0 - global,1 - normal
        templatestatus = 0;//templatestatus:0 - draft,1 - published

        var content = $('<div style="min-width:1780px;"></div>');

        function createHeader() {
            var header = $('<div></div>');
            header.append($('<div class="page_title"></div>').text(gs.title));
            setPageTitle(gs.title, true);
            //var search_bar = $('<div class="search_bar"></div>');
            //header.append(search_bar);
            //header.append('<input type="text" id="searchinputtxt" autocomplete="off" />');
            //var btnRefresh = $('<input class="search" type="button" value="Search" />');
            //header.append(btnRefresh);
            //btnRefresh.click(function () {
            //    gs.refresh();
            //});

            var func = $('<div class="function_title"></div>');
            var iconAdd = $('<span class="sbutton iconadd">' + GetTextByKey("P_IPT_ADD", "Add") + '</span>').click(function () {
                gs.addSection();
            });
            func.append(iconAdd);
            var iconRefresh = $('<span class="sbutton iconrefresh">' + GetTextByKey("P_IPT_REFRESH", "Refresh") + '</span>').click(function () {
                gs.refresh();
            });
            func.append(iconRefresh);
            header.append(func)

            return header;
        }

        content.append(createHeader());

        var dataheader = $('<div class="question-holder no-hover" style="font-weight: bold;font-size:14px;"></div>');
        dataheader.append('<div style="width:60px; flex-shrink: 0"></div>');
        dataheader.append('<div class="question-cell" style="width: 200px">' + GetTextByKey("P_IPT_NAME", "Name") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 310px">' + GetTextByKey("P_IPT_DISPLAYTEXT", "Display Text") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 370px">' + GetTextByKey("P_IPT_TYPE", "Type") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 80px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_DISPLAYONREPORT", "Display on Report") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 80px; text-align: center">' + GetTextByKey("P_IPT_REQUIRED", "Required") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 65px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_INCLUDECOMMENT", "Include Comment") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 70px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_ISIMPORTANT", "Is Important") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 70px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_CUSTOMERVISIBLE", "Customer Visible") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 80px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_SEVERITYLEVEL", "Severity Level") + '</div>');
        dataheader.append('<div style="flex-grow: 1;" style="width: 180px">' + GetTextByKey("P_IPT_NOTES", "Notes") + '</div>');
        dataheader.append('<div class="question-cell" style="width: 90px;padding-right:20px;"></div>');
        content.append(dataheader);

        datacontent = $('<div></div>');
        content.append(datacontent);

        //setTimeout(function () {
        //    gs.resize();
        //});
        //$(window).resize(function () {
        //    gs.resize();
        //});
        if (sections) {
            for (var i = 0; i < sections.length; i++) {
                var s = new Section(sections[i], gs);
                datacontent.append(s.createContent());
            }
        }
        else
            this.refresh();
        return content;
    }

    gs.resize = function () {
        datacontent.css("height", document.documentElement.clientHeight - datacontent.offset().top)
    }

    gs.addSection = function () {
        var s = new AddSection(gs);
        $('#right_popup').empty().append(s.createContent());
        showRightPopup(true);
    }

    gs.refresh = function () {
        datacontent.empty();
        showloading(true);
        inspectionrequest("GetGlobalSections", teamintelligence, function (data) {
            showloading(false);
            datacontent.empty();
            if (data) {
                for (var i = 0; i < data.length; i++) {
                    var s = new Section(data[i], gs);
                    datacontent.append(s.createContent());
                }
            }
        }, function (err) {
            showloading(false);
        });
    }
    return gs;
});