// Published under the GNU General Public License v2.
// You have specific rights under this license, read
// about them at http://www.fsf.org/licensing/licenses/gpl.html
// Author: Mike Flester, http://www.flester.com/, (c) 2005

function exptables_init() {
  // find all tables with class exptable and make them expandable
  if (!document.getElementsByTagName) return;
  var tbls = document.getElementsByTagName("table");
  var numTables = tbls.length;
  for (var ti=0; ti<numTables; ti++) {
    var thisTbl = tbls[ti];
    if ((thisTbl.className.indexOf("exptable") != -1) && thisTbl.id) {
      exp_makeExptable(thisTbl);
    }
  }
}

function exp_makeExptable(tbl) {
  // look for thead
  if (tbl.tHead.rows && tbl.tHead.rows.length > 0) {
    var firstRow = tbl.tHead.rows[0];
  }
  if (!firstRow) return;
  
  // Find the expcontrol cell index and work on it
  var l = firstRow.cells.length;
  var controlCol = -1;
  for (var i=0; i<l; l++) {
    var cell = firstRow.cells[i];
    if (cell.className.indexOf("expcontrol") != -1) {
      controlCol = i;
      cell.onclick = toggleAllDetailRows;
      cell.setAttribute('exp_status','closed');
      cell.title += ' Click to open/close all detail rows';
      break;
    }
  }

  if (controlCol < 0) return;
  if (! tbl.tBodies[0].rows || tbl.tBodies[0].rows.length < 1) return;
  
  // Process each row in the table hook up classes
  // on the summary and detail rows, handlers on the 
  // control column, etc.
  var rows = tbl.tBodies[0].rows;
  var rowlen = rows.length;
  for (var i=0; i<rowlen; i++) {
    var r = rows[i];
    if (r.className.indexOf('summary') > -1) {
      setupSummaryRow(r,controlCol);
    } else if (r.className.indexOf('detail') > -1) {
      setupDetailRow(r,controlCol);
    }
  }
}

function setupDetailRow(row,cellIdx) {
  row.className += ' detailClosed';
}

function setupSummaryRow(row,cellIdx) {
  row.cells[cellIdx].className += ' expclosed';
  row.cells[cellIdx].title += ' Click to open detail rows';
  row.cells[cellIdx].onclick = openDetailRows;
}

function openDetailRows(e) {
  e = e || window.event || {};
  var tbl = getParent(this,'table');
  var row = getParent(this,'tr');
  doOpenDetailRows(tbl,row,this);
}

function doOpenDetailRows(tbl,row,cell) {
  cell.className = cell.className.replace(/expclosed/,'expopen');
  cell.title = cell.title.replace(/ open /,' close ');
  cell.onclick = closeDetailRows;
  if (tbl) {
    var rows = tbl.tBodies[0].rows;
    var rowlen = rows.length;
    var thisRowIdx = -1;
    for (var i=0; i<rowlen; i++) {
      if (thisRowIdx == -1) {
	if (row == rows[i]) {
	  thisRowIdx = i; // got my row turn it on
	}
      }
      else if (thisRowIdx > -1 &&
	       rows[i].className.indexOf('detail') > -1) {
	rows[i].className = 
	  rows[i].className.replace(/detailClose/,'detailOpen');
      } else {
	thisRowIdx = -1; // past it, turn it off
      }
    }
  }
}

function closeDetailRows(e) {
  e = e || window.event || {};
  var tbl = getParent(this,'table');
  var row = getParent(this,'tr');
  doCloseDetailRows(tbl,row,this);
}

function doCloseDetailRows(tbl,row,cell) {
  cell.className = cell.className.replace(/expopen/,'expclosed');
  cell.title = cell.title.replace(/ close /,' open ');
  cell.onclick = openDetailRows;
  if (tbl) {
    var rows = tbl.tBodies[0].rows;
    var rowlen = rows.length;
    var thisRowIdx = -1;
    for (var i=0; i<rowlen; i++) {
      if (thisRowIdx == -1) {
	if (row == rows[i]) {
	  thisRowIdx = i; // got my row turn it on
	}
      }
      else if (thisRowIdx > -1 &&
	       rows[i].className.indexOf('detail') > -1) {
	rows[i].className = 
	  rows[i].className.replace(/detailOpen/,'detailClose');
      } else {
	thisRowIdx = -1; // past it, turn it off
      }
    }
  }
}

function toggleAllDetailRows(e) {
  e = e || window.event || {};
  var tbl = getParent(this,'table');
  var status = this.getAttribute('exp_status');
  this.setAttribute('exp_status',status == 'closed' ? 'open' : 'closed');
  var rows = tbl.tBodies[0].rows;
  var rowlen = rows.length;
  for (var i=0; i<rowlen; i++) {
    if (rows[i].className.indexOf('summary') > -1) {
      if (status == 'closed') 
	doOpenDetailRows(tbl,rows[i],rows[i].cells[0]);
      else
	doCloseDetailRows(tbl,rows[i],rows[i].cells[0]);
    }
  }
}

function getParent(el,pTagName) {
  var ptag = pTagName.toLowerCase();
  if (el == null)
    return null;
  else 
    if (el.nodeType == 1 && el.tagName.toLowerCase() == ptag)
      return el;
  else 
    return getParent(el.parentNode,pTagName);
}

// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

addEvent(window,'load',exptables_init);

