/*************************
 **
 ** FootNotes 0.9.1b by Your Mom
 **
 ** Released to the Public Domain
 **
 ** Works on modern browsers, fails gracefully on old ones.
 **
 
 inspired by OverLib ... but more simple and to the point,
 about 6K of code but supports fewer browsers.
 
 tested and works on: 
 windows XP + IE 6
 windows 2000 + IE 5.00.3502.1000
 mac firefox 0.8 (should work on regular Mozilla & recent Netscapes)
 windows firefox 0.8
 mac Safari 1.2.1 (v125.1)   (bodes well for Konqueror)
 
 Does not "work", but bails out without error messages on:
 
 mac os X opera 6.03
 windows IE 3
 mac os 9 netscape 4.7
 
** ** ** ** ** ** ** ** ** ** ** **

How to apply to your page:

1) put all footnotes at the bottom of the page, 
   inside a div with id="footnotes"
   
2) the footnotes MUST in a <ul> or <ol> 
   with id="footnotelist"

3) it's up to you to make sure the number of footnotes in 
   the text matches the ones at the bottom, and they match up,
   meaning the first footnote link appearing in the text
   corresponds to the first footnote in the list, and so on with
   the second, third, etc.

4) for each footnote in the text, you must make the link (<a> tag)
    have class="fnmark". it's up to you to come up with a consistent
    href and  id scheme. I highly recommend you use the one shown in
    footnote_test.htm included in this archive.

5) at the end of each footnote's text, you should put a return link
   which will be used by old browsers to get back to the text after
   reading the footnote. if you give a class="returner", it won't be
   shown on modern browsers that don't need it, or print it

6) attach the style sheet "footnotes.css" as shown in footnote_test.htm
   ... edit the style sheet if you know CSS well and need to change
   colors,etc..
 
7) add this as the very first line after the <body> tag:
   <div id="footnoteview" style="position:absolute; z-index:1000; visibility:hidden;"></div>

8) save a few bytes of bandwidth by deleting these comments

 *    
 */

// user-definable options:

FN_Heading = "Footnote:";
FN_CloseGadget = "[&nbsp;X&nbsp;]";

FN_bgColor = "#eee";
FN_textColor = "#000";
FN_headBgColor = "#000";
FN_headTextColor = "#fff";

FN_Width = "200px"; // use CSS notation in here 200px, 10em, etc.

FN_borderWidth = "2px";
FN_borderColor = "#777";
FN_borderStyle = "solid";
FN_padding= "1em";

// don't edit below here unless you know something about something

var	FN_Div, FN_List, FN_Lang,  FN_CurrentOpen, FN_Bailed;
var FN_FootNotes = new Array();
var FN_Version = "0.9b";


var FN_Head = '<p class="fnhead"><a onmouseover="FN_Close();" style="float:right">' + FN_CloseGadget + '</a>' + FN_Heading + '</p>';


function setUpFootNotes() {
	// do nothing in browsers that do not support DOM
	FN_Bailed = false;
	var bailOut = true;
	if ( document.getElementById && document.getElementsByTagName  ) {
		bailOut = false;
	}
	if ( bailOut == true ) {
		FN_Bailed = true;
		return true;
	}
	FN_Div = document.getElementById("footnoteview");
	// again make sure the browser can "handle it"
	bailOut = true;
	if ( FN_Div.innerHTML != null && FN_Div.cloneNode ) {
		bailOut = false;
	}
	if ( bailOut == true ) {
		FN_Bailed = true;
		return true;
	}
	// ok, if we got this far, it's looking good
	// get the fn texts
	var fnroot = document.getElementById("footnotelist");
	FN_List = new Array();
	var fnindex = 0;
	for ( var x = 0; x < fnroot.childNodes.length; ++x ) {
		// only add element nodes, because some browsers include
		// text nodes in the childNodes array
		if ( fnroot.childNodes[ x ].nodeType == 1 ) {
			FN_List[fnindex] = fnroot.childNodes[x];
			++fnindex;
			// not using Array.push because IE5 doesn't support it
		}
		
	}
	
	var anx = document.getElementsByTagName("a");
	var fnMarkCount = 0;
	for ( var i = 0; i < anx.length; ++i ) {
		if ( typeof anx[i].className !== undefined ) {
			var aclass = anx[i].className;
		} else {
			var aclass = anx[i].getAttribute("class");
		}
		if ( aclass == "fnreturn" ) {
			anx[i].setAttribute("class", "noshow");
			anx[i].className = "noshow";
			continue;
		}
		if ( aclass !== "fnmark" ) continue;
		
		FN_FootNotes[fnMarkCount] = new FootNote( anx[i], FN_List[ fnMarkCount ], fnMarkCount );
		++fnMarkCount;
		
	}
	//document.getElementById("footnotes").style.display = "none";
}


// FootNote Object
function FootNote ( srcAnchor, listItem, index ) {
	this.srcAnchor = srcAnchor;
	this.listItem = listItem;
	this.index = index;
	if ( typeof listItem != 'undefined' ) {
		this.overStuff = FN_Head + "\n" + listItem.innerHTML;
	}
	else
	{
		this.overStuff = FN_Head;
	}
	if ( srcAnchor.addEventListener ) {
		srcAnchor.addEventListener("mouseover",FN_Show,true);
	}
	else if ( srcAnchor.attachEvent ) {
		srcAnchor.attachEvent("onmouseover",FN_Show);
	}
	
	return this;
}

function FN_Show() {
	ev = FN_Show.arguments[0];
	// determine target object
	if ( ev.srcElement ) var obj = ev.srcElement;
	  else if ( ev.target ) var obj = ev.currentTarget;
	    else var obj = null;
		
	
	// If browser gives the text node, bubble up until we get an Element
	
	while ( obj.nodeType !== 1 ) obj = obj.parentNode;
	
	var found = false;
	FN_CurrentOpen = -1;
	for ( var i = 0; i < FN_FootNotes.length; ++i ) {
		if ( FN_FootNotes[i].srcAnchor.id == obj.id ) {
			found = true;
			FN_CurrentOpen = i;
			break;
		}
	}
	var x = ev.clientX;
	var y = findPosY(obj) + 12;
	fv = document.getElementById("footnoteview");
	fv.innerHTML = FN_FootNotes[FN_CurrentOpen].overStuff;
	sty = "top:" + y + "px; right:200px; position:absolute; visibility:visible; background-color:"+FN_bgColor+"; color:"+FN_textColor+"; z-index:1000; border: "+FN_borderWidth+" "+FN_borderStyle+" " +FN_borderColor+"; padding:"+FN_padding+";";
	if ( fv.style.position !== null ) {
 		 fv.style.position         = "absolute";
		 fv.style.visibility       = "visible";
		 fv.style.top              = y + "px";
		 fv.style.backgroundColor  = FN_bgColor;
		 fv.style.color            = FN_textColor;
		 fv.style.width            = FN_Width;
		 fv.style.right			   = "200px";
		 fv.style.borderWidth	   = FN_borderWidth;
		 fv.style.borderColor      = FN_borderColor;
		 fv.style.borderStyle      = FN_borderStyle;
		 fv.style.padding          = FN_padding;
	}
	else if ( fv.setAttribute ) {
		fv.setAttribute( "style" , sty);
	}
	return true;
}

function FN_Close() {
	FN_Div.style.visibility="hidden";
	FN_Div.innerHTML='';
	return true;
}



// thanks to quirksmode.org for this function:
function findPosY(obj)
{
        var curtop = 0;
        if (obj.offsetParent)
        {
                while (obj.offsetParent)
                {
                        curtop += obj.offsetTop
                        obj = obj.offsetParent;
                }
        }
        else if (obj.y)
                curtop += obj.y;
        return curtop;
}

if ( window.addEventListener ) window.addEventListener("load",setUpFootNotes,true);
  else if ( window.attachEvent ) window.attachEvent("onload",setUpFootNotes);
    else window.onload=setUpFootNotes;
