// Allow presentation location to be optionally displayed

"use strict";

// open anonymous function
(function()
{
	var toggle_location = document.getElementById('toggle-location');
	// no location, exit
	if (!toggle_location)
		return;

	// define here because of later function references
	var is_visible;

	function honor_location_visibility()
	{
		this.firstChild.nodeValue = 
			is_visible ? "Hide Locations & Recordings" : "Show Locations & Recordings";

		// set display of all submenus
		var ulList = document.getElementsByTagName('ul');
		for (var ul_num = 0; ul_num < ulList.length; ul_num++)
			if (ulList[ul_num].className == 'location')
				ulList[ul_num].style.display = is_visible ? "block" : "none";
	
		this.blur();
	}

	// Create text child
	if (!toggle_location.firstChild)
		toggle_location.appendChild(document.createTextNode(""));

	var cookie_name = "display_location=";

	toggle_location.onclick = function() {
		is_visible = !is_visible;

		// must use real variable, not a copy
		honor_location_visibility.call(toggle_location);
		document.cookie = cookie_name + is_visible;

		return false;
	};

	var my_cookies = document.cookie;
	var value_offset = my_cookies.indexOf(cookie_name);

	if (value_offset != -1)
	{
		var start = value_offset + cookie_name.length;
		var end = my_cookies.indexOf(";", start);
		if (end == -1)
			end = my_cookies.length;
		// cast string to true/false
		is_visible = (my_cookies.substring(start, end) == 'true');
	}
	else
		// start locations as invisible
		is_visible = false;

	honor_location_visibility.call(toggle_location);

// close anonymous function
})();


