// Used for the minutes and photos pages
// Photos: Enables the user to click on the name of an event and see all photos associated with it
// 	If the user clicks a different event, the photos for that event replace the photos for the first event
// Minutes: Enables the user to click on a date and see the minutes for that date; clicking on the date again hides the minutes
//	Can view minutes for multiple meetings at once

var year;
var eventName;
var usesIE = navigator.appName == "Microsoft Internet Explorer";

window.onload = function() {
	var events = $$(".event");
	for (var i = 0; i < events.length; i++) {
		events[i].onmouseover = highlight;
		events[i].onmouseout = unhighlight;
		events[i].onclick = togglePics;
	}

	var meetings = $$(".meeting");
	for (var i = 0; i < meetings.length; i++) {
		meetings[i].onmouseover = highlight;
		meetings[i].onmouseout = unhighlight;
		meetings[i].onclick = toggleMinutes;
	}
}

function highlight() {
	this.addClassName("highlighted");
}

function unhighlight() {
	this.removeClassName("highlighted");
}

// Shows all the pictures for the topic the user clicked, replacing any pictures that were already being viewed
function togglePics() {
	$("photos").addClassName("hasPhotos");
	if (usesIE) {
		var text = this.parentNode.parentNode.parentNode.innerText;
		year = text.split("\n")[0];
		eventName = this.innerText;
	} else {
		year = this.parentNode.parentNode.parentNode.firstChild.textContent;
		eventName = this.textContent;
	}
	var pics = $$("#photos a");
	for (var i = 0; i < pics.length; i++) {
		$("photos").removeChild(pics[i]);
	}
	url = "getPhotos.php?year=" + year + "&event=" + eventName;
	new Ajax.Request (
			url,
		{
			method: "get",
			onCreate: showPicsLoading,
			onSuccess: postPhotos
		}
	);
}

function showPicsLoading(ajax) {
	$("loading").style.display = "block";
}

// Injects the photos from a given event into the page
function postPhotos(ajax) {
	$("loading").style.display = "none";
	var imgNames = ajax.responseText.split("\n");
	if (usesIE) {
		$("about").innerText = imgNames[0];
	} else {
	 	$("about").textContent = imgNames[0];
	}
	var path = "photos/" + year + "/" + eventName + "/";
	for  (var i = 1; i < imgNames.length - 1; i++) {
		var link = document.createElement("a");
		link.href = path + imgNames[i];
		var img = document.createElement("img");
		img.src = path + "thumbnails/" + imgNames[i];
		link.appendChild(img);
		link.target = "_blank";
		$("photos").appendChild(link);
	}
}

// Displays the minutes for that date
function toggleMinutes() {
	if (usesIE) {
		var date = this.innerText;
		var year = this.parentNode.parentNode.parentNode.firstChild.nodeValue;
	} else {
		var date = this.textContent;
		var year = this.parentNode.parentNode.parentNode.firstChild.textContent;
	}

	url = "getMinutes.php?year=" + year + "&date=" + date;
	new Ajax.Request (
			url,
		{
			method: "get",
			onCreate: showMinsLoading,
			onSuccess: postMinutes
		}
	);
}

function showMinsLoading() {
	$("minuteText").removeClassName("hasMinutes");
	var children = $("minuteText").childElements();
	for (var i = 0; i < children.length; i++) {
		$("minuteText").removeChild(children[i]);
	}
	var p = document.createElement("p");
	p.innerText = "...Loading...";
	p.textContent = "...Loading...";
	$("minuteText").appendChild(p);
}

// Injects the minutes for the correct meeting into the page
// I originally had a different format for the minutes - each meeting was broken into separate topics with its own heading
// But after looking at this year's minutes, I changed the format so they were just one big list
function postMinutes(ajax) {
	var lines = ajax.responseText.split(/\n+/);
	var h2 = document.createElement("h2");
	h2.innerText = lines[0];
	h2.textContent = lines[0];
	var ul = addLines(lines, 1, 0);

	$("minuteText").removeChild($("minuteText").firstChild); 	// Remove loading message
	$("minuteText").addClassName("hasMinutes");
	$("minuteText").appendChild(h2)
	$("minuteText").appendChild(ul[0]);
}
/*
	var topics = ajax.responseText.split(/\n{2,}/);
	var h2 = document.createElement("h2");
	h2.innerText = topics[0];
	h2.textContent = topics[0];
	$("minuteText").appendChild(h2);
	for (var i = 1; i < topics.length; i++) {
		var lines = topics[i].split("\n");
		if (lines.length > 1) {	// if this topic has more than just a title (error correction)
			var h3 = document.createElement("h3");
			h3. innerText = lines[0];
			h3.textContent = lines[0];
			$("minuteText").appendChild(h3);
			var ul = addLines(lines, 1, 0);
			$("minuteText").appendChild(ul[0]);
		}
	}
}
*/

function addLines(lines, index, tabCnt) {
	var ul = document.createElement("ul");
	var li;
	while (index < lines.length) {
		if (lines[index].charAt(tabCnt) == "\t") {	// Go up a level
			var returnVals = addLines(lines, index, tabCnt + 1);
			li.appendChild(returnVals[0]);
			index = returnVals[1];
		} else if (tabCnt ==0 || lines[index].charAt(tabCnt - 1) == "\t") {	// Stay at current level
			li = document.createElement("li");
			li.innerText = lines[index].substring(tabCnt);
			li.textContent = lines[index].substring(tabCnt);
			ul.appendChild(li);
			index++;
		} else {	// Go down a level
			break;
		}
	}
	return [ul, index];
}