/*     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version. 
    
    For a full copy of the license visit the GNU GPL website
    http://www.gnu.org/copyleft/gpl.html
    
    Copyright 2006 Mashhoor Al Dubayan
*/


// GLOBAL VARIABLES - you should edit those only.
tableID = 'sample';
hoverColor = '#BDD2EA';
highlightColor = 'yellow'; // when a row is clicked
clear_linkID = 'clearall'; // the link that, when clicked, clears all highlights
colorsDivID = 'colors'; // the DIV that containts all color links

alternate = false; // set to 'true' to enable color alternation
color1 = '#DEDEDE';
color2 = 'white';
// ----------------

function prepareTable()
{

  // prepare the table
  if(document.getElementById(tableID))
  {
    var table = document.getElementById(tableID);
    var rows = table.getElementsByTagName('tr');

    for(i=0; i<rows.length; i++)
    {
      rows[i].onclick = function() { setHighlight(this); };
      rows[i].onmouseover = function() {setHover(this); };
      rows[i].onmouseout = function() { removeHover(this); };
    }
  } else {
    return;
  }
  
  // setup the 'clear highlights' link
  if(document.getElementById(clear_linkID))
  {
    var clearlink = document.getElementById(clear_linkID);
    clearlink.onclick = function() { clearAll(); return false;};
  }
  
  // setup colors
  if(document.getElementById(colorsDivID))
  {
    var colorsdiv = document.getElementById(colorsDivID);
    var colorlinks = colorsdiv.getElementsByTagName('a');
    
    for(var i=0; i<colorlinks.length; i++)
      colorlinks[i].onclick = function() { highlightColor = this.getAttribute('title'); return false; };
  } else {
    return;
  }
  
  alternateRows(rows);
}

function alternateRows(rows)
{
  if(alternate)
  { 
    for(var i=0; i < rows.length; i++)
    {
      rows[i].id = i;
      if(i%2 == 0)
      {
        rows[i].style.backgroundColor = color2;
        continue;
      }
      rows[i].style.backgroundColor = color1;
    }
  }
}
      
    

// clears all highlights
function clearAll()
{
  var tablerows = document.getElementById(tableID).getElementsByTagName('tr');
  for(var i=0; i<tablerows.length; i++)
  {
    if(alternate) 
    {
      alternateRows(tablerows);
    } else {
    tablerows[i].style.backgroundColor = 'transparent';
    }
    tablerows[i].on = false;
  }
}
  
// sets and removes highlights on table rows on mouse click
function setHighlight(row)
{
  if(row.on != true)
  {
    row.on = true;
    row.bg = highlightColor;
    return;
  }
  
  row.on = false;
}


// hilights a row when the mouse pointer hovers over it
function setHover(row)
{
    row.style.backgroundColor = hoverColor;
}

// reset the row's background color when the mouse pointer hovers out
function removeHover(row)
{
  if(alternate)
  {
    row.id % 2 == 0 ? row.style.backgroundColor = color2: row.style.backgroundColor = color1;
  } else {
    row.style.backgroundColor = 'transparent';
  }
    
  if(row.on == true)
    row.style.backgroundColor = row.bg;
}

window.onload = prepareTable;

