// M4/HTML macro package - Calendar support
// Copyright 2010 Perette Barella.
// All rights reserved.
// vim:ts=8:sw=8
// $Id: calendar.js,v 1.3 2011/02/06 13:49:10 perette Exp $

// ================== Convert between 12/24 hour time ====================


function elementHasClass (element, classname) {
	if (element.className == undefined) {
		return false;
	}
	var classes = element.className.split (" ");
	for (var i in classes) {
		if (classes [i] == classname) {
			return true;
		}
	}
	return false;
}


// Change the time, stored in a text element inside the passed SPAN,
// to a given format.
// timeFormat is one of:
//   "12" - to 12 hour time (loses AM/PM, so no return to prior format).
//   "24" - to 24 hour format.
//   "ampm" - to 12 hour format with trailing AM/PM.
function change_time_format (element, timeFormat) {
	if (element.childNodes.length != 1) {
		return;
	}
	var time = element.childNodes [0];
	if (!time || time.nodeName != "#text") {
		return;
	}
	var brokenTime = time.data.split (":");
	if (brokenTime.length != 2) {
		return;
	}
	var hour = parseInt (brokenTime [0], 10);
	var minute = brokenTime [1].substring (0, 2);
	if (isNaN (hour) || isNaN (minute)) {
		return;
	}
	var brokenMinute = brokenTime [1].split (" ");
	if (brokenMinute.length == 2) {
		if (hour == 12) {
			hour = 0;
		}
		if (brokenMinute [1] == "PM") {
			hour += 12;
		}
	}
	if (timeFormat == "24") {
		time.data = hour + ":" + minute;
	} else {
		var am = " AM";
		if (hour > 11) {
			am = " PM";
			hour -= 12;
		}
		time.data = (hour == 0 ? 12 : hour) + ":" +
			    minute +
			(timeFormat == "12" ? "" : am);
		}
}


function change_calendar_time_format (timeFormat) {
	// Handle calendar format
	var spans = document.getElementsByTagName ("SPAN");
	for (var i in spans) {
		var span = spans [i];
		if (elementHasClass (span, "time")) {
			change_time_format (span, timeFormat);
		}
	}

	var abbrs = document.getElementsByTagName ("ABBR");
	for (var i in abbrs) {
		var abbr = abbrs [i];
		if (elementHasClass (abbr, "dtstart") ||
		    elementHasClass (abbr, "dtend")) {
			change_time_format (abbr, timeFormat);
		}
	}
}


// ================== Show/hide event categories ====================

var eventCategories;

function save_display_setting () {
	for (var i = 0; i < eventCategories.length; i++) {
		var category = eventCategories [i];
		var toggle = document.getElementById (category + "category");
		localStorage ['show' + category] = toggle.checked;
	}

	/* Save the 12/24 hour time selection. */
	toggle = document.getElementById ("timeformatcheckbox");
	if (toggle.checked) {
		localStorage ['timeformat'] = "24";
	} else {
		delete localStorage ['timeformat'];
	}
	/* Use the restore function to update display. */
	restore_display_setting();
}


function restore_display_setting () {
	/* Build a list of classes to *not* display. */
	var bodyclass = "";
	for (var i = 0; i < eventCategories.length; i++) {
		var category = eventCategories [i];
		show = (localStorage ['show' + category] != 'false');
		var toggle = document.getElementById (category + "category");
		if (show) {
			toggle.checked = true;
		} else {
			bodyclass += " no" + category;
			toggle.checked = false;
		}
	}
	/* Stick hidden categories list into the <BODY> tag,
	   and expect CSS does the rest. */
	var bodies = document.getElementsByTagName ("BODY");
	bodies [0].className = bodyclass;
	/* Update times to 12/24 hour per selection. */
	var toggle = document.getElementById ("timeformatcheckbox");
	if (localStorage ['timeformat']) {
		toggle.checked = true;
	}
	change_calendar_time_format (toggle.checked ? "24" : "ampm");
}


function initialize_category_display (categories) {
	eventCategories = categories.split (/[, \t]+/);
	var controls = document.getElementsByClassName ("controls");
	for (var i = 0; i < controls.length; i++) {
		control = controls [i];
		control.style.display = "";
	}
	restore_display_setting()
}


