function addListenerNav(id){
	tgt = document.getElementById('L'+id);
	tgt.onmouseover = checkNav;
	tgt.onmouseout = checkNav;
}

function checkNav(event){
	var e = window.event || event;
	
	if (e.target){
		var tgt = e.target;

		if (tgt.className.indexOf('inner') != -1 && e.type == 'mouseover'){
			tgt.style.backgroundColor = '#3E6FA0';
		}else if (tgt.className.indexOf('inner') != -1 || tgt.id.indexOf('link')){
			tgt.style.backgroundColor = '#000033';
		}else if (tgt.id.indexOf('link') != -1 && e.type == 'mouseover'){
			new_id = tgt.id.replace('link','nav');
			document.getElementById(new_id).style.backgroundColor = '#3E6FA0';
		}else if (tgt.id.indexOf('link') != -1){
			new_id = tgt.id.replace('link','nav');
			document.getElementById(new_id).style.backgroundColor = '#000033';
		}
	}else{
		var tgt = e.srcElement;

		if (tgt.className.indexOf('outer') != -1 && e.type == 'mouseover'){
			document.getElementById('nav'+tgt.id).style.backgroundColor = '#3E6FA0';
		}else if (tgt.className.indexOf('outer') != -1 || tgt.id.indexOf('link')){
			document.getElementById('nav'+tgt.id).style.backgroundColor = '#000033';
		}else if (tgt.id.indexOf('link') != -1 && e.type == 'mouseover'){
			new_id = tgt.id.replace('link','nav');
			document.getElementById(new_id).style.backgroundColor = '#3E6FA0';
		}else if (tgt.id.indexOf('link') != -1){
			new_id = tgt.id.replace('link','nav');
			document.getElementById(new_id).style.backgroundColor = '#000033';
		}
	}
}

function initNav(exception){
	for (i=1;i<=6;i++){
		if (i != exception){
			addListenerNav(i);
		}else{
			document.getElementById('navL'+i).style.backgroundColor = '#3E6FA0';// = document.getElementById('nav'+i).className.replace('_nav','_nav_over');
		}
	}
}

function checkScroll() {
	if (document.onscroll){
		document.onscroll = function(){
			doScroll();
		}
	}else{
		window.onscroll = function(){
			doScroll();
		}
	}
}

function doScroll(){
	if (getOffset()['Y'] > 5){
		showAnchor();
		if (window.event) {
			updateAnchorPosition();
		}
	}else{
		hideAnchor();
	}
}

function createAnchor(){
	new_div = document.createElement('div');
	new_div.setAttribute("id","top_anchor_link");
	new_div.innerHTML = '<a href="#">Back to Top</a>';
	document.body.appendChild(new_div);
	checkScroll();
}

function updateAnchorPosition(){
	anchor_link = document.getElementById('top_anchor_link');
	anchor_link.style.position = "absolute";
	anchor_link.style.top = (getWindowDims()['h'] + getOffset()['Y']) - 20;
}

function showAnchor() {
	new_div.style.visibility = "visible";
}

function hideAnchor() {
	new_div.style.visibility = "hidden";
}

function getOffset() {
	
	var offset = {};
	
	// Get page scroll so window doesn't pop up
	// to the top of the page when dragged or resized
	
	if (document.documentElement && document.documentElement.scrollTop) {
		offset["X"] = document.documentElement.scrollLeft;
		offset["Y"] = document.documentElement.scrollTop;
	}else if (document.body) {
		offset["X"] = window.document.body.scrollLeft;
		offset["Y"] = window.document.body.scrollTop;
	}else {
		offset["X"] = window.pageXOffset;
		offset["Y"] = window.pageYOffset;
	}
	
	return offset;
	
}

function getWindowDims(){
	
	var dims = {};
	
	if (window.innerHeight) {
		dims['h'] = window.innerHeight
		dims['w'] = window.innerWidth
	}else if (document.documentElement && document.documentElement.clientHeight) {
		dims['h'] = document.documentElement.clientHeight
		dims['w'] = document.documentElement.innerWidth
	}else if (document.body) {
		dims['h'] = document.body.clientHeight
		dims['w'] = document.body.innerWidth
	}	
	
	return dims;
	
}

