// Returns the maximum value in the data part of the array. This is used to determine
// how big the multiplier for the bars will be.
//  onresize="document.location.href=document.location.href"
function findMax()
{
   var max = 0;
	for(i = 1; i < yearData.length; i++)
	{
		if(yearData[i].count > max)
		{
			max = yearData[i].count;
		}
	} 
   return max;  
}

// Recovers the maximum number of rounds that were acheived in one year, along with that 
// year info. The total number of rounds is also calculated, The information is then
// used to print a paragraph for the user.
function printFacts()
{
   var max = 0;
   var year = [];
   var startYear = 1970;
   var total = 0;
   var average;
	var j = 0;
	
   for(i = 0; i < yearData.length; i++)
   {
      total += yearData[i].count;

      if(yearData[i].count > max)
      {
         j = 0;
         max = yearData[i].count;
         year[j] = yearData[i].year;
         j++;
      }
      else if(yearData[i].count == max)
      {
         year[j++] = yearData[i].year;
      }
   }
   average = Math.round(total / (yearData[yearData.length-1].year - startYear));
   
   var pos = (document.all) ? document.all.maxpara : document.getElementById("maxpara");
   var newText = "The largest number of successful rounds in a single year was " + max + " in ";
   
   i = 0
   j--;
   while(j >= 0)
   {
      switch(j)
      {
         case 0:
            newText += year[i]+ ". "
            break;
         case 1:
            newText += year[i] + " and ";
            break;
         default:
            newText += year[i] + ", ";
            break;
      }
      j--;
      i++;
   }
   
   var textChild = document.createTextNode(newText);
   pos.appendChild(textChild);

   var now = new Date();
   if(yearData[yearData.length - 1].year != now.getFullYear())
   {
     newText = "By the end of " + yearData[yearData.length - 1].year + " there had been " + (total -1) + " successful completions of the BGR. ";
   }
   else
   {
     newText = "To date there have been " + (total - 1) + " successful completions of the BGR. ";
   }
   newText += " On average since " + startYear + ", when attempts became more common, there have been around " + average + " successful rounds per year."
   textChild = document.createTextNode(newText);
   pos.appendChild(textChild);
}

// Creates and fills a two column table with a graph from the data from the array.
// The size of each bar in the graph is determined by the size of the enclosing
// column in the page layout table. As the user resizes the page the graph resizes
// also. (There is a minimum size though)
function fillsuccesstable()
{
   var theTable =(document.all) ? document.all.bgrtable : document.getElementById("bgrtable");
   var theTableBody = theTable.tBodies[0];
   var count = 0;
   var total = 0;
   var average = 0;
   
   var multiplier = Math.round(570/findMax()) ;

   if(multiplier < 4)
   {
      multiplier = 4;
   }
   for(i = 0; i < yearData.length; i++)
   {
      total += yearData[i].count;
   }
   average = Math.round(total / (yearData[yearData.length-1].year - 1970));
    
   // To get the page to validate there needs to be a row in the table body, however it is not needed
   // by the viewer of the page, so delete it before adding the real content.
   theTableBody.deleteRow(0);  
   for(i = 0; i < yearData.length; i++)
   {
      var newRow = theTableBody.insertRow(-1);
      var innerText = (i % 2) ? "<b>" + yearData[i].year + "<\/b>" : yearData[i].year ;      
      var clicker = "'JavaScript:newNamePage(" + i + ")' ";
      count += yearData[i].count;

      //if(yearData[i].count > 0)
      if(1)
      {
         newRow.insertCell(0).innerHTML = "<a style='text-decoration:none' href=" + clicker + " target='_self'>" + innerText + "<\/a>";
         //newRow.insertCell(0).innerHTML =  innerText;
         if(yearData[i].count > 0)
         {
            if(yearData[i].count > average)
            {
               newRow.insertCell(1).innerHTML = "<img src='..\/graphics\/green.gif' height='15' width='" + yearData[i].count * multiplier + "'title='" + yearData[i].year + ":" + yearData[i].count + " rounds'>";
            }
            else
            {
               newRow.insertCell(1).innerHTML = "<img src='..\/pictures\/red.gif' height='15' width='" + yearData[i].count * multiplier + "' alt='" + yearData[i].year + ":" + yearData[i].count + " rounds' title='" + yearData[i].year + ":" + yearData[i].count + " rounds'\/>";
            }
         }
         else
         {
            newRow.insertCell(1).innerHTML =  "<b>&nbsp;<\/b>";
         }
      }
      else
      {
         newRow.insertCell(0).innerHTML = innerText;
         newRow.insertCell(1).innerHTML = "";
      }
   }
   printFacts();
}

function newNamePage(index)
{
   storeNameData(index);
   window.location.href="./bgr_year.php";
}

addEvent(window, 'load', fillsuccesstable, false);

