﻿function HighlightTable()
{
    // default values
    this.m_className = "highlightTable";
    this.m_rowHighlight = "#f1f6f9";
    this.m_colHighlight = "#e2ecf2";
    this.m_highlightHeaders = true;
    // accessor methods
    HighlightTable.prototype.getClassname = function()
    {
        return this.m_className;
    }
    HighlightTable.prototype.setClassName = function( className )
    {
        this.m_className = className;
    }
    HighlightTable.prototype.getRowHighlight = function()
    {
        return this.m_rowHighlight;
    }
    HighlightTable.prototype.setRowHighlight = function( rowHighlight )
    {
        this.m_rowHighlight = rowHighlight;
    }
    HighlightTable.prototype.getColHighlight = function()
    {
        return this.m_colHighlight;
    }
    HighlightTable.prototype.setColHighlight = function( colHighlight )
    {
        this.m_colHighlight = colHighlight;
    }
    HighlightTable.prototype.getHighlightHeaders = function()
    {
        return this.m_highlightHeaders;
    }
    HighlightTable.prototype.setHighlightHeaders = function( highlightHeaders )
    {
        this.m_highlightHeaders = highlightHeaders;
    }
    // functional methods
    HighlightTable.prototype.highlightTables = function() {
        if(!document.getElementsByTagName)
            return;
        var tables = this.getElementsByClass(this.m_className, document, "table")
        for (var i = 0; i < tables.length; i++) {
            var tableId = this.m_className + ( i + 1 );
            var table = tables[i];
            var row_count = table.rows.length;
            for (var j = 0; j < row_count; j++) {
                var row = table.rows[j];
                if (row.id == "no-highlighting") continue;
                if (row.id == "no-highlighting-new-table") {
                    i++;
                    continue
                }
                row.id = "tb_" + tableId + "_row_" + (j + 1) + "_";
                var col_count = row.cells.length;
                for (var k = 0; k < row.cells.length; k++) {
                    var cell = row.cells[k];
                    if (cell.getAttribute("colspan") != null)
                        col_count = col_count + (cell.getAttribute("colspan") - 1);
                }
            var col_offset = 0;
            for (var k = 0; k < row.cells.length; k++) {
                var cell = row.cells[k];
                if (!this.getHighlightHeaders() && cell.tagName.toUpperCase() == "TH") continue;
                if (cell.id == "no-highlighting") continue;
                if (cell.getAttribute("scope") == 2) {
                    cell.id = "tb_" + (i + 1) + "_row_" + (j + 1) + "_col_" + ((k + 1) + col_offset) + "_ tb_" + tableId + "_row_" + (j + 1) + "_col_" + (((k + 1) + col_offset) + 1) + "_";
                    cell.onmouseover = createInstanceEvent( this, "highlightMore", (j + 1), ( (k + 1) + col_offset ), tableId, row_count, col_count, true, "2", null );
                    cell.onmouseout  = createInstanceEvent( this, "highlightMore", (j + 1), ( (k + 1) + col_offset ), tableId, row_count, col_count, false, "2", null );
                } else {
                    if (cell.getAttribute("colspan") != null) {
                        var ids = "";
                        if (cell.getAttribute("colspan") == null)
                            colspaner = 1;
                        else
                            colspaner = cell.getAttribute("colspan");
                        for (z = 0; z < colspaner; z++)
                            ids = ids + " tb_" + tableId + "_row_" + (j + 1) + "_col_" + (((k + 1) + col_offset) + z) + "_";
                        cell.id = ids;
                        cell.onmouseover = createInstanceEvent( this, "highlightMore", (j + 1), ( (k + 1) + col_offset ), tableId, row_count, col_count, true, colspaner );
                        cell.onmouseout  = createInstanceEvent( this, "highlightMore", (j + 1), ( (k + 1) + col_offset ), tableId, row_count, col_count, false, colspaner );
                    } else {
                        cell.id = "tb_" + tableId + "_row_" + (j + 1) + "_col_" + ((k + 1) + col_offset) + "_";
                        cell.onmouseover = createInstanceEvent( this, "highlight", (j + 1), ((k + 1) + col_offset), tableId, row_count, col_count, true  );
                        cell.onmouseout  = createInstanceEvent( this, "highlight", (j + 1), ((k + 1) + col_offset), tableId, row_count, col_count, false );
                    }
                }
                if (cell.getAttribute("colspan") != null)
                    col_offset = col_offset + (cell.getAttribute("colspan") - 1);
                }
            }
        }
    }
    HighlightTable.prototype.highlightMore = function(row_id, column_id, table_id, number_of_rows, number_of_columns, isHighlight, cols, rows) {
        for (x = 0; x < parseInt(cols); x++) {
            c_id = parseInt(column_id) + x;
            this.highlight(row_id, c_id, table_id, number_of_rows, number_of_columns, isHighlight);
        }
    }
    HighlightTable.prototype.highlight = function(row_id, column_id, table_id, number_of_rows, number_of_columns, isHighlight) {
        var table = ( table_id ? "tb_" + table_id + "_" : '' );
        temp_row = document.getElementById(table + "row_" + row_id + "_");
        for (i = 1; i <= number_of_columns; i++) {
            for (z = 0; z < temp_row.cells.length; z++) {
                var tmpCell = temp_row.cells[z];
                if (tmpCell.id.search(table + "row_" + row_id + "_col_" + i + "_") != -1) {
                    if( isHighlight ) {
                        this.setCellColour(tmpCell, this.getRowHighlight());
                    } else {
                        this.restoreCellColour(tmpCell);
                    }
                }
            }
        }
        for (i = 1; i <= number_of_rows; i++) {
            if (i != row_id) {
                temp_row = document.getElementById(table + "row_" + i + "_");
                if (temp_row != null) {
                    for (z = 0; z < temp_row.cells.length; z++) {
                        var tmpCell = temp_row.cells[z];
                        if (tmpCell.id.search(table + "row_" + i + "_col_" + column_id + "_") != -1) {
                            if( isHighlight ) {
                                this.setCellColour(tmpCell, this.getColHighlight());
                            } else {
                                this.restoreCellColour(tmpCell);
                            }
                        }
                    }
                }
            }
        }
    }
    HighlightTable.prototype.setCellColour = function(cell, colour) {
        cell["_originalBackgroundColor"] = cell.style.backgroundColor;
        cell.style.backgroundColor = colour;
    }
    HighlightTable.prototype.restoreCellColour = function(cell) {
        cell.style.backgroundColor = cell["_originalBackgroundColor"];
        cell["_originalBackgroundColor"] = null;
    }
    HighlightTable.prototype.getElementsByClass = function(searchClass, node, tag) {
        var classElements = new Array();
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
            if (pattern.test(els[i].className)) {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
    function createInstanceEvent(instance, methodName, row_id, column_id, table_id, number_of_rows, number_of_columns, isHighlight, cols, rows)
    {
        return( 
            function(e) {
                return instance[methodName](row_id, column_id, table_id, number_of_rows, number_of_columns, isHighlight, cols, rows);
            }
        );
    }
}
