//
// Javascript for Union Hall audio sermon site
//
// Find data from the mp3table and populate pulldowns.
// Then toggle CSS visibility for mp3table rows based
// on user's selection from pulldowns.
// 
// (c) Matt Blake 2007
//

// Fixme: we really ought to calculate these dynamically
// from the column headers before the thead section
var thecols = new Array();
thecols["date"]     = "0";
thecols["series"]   = "2";
thecols["bibleref"] = "3";
thecols["preacher"] = "4";

function cursor(val) {
   document.body.style.cursor=val;
}

function s(scriptref) {
   linkref = scriptref.replace(/ /g,'+');
   location.href="http://www.biblegateway.com/bible?passage="+linkref+"&amp;version=NIV-UK";
}

var highlight = 1;    // Used for subtle striped highlighting

function normal()
{
   highlight++;
   if ( highlight % 2 ) return ""; else return "hl";
}

function removefilter()
{
   // Iterate through rows, find the column and hide row unless value matches
   var mp3table = document.getElementById("mp3table");
   var rows = mp3table.getElementsByTagName("tr");
   for (p = 0; p < rows.length; p++) {
      rows[p].className = normal();               // Stripe the rows nicely
   }
}

function filter(columntag, findvalue)
{
   // Iterate through rows, find the column and hide row unless value matches
   var colnumber = thecols[columntag];
   var mp3table = document.getElementById("mp3table");
   var rows = mp3table.getElementsByTagName("tr");      // Get all rows
   for (p = 0; p < rows.length; p++) {
      var cols = rows[p].getElementsByTagName("td"); // Get columns in row
      var col = cols[colnumber];                     // Find the right column
      var coldata = "nothing";
      if (col.hasChildNodes()) {
         var fc = col.firstChild;
         coldata = fc.nodeValue;
         if (coldata == undefined && fc.hasChildNodes()) {
            coldata = fc.firstChild.nodeValue;
         }
      }

      if (coldata.indexOf(findvalue) == 0 ) {
         rows[p].className = normal();
      } else {
         rows[p].className = "hide";
      }
   }
}

function resetall()
{
   resetselectors();
   removefilter();
}

function resetselectors(field)
{
   highlight = 1;
 
   // Reset all selectors apart from this one
   var form = document.forms[0];
   for (p=0; p < form.elements.length; p++) {
      if ( form.elements[p] != form.elements[field] ) {
         form.elements[p].selectedIndex = 0;
      }
   }
}

function select(field)
{
   cursor("wait");
   resetselectors(field);   // Reset all selectors apart from this one

   var box = document.forms[0].elements[field];
   var mysel = box.selectedIndex;
   var sel = box.options[mysel].value;
   if (sel)  { filter(field, sel); }
   else      { removefilter(); }
   cursor("auto");
}

