// references are links that link to the current document

function inClassName(element, className){
    classes=element.className.split(' ')
    for(c in classes){
        if(className == classes[c]){ return true;}
    }
    return false
}
function addClassName(element, className) {
    if(inClassName(element, className)) return;
    element.className += ' ' + className;
}
function removeClassName(element, className) {    
    classes=element.className.split(' ')
    newclasses = []
    for(c in classes){
        if(className != classes[c]){ newclasses[newclasses.length] = classes[c]; }
    }
    element.className = newclasses.join(' ');
}

lastref = false;

function setrefs(){
	links=document.getElementsByTagName("a");
	for(i=0;i<links.length;i++){
		if((document.all || links[i].hasAttribute('href')) && isref(links[i].getAttribute('href'))){
            links[i].onclick=function(){
            
				href=this.getAttribute('href');
				if(document.all){
					locus=window.location.toString();
					if(locus.substring(0, locus.indexOf('#')) == href.substring(0,locus.indexOf('#'))){
						href=href.substring(locus.indexOf('#'));}
				}
				ref = href.substring(1);
                targetref(ref)
            
			}
		}
	}
    if(frag = getfrag(window.location.toString())){
        targetref(frag.substring(1))
    }
}
function targetref(id){
    if(target = document.getElementById(id)){  //.parentNode
    	if(lastref)
    		removeClassName(lastref, 'selected_reference');
    	addClassName(target, 'selected_reference');
    	lastref = target;
    	if(document.all){
    		window.scrollY=document.body.scrollTop;
    		window.innerHeight=document.body.clientHeight;					
    	}
    	y=window.scrollY + window.innerHeight;
    	if(y-100 < target.offsetTop){
    		diff=(120+target.offsetTop-y);
    		window.scrollBy(0,diff);
    	}
    	return true;
    }
}
function getfrag(url){
    n = url.indexOf('#');
    if(n == -1){ return false; }
    return url.substring(n);
}
function isref(href){
	if(document.all){
		locus=window.location.toString();
		if(locus.substring(0, locus.indexOf('#'))==href.substring(0,locus.indexOf('#'))){
			href=href.substring(locus.indexOf('#'));
		}
	}
	return ('#' == href.substring(0,1));
}
