/** Functions for remembering and highlighting your previous place on the message board 
@licstart  The following is the entire license notice for the 
JavaScript code in this page.
Copyright (C) 2009 Pierce

The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend  The above is the entire license notice
for the JavaScript code in this page.
**/

// mark the entry that was most recent the last time you visited the msgboard
// and update the cookie with the new "recent" post.
function recent() {
	if (document.cookie) {
		hilightOldRecent();
		updateRecent();
	}	
}

// mark the entry that was most recent the last time you visited the msgboard
function hilightOldRecent() {
	if (document.cookie && (document.cookie.length > 0)) {
		var maindiv = document.getElementById("main");
		var prevRecentId = getOldRecentId();
		if (prevRecentId != null && prevRecentId != "") {
			var oldRecentEntry = document.getElementById(prevRecentId);
			if (oldRecentEntry != null && oldRecentEntry != maindiv.firstChild) {
				// add a border to indicate the previous breaking point
				var newHR = document.createElement("hr");
				newHR.color = "#c0bcc0";
				var newBR = document.createElement("br");
				
				maindiv.insertBefore(newBR, oldRecentEntry);
				maindiv.insertBefore(newHR, oldRecentEntry);
			}
		};
	}
}

// retrieve the id of the entry that was most recent on your previous visit
function getOldRecentId() {
	var cookiestart = document.cookie.indexOf("recent=");
	if (cookiestart != -1) {
		cookiestart += 7;
		var cookieend = document.cookie.indexOf(";",cookiestart);
		if (cookieend == -1) {
			cookieend = document.cookie.length;
		}
		var id = document.cookie.substring(cookiestart,cookieend);
		return unescape(id);
		
	}
	return null;
}

// update the cookie with the new recent entry id
function updateRecent() {
	var recentId = getRecentEntryId();
	if (recentId != null && recentId != "") {
		setNewRecentCookie(recentId);
	}
}

// find the id of the first <span> entry in the "main" div
function getRecentEntryId() {
	var maindiv = document.getElementById("main");
	if (maindiv != null && maindiv.hasChildNodes()) {
		return maindiv.firstChild.id;
	}
	return null;
}

// update the cookie with an entry id
function setNewRecentCookie(entryId) {

	var expire=new Date();
	expire.setFullYear(expire.getFullYear()+1);
	var expireStr = expire.toGMTString();

	if (entryId != null && document.cookie) {
		document.cookie="recent=" + escape(entryId) + ";expires=" + expireStr;
	}
}

//from MediaWiki/skins/common/wikibits.js
function hookEvent(hookName, hookFunct) {
	if (window.addEventListener) {
		window.addEventListener(hookName, hookFunct, false);
	} else if (window.attachEvent) {
		window.attachEvent("on" + hookName, hookFunct);
	}
}

hookEvent("load", recent);