function adjustNav(){
	if(document.getElementById && document.getElementById('leftnav')){
		var nav = document.getElementById('leftnav');
		var list = nav.getElementsByTagName('ul');
		for(var k=0;k<list.length;k++){
			var node = list[k].lastChild;
			while(node.nodeName.toLowerCase() != 'li'){node=node.previousSibling;}
			node.style.backgroundImage = 'none';
			list[k].parentNode.onmouseover = function(){
				var node = this.firstChild;
				while(node.nodeName.toLowerCase() != 'ul'){node=node.nextSibling;}
				node.style.marginTop = 5-this.scrollHeight+'px';
				node.style.backgroundPosition = "0 "+(this.scrollHeight-6)+'px';
			}
		}
	}
}
function backBtn(){
	if(document.getElementById && document.getElementById('backbtnlink')){
		document.getElementById('backbtnlink').onclick = function(){
			history.back();
			return false;
		}
	}	
}

function emailBtn(){
	if(document.getElementById && document.getElementById('emailbtnlink')){
		document.getElementById('emailbtnlink').onclick = function(){
			//this can be expanded upon
			this.href += document.location;
		}
	}
}

function linkActions(){
	var a = document.getElementsByTagName('a');
	for(var k=0;k<a.length;k++){
		if(a[k].className.indexOf('relateditem') != -1){
			a[k].onclick = function(){
				window.open(this.href,"renalinfo_related","width=500,height=400,scrollbars=yes,resizable=yes,menubar=yes");
				return false;
			}
		}
	}
}

function printBtn(){
	if(document.getElementById && document.getElementById('printbtnlink')){
		document.getElementById('printbtnlink').onclick = function(){
			print();
			return false;
		}
	}
}

function setFontBtns(){
	if(document.getElementById && document.getElementById('decreasefontbtnlink')){
		document.getElementById('decreasefontbtnlink').onclick = function(){
			setFontSize(-1);
			return false;
		}
		document.getElementById('increasefontbtnlink').onclick = function(){
			setFontSize(1);
			return false;
		}
	}
}

function setFontSize(pos){
	var size = Number(hasCookie()?hasCookie():3)+(pos?pos:0);
	size = size<0?0:size;
	size = size>6?6:size;
	saveCookie(size)
	document.documentElement.lastChild.style.fontSize = ['xx-small','x-small','small','medium','large','x-large','xx-large'][size];
}

function saveCookie(size){
	var date = new Date();
	date.setTime(date.getTime()+30*24*60*60*1000);
	document.cookie = "renalInfoFontSize="+size+"; path=/; expires="+date.toGMTString();
	return size;
}

function hasCookie(){
	var cookies = document.cookie.split(';');
	for(var i=0;i < cookies.length;i++){
		var c = cookies[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf("renalInfoFontSize") == 0) return c.substring(18,c.length);
	}
	return false;
}


function init(){
	if(arguments.callee.doOnce){return;}
	arguments.callee.doOnce=true;
	backBtn();
	emailBtn();
	printBtn();
	setFontBtns();
	setFontSize();
	adjustNav();
	linkActions();
}

if(document.addEventListener){document.addEventListener("DOMContentLoaded",init,null);}
window.onload=init;