var selectedTeamId = null;
var lastClickFont = null;
var lastClickColour = null;

function rolloverInit() {
  if (!document.links) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("teamchoice")) return false;
  for (var i=0; i<document.links.length; i++) {
    if (document.links[i].className == "fontbutton" || 
        document.links[i].className == "colourbutton"
        ) {
      setupRollover(document.links[i]);
    }
  }

  var teamchoice_formfield = document.getElementById("teamchoice");
  var fontchoice_formfield = document.getElementById("fontchoice");
  var colourchoice_formfield = document.getElementById("colourchoice");

  var teamchoice = teamchoice_formfield.getAttribute("value");
  var fontchoice = fontchoice_formfield.getAttribute("value");
  var colourchoice = colourchoice_formfield.getAttribute("value");

  if (teamchoice == "") {
    teamchoice = "team1";
  }

  teamchoice = document.getElementById(teamchoice);
  selectedTeamId = teamchoice.getAttribute("id");
  teamchoice.className = "teamselected";
  changeColourSelector(selectedTeamId);


  if (fontchoice != "") {
    var selectedFont = document.getElementById(fontchoice);
    selectedFont.style.backgroundPosition = "-126px 0";
    lastClickFont = selectedFont;
  }

  if (colourchoice != "") {
    var selectedColour = document.getElementById(colourchoice);
    selectedColour.style.backgroundPosition = "-126px 0";
    lastClickColour = selectedColour;
  }
}

function setupRollover(thisLink) {
  if (thisLink.className == "fontbutton") {
    thisLink.onclick = rollClickFont;
  } else if (thisLink.className == "colourbutton") {
    thisLink.onclick = rollClickColour;
  }     
}


function setIconNumber(number) {
  var iconfield = document.getElementById("iconchoice");
  var iconnumber = "icon" + (number+1);

  iconfield.setAttribute("value", iconnumber);

} 

function rollClickFont() {
  var fontfield = document.getElementById("fontchoice");
  var chosenFontId = this.getAttribute("id");

  fontfield.setAttribute("value", chosenFontId);

  if (lastClickFont) {
    lastClickFont.style.backgroundPosition = "0 0";
  }	
  this.style.backgroundPosition = "-126px 0";
  lastClickFont = this;

  return false;
}

function rollClickColour() {
  var colourfield = document.getElementById("colourchoice");
  var chosenColourId = this.getAttribute("id");

  colourfield.setAttribute("value", chosenColourId);

  if (lastClickColour) {
    lastClickColour.style.backgroundPosition = "0 0";
  }	
  this.style.backgroundPosition = "-126px 0";
  lastClickColour = this;

  return false;
}

function chooseteam(team) {
  var selectedTeam = document.getElementById(selectedTeamId);
  selectedTeam.className = "team";

  selectedTeamId = team.getAttribute("id");
  team.className = "teamselected";

  changeColourSelector(selectedTeamId);

  var teamchoice = document.getElementById("teamchoice");
  teamchoice.setAttribute("value", selectedTeamId);

  return false;
}


function changeColourSelector(selectedTeamId) {
  var colours_team1 = document.getElementById("colours_team1");
  var colours_team2 = document.getElementById("colours_team2");
  if (selectedTeamId == "team1") {
    colours_team1.style.display = "block";
    colours_team2.style.display = "none";
  } else {
    colours_team1.style.display = "none";
    colours_team2.style.display = "block";
  }
}
