var historyLength = 5; //number of links to maintain
var docURL = location.href;
//if there is no <title> defined, use the name of the file for the display text in the recent docs module
var docTitle = (document.title == "" || document.title == undefined) ? getFileName() : document.title;


function drawRecentPages() {
	var recentPages = getRecentPagesArray();
	printTableHeader();
	
	if (recentPages.length != 0) {
		var reversePages = recentPages.reverse();
		for (var i=0; i<recentPages.length; i++) {
			printLink(recentPages[i][1], recentPages[i][0]);
		}
	} else {
		printDefaultCopy();
	}
}

function getRecentPagesArray() {
	var curValues = new Array();
	var curCookie = getCookie("recentDocsCookie");
	if (curCookie != null){
		curValues = curCookie.split("|||");
		for (var i=0; i<curValues.length; i++) {
			curValues[i] = curValues[i].split("&&&");
		}
	}
	return curValues;
}

function getFileName() {
	var f = docURL.substring(docURL.lastIndexOf("/") + 1, docURL.length);
	return f;
}

function getMyCookieData() {
	var c = docTitle + "&&&" + docURL;
	return c;
}

function updateRecentPages() {
	var curCookie = getCookie("recentDocsCookie");
	var curString;
	if (curCookie != null){
		var curValues = getRecentPagesArray();
		//remove link to current page if it already exists in the array
		for (var i=0; i<curValues.length; i++) {
			var curElem = curValues[i];
			var curElemTitle = curElem[0];
			if (curElemTitle == docTitle) {
				curValues.splice(i, 1);
				break;
			}
		}
		
		for (var i=0; i<curValues.length; i++) {
			curValues[i] = curValues[i].join("&&&");
		}
		
		curValues.push(getMyCookieData());
		if (curValues.length > historyLength) {
			curValues.shift();
		}
		curString = curValues.join("|||");
	} else {
		curString = getMyCookieData();
	}
	setCookie("recentDocsCookie", curString);
}

function printLine(whichText) {
	document.write(whichText + "\n");
}

function printTableHeader() {
	printLine('<table width="155" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFF7DA">');
	printLine('<tr valign="top" bgcolor="#FFEFB4">');
	printLine('<td width="23"><img src="../images/rtnav_icon_recent_doc.gif" width="29" height="20"></td>');
	printLine('<td>&nbsp;</td>');
	printLine('<td><span class="rntabletop">Recent Documents</span></td></tr>');
	printLine('<tr>');
	printLine('<td colspan="3" bgcolor="#FFFFFF"><img src="../images/spacer.gif" width="1" height="1"></td></tr>');
	printLine('<tr>');
	printLine('<td colspan="3"><img src="../images/spacer.gif" width="1" height="8"></td></tr>');
}

function printLink(whichURL, whichText) {
	printLine('<tr><td width="23">&nbsp;</td>');
	printLine('<td colspan="2"><a href="' + whichURL + '" class="rightnav">' + whichText + '</a></td></tr>');
	printLine('<tr><td colspan="3"><img src="../images/spacer.gif" width="1" height="8"></td></tr>');
}

function printDefaultCopy(whichURL, whichText) {
	printLine('<tr><td width="23">&nbsp;</td>');
	printLine('<td colspan="2" class="rntabletop">For your convenience, we will save links to documents you have recently browsed here.</td></tr>');
	printLine('<tr><td colspan="3"><img src="../images/spacer.gif" width="1" height="8"></td></tr>');
}

function printTableFooter() {
	printLine('<tr>');
	printLine('<td colspan="3"><img src="../images/spacer.gif" width="1" height="8"></td></tr></table>');
}

function deleteRecentDocs() {
	deleteCookie("recentDocsCookie", "/");
}

function alertCookie() {
	var newVal = getCookie("recentDocsCookie");
	alert("recentDocsCookie: " + newVal);
}

function setCookie(cookieName, cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays == null || nDays==0) {
		nDays = 200;
	}
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + '; path=/';
}

// name - name of the cookie
// * return string containing value
// of specified cookie or null if cookie
// does not exist
function getCookie(name) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1) {
		return null;
	}
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) {
		cookieEndIndex = document.cookie.length;
	}
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

// name - name of the cookie
// [path] - path of the cookie
// (must be same as path used to create cookie)
// [domain] - domain of the cookie
// (must be same as domain used to create cookie)
// * path and domain default if assigned
// null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}