/*******************************************************************
* Put all movies here...
* Format:
*   "Movie Title; Rating; Start Date (Month DD, YYYY); End Date (Month DD, YYYY); "
*   **Note** - A plus(+) must be included after each line, except the last line
*              must have a semicolon(;) instead.
* Example:
*   "Fantastic Four: Rise of the Silver Surfer; PG-13; June 22, 2007; August 1, 2007; "
*******************************************************************/
var listing =
"27 Dresses; PG-13; September 28, 2007; October 27, 2008; "+
"Alvin and the Chipmunks; PG; September 28, 2007; October 27, 2008; "+
"National Treasure: Book of Secrets; PG; September 28, 2007; October 27, 2008; "+
"The Bucket List; PG-13;September 28, 2007; October 27, 2008;"+
"I Am Legend; PG-13; September 14, 2007; October 27, 2008; "+
"Cloverfield; PG-13; September 14, 2007; October 27, 2008; "+
"In The Name of the King; PG-13; September 14, 2007; October 27, 2008;";


/***************************
* Do not edit beyond here. *
***************************/

var movies = new Array();

function setupListings()
{
var i = 0;
var j = listing.indexOf(";", i);
var x = 0;

while (j != -1)
{
// Movie Title
movies[x] = new Array();
movies[x][0] = listing.substring(i, j);
i = j + 2;
j = listing.indexOf(";", i);

// Rating
movies[x][1] = listing.substring(i, j);
i = j + 2;
j = listing.indexOf(";", i);

// Start Date
movies[x][2] = listing.substring(i, j);
i = j + 2;
j = listing.indexOf(";", i);

// End Date
movies[x][3] = listing.substring(i, j);
i = j + 2;
j = listing.indexOf(";", i);

x++;
}

// setup the dropdown and now playing list

// start with this week's listing
var currentWeek = new Date();
while (currentWeek.getDay() != 5)
{
currentWeek.setDate(currentWeek.getDate() - 1);
}
var thisWeek = makeDateString(currentWeek.toUTCString());

// get the next three weeks as well
currentWeek.setDate(currentWeek.getDate() + 7);
var nextWeek = makeDateString(currentWeek.toUTCString());
currentWeek.setDate(currentWeek.getDate() + 7);
var twoWeeks = makeDateString(currentWeek.toUTCString());
currentWeek.setDate(currentWeek.getDate() + 7);
var threeWeeks = makeDateString(currentWeek.toUTCString());

// fill the dropdown
document.getElementById('dateSelect').innerHTML =
"<select onchange='generateListing(this.value)'>" +
"<option value='" + thisWeek + "'>" + thisWeek + "</option>" +
"<option value='" + nextWeek + "'>" + nextWeek + "</option>" +
"</select>";

// the following two lines were taken out of the above select tag due to little knowledge of movies to come
//"<option value='" + twoWeeks + "'>" + twoWeeks + "</option>" +
//"<option value='" + threeWeeks + "'>" + threeWeeks + "</option>" +

// setup the current week's listing immediately
generateListing(thisWeek);
}

/* make the correct string for a given date */
function makeDateString(fullDate)
{
// day of the week and ", "
var myDate = fullDate.substr(0,5);

// handle differences in browsers
 if (fullDate.charAt(6) == ' ')
 {
 myDate += "0" + fullDate.substr(5, 10);
 }
 else
 {
 myDate += fullDate.substr(5, 11);
 }

return myDate;
}

function generateListing(date)
{
var myList = new Array();
var x = 0;

for (var movie in movies)
{
if (isBetween(date, movies[movie][2], movies[movie][3]))
{
  myList[x] = movies[movie];
  x++;
}
}

var finalList = "";
finalList += "<table style='border-collapse: collapse; padding-top: 10px; padding-bottom: 10px' width='100%'>";
for (var next in myList)
{
finalList += "<tr>";
finalList += "<td><ul><li>" + myList[next][0] + "</li></ul></td><td align='right'>" + myList[next][1] + "</td>";
finalList += "</tr>";
}
var count = myList.length;
while (count < 7)
{
finalList += "<tr><td><ul style='list-style: none;'><li style='color: black;'>_</li></ul></td></tr>";
count++;
}
finalList += "</table>";
document.getElementById('listingSpace').innerHTML=finalList;
}

function isBetween(date, start, end)
{
if (Date.parse(date) >= Date.parse(start) && Date.parse(date) <= Date.parse(end))
  return true;
else
  return false;
}