window.onload=startup;

function startup() {
//  tickClock("football", new Date("September 2, 2010 20:00:00"), "scoreboard2", "expscoreboard2");
  tickClock("hockey", new Date("October 3, 2010 18:07:00"), "scoreboard1", "expscoreboard1");
//  tickScoreboard(5000);
//  tickScores(300000);
};

function tickScores(delay) {
  var scriptTag = document.getElementById('loadScript');
  var head = document.getElementsByTagName("head")[0];
  if(scriptTag) head.removeChild(scriptTag);
  script = document.createElement('script');
  script.src = 'http://siouxsports.com/scoreupdate.php';
  script.type = 'text/javascript';
  script.id = 'loadScript';
  head.appendChild(script)
  setTimeout("tickScores(" + delay + ")", delay);
};

function updateScores() {
  document.getElementById('idahoscore').innerHTML = updates['idahoscore'];
  document.getElementById('undscore').innerHTML = updates['undscore'];
  document.getElementById('gametime').innerHTML = updates['gametime'];
  document.getElementById('gametimeh').innerHTML = updates['gametimeh'];
  document.getElementById('ndhscore').innerHTML = updates['ndhscore'];
  document.getElementById('manitobascore').innerHTML = updates['manitobascore'];
};

function tickClock(label, enddate, divcountdown, divexpired) {
  var NowDate = new Date();
  var CountDownDate = new Date(enddate);
  secoffsetfromct = (5 * 3600) - (NowDate.getTimezoneOffset() * 60);
  diffsecs = (CountDownDate.getTime() - NowDate.getTime()) / 1000 + secoffsetfromct;
  
  if (diffsecs > 0) {
    days = Math.floor(diffsecs / 60 / 60 / 24);
    hours = Math.floor(diffsecs / 60 / 60) % 24;
    minutes = Math.floor(diffsecs / 60) % 60;
    secs = Math.floor(diffsecs) % 60;
    document.getElementById(label + "time").innerHTML = days + " days " + zerostopad(hours) + hours + ":" + zerostopad(minutes) + minutes + ":" + zerostopad(secs) + secs;
    setTimeout("tickClock('" + label + "', '" + CountDownDate + "', '" + divcountdown + "', '" + divexpired +"')", 1000);
  } else {
    countboard = document.getElementById(divcountdown);
    expiredboard = document.getElementById(divexpired);
    countboard.id = divexpired;
    expiredboard.id = divcountdown;
    setTimeout("tickClock('" + label + "', '" + CountDownDate + "', '" + divexpired + "', '" + divexpired +"')", 1000);
  }
};

function zerostopad(intval) {
  if (intval < 10) return "0";
  else return "";
};

function tickScoreboard(delay) {
  for (i = 1; i <= 2; i++) {
    thissb = document.getElementById("scoreboard" + i);
    if (thissb.style.visibility == "visible") {
      document.getElementById("scoreboard" + i).style.visibility = "hidden";
      if (i == 2) document.getElementById("scoreboard1").style.visibility = "visible";
      else document.getElementById("scoreboard" + (i + 1)).style.visibility = "visible";
      break;
    }
  }
  setTimeout("tickScoreboard(" + delay + ")", delay);
};

