// (c) 2005 by Yves <yves@raubacapeu.net>

var lastVisible = null;

function pageAddress(uri) {
	var i = uri.indexOf('#');
	return (i < 0) ? uri : uri.substr(0,i);
}

function pageFragment(uri) {
	var i = uri.indexOf("#[");
	if (i < 0 ) return null;
	var u1 = uri.substr(i);
	i = u1.indexOf(']');
	if (i < 0 ) return null;
	return u1.substr(2, i-2);
}
		
function updateURLAnchor(name) {
	var uri = pageAddress(location.href);
	uri = uri + "#[" + name + "]";
	if (uri != location.href) {
		location.href = uri;
	}
}

function updateMetaContent(name) {
	var tDiv = findDiv(name);
	if (tDiv == null) return;
	var ps = tDiv.getElementsByTagName("p");
	if (ps == null) return;
	var allMeta = document.getElementsByTagName("meta");
	var ok = false;
	for (var i=0; i < allMeta.length; i++) {
		if (allMeta[i].getAttribute("name") == "description") {
			allMeta[i].setAttribute("content", ps[0].firstChild.nodeValue);
			ok = true;
			break;
		}
	}
	if (!ok) {
		var allHead = document.getElementsByTagName("head");
		var newMeta = document.createElement("meta");
		newMeta.setAttribute("name", "description");
		newMeta.setAttribute("content", ps[0].firstChild);
		allHead[0].appendChild(newMeta);
	}
}

function updatePrevLink(name) {
	var tDiv = findDiv(name);
	if (tDiv == null) return;
	var allLinks = document.getElementsByTagName("link");
	var ok = false;
	var sibl = tDiv.previousSibling;
	if (allLinks != null) {
		for (var i=0; i < allLinks.length; i++) {
			if (allLinks[i].getAttribute("rel") == "prev") {
				if ((sibl instanceof Element) && (sibl.tagName == "DIV")) {
					allLinks[i].setAttribute("href", "#["+sibl.getAttribute("id")+"]");
				} else {
					allLinks[i].parentNode.removeChild(allLinks[i]);
				}
				ok = true;
				break;
			}
		}
	}
	if (!ok && (sibl instanceof Element) &&  (sibl.tagName == "DIV")) {
		var allHead = document.getElementsByTagName("head");
		var newLink = document.createElement("link");
		newLink.setAttribute("rel", "prev");
		newLink.setAttribute("href", "#["+sibl.getAttribute("id")+"]");
		newLink.onclick = followHeadLink;
		allHead[0].appendChild(newLink);
	}
}

function updateNextLink(name) {
	var tDiv = findDiv(name);
	if (tDiv == null) return;
	var allLinks = document.getElementsByTagName("link");
	var ok = false;
	var sibl = tDiv.nextSibling;
	if (allLinks != null) {
		for (var i=0; i < allLinks.length; i++) {
			if (allLinks[i].getAttribute("rel") == "next") {
				if ((sibl instanceof Element) && (sibl.tagName == "DIV")) {
					allLinks[i].setAttribute("href", "#["+sibl.getAttribute("id")+"]");
				} else {
					allLinks[i].parentNode.removeChild(allLinks[i]);
				} 
				ok = true;
				break;
			}
		}
	}
	if (!ok && (sibl instanceof Element) && (sibl.tagName == "DIV")) {
		var allHead = document.getElementsByTagName("head");
		var newLink = document.createElement("link");
		newLink.setAttribute("rel", "next");
		newLink.setAttribute("href", "#["+sibl.getAttribute("id")+"]");
		newLink.onclick = followHeadLink;
		allHead[0].appendChild(newLink);
	}
}

function updateMetaInformation(name) {
	updateMetaContent(name);
	updatePrevLink(name);
	updateNextLink(name);
}

function findDiv(name) {
	var allDivs = document.getElementsByTagName("div");
	for (var i=0; i < allDivs.length; i++) {
		if (allDivs[i].getAttribute("id") == name) {
			return allDivs[i];
		}
	}
}

function displayPic(name) {
	// find the right id, as getElementById doesn't work
	var allDivs = document.getElementsByTagName("div");
	var fdiv = findDiv(name);
	if (fdiv != null) {
		if (lastVisible != null) {
			lastVisible.style.visibility = "hidden";
			lastVisible.setAttribute("class","invisible");
		}
		lastVisible = fdiv;
		lastVisible.style.visibility = "visible";
		lastVisible.setAttribute("class","");
		updateURLAnchor(name);
		updateMetaInformation(name);
	}
}

function displayPicture(e) {
	// get the target node, then its next sibling
	var node = e.currentTarget;
	var base = node.getAttribute("src");
	var medName = base.substr(base.lastIndexOf('/')+1).replace(".png", "");
	displayPic(medName);
}

function followHeadLink(e) {
	// get the target node, then its next sibling
	var linkNode = e.currentTarget;
	var base = linkNode.getAttribute("href");
	if (base != null) {
		var medName = pageFragment(base);
		displayPic(medName);
	}
}

function init() {
	var divs = document.getElementsByTagName("div");
	var mainDiv = null;
	var first = null;
	var uriFrag = pageFragment(location.href);
	var checkfirst = (uriFrag == null);

	for (var i = 0; i < divs.length; i++) {
		if (divs[i].getAttribute("class") == "fbody") {
			mainDiv = divs[i];
			break;
		}
	}
	var imgList = document.getElementsByTagName("img");
	var fragment = document.createDocumentFragment();
	var realfirst = null;
	for (var i = 0; i < imgList.length; i++) {
		var navfrag = document.createDocumentFragment();
		var pNav = document.createElement("p");
		var bpNav = document.createElement("p");
		pNav.setAttribute("class", "navbar");
		bpNav.setAttribute("class", "navbar");
		if ( i == 0 ) {
			pNav.appendChild(document.createTextNode("\u00a0\u00a0\u00a0\u00a0\u00a0|"));
			bpNav.appendChild(document.createTextNode("\u00a0\u00a0\u00a0\u00a0\u00a0|"));
		} else {
			var prevSrc = imgList[i-1].getAttribute("src");
			pName = prevSrc.substr(prevSrc.lastIndexOf('/')+1).replace(".png", "");
			var newA = document.createElement("a");
			newA.setAttribute("href", "#["+pName+"]");
			newA.appendChild(document.createTextNode("prev"));
			newA.onclick = followHeadLink; 
			pNav.appendChild(newA);
			pNav.appendChild(document.createTextNode(" |"));
			var newbA = newA.cloneNode(true);
			newbA.onclick = followHeadLink; 
			bpNav.appendChild(newbA);
			bpNav.appendChild(document.createTextNode(" |"));
		}
		if ( (i+1) == imgList.length ) {
			pNav.appendChild(document.createTextNode("\u00a0\u00a0\u00a0\u00a0\u00a0"));
			bpNav.appendChild(document.createTextNode("\u00a0\u00a0\u00a0\u00a0\u00a0"));
		} else {
			var nextSrc = imgList[i+1].getAttribute("src");
			nName = nextSrc.substr(nextSrc.lastIndexOf('/')+1).replace(".png", "");
			var newA = document.createElement("a");
			newA.setAttribute("href", "#["+nName+"]");
			newA.appendChild(document.createTextNode("next"));
			newA.onclick = followHeadLink; 
			pNav.appendChild(document.createTextNode(" "));
			pNav.appendChild(newA);
			var newbA = newA.cloneNode(true);
			newbA.onclick = followHeadLink; 
			bpNav.appendChild(document.createTextNode(" "));
			bpNav.appendChild(newbA);
		}
		var node = imgList[i];
//		if (node.hasAttribute("width")) {
			node.onclick = displayPicture;
			var newDiv = document.createElement("div");
			var base = node.getAttribute("src");
			var medName = base.substr(base.lastIndexOf('/')+1).replace(".png", "");
			if (realfirst == null) {
				realfirst = medName;
			}
			newDiv.setAttribute("id", medName);
			var newImg = document.createElement("img");
			newImg.setAttribute("src",medName + "-med.jpg");
			var altText = node.getAttribute("alt");
			newImg.setAttribute("alt", altText);
			newDiv.appendChild(pNav);
			newDiv.appendChild(newImg);
			var newP = document.createElement("p");
			newP.setAttribute("class", "caption");
			newP.appendChild(document.createTextNode(altText));
//			newP.appendChild(document.createTextNode(" [checkfirst=" + checkfirst + "]"));
//			newP.appendChild(document.createTextNode(" [first=" + first + "]"));
//			newP.appendChild(document.createTextNode(" [uriFrag=" + uriFrag + "]"));
//			newP.appendChild(document.createTextNode(" [name=" + medName + "]"));
			newDiv.appendChild(newP);
			newDiv.appendChild(bpNav);
			fragment.appendChild(newDiv);
			if ((checkfirst && (first == null)) || 
			    (!checkfirst && (medName == uriFrag))) {
				first = medName;
			} else {
				newDiv.setAttribute("class","invisible");
			}
//		}
	}
	mainDiv.appendChild(fragment);
	if (first == null) {
		displayPic(realfirst);
	} else {
		displayPic(first);
	}
}

