//copyright 2020 @FinIQ Consulting India Pvt. Ltd.
//Developed by Shekhar L. @Aug 2020.
//<link href="../FinIQJS/FreezeGrid.css" type="text/css" rel="stylesheet">
//<script src="../FinIQJS/FreezeGrid.js" type="text/javascript"></script> 
 
var GridX, GridY = 0;
var freezeCols = 0;
var tdWidth = [];

var MenuHT = 0, ht = 0, left = 0, rowspan = 0, TotalWt = 0;

(function SetGridColumnFreeze() {
    $(document).ready(function () {
        try {
            //console.log("SetGridColumnFreeze");
            GetMenuHeihgt();
            freezeCols = Number(freezeCols);

            TotalWt = 0;

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

                $(grid).closest('div').css("display", "inline-block !important");

                $(grid).addClass('frozenGrid');

                if (tdWidth.length != $('.GridItem:first > td, .GridItemSelect:first > td, tr.AlternatItemStyle > td', grid).length) {
                    tdWidth = [];
                }


                if (tdWidth.length != $('.GridItem:first > td, .GridItemSelect:first > td, tr.AlternatItemStyle > td', grid).length) {
                    tdWidth = [];
                }

                var widthTD, widthTH, maxWidth;


                $('tr.GridHeader:first > th, tr:first > td', grid).each(function (i, elementTH) {

                    widthTD = widthTH = parseInt($(elementTH)[0].offsetWidth);
                    // widthTD = parseInt($('tr.GridItem:first > td:nth-child(' + (i + 1) + '), tr.AlternatItemStyle:first > td:nth-child(' + (i + 1) + ')', grid)[0].offsetWidth) - 1;

                    pl = parseInt($(elementTH).css("padding-left"));
                    pr = parseInt($(elementTH).css("padding-right"));

                    if (pl != pr) {
                        widthTH = widthTH - (pr + pl);
                        widthTD = widthTH + ((pr - pl));
                    } else {
                        widthTH = widthTH - (pl + pr);
                        widthTD = widthTD - (pl + pr);
                    }

                    $(elementTH).css({ "width": widthTH + 'px', "min-width": widthTH + 'px' });

                    // console.log($('tr.GridItem > td, tr.AlternatItemStyle > td, .GridItemSelect:first > td', grid).attr('rowspan'));
                    // console.log(i+" : "+ widthTD +" : "+ widthTH);
                    if ($('tr.GridItem > td, tr.AlternatItemStyle > td, .GridItemSelect:first > td', grid).attr('rowspan') == undefined) {
                        $('tr.GridItem > td:nth-child(' + (i + 1) + '), tr.GridItemSelect > td:nth-child(' + (i + 1) + '), tr.AlternatItemStyle > td:nth-child(' + (i + 1) + ')', grid).css({ "width": (widthTD) + 'px', "min-width": (widthTD) + 'px', "position": "" });
                    }
                    else {
                        $('tr.GridItem:first > td:nth-child(' + (i + 1) + '), tr.GridItemSelect:first > td:nth-child(' + (i + 1) + '), tr.AlternatItemStyle > td:nth-child(' + (i + 1) + ')', grid).css({ "width": (widthTD) + 'px', "min-width": (widthTD) + 'px', "position": "" });
                    }
                    tdWidth.push(widthTH);
                });

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

                if ($('tfoot', grid).length == 0) {
                    $('tfoot', grid).remove();
                    $('<tfoot></tfooy>').insertAfter($('tbody:first', grid));
                    $('tfoot', grid).append($('.GridPager:first', 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 + '), thead > td:nth-child(' + i + ')', grid).css({ "position": "relative", "left": "0px" }).addClass("freezeCols");
                    }
                }

                if (window.self != window.top) {
                    $('tbody:first', grid).height(parseInt(window.innerHeight) - (parseInt($('thead:first', grid)[0].offsetHeight) + parseInt($('tfoot:first', grid)[0].offsetHeight)));
                }

                $('tbody:first', grid).on("scroll", function (event) {
                    try{
                        event = event || window.event;
                       
                        $('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": event.currentTarget.scrollLeft }).addClass("freezeCols");
                                $('thead > th:nth-child(' + i + '), thead > td:nth-child(' + i + ')', grid).css({ "position": "relative", "left": event.currentTarget.scrollLeft }).addClass("freezeCols");
                            }
                        }
                       
                        event.target.dispatchEvent(event2);
                        event.stopPropagation();
                    } catch (er) { }
                    finally {
                        event = null;
                        window.event = null;
                        return false;
                    }
                });
               
                window.addEventListener('scroll', windowOnScroll, true);
            /*Added by Shekhar & Arati K. @ 08-Mar-2021 for empty grid "no data found" nowrap */
 if($('thead:first', grid).find('tr').length<=0)
 {
	 $(grid).removeClass('frozenGrid').addClass('noFreeze');
 }


            });//Grid

            Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(SetGridColumnFreeze);
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(SetGridColumnFreeze);

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

function GetMenuHeihgt() {
    try {
        // $(document).ready(function () {
        if ($('.Menu_FormName', document).length > 0) {
            MenuHT = parseInt($('.Menu_FormName', document)[0].offsetHeight);
            $('.menuOverlay').css({ "width": parseInt($(document).width()) + "px", "height": (MenuHT + "px"), "position": "fixed", "z-index": "-1", "left": "0px", "top": "0px", "background": $('.FinIQ_MenuBG').css("background"), "background-color": $('.FinIQ_MenuBG').css("background-color") });
            $('.menuOverlay').show();
        } else {
            if ($('.frozenGrid').length > 0) {
                MenuHT = 0;
                $('.menuOverlay').hide();
            }
        }
        if (window.location.href.indexOf("Master_Popup") > -1) {
            $(".menuOverlay").css("display", "none");
            $('.menuOverlay').hide();
        } else {
            $('.menuOverlay').show();
        }
        return;
        // });
    } catch (er) {
        console.log(er.message);
    }
}

function windowOnScroll(event) {
    try { 
        event = event || window.event;
        if (event != undefined) {
            if (event.stopPropagation) {
                event.stopPropagation();   // W3C model
            } else {
                event.cancelBubble = true; // IE model
            }
        }
        window.removeEventListener('scroll', windowOnScroll); //move by Shekhar @10-Feb-2021 from line-112 to here 
        if ($('.frozenGrid').length > 0) {
            if ($('.Menu_FormName', document).length > 0) {
                if (MenuHT != parseInt($('.Menu_FormName', document)[0].offsetHeight)) {
                    MenuHT = parseInt($('.Menu_FormName', document)[0].offsetHeight);
                    GetMenuHeihgt();
                }
            }
            left = parseInt($('.frozenGrid').offset().left);
            ht = (MenuHT + parseInt($('.frozenGrid > thead:first').height()));

            availHT = screen.availHeight - ht;

            $('.frozenGrid > tbody:first').css({ "height": "auto" });
            $('.frozenGrid > thead:first').css({ "position": "relative", "top": "", "left": "" });

            if ($('.frozenGrid').offset().top < ($(window).scrollTop() + MenuHT)) {
                if ($('.FinIQ_lblFormName:not(:empty)').css('position') == 'fixed') {
                    $('.frozenGrid > thead:first').css({ "position": "fixed", "top": MenuHT + parseInt($('.FinIQ_lblFormName:not(:empty)', document)[0].offsetHeight) + "px", "left": parseInt($('.frozenGrid').offset().left+1) - parseInt($(window).scrollLeft()) });
                }
                else {
                    $('.frozenGrid > thead:first').css({ "position": "fixed", "top": MenuHT + "px", "left": parseInt($('.frozenGrid').offset().left) - parseInt($(window).scrollLeft()) });
                }
            }
        }
        //+ parseInt($('.FinIQ_lblFormName:not(:empty)', document)[0].offsetHeight)

        return;       
         
    } catch (er) {
        console.log(er.message);
    } finally { if (event != undefined) { event.preventDefault ? event.preventDefault() : (event.returnValue = false); } }
}


// console.clear()
//  console.log($('body')[0].scrollTop + " : " + $('body')[0].scrollHeight + " : " + $('body')[0].clientHeight);
//  console.log($('.frozenGrid').offset().top + " : " + $(window).scrollTop());