3,6d2
< function num(el, prop) {
<   return parseInt($.css(el.jquery?el[0]:el,prop,true))||0;
< };
< 
16c12
<   // Only first time: Clone all table headers and initialize data.
---
>   var z = 0;
22,23d17
<     var z = 1;
<     table.maxCellHeight = 0;
37,39c31,32
<       // Clone and wrap cell contents in sticky wrapper that overlaps the cell's padding,
<       // and duplicates its borders exactly.
<       $('<div class="sticky-header" style="position: fixed; visibility: hidden; top: 0px; margin: 0; padding: 0">'+ html +'</div>').prependTo(this);
---
>       // Clone and wrap cell contents in sticky wrapper that overlaps the cell's padding.
>       $('<div class="sticky-header" style="position: fixed; visibility: hidden; top: 0px;">'+ html +'</div>').prependTo(this);
41,52c34,38
<         'borderLeftWidth': num($(this), 'borderTopWidth'),
<         'borderLeftColor': $(this).css('borderLeftColor'),
<         'borderLeftStyle': $(this).css('borderLeftStyle'),
<         'borderRightWidth': num($(this), 'borderRightWidth'),
<         'borderRightColor': $(this).css('borderRightColor'),
<         'borderRightStyle': $(this).css('borderRightStyle'),
<         'borderTopWidth': num($(this), 'borderTopWidth'),
<         'borderTopColor': $(this).css('borderTopColor'),
<         'borderTopStyle': $(this).css('borderTopStyle'),
<         'borderBottomWidth': num($(this), 'borderBottomWidth'),
<         'borderBottomColor': $(this).css('borderBottomColor'),
<         'borderBottomStyle': $(this).css('borderBottomStyle'),
---
>         'marginLeft': '-'+ $(this).css('paddingLeft'),
>         'marginRight': '-'+ $(this).css('paddingRight'),
>         'paddingLeft': $(this).css('paddingLeft'),
>         'paddingTop': $(this).css('paddingTop'),
>         'paddingBottom': $(this).css('paddingBottom'),
57,93c43,44
<       // Prepare various dimensions for the cell
<       var paddingL = num($(this), 'paddingLeft');
<       var paddingR = num($(this), 'paddingRight');
<       var paddingT = num($(this), 'paddingTop');
<       var paddingB = num($(this), 'paddingBottom');
<       var scrollH = document.documentElement.scrollLeft || document.body.scrollLeft;
<       div.addV = paddingB + paddingT + num($(this), 'borderTopWidth') + num($(this), 'borderBottomWidth');
<       if ($(this).css('borderCollapse') == 'collapse') {
<         div.addH = paddingL + paddingR + num($(this), 'borderRightWidth');
<         div.cellOffset = num($(this), 'borderLeftWidth') + num($(table), 'borderLeftWidth');
<       }
<       else {
<         div.addH = paddingL + paddingR;
<         div.cellOffset = num($(this), 'borderLeftWidth');
<       }
< 
<       // Note that height() and width() are not used, because they sometimes
<       // return distorted values.
<       div.cellWidth = num($(this), 'width') + div.addH;
<       div.cellLeft = $(this).offset().left - div.cellOffset - scrollH;
< 
<       // Keep track of biggest cell-height.
<       var cellHeight = num($(this), 'height'); //  - div.addV;
<       if (table.maxCellHeight < cellHeight) {
<         table.maxCellHeight = cellHeight;
<       }
< 
<       // Wrap the contents in another wrapper, to re-introduce its padding.
<       $(div).wrapInner('<div style="border: 0; margin: 0;"></div>');
<       $(div).children().css({
<         'paddingLeft': paddingL +'px',
<         'paddingRight': paddingR +'px',
<         'paddingTop': paddingT +'px',
<         'paddingBottom': paddingB +'px'
<       })[0];
< 
<       // Remember first/last cell.
---
>       // Adjust width to fit cell/table.
>       var ref = this;
95c46,48
<         table.first = div;
---
>         // The first cell is as wide as the table to prevent gaps.
>         ref = table;
>         div.wide = true;
97c50
<       table.last = div;
---
>       $(div).width(Math.max(0, $(ref).width() - parseInt($(div).css('paddingLeft'))));
99c52
<       // Store data for later updates.
---
>       // Get position and store.
104,113d56
<       div.savedScrollH = scrollH;
<     });
< 
<     $('div.sticky-header', this).each(function () {
<       // Set position and size of the cloned cell.
<       $(this).css({
<         'left': this.cellLeft +'px',
<         'width': this.cellWidth +'px',
<         'height': table.maxCellHeight +'px'
<       })[0];
115,146d57
< 
<     // Adjust first/last cell order for RTL.
<     if ($(table).css('direction') == 'rtl') {
<       var temp = table.first;
<       table.first = table.last;
<       table.last = temp;
<     }
< 
<     // Wrap the contents of leftmost cell into yet another sticky wrapper, to
<     // ensure correct position and alignment, and make the outer wrapper as
<     // wide as the table, to serve as background, masking any gaps between
<     // cloned cells. Recreate the right border of original cell on the inner
<     // wrapper.
<     table.first.innerOffset = num($(table.first), 'borderLeftWidth');
<     $(table.first).wrapInner('<div style="position: fixed; margin: 0; padding: 0; border-top: 0; border-bottom: 0; border-left: 0;"></div>');
<     $(table.first).children('div').css({
<       'z-index': parseInt($(table.first).css('z-index')),
<       'borderRightWidth': num($(table.first), 'borderRightWidth'),
<       'borderRightStyle': $(table.first).css('borderRightStyle'),
<       'borderRightColor': $(table.first).css('borderRightColor'),
<       'top': num($(table.first), 'borderTopWidth') +'px',
<       'left': (table.first.cellLeft + table.first.innerOffset) +'px',
<       'width': table.first.cellWidth +'px',
<       'height': table.maxCellHeight +'px'
<     })[0];
< 
<     $(table.first).css({
<       'z-index': 1,
<       'width': (table.last.cellLeft - table.first.cellLeft + table.last.cellWidth) +'px'
<     })[0];
<     table.first.wide = true;
< 
153c64
<       // Vertical.
---
>       // Fetch scrolling position.
156c67
<       if (offset > 0 && offset < this.stickyMax - 50) {
---
>       if (offset > 0 && offset < this.stickyMax - 100) {
162,171d72
<       // Horizontal.
<       var scrollH = document.documentElement.scrollLeft || document.body.scrollLeft;
<       if (this.savedScrollH != scrollH) {
<         this.savedScrollH = scrollH;
<         var cellLeft = $(this.cell).offset().left - this.cellOffset - scrollH;
<         if (this.wide) {
<           $(this).children('div').css('left', (cellLeft + this.innerOffset) +'px');
<         }
<         $(this).css('left', cellLeft +'px');
<       }
186c87
<       // Precalculate table heights.
---
>       // Precalculate table heights
189,224d89
< 
<         // Find the biggest cell-height.
<         this.maxCellHeight = 0;
<         $('div.sticky-header', this).each(function () {
<           var cellHeight = num($(this.cell), 'height') + this.addV;
<           if (this.table.maxCellHeight < cellHeight) {
<             this.table.maxCellHeight = cellHeight;
<           }
<         });
< 
<         $('div.sticky-header', this).each(function () {
<           // Get position.
<           this.stickyPosition = $(this.cell).offset().top;
<           this.stickyMax = this.table.savedHeight;
<           var scrollH = document.documentElement.scrollLeft || document.body.scrollLeft;
<           this.cellWidth = num($(this.cell), 'width') + this.addH;
<           this.cellLeft = $(this.cell).offset().left - this.cellOffset - scrollH;
< 
<           // Reflow the cell.
<           $(this).css({
<             'left': this.cellLeft +'px',
<             'width': this.cellWidth +'px',
<             'height': this.table.maxCellHeight +'px'
<           })[0];
<         });
< 
<         // Update the special leftmost cell.
<         $(this.first).children('div').css({
<           'left': (this.first.cellLeft + this.first.innerOffset) +'px',
<           'width': this.first.cellWidth +'px',
<           'height': this.maxCellHeight +'px'
<         })[0];
< 
<         $(this.first).css({
<           'width': (this.last.cellLeft - this.first.cellLeft + this.last.cellWidth) +'px'
<         })[0];
227,228c92,104
<       // Resizing may change position of the tables, so update visibility.
<       $(scroll);
---
>       $('table.sticky-table div.sticky-header').each(function () {
>         // Get position.
>         this.stickyPosition = $(this.cell).offset().top;
>         this.stickyMax = this.table.savedHeight;
> 
>         // Reflow the cell.
>         var ref = this.cell;
>         if (this.wide) {
>           // Resize the first cell to fit the table.
>           ref = this.table;
>         }
>         $(this).width(Math.max(0, $(ref).width() - parseInt($(this).css('paddingLeft'))));
>       });
