// Scrollable Tables for IE and Mozilla
// License: http://www.gnu.org/licenses/gpl.txt
(function($) {
    var tbodyWidth = 0;
    $.fn.scrollable = function(options) {
        var opts = $.extend({},
        $.fn.scrollable.defaults, options);
        return this.each(function() {
            var scrolTable = jQuery(this);
            opts.tableHeight = opts.tableHeight ? opts.tableHeight: scrolTable.height();
            if (jQuery.browser.msie || jQuery.browser.safari) {
                var containerDiv = jQuery("<div></div>");
                containerDiv.insertBefore(scrolTable);
                var tableFoot = scrolTable.find("tfoot");
                var tableHead = scrolTable.find("thead");
                if (tableHead) {
                    var headerTable = scrolTable.clone(true);
                    headerTable.find("tbody").remove();
                    headerTable.find("tfoot").remove();
                    headerTable.css("margin", "0px");
                    headerTable.attr("id","headertable");
                    scrolTable.find("thead").remove();
                    containerDiv.append(headerTable)
                }
                var tableDiv = jQuery("<div></div>");
                tableDiv.append(scrolTable);
                tableDiv.width(opts.tableWidth);
                tableDiv.css("overflow", "auto");
                tableDiv.css("overflow-x", "hidden");
                tableDiv.height(opts.tableHeight);
                scrolTable.css("margin", "0px");
                scrolTable.attr("id","bodytable");
                // $.scrollable.syncWidths(containerDiv, scrolTable, opts)
                containerDiv.append(tableDiv);
            } else if (jQuery.browser.mozilla) {
                var theadHeight = scrolTable.find("thead").height() ? scrolTable.find("thead").height() : 0;
                var tfootHeight = scrolTable.find("tfoot").height() ? scrolTable.find("tfoot").height() : 0;
                var tbodyHeight = scrolTable.find("tbody").height();
                
                var calulatedHeight = opts.tableHeight - (theadHeight + tfootHeight);
                if (tbodyHeight >= calulatedHeight) {
                    scrolTable.find("tbody").css("overflow", "-moz-scrollbars-vertical")
                } else {
                    scrolTable.find("tbody").css("overflow", "-moz-scrollbars-none")
                }
                var cellSpacing = (scrolTable.attr("offsetHeight") - (tbodyHeight + theadHeight + tfootHeight)) / 4;
                var tBodyHeight = (opts.tableHeight - (theadHeight + cellSpacing * 2) - (tfootHeight + cellSpacing * 2));
                scrolTable.find("tbody").css("height", tBodyHeight + 'px');
            }
        })
    };
    $.scrollable = {
        syncWidths: function(container, table, opts) {
            if (jQuery.browser.msie || jQuery.browser.safari) {
                var tbody = container.find("tbody");
                    try {
                      tbody.css("min-width",0);
                    } catch(e) {
  					}
                    $.each(tbody.find("tr"),
                    function(i, tr) {
                        $.each(jQuery(tr).find("td"),
                        function(i, td) {
                            jQuery(td).width(opts.columnWidths[i]);
                            jQuery(td).css("width",(opts.columnWidths[i]) + "px");
                            jQuery(td).css("font-size","7pt");
                        })
                    });
                 showArray(opts.columnWidths);
             }
        }
    };
    $.fn.scrollable.defaults = {
        synchWidthsWith: "tbody",
        scrollWidth: 17
    }
})(jQuery);