// I could probably use this to generate the HTML checkboxes, but didn't feel like setting that up
// Instead, this and the HTML were generated by a PHP script.
var arrTopics = [ 
  ["technology","Technology",
    [["apple","Apple"],
    ["design","Design"],
    ["gadgets","Gadgets"],
    ["hardware","Hardware"],
    ["tech_news","Tech Industry News"],
    ["linux_unix","Linux/Unix"],
    ["microsoft","Microsoft"],
    ["mods","Mods"],
    ["programming","Programming"],
    ["security","Security"],
    ["software","Software"],
    ["tech_deals","Tech Deals"]
    ]
  ],
  ["science","Science",
    [["space","Space"],
    ["environment","Environment"],
    ["health","Health"],
    ["general_sciences","General Sciences"]
    ]
  ],
  ["world_business","World & Business",
    [["business_finance","Business & Finance"],
    ["politics","Political News"],
    ["2008_us_elections","US Elections 2008"],
    ["political_opinion","Political Opinion"],
    ["world_news","World News"],
    ["offbeat_news","Offbeat News"]
    ]
  ],
  ["sports","Sports",
    [["baseball","Baseball"],
    ["basketball","Basketball"],
    ["extreme_sports","Extreme"],
    ["football","American & Canadian Football"],
    ["golf","Golf"],
    ["hockey","Hockey"],
    ["motorsport","Motorsport"],
    ["soccer","Soccer"],
    ["tennis","Tennis"],
    ["other_sports","Other Sports"]
    ]
  ],
  ["entertainment","Entertainment",
    [["celebrity","Celebrity"],
    ["movies","Movies"],
    ["music","Music"],
    ["television","Television"]
    ]
  ],
  ["gaming","Gaming",
    [["gaming_news","Gaming News"],
    ["playable_web_games","Playable Web Games"],
    ["pc_games","PC Games"],
    ["nintendo_wii","Nintendo Wii"],
    ["playstation_3","Playstation 3"],
    ["xbox_360","Xbox 360"]
    ]
  ],
  ["videos","Videos",
    [["videos_animation","Animation Videos"],
    ["videos_comedy","Comedy Videos"],
    ["videos_educational","Educational Videos"],
    ["videos_gaming","Gaming Videos"],
    ["videos_music","Music Videos"],
    ["videos_people","People Videos"],
    ["videos_sports","Sports Videos"]
    ]
  ]
];
var i, j, k = 0, objTopics = {};

// sets up reverse-searching for easy access
for (i=0;i<arrTopics.length;i++)
{
  objTopics[arrTopics[i][0]] = {
    id : k,
    friendly : arrTopics[i][1]
  };
  k++;
  for (j=0;j<arrTopics[i][2].length;j++)
  {
    objTopics[arrTopics[i][2][j][0]] = {
      id : k,
      friendly : arrTopics[i][2][j][1]
    };
    k++;
  }
}

function topicIdToFriendly (id)
{
  for (var i in objTopics)
  {
    if (objTopics[i].id == id)
    {
      return objTopics[i].friendly;
    }
  }
}

function topicIdToName (id)
{
  for (var i in objTopics)
  {
    if (objTopics[i].id == id)
    {
      return i;
    }
  }
}

function containerCheck(id)
{
	c = document.getElementById("chkContainer"+id);
	if (c.checked != c.lastState)
	{
		var i = 0;
		while (t = document.getElementById("chkContainer"+id+"Topic"+i))
		{
			if (c.checked)
			{
				t.checked = true;
			} else
			{
				t.checked = false;
			}
			i++;
		}
	}
	if (c.lastState === undefined)
		c.lastState = c.checked;
	c.lastState = c.checked;
}

function topicCheck(cid,tid)
{
  c = document.getElementById("chkContainer"+cid);
  t = document.getElementById("chkContainer"+cid+"Topic"+tid);
  if (t.checked && !c.checked)
  {
    c.lastState = c.checked = true;
  }
  var anyChecked = false;
  var i = 0;
  while (t = document.getElementById("chkContainer"+cid+"Topic"+i))
  {
    if (t.checked)
    {
      anyChecked = true;
    }
    i++;
  }
  c.lastState = c.checked = anyChecked;
}

function displayTopicFilters ()
{
  document.getElementById("divForm").style.display = "none";
  document.getElementById("topicFilters").style.display = "block";
  return false;
}

function hideTopicFilters ()
{
  document.getElementById("divForm").style.display = "block";
  document.getElementById("topicFilters").style.display = "none";
  return false;
}

