/************************************************************************************************************
(C) www.dhtmlgoodies.com, November 2005
  
This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.  
  
Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.
  
Thank you!
  
www.dhtmlgoodies.com
Alf Magne Kalleland
  
// amended to reference all tables by class rather than specific ids.
// amended to validate against JSLint
// amended to include id="NoSelect" means only roll over states applied
  
************************************************************************************************************/

/*global document: false */

var arrayOfRolloverClasses = [];
var arrayOfClickClasses = [];
var activeRow = false;
var activeRowClickArray = [];

function highlightTableRow() {
    var tableObj = this.parentNode;
    if (tableObj.tagName != 'table') { tableObj = tableObj.parentNode; }

    if (this != activeRow) {
        this.setAttribute('origCl', this.className);
        this.origCl = this.className;
    }
    this.className = arrayOfRolloverClasses[tableObj.className];

    activeRow = this;

}

function clickOnTableRow() {
    var tableObj = this.parentNode;
    if (tableObj.tagName != 'table') { tableObj = tableObj.parentNode; }

    if (activeRowClickArray[tableObj.className] && this != activeRowClickArray[tableObj.className]) {
        activeRowClickArray[tableObj.className].className = '';
    }
    this.className = arrayOfClickClasses[tableObj.className];

    activeRowClickArray[tableObj.className] = this;

}

function resetRowStyle() {
    var tableObj = this.parentNode;
    if (tableObj.tagName != 'table') { tableObj = tableObj.parentNode; }

    if (activeRowClickArray[tableObj.className] && this == activeRowClickArray[tableObj.className]) {
        this.className = arrayOfClickClasses[tableObj.className];
        return;
    }

    var origCl = this.getAttribute('origCl');
    if (!origCl) { origCl = this.origCl; }
    this.className = origCl;

}

function addTableRolloverEffect(tableClass) {
    arrayOfRolloverClasses[tableClass] = 'RollOverEffect';
    arrayOfClickClasses[tableClass] = 'RowClickEffect';

    //var tableObj = document.getElementById(tableId);
    var tableObjs = document.getElementsByTagName("table");

    for (var i = 0; i < tableObjs.length; i++) {

        var tableObj = tableObjs[i];


        if (tableObj.className == tableClass) {
            var tBody = tableObj.getElementsByTagName('tbody');
            var rows;
            if (tBody.length > 0) {
                rows = tBody[0].getElementsByTagName('tr');
            } else {
                rows = tableObj.getElementsByTagName('tr');
            }
            for (var no = 0; no < rows.length; no++) {

                if (rows[no].getElementsByTagName('th').length === 0) {

                    rows[no].onmouseover = highlightTableRow;
                    rows[no].onmouseout = resetRowStyle;

                    if (tableObj.id != "NoSelect") {
                        rows[no].onclick = clickOnTableRow;
                    }
                }
            }
        }
    }

}

addTableRolloverEffect("TableEffects");
