| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | <html> | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  | <title>Teacher's Timer</title> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .timer { | 
					
						
							|  |  |  | 	position: relative; | 
					
						
							|  |  |  | 	min-height: 30px; | 
					
						
							|  |  |  | 	min-width: 100px; | 
					
						
							|  |  |  | 	border: solid 1px silver; | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 	font-size: 32px; | 
					
						
							|  |  |  | 	margin: 10px; | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .timer .sum { | 
					
						
							|  |  |  | 	display: inline-block; | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 	min-width: 100px; | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 	background: gray;	 | 
					
						
							|  |  |  | 	margin: 5px; | 
					
						
							|  |  |  | 	padding-left: 5px; | 
					
						
							|  |  |  | 	padding-right: 5px; | 
					
						
							|  |  |  | 	text-align: right; | 
					
						
							|  |  |  | 	color: white; | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	border-left: solid 10px black; | 
					
						
							|  |  |  | 	border-right: solid 10px black; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	opacity: 0.8; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | .timer .sum > span { | 
					
						
							|  |  |  | 	margin-left: 10px; | 
					
						
							|  |  |  | 	color: silver; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | .timer .sum > span:last-child { | 
					
						
							|  |  |  | 	margin-right: 10px; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | .timer .sum > span > span { | 
					
						
							|  |  |  | 	color: white; | 
					
						
							|  |  |  | 	text-shadow: black 0.1em 0.1em 0.2em; | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .timer .run { | 
					
						
							|  |  |  | 	display: inline-block; | 
					
						
							|  |  |  | 	background: silver; | 
					
						
							|  |  |  | 	margin: 5px; | 
					
						
							|  |  |  | 	color: white; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .timer .start, | 
					
						
							|  |  |  | .timer .end, | 
					
						
							|  |  |  | .timer .duration { | 
					
						
							|  |  |  | 	margin: 10px;  | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | .timer .duration { | 
					
						
							|  |  |  | 	font-weight: bold; | 
					
						
							|  |  |  | 	text-shadow: black 0.1em 0.1em 0.2em; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .timer .run:not(:nth-child(2)) .start, | 
					
						
							|  |  |  | .timer .run:not(:last-child) .end { | 
					
						
							|  |  |  | 	display: none; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | .timer .run:nth-child(2) { | 
					
						
							|  |  |  | 	border-left: solid 10px gray; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | .timer .run:last-child { | 
					
						
							|  |  |  | 	border-right: solid 10px gray; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 14:34:53 +04:00
										 |  |  | #new_timer { | 
					
						
							|  |  |  | 	width: 40px; | 
					
						
							|  |  |  | 	height: 40px; | 
					
						
							|  |  |  | 	margin: 10px; | 
					
						
							|  |  |  | 	border: solid 1px gray; | 
					
						
							|  |  |  | 	border-radius: 50%; | 
					
						
							|  |  |  | 	color: gray; | 
					
						
							|  |  |  | 	font-size: 39px; | 
					
						
							|  |  |  | 	text-align: center; | 
					
						
							|  |  |  | 	cursor: hand; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | var DEBUG = true | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | var TIMER_TEMPLATE = '<div class="run">'+ | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 		'<span class="start" title="Start time">00:00:00</span>'+ | 
					
						
							|  |  |  | 		'<span class="duration" title="Duration">0</span>'+ | 
					
						
							|  |  |  | 		'<span class="end" title="Current/End time">00:00:00</span>'+ | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 	'</div>' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getFormattedTime(t){ | 
					
						
							|  |  |  | 	if(t != null){ | 
					
						
							|  |  |  | 		t = new Date(t) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t = new Date() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var h = t.getHours() | 
					
						
							|  |  |  | 	var m = t.getMinutes() | 
					
						
							|  |  |  | 	var s = t.getSeconds() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (h < 10 ? '0' + h : h) +':'+ | 
					
						
							|  |  |  | 			(m < 10 ? '0' + m : m) +':'+  | 
					
						
							|  |  |  | 			(s < 10 ? '0' + s : s) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getDurationInMinutes(a, b){ | 
					
						
							|  |  |  | 	var s = b - a | 
					
						
							|  |  |  | 	// return the duration in seconds... | 
					
						
							|  |  |  | 	if(window.DEBUG){ | 
					
						
							|  |  |  | 		// for debug use seconds for faster updates... | 
					
						
							|  |  |  | 		return Math.floor(s/1000) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return Math.floor(s/1000/60) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | function formatSum(m){ | 
					
						
							|  |  |  | 	return '<span title="Astronomical hours">h:<span>'+ (m/60).toFixed(2) +'</span></span>'+ | 
					
						
							|  |  |  | 			'<span title="Academic hours">a:<span>'+ (m/45).toFixed(2) +'</span></span>'+ | 
					
						
							|  |  |  | 			'<span title="Minutes">m:<span>'+ m +'</span></span>' | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 14:34:53 +04:00
										 |  |  | function createTimer(parent){ | 
					
						
							|  |  |  | 	var timer = document.createElement('div') | 
					
						
							|  |  |  | 	timer.className = 'timer' | 
					
						
							|  |  |  | 	timer.innerHTML = '<div class="sum">0</div>' | 
					
						
							|  |  |  | 	parent.appendChild(timer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	timer.setAttribute('onclick', 'toggleTimer(this)') | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | function toggleTimer(elem){ | 
					
						
							|  |  |  | 	elem = elem | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 	var t = elem.getAttribute('timer')*1 | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// stop timer... | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 	if(t){ | 
					
						
							|  |  |  | 		clearInterval(t) | 
					
						
							|  |  |  | 		elem.removeAttribute('timer') | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		var start = elem.getAttribute('start')*1 | 
					
						
							|  |  |  | 		var end = Date.now() | 
					
						
							|  |  |  | 		var d = getDurationInMinutes(start, end) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var duration = elem.getElementsByClassName('duration') | 
					
						
							|  |  |  | 		duration = duration[duration.length-1] | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 		duration.innerText = d  | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		var run = elem.getElementsByClassName('run') | 
					
						
							|  |  |  | 		run = run[run.length-1] | 
					
						
							|  |  |  | 		run.setAttribute('duration', d) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// setup a new timer... | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		var now = Date.now() | 
					
						
							|  |  |  | 		elem.innerHTML += TIMER_TEMPLATE | 
					
						
							|  |  |  | 		elem.setAttribute('start', now) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var run = elem.getElementsByClassName('run') | 
					
						
							|  |  |  | 		run = run[run.length-1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var start = elem.getElementsByClassName('start') | 
					
						
							|  |  |  | 		start = start[start.length-1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var end = elem.getElementsByClassName('end') | 
					
						
							|  |  |  | 		end = end[end.length-1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var duration = elem.getElementsByClassName('duration') | 
					
						
							|  |  |  | 		duration = duration[duration.length-1] | 
					
						
							|  |  |  | 		duration.innerText = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		run.setAttribute('duration', 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// initial data... | 
					
						
							|  |  |  | 		start.innerText = getFormattedTime(now) | 
					
						
							|  |  |  | 		end.innerText = getFormattedTime(now) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 		elem.setAttribute('timer', setInterval(function(){ | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 			var start = elem.getAttribute('start')*1 | 
					
						
							|  |  |  | 			var now = Date.now() | 
					
						
							|  |  |  | 			var d = getDurationInMinutes(start, now) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			run.setAttribute('duration', d) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// update end and duration values... | 
					
						
							|  |  |  | 			end.innerText = getFormattedTime(now) | 
					
						
							|  |  |  | 			duration.innerText = d | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// update total... | 
					
						
							|  |  |  | 			var sum = 0 | 
					
						
							|  |  |  | 			var runs = elem.getElementsByClassName('run') | 
					
						
							|  |  |  | 			for(var i=0; i < runs.length; i++){ | 
					
						
							|  |  |  | 				sum += (runs[i].getAttribute('duration')*1) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 			elem.getElementsByClassName('sum')[0].innerHTML = formatSum(sum) | 
					
						
							|  |  |  | 		}, 1000)) | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 14:34:53 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function setup(){ | 
					
						
							|  |  |  | 	//createTimer(document.getElementById('timers')) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </head> | 
					
						
							| 
									
										
										
										
											2013-03-15 14:34:53 +04:00
										 |  |  | <body onload="setup()"> | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 14:34:53 +04:00
										 |  |  | <div id="timers"></div> | 
					
						
							|  |  |  | <div id="new_timer" title="Add new timer" onclick="createTimer(document.getElementById('timers'))">+</div> | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 03:13:13 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 17:14:45 +04:00
										 |  |  | </body> | 
					
						
							|  |  |  | <!-- vim:set ts=4 sw=4 : --> | 
					
						
							|  |  |  | </html> |