$(document).ready(function () {
    // SetSorting_Filter1();
    if (typeof SetGridColumnFreeze == "function") {
        SetGridColumnFreeze();
    }

});

var GridX, GridY = 0;
var freezeCols = 0;
var tdWidth = [];
function SetGridColumnFreeze() {
    try {
        freezeCols = Number(freezeCols);

        var TotalWt = 0;

        $('.Grid:not(.noFreeze)').each(function (index, grid) {

            if (tdWidth.length != $('.GridItem:first  > td', grid).length) {
                tdWidth = [];
            }
            $(this).closest('div').css("display", "inline-block !important");

            $('th', grid).each(function (i, j) {


                var wt = 0;
                if (tdWidth.length == $('.GridItem:first  > td', grid).length) {
                    wt = tdWidth[i];

                }

                else {
                    wt = $('.GridItem  > td', grid).eq(i)[0].offsetWidth;
                    console.log(i + ":" + wt);
                    tdWidth.push(wt);

                }

                $(j).css({ "width": wt - 28, "min-width": wt - 28 });

                $('.GridItem > td:eq(' + i + ')', grid).each(function (m, n) {
                    //  console.log("GridItem "+m +" : "+i)
                    //$(n).children().eq(i).css({ "width": wt, "min-width": wt });
                    $(n).css({ "width": wt - 16, "min-width": wt - 16 });
                    //$(y).find("td:first").eq(m).css({ "width": wt - THPadding, "min-width": wt - THPadding });

                });
                // $('.GridItem td', grid).eq(i).css({ "width": wt, "min-width": wt });

                TotalWt += wt;

            });

            if ($('thead:first-child', grid).length == 0) {

                $('thead:first-child', grid).remove();
                $('<thead></thead>').insertBefore($('tbody:first', grid));
                $('thead:first-child', grid).append($('.GridHeader', grid));
            }


            if ($('tfoot', grid).length == 0) {
                $('tfoot', grid).remove();
                $('<tfoot></tfooy>').insertAfter($('tbody:first', grid));
                $('tfoot', grid).append($('.GridPager', grid));
            }

            if (freezeCols != 0) {
                for (i = 0; i <= freezeCols; i++) {
                    $('tbody:first td:nth-child(' + i + ')', grid).css({ "position": "relative", "left": "0px" }).addClass("freezeCols");
                    $('thead th:nth-child(' + i + ')', grid).css({ "position": "relative", "left": "0px" }).addClass("freezeCols");
                }
            }

            $('tbody:first', grid).on("scroll", function (e) {
                $('thead:first', grid).css("left", (-1) * Number(e.currentTarget.scrollLeft));
                if (freezeCols != 0) {
                    for (i = 0; i <= freezeCols; i++) {
                        $('tbody:first td:nth-child(' + i + ')', grid).css({ "position": "relative", "left": e.currentTarget.scrollLeft }).addClass("freezeCols");
                        $('thead th:nth-child(' + i + ')', grid).css({ "position": "relative", "left": e.currentTarget.scrollLeft }).addClass("freezeCols");
                    }
                }
            });

            GridX = ($(grid).offset().left + 20);
            GridWH = Number((window.innerWidth - 12) - GridX);

            if (freezeCols >= 1) {
                $(grid).prop("width", GridWH + 'px');
                if (($(grid)[0].offsetWidth) >= TotalWt) {
                    $(grid).prop("width", (TotalWt + 20) + 'px');
                } else {
                    $(grid).prop("width", GridWH + 'px');
                }
            }




            //GridY = ($(grid).offset().top + $('thead', grid).height()) + 20;
            //GridHT = Number(window.innerHeight - 20) - GridY;



            GridY = ($(grid).position().top + $('thead', grid).height() + $('tfoot', grid).height()) + 20;
            GridHT = Number(window.innerHeight - 200) - GridY;

            GridHT = Number(GridHT <= 80 ? '100' : GridHT);

            //  $('tbody:first', grid).css({ "overflow-y": "scroll", "position": "absolute" });

            //if ($('tbody:first', grid).height() >= GridHT)
            //{
            $('tbody:first', grid).height(GridHT);
            // }

            $(window).resize(function () {
                GridWH = Number((window.innerWidth - 12) - GridX);
                $(grid).prop("width", GridWH + 'px');
                if (($(grid)[0].offsetWidth) >= TotalWt) {
                    $(grid).prop("width", (TotalWt + 20) + 'px');
                } else {
                    $(grid).prop("width", GridWH + 'px');
                }

                //GridY = ($(grid).offset().top + $('.Grid thead').height()) + 20;
                //GridHT = Number(window.innerHeight - 40) - GridY;


                GridY = ($(grid).position().top + $('thead', grid).height() + $('tfoot', grid).height()) + 20;
                GridHT = Number(window.innerHeight - 200) - GridY;

                GridHT = Number(GridHT <= 80 ? '100' : GridHT);
                //$('tbody:first', grid).css({ "overflow-y": "scroll", "position": "absolute" });
                // if ($('tbody:first', grid).height() >= GridHT) {
                $('tbody:first', grid).height(GridHT);
                // }
            });

        });//Grid

        Sys.WebForms.PageRequestManager.getInstance().remove_endRequest();
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () { SetGridColumnFreeze(); });

    } catch (er) {
        console.log(er.message);
    }
}