// For these functions to work, there must be an empty summary-class DIV
// inside the very end of each collapse-class DIV.  These functions
// simply alternately toggle the display attribute of these two
// classes of DIV tags between 'inline' and 'none'.  The getscore
// function populates the text of the summary-class DIV, returning "No
// match held." if it can't parse the score from the table in the
// collapse-class DIV.

var aDiv = new Array();

// This function must be run in the pages onLoad event in order to
// a) create the buttons for the characters and
// b) populate the aSpans array for use by the hide and show functions.
function makebuttons() {
   aDiv = document.getElementsByTagName("div");
   var aH2 = new Array();
   aH2 = document.getElementsByTagName("h2");
   for (i=0; i<aH2.length; i++) {
      if (aH2[i].className && aH2[i].className=='year') {
//         aH2[i].innerHTML = aH2[i].innerHTML + " <input type=\"button\" value=\"+/-\" onclick=\"showhide('" + aH2[i].parentNode.id + "');\">";
         var ins = "<span style=\"float: right; font-size: smaller;\">[<a href=\"javascript:showhide('" + aH2[i].parentNode.id + "');\">+/-</a>]</span>";
         aH2[i].innerHTML = ins + aH2[i].innerHTML;
      }
   }

   for (i=0; i<aDiv.length; i++) {
      if (aDiv[i].className && aDiv[i].className=='control') {
         aDiv[i].style.border = '1px solid #00f';
         aDiv[i].innerHTML = "<a href=\"javascript:show();\">Show all</a> * <a href=\"javascript:hide();\">Collapse all</a>";
      }
   }

   //Some browsers (Firefox 2) after opening the page to an internal anchor
   //and running the above code, will then jump to a slightly different spot.
   //This code compensates for that.
   if (window.location.href.indexOf('#') > -1) {
      anc = window.location.href.substring(window.location.href.indexOf('#'));
      //alert(anc);
      window.location = anc;
   }
}

function showhide(id) {
   var ele = document.getElementById(id);
   var score = getScore(ele);

   var eleDivs = ele.getElementsByTagName("div");
   var last = 'none';
   for (var i = 0; i < eleDivs.length; i++)
   {
      if (eleDivs[i].className)
      {
         if (eleDivs[i].className == "collapse")
         {
            if (eleDivs[i].style.display == 'none') {
               eleDivs[i].style.display = 'inline';
               last = 'inline';
            } else {
               eleDivs[i].style.display = 'none';
               last = 'none';
            }
         }
         else if (eleDivs[i].className == "summary")
         {
            if (last == 'inline') {
               eleDivs[i].style.display = 'none';
            }
            else {
               eleDivs[i].innerHTML = score;
               eleDivs[i].style.display = 'inline';
            }
         }
      }
   }
}

function hide() {
   var score = '';
   for (i=0; i<aDiv.length; i++) {
      if (aDiv[i].className) {
         if (aDiv[i].className=='collapse') {
            score = getScore(aDiv[i]);
            aDiv[i].style.display='none';
         }
         else if (aDiv[i].className=='summary') {
            aDiv[i].innerHTML = score;
            aDiv[i].style.display = 'inline';
         }
      }
   }
}

function show() {
   for (i=0; i<aDiv.length; i++) {
      if (aDiv[i].className) {
         if (aDiv[i].className=='collapse') {
            score = getScore(aDiv[i]);
            aDiv[i].style.display='inline';
         }
         else if (aDiv[i].className=='summary') {
            aDiv[i].style.display = 'none';
         }
      }
   }
}

// <th class="win">BII (280)</th>
// <th>vs.</th>
// <th>PP (125)</th>
function getScore(ele) {
   var eleTrs = ele.getElementsByTagName("tr");
   var score = "";
   for (var i = 0; i < eleTrs.length; i++)
   {
      if (eleTrs[i].className && eleTrs[i].className == "score")
      {
         // IE 6 converts all HTML into uppercase, so the
         // case-insensitive option (i) is required on HTML checks.
         // Firefox 2 doesn't have this problem.
         var newText = eleTrs[i].innerHTML;
         newText = newText.replace( /a\.k\.a\.(\s['\/\.\w]+)+/g, '' );
         newText = newText.replace( /<th class=\"win\">([A-Z]+ \(\d+\))/i, "<span class=\"win\">$1</span>");
         newText = newText.replace( /<th class=\"win\">([A-Z]+ \(\?\))/i, "<span class=\"win\">$1</span>");
         newText = newText.replace( /<\/?th>/gi, '' );
         newText = newText.replace( /<br>/gi, '' );
//alert(newText);
         return newText;
      }
   }

   return "No match held.";
}

