// http server and directory where the ogg files are
var videoURL = "videos/";

// list of categories with names, each with a list of videos
var categories = [
  { en: "Speed Geeking", nl: "Speed Geeking", videos: [
    { url: "makeart-stream-speedgeeking1-2010-03-05_14-34-50.ogg", en: "Part 1", nl: "Part 1" },
    { url: "makeart-stream-speedgeeking2-2010-03-05_15-11-37.ogg", en: "Part 2", nl: "Part 2" },
    { url: "makeart-stream-speedgeeking3-2010-03-05_15-47-29.ogg", en: "Part 3", nl: "Part 3" }
  ]},
  { en: "Pecha Gnucha", nl: "Pecha Gnucha", videos: [
    { url: "makeart-stream-pechaGnucha-part1-2010-03-05_20-10-20.ogg", en: "Part 1", nl: "Part 1" },
    { url: "makeart-stream-pechaGnucha-part2-2010-03-05_21-03-08.ogg", en: "Part 2", nl: "Part 2" },
    { url: "makeart-stream-pechaGnucha-part3-2010-03-05_21-15-13.ogg", en: "Part 3", nl: "Part 3" },
    { url: "makeart-stream-pechaGnucha-part4-2010-03-05_21-26-11.ogg", en: "Part 4", nl: "Part 4" }
  ]},
  { en: "Breakfast Club", nl: "Breakfast Club", videos: [
    { url: "makeart-stream-breakfast-club-saturday-part1-2010-03-06_11-06-15.ogg", en: "Saturday Part 1", nl: "Saturday Part 1" }
  ]},
  { en: "Hocus Pocus", nl: "Hocus Pocus", videos: [
    { url: "makeart-stream-hocus-pocus-(intro-by-am)-martin-howse-2010-03-06_14-20-21.ogg", en: "Martin Howse", nl: "Martin Howse" },
    { url: "makeart-stream-hocus-pocus-dmytri-kleiner-2010-03-06_15-36-52.ogg", en: "Dmytri Kleiner", nl: "Dmytri Kleiner" },
    { url: "makeart-stream-hocus-pocus-florian-cramer-2010-03-06_16-23-20.ogg", en: "Florian Cramer", nl: "Florian Cramer" }
  ]},
  { en: "Placard", nl: "Placard", videos: [
    { url: "makeart-stream-placard-no-copy-paste-2010-03-06_21-22-56.ogg", en: "No Copy Paste", nl: "No Copy Paste" },
    { url: "makeart-stream-placard-iohannes-2010-03-06_22-03-25.ogg", en: "IOhannes", nl: "IOhannes" },
    { url: "makeart-stream-placard-dave-griffiths-2010-03-06_22-50-40.ogg", en: "Dave Griffiths", nl: "Dave Griffiths" }
  ]}
];

// return a closure that adds a video with the right url and fallback text
function addvideo(url, text) { return function(evt) {
  var download = { en: "download", nl: "download" }; // TODO FIXME TRANSLATE
  var doc = document;
  var eh3 = evt.target;
  var ediv = eh3.parentNode;
  var edivv = doc.createElement("div");
  var ev = doc.createElement("video");
  ev.setAttribute("autoplay", "autoplay");
  ev.setAttribute("controls", "controls");
  ev.setAttribute("src", url);
//  ev.setAttribute("poster", url + ".jpg"); // do any browsers support this yet?
  var ea = doc.createElement("a");
  ea.setAttribute("href", url);
  ea.setAttribute("title", text);
  var edown = doc.createTextNode(download[lang]);
  ea.appendChild(edown);
  ev.appendChild(ea);
  edivv.appendChild(ev);
  ediv.appendChild(edivv);
  eh3.onclick = removevideo(url, text); // behaviour change: toggle
}; }

// return a closure that removes the neighbouring video
function removevideo(url, text) { return function(evt) {
  var doc = document;
  var eh3 = evt.target;
  var ediv = eh3.parentNode;
  var ev = eh3.nextSibling;
  ediv.removeChild(ev);
  eh3.onclick = addvideo(url, text); // behaviour change: toggle
}; }

// called on body load
function init() {
  var doc = document;
  var evs = doc.getElementById("videos");
  if (evs) { // if this page is the videos page
    var c;
    for (c = 0; c < categories.length; c = c + 1) {
      // add a heading for each category that has a non-empty video inside
      var videos = categories[c].videos;
      var nonempty = false;
      var v;
      for (v = 0; v < videos.length; v = v + 1) {
        nonempty = nonempty || videos[v].url != "";
      }
      if (nonempty) {
        var eh3 = doc.createElement("h3");
        var ehead = doc.createTextNode(categories[c][lang]);
        eh3.appendChild(ehead);
        evs.appendChild(eh3);
        var i;
        for (i = 0; i < videos.length; i = i + 1) {
          // add a link for each video that has a non-empty url..
          var video = videos[i];
          if (video.url != "") {
            var url = videoURL + video.url;
            var hash = video.url;
            var text = video[lang];
            var ediv = doc.createElement("div");
            ediv.setAttribute("class", "video");
            var ea = doc.createElement("a");
            ea.setAttribute("href", "#"); // link to nowhere
            ea.setAttribute("class", "videolink");
            var ehead = doc.createTextNode(text);
            ea.appendChild(ehead);
            ediv.appendChild(ea);
            ea.onclick = addvideo(url, text); // link behaviour
            evs.appendChild(ediv);
          }
        }
      }
    }
  }
}


