//
// Javascript for Eden's MP3 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) Jonathan Thackray 2006
//

// Fixme: we really ought to calculate these dynamically
// from the column headers before the thead section
var thecols = new Array();
thecols["dateselect"]     = "0";
thecols["seriesselect"]   = "2";
thecols["biblerefselect"] = "3";
thecols["prselect"]       = "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+"&version=64";
}

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

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

function removefilter()
{
   // Iterate through rows, and make them all visible
   var allrows = document.getElementById("mp3table").rows;
   for (p = 0; p < allrows.length; p++) {
      allrows[p].className = normal();
   }
}

function filter(columntag, findvalue, firstnmatches)
{
   // Iterate through rows, find the column and hide row unless value matches
   var allrows = document.getElementById("mp3table").rows;
   var colnumber = thecols[columntag];
   var found = 0;
   for (p = 0; p < allrows.length; p++) {
      var coldata = "nothing";
      var col = allrows[p].cells[colnumber];    // Find the right column
      if (col.hasChildNodes()) {                // Data could be in <a href> inside <td>
         var fc = col.firstChild;
         coldata = fc.nodeValue;
         if (coldata == undefined && fc.hasChildNodes()) {
            coldata = fc.firstChild.nodeValue;
         }
      }

      if (found < firstnmatches &&
          coldata.indexOf(findvalue) == 0) {    // Find value, matching from start
         allrows[p].className = normal();       // Stripe the rows nicely
         found++;
      } else {
         allrows[p].className = "hide";         // Hide results that don't match
      }
   }
   setfound(found);
}

function setfound(found)
{
   var foundtext = document.getElementById("foundtext");
   if (found > 0) {
     foundtext.innerHTML = "Found " + found + " results";
   } else {
     foundtext.innerHTML = "&nbsp;";
   }
}

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

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(id)
{
   cursor("wait");
   resetselectors(id);   // Reset all selectors apart from this one

   var selector = document.getElementById(id);
   var selected = selector.selectedIndex;
   var curvalue = selector.options[selected].value;

   if (curvalue)  { filter(id, curvalue, 9999); }
   else           { removefilter(); }
   cursor("auto");
}

