/**
*
*  Scrollable table body
*  http://www.webtoolkit.info/
*
**/

function ScrollableTable(tableId, tableHeight) {
    this.tableId = tableId;
    this.tableObject = document.getElementById(this.tableId);
    this.tableInfo = this.getTableInfo();
    this.containerObject = this.createContainer();
    this.containerObject.style.overflow = 'hidden';
    this.containerObject.style.height = '1px';
    this.tableInfo.height = tableHeight;
    this.scrollWidth = 16;

    if (document.all && document.getElementById && !window.opera)   { this.initIE(); }
    if (!document.all && document.getElementById && !window.opera)  { this.initFF(); }
}


ScrollableTable.init = function(tableID, tableHeight) {
    var table = new ScrollableTable(tableID, tableHeight);
}


ScrollableTable.prototype.getTableInfo = function () {
    var info = {};
    this.tableObject.style.width = this.tableObject.clientWidth + 'px';
    info.width = this.tableObject.offsetWidth;
    info.height = parseInt(this.tableObject.style.height);
    this.tableObject.style.height = 'auto';
    this.tableObject.removeAttribute('height');
    return info;
}


ScrollableTable.prototype.createContainer = function () {
    var c = this.tableObject.parentNode.insertBefore(document.createElement('div'), this.tableObject);
    c.appendChild(this.tableObject);
    return c;
};


ScrollableTable.prototype.initIE = function () {
    this.containerObject.style.clear = 'both';
    this.containerObject.style.height = this.tableInfo.height + 'px';
    this.containerObject.style.width = this.tableInfo.width + 'px';
    this.containerObject.style.overflowY = 'auto';
    this.tableObject.style.float = 'left';

    if (this.tableObject.parentElement.clientHeight - this.tableObject.offsetHeight < 0) {
        this.tableObject.style.width = parseInt(this.tableInfo.width) - this.scrollWidth +'px';
    } else {
        this.tableObject.style.width = parseInt(this.tableInfo.width) +'px';
    }

    var thead = this.tableObject.getElementsByTagName('thead');
    if (thead.length > 0) {
        var trs = thead[0].getElementsByTagName('tr');
        for (x=0; x<trs.length; x++) {
            trs[x].style.position ='relative';
            trs[x].style.setExpression("top",  "this.parentElement.parentElement.parentElement.scrollTop + 'px'");
        }
    }

    var tfoot = this.tableObject.getElementsByTagName('tfoot');
    if (tfoot.length > 0) {
        var trs = tfoot[0].getElementsByTagName('tr');
        for (x=0; x<trs.length; x++) {
            trs[x].style.position ='relative';
            trs[x].style.setExpression("bottom",  "(this.parentElement.parentElement.offsetHeight - this.parentElement.parentElement.parentElement.clientHeight - this.parentElement.parentElement.parentElement.scrollTop) + 'px'");
        }
    }

    eval("window.attachEvent('onresize', function () { ScrollableTable.fixIE('" + this.tableObject.id + "') } )");
}


ScrollableTable.fixIE = function(tableID) {
    var table = document.getElementById(tableID);
    if (table) {
        var thead = table.getElementsByTagName('thead');
        if (thead.length > 0) {
            var trs = thead[0].getElementsByTagName('tr');
            for (x=0; x<trs.length; x++) {
                trs[x].style.display = 'none';
                trs[x].style.display = 'block';
            }
        }
    }
}


ScrollableTable.prototype.initFF = function (tableId, params) {
    this.containerObject.style.overflow = 'hidden';
    this.containerObject.style.height = this.tableInfo.height + 'px';
    this.containerObject.style.width = this.tableInfo.width + 'px';
    this.tableObject.style.margin = '0px 0px 0px 0px';
    this.tableObject.style.width = '100%';

    var thead = this.tableObject.tHead;
    var tfoot = this.tableObject.tFoot;
    var tbody = (this.tableObject.tBodies.length > 0) ? this.tableObject.tBodies[0] : null;

    if (tbody) {

        var height = (thead) ? thead.offsetHeight : 0;
        height += (tfoot) ? tfoot.offsetHeight : 0;

        var h = 0;
        var trs = tbody.getElementsByTagName('tr');
        for (x=0; x<trs.length; x++) {
            h += trs[x].clientHeight;
        }

        if (h >= (this.tableInfo.height - height)) {
            tbody.style.overflow = '-moz-scrollbars-vertical';
            for (x=0; x<trs.length; x++) {
                var tds = trs[x].getElementsByTagName('td');
                tds[tds.length-1].style.paddingRight = this.scrollWidth + 'px';
            }
        } else {
            for (x=0; x<trs.length; x++) {
                var tds = trs[x].getElementsByTagName('td');
                tds[tds.length-1].style.paddingRight = '0px';
            }
            tbody.style.overflow = '-moz-scrollbars-none';
        }

        tbody.style.height = (this.tableInfo.height - height) + 'px';
    }

}
