var checking			= -1;
var positioning			= -1;
var animating			= -1;

var positioningMeta		= -1;
var animatingMeta		= -1;

var scrollCheckDelay	= 10;
var positionNavDelay	= 1000;
var positionMetaDelay	= 500;
var step				= 1;
var animateDelay		= 15;

if (((navigator.appName=="Netscape")&&(parseInt(navigator.appVersion) > 4)) || ((navigator.appName.indexOf("Microsoft")!=-1)&&(parseInt(navigator.appVersion.substr(versionIndex, 5)) == 4)) ) {
//MAC or NS6 or IE4, all slower than normal
//replace with browser test with speedtest
	positionNavDelay = 10;
	positionMetaDelay = 5;
	animateDelay = 5;
	step = 2;
}

var isHome;
var navigationContainer = null;
var metaNav = null;

function setNavigationHandlers(menuID, isHomePage) {
	isHome = isHomePage;
	if(menuID >= 0) navigationContainer = getMenu(menuID).container; //Get container from menu
	metaNav = new metaNavObject(); //Get metalayer from content
	if (navigator.platform.indexOf('Mac')==0) {
		if ((navigator.appName.indexOf("Microsoft")!=-1))	window.onscroll = window.onresize = setMacMover;
		else checking = setTimeout("moveMenusMac()", scrollCheckDelay);
		metaNav.move(0, calculateBottom());
		metaNav.show();
	} else {
		checking = setTimeout("checkForScroll()", scrollCheckDelay);
	}
}

function checkForScroll() {
	clearTimeout(checking);
	checking = -1;
	var continueChecking = true;
	if ((calculateBottom() - 16) != metaNav.y) {
		if ((positioningMeta == -1) && (animatingMeta == -1)) {
			continueChecking = false;
			metaNav.hide();
			positioningMeta = setInterval("startMeta()",positionMetaDelay);
		}
	}
	if (!isHome && (calculateTop() !=  navigationContainer.y )) {
		if ((positioning == -1) && (animating == -1)) {
			continueChecking = false;
			navigationContainer.hide();
			positioning = setInterval("startTop()",positionNavDelay);
		}
	}
	if (continueChecking) checking = setTimeout("checkForScroll()", scrollCheckDelay);
}

function setMacMover() {
	if (checking == -1) checking = setTimeout("moveMenusMac()", scrollCheckDelay*10);
}
function moveMenusMac() {
	if (!(navigator.appName.indexOf("Microsoft")!=-1)) {
		clearTimeout(checking);
	}
	checking = -1;
	metaNav.hide();
	metaNav.move(0, calculateBottom());
	metaNav.show();

	if (!isHome) {
		navigationContainer.hide();
		navigationContainer.move(0, calculateTop());
		navigationContainer.show();
	}
	if (!(navigator.appName.indexOf("Microsoft")!=-1))
		checking = setTimeout("moveMenusMac()", scrollCheckDelay);
}

var topsteps;
function startTop() {
	var newPosition = calculateTop();
	navigationContainer.move(0, newPosition - 16);
	clearInterval(positioning);
	positioning = -1;
	topsteps = 16;
	navigationContainer.show();
	animating = setInterval("animateTop()",animateDelay);
}

var metasteps;
function startMeta() {
	var newPosition = calculateBottom();
	metaNav.move(0, newPosition);
	clearInterval(positioningMeta);
	positioningMeta = -1;
	metasteps = -16;
	metaNav.show();
	animatingMeta = setInterval("animateMeta()",animateDelay);
}

function animateTop() {
	topsteps = topsteps - step;
	if(topsteps > 0) navigationContainer.move(0, navigationContainer.y + step);
	else {
		navigationContainer.move(0, navigationContainer.y + (topsteps+step));
		clearInterval(animating);
		animating = -1;
		if (checking == -1) checking = setTimeout("checkForScroll()", scrollCheckDelay);
	}
}

function animateMeta() {
	metasteps = metasteps + step;
	if(metasteps <= 0) metaNav.move(0, metaNav.y - step);
	else {
		metaNav.move(0, metaNav.y - (metasteps-step));
		clearInterval(animatingMeta);
		animatingMeta = -1;
		if (checking == -1) checking = setTimeout("checkForScroll()", scrollCheckDelay);
	}
}

function calculateTop() {
/*	if(document.documentElement) return document.documentElement.scrollTop;
	else if(document.body) return document.body.scrollTop;
	else return window.pageYOffset;
*/
	if (navigator.appName.indexOf("Microsoft")!=-1) {
		if (parseInt(navigator.appVersion.substr(versionIndex, 5)) == 6)
			return (document.documentElement.scrollTop);
		else return (document.body.scrollTop);
	} else return (window.pageYOffset);
}

function calculateBottom() {
/*	var newPosition;

	if(document.documentElement) newPosition = document.documentElement.scrollTop + document.documentElement.clientHeight;
	else if(document.body) newPosition = document.body.scrollTop + document.body.clientHeight;
	else newPosition = window.pageYOffset + window.innerHeight;
*/
	var newPosition;
	var oCanvas;

	if(navigator.appName.indexOf("Microsoft")!=-1) {
/*		if(parseInt(navigator.appVersion.substr(versionIndex,5)) == 6)
			oCanvas = document.documentElement;
		else*/ oCanvas = document.body;
		newPosition = oCanvas.scrollTop + oCanvas.clientHeight;
	} else newPosition = window.pageYOffset + window.innerHeight;

	if(navigator.platform.indexOf('Mac')==0) newPosition -= 16;
	if(newPosition < 460) newPosition = 460;//maximum height
	return(newPosition);
}

function metaNavObject() {

	if (dom == W3C) this.div = document.getElementById("metanav");
	else if (dom == IE) this.div = document.all.metanav;
	else if (dom == NS) this.layer = document.layers.metanav;
 
	switch(dom) {
		case W3C: case IE:
			this.move = function moveMeta(x,y){if(isNaN(y)) return; this.div.style.left = x+"px"; this.div.style.top = y+"px"; this.x = x; this.y = y;}
			this.hide = function hideMeta(){this.div.style.visibility = "hidden";this.div.style.display = "none";}
			this.show = function showMeta(){this.div.style.visibility = "visible";this.div.style.display = "block";}
		break;
		case NS:
			this.move = function moveMeta(x,y) {if(isNaN(y)) return; this.layer.moveTo(x,y);this.y = y; this.x = x};
			this.hide = function hideMeta(){this.layer.visibility = "hide";};
			this.show = function showMeta(){this.layer.visibility = "show";};
		break;
	}
	return this;
}