////////////////////////////////////////////////////////////////////////////////////////////////////
// hours.js: Display library open hours
// used by http://lib.colostate.edu/about/hours/ including related pages for intersessions, breaks and exams
// also used by http://lib.colostate.edu/users/ - 5 pages 
//
// Greg Vogl
// Colorado State University Libraries
// Created: 2007-01-12 Last updated: 2008-11-06
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// these hours need to be manually updated every semester - see below for editable part of this file
////////////////////////////////////////////////////////////////////////////////////////////////////

// add a zero in front of numbers less than 10
function addZero(i) { if (i < 10) i = "0" + i; return i; } 

// display time in format hh:mm am
function displayTime(hour) { 
	var ampm = hour<12 || hour == 24 ? "am" : "pm";
	var h = Math.floor(hour); if (hour >= 13) h -=12; if (hour<1) h +=12;
	var m = Math.floor(60*(hour - Math.floor(hour))); m = m == 0 ? "" : ":" + addZero(m);
	return h + m + ampm;
}

// get library hours for today and set in libHoursID element in HTML document
function getLibHours(today, libHoursID) {
	var year = today.getFullYear();
	var month = addZero(today.getMonth() + 1);
	var day = addZero(today.getDate());
	var weekday = today.getDay();
	var weekdays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var hours = today.getHours();
	var minutes = today.getMinutes();
	var seconds = today.getSeconds();
	var h = addZero(hours);
	var m = addZero(minutes);
	var s = addZero(seconds);
	var open1 = 0;
	var close1 = 24;
	var open2 = 24;
	var close2 = 24;

///////////////////////////////////////////////////////////////////////////////////////////
	// The following hours need to be updated every semester!
	// do not edit this file above this line
	
	// Holidays (library closed all day)
	if ((month==1 && (day==1 || day==2 || day==19)) || //new year, MLK
	    (month==5 && (day==26)) || // Memorial day
	    (month==7 && (day==4)) || // Independence day
	    (month==9 && (day==1)) || // Labor day
	    (month==11 && (day==27 || day==28)) || // Thanksgiving
		(month==12 && (day==24 || day==25 || day==26)) || // Christmas
		1==0
	) close1 = 0;
	
	// Summer intersession hours: M-R 7am-6pm, F 7am-5pm, S-S 1-5pm
	else if ((month==8 && day>=8 && day <=23))
	switch (weekday) {
		case 5: open1 = 7; close1 = 17; break;
		case 0: case 6: open1 = 13; close1 = 17; break;
		default: open1 = 7; close1 = 18; break;
	}
	
	// Non-summer intersession hours (winter, spring and fall breaks): M-f 7am-6pm, S-S 1-5pm
	else if ((month==12 && day>=20) || (month==1 && day<=18) 
		|| (month==3 && day>=13 && day <=21) 
		|| (month==11 && day>=21 && day<=29))
	switch (weekday) {
		case 0: case 6: open1 = 13; close1 = 17; break;
		default: open1 = 7; close1 = 18; break;
	}
	
	// December final exam hours
	else if (month==12 && day>=1 && day<=5) { close1 = 2; open2 = 7; } 
	else if (month==12 && day==6) { close1 = 1; open2 = 9; }
	else if (month==12 && day==7) { close1 = 1; open2 = 10; }
	else if (month==12 && day>=8 && day<=12) { close1 = 2; open2 = 7; } 
	else if (month==12 && day==13) { close1 = 1; open2 = 9; }
	else if (month==12 && day==14) { close1 = 1; open2 = 10; }
	else if (month==12 && day>=15 && day<=18) { close1 = 2; open2 = 6.5; } 
	else if (month==12 && day==19) { close1 = 2; open1 = 6.5; }
	else if (month==12 && day==20) { open1 = 13; close1 = 17; }

	// May pre-final exam hours
	else if (month==5 && day>=4 && day<=7) { close1 = 2; open2 = 7; } 
	else if (month==5 && day==8) { close1 = 2; open2 = 7; }

	// May final exam hours
	else if (month==5 && day==9) { close1 = 1; open2 = 9; }
	else if (month==5 && day==10) { close1 = 1; open2 = 10; }
	else if (month==5 && day>=11 && day<=14) { close1 = 2; open2 = 6.5; } 
	else if (month==5 && day==15) { close1 = 2; open2 = 6.5; close2 = 18; }
	else if (month==5 && day>=16 && day<=17) { open1 = 13; close1 = 17; } 
	
	// Summer hours (May 19-Aug 8): M-R 7am-10pm, F 7am-5pm, Sat 9am-5pm, Sun 2pm-10pm
	else if ((month==5 && day>=18) || month==6 || month==7 || (month==8 && day<=7))
	switch (weekday) {
		case 5: open1 = 7; close1 = 17; break;
		case 6: open1 = 9; close1 = 17; break;
		case 0: open1 = 14; close1 = 22; break;
		default: open1 = 7; close1 = 22; break;
	}

	// normal hours: M-R 7am-midnight, F 7am-10pm, S 9am-10pm, Sun noon-midnight
	else switch (weekday) {
		case 5: open1 = 7; close1 = 22; break;
		case 6: open1 = 9; close1 = 22; break;
		case 0: open1 = 12; break;
		default: open1 = 7; break;
	}
	
	// do not edit this file below this line
///////////////////////////////////////////////////////////////////////////////////////////

	// prepare display of today's hours
	var openToday = open1 != close1;
	var libHours = "closed";
	var libOpenNow = "closed";
	if (openToday) {
		var hm = hours + minutes/60;
		var openNow = (hm >= open1 && hm < close1) || (hm >=open2 && hm < close2);
		libOpenNow = openNow ? "open" : "closed";
		libHours = displayTime(open1) + "-" + displayTime(close1);
		if (open2 != close2)
			libHours += "," + displayTime(open2) + "-" + displayTime(close2);
	}
	// display today's open hours
	document.getElementById(libHoursID).innerHTML = libHours;

	// display current time
	if (libHoursID == 'libHoursToday') 
	document.getElementById('currtime').innerHTML =  displayTime(hours + minutes/60) + " " + weekdays[weekday] + " " + month + "/" + day + "/" + year;

	return libOpenNow;
}

function startTime()
{
	// display whether the library is open or closed right now
	var today = new Date();
	var libOpenNow = getLibHours(today, 'libHoursToday');
	document.getElementById('libOpenNow').innerHTML = libOpenNow;

	// display whether the library is open or closed tomorrow
	var tomorrow = new Date();
	tomorrow.setDate(tomorrow.getDate()+1);
	var libOpenTomorrow = getLibHours(tomorrow, 'libHoursTomorrow');

	// check again every 30 seconds
	var t = setTimeout('startTime()',30000);
}
