var gYears;
var gRatings;

/*
http://en.wikipedia.org/wiki/Academy_Award_for_Best_Picture
http://www.filmratings.com/
 */
function initialize() {
   var moviesRaw = [
    /*
      ["1992", "Unforgiven", "Rated R for language, and violence, and for a scene of sexuality"],
      ["1993", "Schindler's List", "Rated R for language, some sexuality and actuality violence"],
    */
      ["1994", "Forrest Gump", "Rated PG-13 for drug content, some sensuality and war violence"],
      ["1995", "Braveheart", "Rated R for brutal medieval warfare"],
      ["1996", "The English Patient", "Rated R for sexuality, some violence and language"],
      ["1997", "Titanic", "Rated PG-13 for disaster related peril and violence, nudity, sensuality and brief language"],
      ["1998", "Shakespeare in Love", "Rated R for sexuality"],
      ["1999", "American Beauty", "Rated R for strong sexuality, language, violence and drug content"],
      ["2000", "Gladiator", "Rated R for intense, graphic combat"],
      ["2001", "A Beautiful Mind", "Rated PG-13 for intense thematic material, sexual content and a scene of violence"],
      ["2002", "Chicago", "Rated PG-13 for sexual content and dialogue, violence and thematic elements"],
      ["2003", "The Lord of the Rings: The Return of the King", "Rated PG-13 for intense epic battle sequences and frightening images"],
      ["2004", "Million Dollar Baby", "Rated PG-13 for violence, some disturbing images, thematic material and language"],
      ["2005", "Crash", "Rated R for language, sexual content and some violence"],
      ["2006", "The Departed", "Rated R for strong brutal violence, pervasive language, some strong sexual content and drug material"],
      ["2007", "No Country for Old Men", "Rated R for strong graphic violence and some language"],
      ["2008", "Slumdog Millionaire", "Rated R for some violence, disturbing images and language"]
   ];

   var movies = [];
   for (var i = 0; i < moviesRaw.length; i++) {
      var movieRaw = moviesRaw[i];
      var movie = {year: movieRaw[0], title: movieRaw[1], rating: movieRaw[2]};
      movies.push(movie); 
   }

   gYears = [];
   gRatings = [];
   movies.sort(function(m0, m1) {if (m0.rating < m1.rating) return -1; return (m0.rating == m1.rating ? 0 : 1);});
   for (var i = 0; i < movies.length; i++) {
      gRatings.push({text: movies[i].rating});
      movies[i].ratingIndex = i;
   }
   movies.sort(function(m0, m1) {if (m0.year < m1.year) return -1; return (m0.year == m1.year ? 0 : 1);});
   for (var i = 0; i < movies.length; i++) {
      gYears.push({text: movies[i].title + " (" + movies[i].year + ")"});
      movies[i].yearIndex = i;
   }
   for (var i = 0; i < movies.length; i++) {
      var movie = movies[i];
      gYears[movie.yearIndex].otherIndex = movie.ratingIndex;
      gRatings[movie.ratingIndex].otherIndex = movie.yearIndex;
      
   }

   var parentNode = document.getElementById("content");

   var table = parentNode.appendChild(document.createElement("table")).
    appendChild(document.createElement("tbody"));
   for (var i = 0; i < movies.length; i++) {
      var row = table.appendChild(document.createElement("tr"));
      var cell;
      var label;

      cell = row.appendChild(document.createElement("td"));
      label = document.createElement("span");
      label.id = "year," + i;
      label.onclick = onClick.bindAsEventListener();
      label.appendChild(document.createTextNode(gYears[i].text));
      cell.appendChild(label);

      cell = row.appendChild(document.createElement("td"));
      label = document.createElement("a");
      label.id = "rating," + i;
      label.onclick = onClick.bindAsEventListener();
      label.appendChild(document.createTextNode(gRatings[i].text));
      cell.appendChild(label);
   }

   // !!!
   /*
   for (var i = 0; i < 12; i++) {
      console.log(i + ": " + standardBackgroundColor(i).join(","));
   }
   */
}

function onClick(event) {
   var element = Event.element(event);
   var id = element.id.split(",");
   var listName = id[0];
   var otherListName = (listName == "year" ? "rating" : "year");
   var index = parseInt(id[1]);
   var list = (listName == "year" ? gYears : gRatings);
   var otherList = (otherListName == "year" ? gYears : gRatings);
   var otherIndex = list[index].otherIndex;
   var otherId = otherListName + "," + otherIndex;
   var otherElement = document.getElementById(otherId);

   var color = "";
   var isEmpty = (element.style["backgroundColor"] == "");
   //console.log(isEmpty);
   if (isEmpty) {
      var yearIndex = (listName == "year" ? index : otherIndex);
      var colorValues = standardBackgroundColor(yearIndex);
      color = "rgb(" + colorValues.join(",") + ")";
   }
   element.style["backgroundColor"] = color;
   otherElement.style["backgroundColor"] = color;
}

// Colors for reasonably small numbers should be far apart.
function standardBackgroundColor(n) {
   var colorValues = [];
   var increment = [];
   // Keep it pale.
   var start = 128;
   for (var i = 0; i < 3; i++) {
      colorValues[i] = start;
      increment[i] = (256 - start) / 2;
   }
   var colorIndex = 0;
   while (n != 0) {
      var nextN = n >> 1;
      if (n & 1 == 1)
         colorValues[colorIndex] += increment[colorIndex];

      increment[colorIndex] /= 2;
      colorIndex = (colorIndex + 1) % 3;
      n = nextN;
   }
   return colorValues;
}
