function T2F(h, m){ return parseInt(h, 10) + parseInt(m, 10)/60.0; }
function F2T(f){
	var t = Math.round((f-Math.floor(f))*60);
	return Math.floor(f)+":"+(t < 9 ? "0"+t : t);
}

// Activity example: "Monday 9:00 1:35 red Math 501"
function makeSchedule(e, Activity){
	var daysOfWeek = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
	
	var re = new RegExp("^(\\S+)\\s+(\\d+):(\\d+)\\s+(\\d+):(\\d+)\\s+(\\S+)\\s+(.*?)\\s*$");

	// Finding the earliest and the latest activity
	var start = 9, end = 18;
	for (var i = 0; i < Activity.length; i++){
		var m = re.exec(Activity[i]);
		if (parseInt(m[2], 10) < start) start = parseInt(m[2], 10);
		var t = T2F(m[2], m[3]) + T2F(m[4], m[5]);
		if (end < Math.ceil(t)) end = Math.ceil(t);
	}
	
	// Creating the table
	var oTable = document.createElement("Table");
	oTable.style.borderStyle = "groove";
	oTable.style.borderWidth = "thin";
	
	var oColGroup = document.createElement("Colgroup");
	oColGroup.span = 6;

	for (var i = 0; i < 6; i++){
		var oCol = document.createElement("Col");
		if (i) oCol.width = "20%";
		oColGroup.appendChild(oCol);
	}
	oTable.appendChild(oColGroup);

	/////////////////
	// the header
	var oRow = oTable.insertRow(0);

	// Header
	var header = new Array("Time", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
	for (var i = 0; i < header.length; i++){
		oCell = oRow.insertCell(i);
		oCell.innerHTML = header[i];
		oCell.style.textAlign = "center";
		oCell.style.fontWeight = "bold";
		oCell.style.background = "#A0A0A0";
		oCell.style.color = "white";
	}
	
	/////////////////
	// the body
	oRow = oTable.insertRow(1);
	
	// insert the time column
	oCell = oRow.insertCell(0);
	var oDivPlace = document.createElement("Div");
	oDivPlace.style.height = (end-start)*2+"em";
	for (var i = start; i < end; i++){
		var o = document.createElement("Table")
		o.style.height = 100/(end-start)+"%";
		o.style.width = "100%";
		o.border = o.cellSpacing = 0;
		oDivPlace.appendChild(o);
		o = o.insertRow(0).insertCell(0);
		o.innerHTML = i+":00";
		o.style.background = i%2 ? "#E0E0E0" : "#F0F0F0";
		o.style.padding = "0 0.5em";
		o.style.textAlign = "right";
	}
	oCell.appendChild(oDivPlace);
	
	// insert days
	for (var j = 0; j < 5; j++){
		oCell = oRow.insertCell(1+j);
		oDivPlace = document.createElement("Div");
		oDivPlace.style.height = (end-start)*2+"em";
		oDivPlace.style.overflow = "hidden";
		// insert timeline
		for (var i = start; i < end; i++){
			var oDiv = document.createElement("Div");
			oDiv.style.background = i%2 ? "#E0E0E0" : "#F0F0F0";
			oDiv.style.height = 100/(end-start)+"%";
			oDivPlace.appendChild(oDiv);
		}

		oCell.appendChild(oDivPlace);

		// insert activity
		var crHeight = 100;
		for (var i = 0; i < Activity.length; i++){
			var m = re.exec(Activity[i]);
			if (m[1] == daysOfWeek[j]) {
				var title = m[7]+", "+m[2]+":"+m[3]+"-"+F2T(T2F(m[2], m[3])+T2F(m[4], m[5]));

				// the height of the activity
				var h = T2F(m[4], m[5])*100/(end-start);
				
				// the offset of the activity
				var o = (T2F(m[2], m[3])-start)*100/(end-start);

				var A = document.createElement("Table");
				A.cellSpacing = "0px";
				A.cellPadding = "0px";
				A.border = 0;
				A.title = title;
				A.style.width = "100%";
				A.style.position = "relative";
				A.style.height = h+"%";
				A.style.top = -(crHeight-o)+"%";

				oDivPlace.appendChild(A);
				switch (m[6]){
					case "red":
						A = A.insertRow(0).insertCell(0);
						A.style.background = "#FFE0E0";
						A.style.border = "1px solid #FF9090";
						break;
					case "green":
						A = A.insertRow(0).insertCell(0);
						A.style.background = "#E0FFE0";
						A.style.border = "1px solid #90FF90";
						break;
					case "blue":
						A = A.insertRow(0).insertCell(0);
						A.style.background = "#E0E0FF";
						A.style.border = "1px solid #9090FF";
						break;
					default:
						A = A.insertRow(0).insertCell(0);
						A.style.background = "white";
						A.style.border = "1px solid black";
				}

				if (A.style.setProperty) A.style.setProperty("-moz-border-radius", "0.5em", "");
				A.style.verticalAlign = "middle";
				crHeight += h;
				
				oDiv = document.createElement("Div");
				oDiv.title = title;
				oDiv.style.height = "1px";
				A.appendChild(oDiv);

				// adding the label
				A = document.createElement("Table");
				A.border = 0;
				A.cellSpacing = "0px";
				A.cellPadding = "0px";
				A.style.height = h+"%";
				A.style.width = "100%";
				A.style.position = "relative";
				A.style.top = -(crHeight-o)+"%";
				A.title = title;
				crHeight += h;
				
				oDivPlace.appendChild(A);
				
				A = A.insertRow(0).insertCell(0);
				A.style.verticalAlign = "middle";
								
				oDiv = document.createElement("Div");
				oDiv.style.padding = "0em 0.5em";
				oDiv.style.textAlign = "center";
				oDiv.style.whiteSpace = "nowrap";
				oDiv.style.overflow="visible";
				oDiv.style.maxHeight = 2*(end-start)*h/100+"em";
				oDiv.innerHTML = m[7];
				oDiv.title = title;

				A.appendChild(oDiv);
			}
		}
	}

	e.appendChild(oTable);
}
