/*     
    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
*/

// originally from http://www.mashhoor.ws/examples/tablehighlight/

// GLOBAL VARIABLES - you should edit those only.
tableID			= 'courseGrid';
hoverColor		= '#FFFFAA';//'#BDD2EA';
highlightColor	= '#FFFFCC'; // 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	= true; // set to 'true' to enable color alternation
color1		= '#DEDEDE';
color2		= 'white';
window_onload = window.onload;	// save old value for pass-thru

// ----------------

function prepareTable()
{
//	window_onload();// call any prev onload func first
	
	// prepare the table
	if( !document.getElementById( tableID ) )	{
//		console.log( "Table ID " + tableID + " not found." );
		return;
	}
//	console.log( "Table ID " + tableID + " found." );
		
	var table = document.getElementById(tableID);
	var rows = table.getElementsByTagName('tr');
	
	for( i = 0; i < rows.length; i++ )	{
		var row = rows[ i ];
		row.backgroundColorOrig = rows[ i ].style.backgroundColor;	// save for later
//		row.onclick		= function() { setHighlight( this	); };
		row.onmouseover	= function() { setHover( this		); };
		row.onmouseout	= function() { removeHover( this	); };

		var cells = row.getElementsByTagName('td');
		for( j = 0; j < cells.length; j++ )	{
			var cell = cells[ j ];
			cell.onclick = function() { setColHighlight( this ); };
		}
	}
  
  // 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 = color1;//'transparent';
    }
    tablerows[i].on = false;
  }
}
  
// sets and removes highlights on table rows on mouse click
function setHighlight(row)	{
/*  if( !row.on )	{
    row.on = true;
    row.bg = highlightColor;
  }
  else row.on = false;*/
}

// sets and removes highlights on table columns on mouse click
function setColHighlight( cell )	{
	var colID = cell.getAttribute( "COLID" );
	var col;
	var rows = document.getElementById( tableID ).getElementsByTagName( 'tr' );
	
	for( var i = 0; i < rows.length; i++ )	{
		row = rows[ i ];
		var cols = row.getElementsByTagName( "td" );
		for( j = 0; j < cols.length; j++ )	{
			col = cols[ j ];
			if( col.getAttribute( "COLID" ) == colID )	{
				if( col.on )	{
					col.on = false;
					col.style.backgroundColor = "transparent";//row.backgroundColorOrig;
				} else {
					col.on = true;
					col.style.backgroundColor = highlightColor;
				}
			}
		}
	}
}

// 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;
	}
	else {
		row.style.backgroundColor = row.backgroundColorOrig;	//'transparent';
	}
}

window.onload = prepareTable;
