﻿// Handler for the onMouseOut event for a CrosshairGridView Cell
// It reverts highlighted cells back to their normal color
function cgvMouseOut(cell, rowColor, alternatingRowColor)
{
   var columnIndex = cgvGetColumnIndex(cell);

   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;
   
   // Iterate over all the rows.  For most rows, we just have to switch the color
   // of the cell in the highlighted column.  But for the row containing the current
   // cell, we have to switch the color of all the cells in the row
   var rowNum = 0;
   for(var i = 0; i < rows.length; i++)
   {
      var row2 = rows[i];
      
      // Skip any pager header row(s)
      if (row2.className == null || (row2.className.indexOf("pager_row") < 0)) {
      
         // Choose the normal color (regular or alternating)
         var color = (rowNum%2 == 0) ? alternatingRowColor : rowColor;
         
         // Is this the highlighted row?
         if(row2 == row)
         {
            var cells = row.childNodes;
            
            //  For the highlighted row, switch every cell back
            for(var j = 0; j < cells.length; j++)
            {
               if(cells[j] && (cells[j].nodeName == 'TD'))
               {
                  cells[j].style.backgroundColor = color;
               }
            }
         }
         else
         {
            // For the non-highlighted row, we only have to switch the cell in highlighted column
            var highlightCell = row2.childNodes[columnIndex];
            if(highlightCell && (highlightCell.nodeName == 'TD'))
            {
               highlightCell.style.backgroundColor = color;
            }
         }

         rowNum++;
      }
   }
}

// Handler for the onMouseOver event for a CrosshairGridView Cell
// It highlights the row and column of the current cell.  The cell itself
// gets the bullseye color
function cgvMouseOver(cell, crosshairColor, bullseyeColor)
{
   var columnIndex = cgvGetColumnIndex(cell);

   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;

   // Iterate over all the rows.  For most rows, we just have to switch the color
   // of the cell in the highlighted column.  But for the row containing the current
   // cell, we have to switch the color of all the cells in the row
   for(var i = 0; i < rows.length; i++)
   {
      var row2 = rows[i];  

      // Is this the highlighted row?
      if(row2 == row)
      {
         //  For the highlighted row, highlight every cell
         var cells = row.childNodes;
         for(var j = 0; j < cells.length; j++)
         {
            if(cells[j] && (cells[j].nodeName == 'TD'))
            {
               cells[j].style.backgroundColor = crosshairColor;
            }
         }
      }
      else
      {
         // For the non-highlighted row, only highlight the cell in highlighted column
         var highlightCell = row2.childNodes[columnIndex];
         if(highlightCell && (highlightCell.nodeName == 'TD'))
         {
            highlightCell.style.backgroundColor = crosshairColor;
         }
      }
   }
   cell.style.backgroundColor = bullseyeColor;
}

// Handler for the onMouseOut event for a CrosshairGridView Label Cell
// These cells only highlight rows, not columns.  And, they don't use
// the bullseye color
function cgvLabelMouseOut(cell, rowColor, alternatingRowColor)
{
   var columnIndex = cgvGetColumnIndex(cell);

   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;

   // Iterate over all the rows.  For most rows, we just have to switch the color
   // of the cell in the highlighted column.  But for the row containing the current
   // cell, we have to switch the color of all the cells in the row
   var rowNum = 0;
   for(var i = 0; i < rows.length; i++)
   {
      var row2 = rows[i];
      
      // Skip any pager header row(s)
      if (row2.className == null || (row2.className.indexOf("pager_row") < 0)) {

         // Is this the highlighted row?
         if(row2 == row)
         {
            // Choose the normal color (regular or alternating)
            var color = (rowNum%2 == 0) ? alternatingRowColor : rowColor;

            var cells = row.childNodes;

            //  For the highlighted row, switch every cell back
            for(var j = 0; j < cells.length; j++)
            {
               if(cells[j] && (cells[j].nodeName == 'TD'))
               {
                  cells[j].style.backgroundColor = color;
               }
            }
            break;
         }

         rowNum++;
      }
   }
}

// Handler for the onMouseOver event for a CrosshairGridView Label Cell
// These cells only highlight rows, not columns.  And, they don't use
// the bullseye color
function cgvLabelMouseOver(cell, crosshairColor)
{
   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;

   //  For the highlighted row, highlight every cell
   var cells = row.childNodes;
   for(var j = 0; j < cells.length; j++)
   {
      if(cells[j] && (cells[j].nodeName == 'TD'))
      {
         cells[j].style.backgroundColor = crosshairColor;
      }
   }
}

// Utility function to find the column index of a cell
function cgvGetColumnIndex(cell)
{
   var row = cell.parentNode;
   var cellsInRow = row.childNodes
   for(var i = 0; i < cellsInRow.length; i++)
   {
      if(cellsInRow[i] == cell)
      {
         return i;
      }
   }
}

