// markup.js
// Production Information
// Chamber Dance Company 2009
// University of Washington
// Author: Thomas Van Doren
// Release: 2008-12-01

// Global arrays for pieces and months
var pieceArray = new Array();
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

// Main functions
function main() {
	
	// Change title
	document.title = "Production Information - Chamber Dance Company " + year;
	
	// Change subhead
	document.getElementById("subhead").innerHTML = "Chamber Dance Company " + year;
	
	// Change menu item [year] Performance and box item title
	var menuItems = document.getElementById("menu").getElementsByTagName("a");
	menuItems[2].innerHTML = year + " Performance";
	
	document.getElementById("perfyear").innerHTML = year + ' Performance<a href="#top" name="perf">Top</a>';
	
	// Rearrange boxes
	thisweek();
	performance();
	casting();
	calendar();
	
	
	// Load functions from functions.js
	loaders();
}

// Arrange this week box
function thisweek() {
	// Get data and split into lines
	var thisContents = document.getElementById("thisweek").innerHTML;
	var lines = thisContents.split("\n");
	
	var curLine = 1;
	var outstr = "";
	
	// Add notes at beginning
	while ( lines[curLine] != "#!#!?!?!#!#" ) {
		if ( curLine == 1 ) {
			outstr += "<div>\n";
		}
		outstr += lines[curLine] + "<br />\n";
		curLine++;
	}
	
	if ( curLine > 1 ) {
		outstr += "</div>\n";
	}
	curLine++;
	
	var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var startLine = curLine;
	
	// Cycle through all days
	while ( curLine < lines.length - 1 ) {
		if ( lines[curLine] == "###NEW###NEW###NEW###" ) {
			if ( curLine != startLine ) {
				outstr += "</ul>\n";
			}
			curLine++;
			var curDate = new Date(lines[curLine]);
			outstr += '<div class="day">';
			outstr += days[curDate.getDay()] + ", " + months[curDate.getMonth()] + " " + curDate.getDate();
			outstr += "</div>\n<ul>";
			curLine++;
		}
		outstr += '<li><span class="cd">' + lines[curLine] + "</span> " + lines[curLine + 1] + "</li>\n";
		curLine += 2;
	}
	
	outstr += "</ul>\n";
	document.getElementById("thisweek").innerHTML = outstr;
}

// Arrange performance box
function performance() {
	// Get data and split into lines
	var perfContents = document.getElementById("performance").innerHTML;
	var lines = perfContents.split("\n");
	
	var outstr = "<table>\n";
	
	// Create performance table and updates pieceArray
	for ( var i = 0; i < lines.length / 2 - 1; i++ ) {
		outstr += "<tr>";
		
		// Intermission case
		if ( lines[2 * i + 1].match("--Intermission--") == "--Intermission--" ) {
			outstr += '<td class="tc" colspan="2">Intermission</td></tr>\n';
		} 
		// Regular case
		else {
			pieceArray[pieceArray.length] = lines[2 * i + 1];
			outstr += '<td class="th">' + lines[2 * i + 1] + "</td>";
			outstr += "<td>" + lines[2 * i + 2] + "</td></tr>\n";
		}
	}
	outstr += "</table>\n";
	document.getElementById("performance").innerHTML = outstr;
}

// Arrange casting box
function casting() {
	// Get data and split into lines
	var castContents = document.getElementById("casting").innerHTML;
	var lines = castContents.split("\n");
	
	var outstr = "";
	
	for ( var i = 0; i < casts; i++ ) {
		outstr += "<b>" + lines[(pieceArray.length + 1) * i + 1] + "</b>\n";
		outstr += "<table>\n";
		for ( var j = 0; j < pieceArray.length; j++ ) {
			outstr += "<tr>\n";
			outstr += '<td class="th">' + pieceArray[j] + "</td>\n";
			outstr += "<td>" + lines[(pieceArray.length + 1) * i + j + 2] + "</td>";
			outstr += "</tr>\n"
		}
		outstr += "</table>\n";
	}
	
	document.getElementById("casting").innerHTML = outstr;
	
	/*
		</table>
		<b>Friday</b>
		<table>
		
			<tr>
				<td class="th">Night</td>
				<td>Catherine Cabeen</td>
			</tr>
	*/
}

// Arrange calendar box
function calendar() {
	// Get data and split into lines
	var calContents = document.getElementById("calendar").innerHTML;
	var lines = calContents.split("\n");
	
	var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	var month = -1;
	var outstr = "";
	
	for ( var i = 0; i < lines.length / 2 - 1; i++ ) {
		var lineDate = lines[2 * i + 1];
		var dateObj = new Date(lineDate);
		
		// If new month is needed
		if ( dateObj.getMonth() != month ) {
			if ( month > 0 ) {
				outstr += "</ul>\n";
			}
			month = dateObj.getMonth();
			outstr += "<b>" + months[month] + "</b>\n";
			outstr += "<ul>\n";
		}
		
		// Add date
		outstr += '<li><span class="cd">';
		var minutes = dateObj.getMinutes();
		if ( minutes < 10 ) {
			minutes = "0" + minutes;
		}
		outstr += days[dateObj.getDay()] + " " + dateObj.getDate() + " " + dateObj.getHours() + ":" + minutes;
		outstr += "</span> ";
		
		// Add text and close tags/add linebreak
		outstr += lines[2 * i + 2] + "</li>\n";
		
	}
	outstr += "</ul>\n";
	document.getElementById("calendar").innerHTML = outstr;
	
}