HIGHLIGHTED_ROW = null;

function highlightRow(row, branch) {
    // 'branch' is een boolean die aangeeft of de rij een branch is of niet
    row = $(row);
    var before = HIGHLIGHTED_ROW;
    var doBefore = HIGHLIGHTED_ROW && HIGHLIGHTED_ROW!=row.identify();
    if (HIGHLIGHTED_ROW && HIGHLIGHTED_ROW!=row.identify()) {
        setRowHighlight($(HIGHLIGHTED_ROW), false);
    }

    setRowHighlight(row, true, branch);
    
    HIGHLIGHTED_ROW = row.identify();
    //new Insertion.Bottom('page-body-uitleg', '|hR' + HIGHLIGHTED_ROW + ";" + before + "," + doBefore);
}

function highlightAV(row) {
    if (row.getAttribute('origBackgroundColor')===null) {
        row.setAttribute('origBackgroundColor', row.style.backgroundColor);
        row.setAttribute('origTextColor', row.style.color);
    }
    if (typeof ROW_HIGHLIGHT_COLOR != 'undefined') row.style.backgroundColor = ROW_HIGHLIGHT_COLOR;
    if (typeof ROW_TEXT_HIGHLIGHT_COLOR != 'undefined') row.style.color = ROW_TEXT_HIGHLIGHT_COLOR;
}

function unHighlightRow(row, branch) {
    // 'branch' is een boolean die aangeeft of de rij een branch is of niet
    row = $(row);
    setRowHighlight(row, false, branch);
    
    //new Insertion.Bottom('page-body-uitleg', '|uR' + HIGHLIGHTED_ROW);
    if (row.identify() == HIGHLIGHTED_ROW) {
        //new Insertion.Bottom('page-body-uitleg', ',cl');
        HIGHLIGHTED_ROW = null;
    }
}

function unHighlightAV(row) {
    row.style.backgroundColor = row.getAttribute('origBackgroundColor');
    row.style.color = row.getAttribute('origTextColor');
}

function unHighlightAll() {
    $('main').childElements().each( function(tHeadOrBody) {
        if (tHeadOrBody.tagName.toLowerCase()=="tbody") {
            tHeadOrBody.childElements().each(function(row) {
                setRowHighlight(row, false);
            });
        }
      }
    );
    HIGHLIGHTED_ROW = null;
//    $('page-body-uitleg').update('unAll HR: ' + HIGHLIGHTED_ROW);
}

function setRowHighlight(row, highlightOn, branch) {
    //new Insertion.Bottom('page-body-uitleg', '!set'+row.identify()+','+highlightOn);
    var children = row.childNodes;
    for (var i = 0; i < children.length; i++) {
        if (children[i].nodeType==1) setCellHighlight(children[i], highlightOn, branch);
    }
}

function setCellHighlight(cell, highlightOn, branch) {
    if (!isIE8 && isIE) {
        try {
            if (cell.firstChild.tagName.toUpperCase() == "DIV" && cell.parentNode.className.indexOf('leaf') == -1) {
                cell = cell.firstChild;
            }
        } catch (exception) {}
        

        if (highlightOn) {
          if (cell.getAttribute('origBackgroundColor')===null) { // IE doesn't nave hasAttribute
              // store original background color
              //new Insertion.Bottom('page-body-uitleg', '!S'+cell.style.backgroundColor);
              cell.setAttribute('origBackgroundColor', cell.style.backgroundColor);
          }
          if (typeof ROW_HIGHLIGHT_COLOR != 'undefined') cell.style.backgroundColor = ROW_HIGHLIGHT_COLOR;
          if (typeof ROW_TEXT_HIGHLIGHT_COLOR != 'undefined') cell.style.color = ROW_TEXT_HIGHLIGHT_COLOR;
        } else {
          //new Insertion.Bottom('page-body-uitleg', '!G'+cell.getAttribute('origBackgroundColor'));
          cell.style.backgroundColor = cell.getAttribute('origBackgroundColor');
          if (branch) {
              if (typeof ROW_TEXT_ORIG_COLOR != 'undefined') cell.style.color = ROW_TEXT_ORIG_COLOR;
          } else {
              if (typeof LEAF_TEXT_ORIG_COLOR != 'undefined') cell.style.color = LEAF_TEXT_ORIG_COLOR;
              else if (typeof ROW_TEXT_ORIG_COLOR != 'undefined') cell.style.color = ROW_TEXT_ORIG_COLOR;
          }
        }
    } else {
        if (highlightOn) $(cell).addClassName('highlight');
        else $(cell).removeClassName('highlight');
    }
}

