var checking			= -1;

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

var scrollCheckDelay	= 10;
var positionMetaDelay	= 500;
var step				= 1;
var animateDelay		= 15;
var metaHeight = 21;

var metaNav = null;
var alignTD = null;
var linksDiv = null;

function setBottomHandler() {
	metaNav = new MetaNavObject(); //Get div from content
	linksDiv = new LinksDivObject(); 
	alignTD = document.getElementById("alignTD");
	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 MetaNavObject() {
	this.div = document.getElementById("bottommenu");
	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";}
	
}
function LinksDivObject() {
	this.div = document.getElementById("linksdiv");
	this.move = function (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 (){this.div.style.visibility = "hidden";this.div.style.display = "none";}
	this.show = function (){this.div.style.visibility = "visible";this.div.style.display = "block";}	
}

function checkForScroll() {
	clearTimeout(checking);
	checking = -1;
	var continueChecking = true;
	if ((calculateBottom() - metaHeight) != metaNav.y) {
		if ((positioningMeta == -1) && (animatingMeta == -1)) {
			continueChecking = false;
			metaNav.hide();
			//linksDiv.hide();
			positioningMeta = setInterval("startMeta()",positionMetaDelay);
		}
	}
	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 (!(navigator.appName.indexOf("Microsoft")!=-1))
		checking = setTimeout("moveMenusMac()", scrollCheckDelay);
}

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

function animateMeta() {
	metasteps = metasteps + step;
	if(metasteps <= 0) metaNav.move(0, metaNav.y - step);
	else {
		metaNav.move(0, metaNav.y - (metasteps-step));

		// Popup Links Position
		//window.status = alignTD.offsetLeft;
		linksDiv.move(alignTD.offsetLeft + 610,metaNav.y - linksDiv.div.offsetHeight);
		
		clearInterval(animatingMeta);
		animatingMeta = -1;
		if (checking == -1) {
			checking = setTimeout("checkForScroll()", scrollCheckDelay);
		}
	}
}

function calculateBottom() {
	var newPosition;

	if(navigator.appName.indexOf("Microsoft")!=-1) {
		if (window.document.documentElement.clientHeight) {
			newPosition = document.documentElement.scrollTop + parseInt(document.documentElement.clientHeight);
		} else {
			newPosition = document.body.scrollTop + parseInt(document.body.clientHeight);
		}
	} else newPosition = window.pageYOffset + window.innerHeight;

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

