/*
	Description: This function can be called to display the current class Mod in Moodle 
	Created by: Jason Dahl
	Creation date: 1/18/11
	
*/


function updateModText() {

	// Configuration variables
	var blockText = new String()
	var modstarttime = document.getElementById('modstarttime').value
	var modLength = document.getElementById('modlength').value
	var num_mods = document.getElementById('num_mods').value
	var passinginterval = document.getElementById('passinginterval').value
	var modcomment = document.getElementById('modcomment').value
	//var schedulecomment = ""
	
	today = new Date() 		//Current date & time
	startDate = new Date()	//School day start, set to 8:05 below
	endDate = new Date()	//School day end, set to 2:41 below
	
	var startsplit = modstarttime.split(":")
	
	
	// Set start of day to 8:05 
	startDate.setHours(startsplit[0])
	startDate.setMinutes(startsplit[1])
	if (startsplit[2] == null) {
		startDate.setSeconds(0)
	} else {
		startDate.setSeconds(startsplit[2])
	}

	//Diagnostic only, comment following 2 lines out when not using diagnostics
	//today.setHours(8)
	//today.setMinutes(5)

	currTime = today.getTime() 
	startTime = startDate.getTime()

	dayOfWeek=startDate.getDay()
	elapsedMS = currTime - startTime 				// # of milliseconds elapsed since start of day
	endOfDay = (num_mods * modLength)

	seconds = (elapsedMS/1000) 		//# of seconds elapsed since the start of day
	elapsedMinutes = seconds / 60 	//# of minutes elapsed since the start of day
	modNum = (elapsedMinutes + (modLength /2)) / modLength
	modMin = elapsedMinutes % modLength	//# of minutes elapses in current mod
	
	blockText = blockText + "<b><p style = \"font-size : 1.3em; text-align : center; line-height : 1.2em\">"
	if (dayOfWeek > 0 && dayOfWeek < 6) {
		if (elapsedMinutes > 0 && elapsedMinutes < (endOfDay - passinginterval)) {
			modNum = Math.round(modNum)
			modNum2 = modNum + 1
			if (modMin > 16) {
				blockText = blockText + "Passing time<br />"
				blockText = blockText + "Mods " + modNum + "-" + modNum2 + "\n"
			} else {
				blockText = blockText + "It is now <br />"
				blockText = blockText + "Mod " + modNum + "\n"
			}
		} else {
			blockText = blockText + "Day is over</p></b>"
		}
	} else {
		blockText = blockText + "Enjoy your weekend"
	}
	blockText = blockText + "</p></b>"
	
	if (!modcomment == "") {
		//blockText = "<span>" + schedulecomment + "</span>" + blockText
		document.getElementById('modcomment').innerHTML = "<span>" + modcomment + "</span>"
	} 
	
	document.getElementById('modText').innerHTML = blockText
	setTimeout("updateModText();", 500);
}
