var totalshows = season.length;
var months = new Array('January','February','March','April',
      'May','June','July','August','September','October','November','December');
var dayofweek = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
today = new Date();
threedays = 3*86400000; // 3 days of milliseconds
hr19 = 68400000  // 19 hours of milliseconds
hr14 = 14*3600000 // 14 hours
sixdays = 6*86400000; // 6 days of milliseconds

// class=show-inactive
// class=show-active

// data in the SEASON array is 0=Name; 1=URL; 2=opening date; 3=closing date 4=date string


// start at the beginning of the season, skip a show if the closing date is in the past

for (var i = 0; i <= totalshows-1; i++) {

	m = season[i][3].substring(5,7); // end date
	d = season[i][3].substring(8,10);
	y = season[i][3].substring(0,4);
	while (m.substring(0,1) == '0') m = m.substring(1); // remove leading 0
	m = m - 1;
	while (d.substring(0,1) == '0') d = d.substring(1); // remove leading 0
	var closing = new Date(y, m, d, 15, 0, 0); // assumes the closing time is 3pm
	cdate = months[m] + " " + d + ", " + y;

// if show is in the past then just display the name and go to the next one

	if ((today.getTime()) > closing.getTime()) { // show has already closed 
		message = "Closed "+ cdate;
		showname = "<a href='"+season[i][1]+"'>"+season[i][0]+"</a>";
		document.write("<p class='show-inactive'>"+showname+"</p>");
		document.write("<p class='show-inactive-date'>"+season[i][4]+"</p>");
	}
	else {
		nextshow = i;
		break;
	}
}

// the next show is the one to highlight (unless we're already at the end of the season)

// document.write("i="+i+" and totalshows="+totalshows);

if (i < totalshows) { // there is a next show and it is the "active" one
	
	// get the start date

	i = nextshow;
	showname = "<a href='"+season[i][1]+"'>"+season[i][0]+"</a>";
	document.write("<p class='show-active'>"+showname+"</p>");  
	
	om = season[i][2].substring(5,7);  // momth
	od = season[i][2].substring(8,10); // day
	oy = season[i][2].substring(0,4);  // year
	while (om.substring(0,1) == '0') om = om.substring(1); // remove leading 0
	om = om - 1;
	while (od.substring(0,1) == '0') od = od.substring(1); // remove leading 0
	
	opening = new Date(oy, om, od, 20, 0, 0); // assumes the start time is is 8pm 
	oshortdate = months[om] + " " + od;
	oday = dayofweek[opening.getDay()];

	// get the closing date

	cm = season[i][3].substring(5,7); // end date
	cd = season[i][3].substring(8,10);
	cy = season[i][3].substring(0,4);
	while (cm.substring(0,1) == '0') cm = cm.substring(1); // remove leading 0
	cm = cm - 1;
	while (cd.substring(0,1) == '0') cd = cd.substring(1); // remove leading 0
	closing = new Date(cy, cm, cd, 15, 0, 0); // assumes the closing time is 3pm
	cday = dayofweek[closing.getDay()];
	
//	message = "Opens " + oshortdate;
	message = season[i][4];
	if ((today.getTime()+sixdays) > opening.getTime())
	   message = "Opens this "+oday+", " + oshortdate;
	if ((today.getTime()+hr19) > opening.getTime())
	   message = "<font color='red'>Opens tonight!</font>";
	if (today.getTime() > opening.getTime())
	   message = "<font color='red'>Now playing.</font>";
	if ((today.getTime()+sixdays) > closing.getTime() && (closing.getTime() - opening.getTime() > threedays) )
	   message = "<font color='red'>Last week - closes "+cday+"!</font>";
	if ((today.getTime()+threedays) > closing.getTime() && (closing.getTime() - opening.getTime() > threedays) )
	   message = "<font color='red'>Last days!</font>";
	if ((today.getTime()+hr14) > closing.getTime())
	   message = "<font color='red'>Last performance today!</font>";
	   
	document.write("<p class='show-active-date'>"+message+"<br />");
	etixmonth = om + 1;
//	document.write("<a target='_blank' href='http://www.etix.com/ticket/servlet/onlineSearch?action=previousCalendar&venue_id=279&display_month="+etixmonth+"&display_year="+oy+"&cobrand=RLT'>Reserve tickets now</a></p>");
	document.write("</p>");

// display just the names of the remaining shows.
	
	nextshow = i+1;
	
	for (var i = nextshow; i <= totalshows-1; i++) {
	
		om = season[i][2].substring(5,7);  // momth
		od = season[i][2].substring(8,10); // day
		oy = season[i][2].substring(0,4);  // year
		while (om.substring(0,1) == '0') om = om.substring(1); // remove leading 0
		om = om - 1;
		while (od.substring(0,1) == '0') od = od.substring(1); // remove leading 0

		odate = months[om] + " " + od + ", " + oy;
		message = "Opens "+ odate;
		showname = "<a href='"+season[i][1]+"'>"+season[i][0]+"</a>";
		showdate = season[i][4];
		document.write("<p class='show-inactive'>"+showname+"</p>");
		document.write("<p class='show-inactive-date'>"+season[i][4]+"</p>");
	}
}