var spacing = 5;
var visibles = new Array();
var mouseOuts = new Array();
var isOver = false;
var parentContainer = null;
var overId = null;
var cScroll = 0;
var cContainer = null;

var isScrolling = false;
var lastCScroll = 0
var isChecking = false;

function checkScroll() {
	if(lastCScroll == cScroll) {
		isScrolling = false;
	}
	else {
		isScrolling = true;
		setTimeout('hideFromScroll()', 2000);
	}
	lastCScroll = cScroll;
	setTimeout('checkScroll()', 500);
}

function hideFromScroll() {
	if(!isOver && !isScrolling) {
		for(var i = visibles.length-1; i >= 0; i--) {
			Element.hide($(visibles[i].uniqueId + '-' + visibles[i].id + '-container'));
			visibles.pop();
		}
	}
}

function menuItem(id, trackId, level, uniqueId, parentId) {
	this.id = id;
	this.trackId = trackId;
	this.level = level;
	this.uniqueId = uniqueId;
	this.parentId = parentId;
}

function end(a) {
	return a[a.length-1];	
}

function showMenu(id, trackId, level, parentId, uniqueId, pos, subPos, dir) {
	overId = id;
	if(!isChecking) {
		checkScroll();
		isChecking = true;
	}
	var thisItem = $(uniqueId + '-' + id + '-item');
	var container = $(uniqueId + '-' + id + '-container');
	
	if(parentId > 0) {
		parentContainer = $(uniqueId + '-' + parentId + '-container');
	}
	else {
		parentContainer = null;	
	}
	
	var tempItem = new menuItem(id, trackId, level, uniqueId, parentId);
	
	if(visibles.length == 0) {
		visibles.push(tempItem);
		cScroll = 0;
		lastCScroll = 0;
	}
	else {
		if(end(visibles).trackId != trackId || end(visibles).uniqueId != uniqueId) {
			hideMenus(0);
			cScroll = 0;
			lastCScroll = 0;
			if(container != null) {
				visibles.push(tempItem);
			}
		}
		else if(end(visibles).level >= level) {
			hideMenus(level);
			if(container != null) {
				visibles.push(tempItem);
			}
		}
		else {
			if(container != null) {
				visibles.push(tempItem);
			}
		}
	}
	if(container != null) {
		container.style.position = 'absolute';
		Element.showb(container);
		container.zIndex = level*10;
		container.height = '';
	}
	isOver = true;

	if(container != null) {
		if(visibles.length == 1) {
			if(pos == 'left') {
				container.style.left = fromLeft(thisItem) - container.offsetWidth - spacing + 'px';
				container.style.top = fromTop(thisItem) + 'px';
			}
			if(pos == 'up') {
				container.style.left = fromLeft(thisItem) + 'px';
				container.style.top = fromTop(thisItem) - container.offsetHeight - spacing + 'px';
			}
			if(pos == 'right') {
				container.style.left = fromLeft(thisItem) + thisItem.offsetWidth + spacing + 'px';
				container.style.top = fromTop(thisItem) + 'px';
			}
			if(pos == 'down') {
				container.style.left = fromLeft(thisItem) + 'px';
				container.style.top = fromTop(thisItem)  + thisItem.offsetHeight + spacing + 'px';
			}
		}
		else {
			if(subPos == 'left') {
				if(dir == 'down') {
					container.style.left = fromLeft(thisItem) - container.offsetWidth - spacing + 'px';
					container.style.top = fromTop(thisItem) + 'px';
				}
				else if(dir == 'up') {
					container.style.left = fromLeft(thisItem) + thisItem.offsetWidth + spacing + 'px';
					container.style.top = fromTop(thisItem) - container.offsetHeight + thisItem.offsetHeight + 'px';
				}
			}
			if(subPos == 'up') {
				container.style.left = fromLeft(thisItem) + 'px';
				container.style.top = fromTop(thisItem) - container.offsetHeight - spacing + 'px';
			}
			if(subPos == 'right') {
				if(dir == 'down') {
					container.style.left = fromLeft(thisItem) + thisItem.offsetWidth + spacing + 'px';
					container.style.top = fromTop(thisItem) - cScroll + 'px';
					//alert(container.style.top);
				}
				else if(dir == 'up') {
					container.style.left = fromLeft(thisItem) + thisItem.offsetWidth + spacing + 'px';
					container.style.top = fromTop(thisItem) - container.offsetHeight + thisItem.offsetHeight + 'px';
				}
			}			
			if(subPos == 'down') {
				container.style.left = fromLeft(thisItem) + 'px';
				container.style.top = fromTop(thisItem)  + thisItem.offsetHeight + spacing + 'px';
			}
		}
		keepInWindow(container);
	}
}
function updateScroll() {
	cScroll = cContainer.scrollTop;	
}
function keepInWindow(container) {
	normal(container);
	if(tooFarDown(container)) {
		moveToBottom(container);
		if(tooFarUp(container)) {
			moveToTop(container);
			moveRight(container);
			addScrollBar(container);
		}
	}	
	// if the menu goes too far right and is not the first level, move it to the left of the menu and hide other levels
	
	if(tooFarRight(container)) {
		moveLeft(container);
		if(tooFarLeft(container)) {
			moveRight(container);
		}
		hideMenus2(0, visibles.length-3);
	}
}

function normal(container) {
	if(container.style.overflowY == 'scroll') {
		if(!netscape()) {
			container.style.width = container.offsetWidth - 16 + 'px';
		}	
	}
	container.style.width = container.offsetWidth + 'px';
	container.style.height = '';
	container.style.overflowY = 'auto';
	container.style.overflowX = 'hidden';
}

function tooFarDown(container) {
	if(fromTop(container) + container.offsetHeight > screenBottom()) {
		return true;
	}
	else {
		return false;
	}
} 

function tooFarUp(container) {
	if(fromTop(container) < screenTop()) {
		return true;
	}
	else {
		return false;
	}
}

function tooFarRight(container) {
	return fromLeft(container) + container.offsetWidth > screenRight();
}

function tooFarLeft(container) {
	return fromLeft(container) < screenLeft();
}

function moveLeft(container) {
	var temp = visibles[visibles.length-1];
	var tempParent = $(temp.uniqueId + '-' + temp.parentId + '-container');
	container.style.left = fromLeft(tempParent) - 10 - container.offsetWidth + 'px';
}

function moveRight(container) {
	var temp = visibles[visibles.length-1];
	var tempParent = $(temp.uniqueId + '-' + temp.parentId + '-container');
	if(tempParent != null) {
		container.style.left = fromLeft(tempParent) + 10 + tempParent.offsetWidth + 'px';
	}
	else {
		container.style.left = fromLeft(container) + 32 + 'px';
	}
}

function moveToBottom(container) {
	container.style.top = (screenBottom() - container.offsetHeight).toString() + 'px';
}

function moveToTop(container) {
	container.style.top = screenTop().toString() + 'px';
}

function debug(str) {
	$('debug').innerHTML = str;
}

function addScrollBar(container) {
	cContainer = container;
	container.onscroll = updateScroll;
	container.style.overflowY = 'scroll';
	container.style.overflowX = 'hidden';
	container.style.height = getScreenHeight().toString() + 'px';
	if(!netscape()) {
		container.style.width = container.offsetWidth + 16 + 'px';
	}	
}

function getScrollX() {
	var scrOfX = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfX = window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfX = document.body.scrollLeft;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfX;	
}

function getScrollY() {
	var scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
	}
	return scrOfY;
}

function screenTop() {
	return getScrollY();
}

function screenRight() {
	return getScreenWidth() + getScrollX();
}

function screenBottom() {
	return getScreenHeight() + getScrollY();
}

function screenLeft() {
	return getScrollX();
}

function getScreenWidth() {
	if(netscape()) {
		return window.innerWidth;
	}
	else {
		return document.body.clientWidth;
	}
}

function getScreenHeight() {
	if(netscape()) {
		return window.innerHeight;
	}
	else {
		return document.body.clientHeight;
	}
}

function hideMenus(stopLevel) {
	for(var i=visibles.length-1; i>=0; i--) {
		if(visibles[i].level >= stopLevel) {
			var temp = $(visibles[i].uniqueId + '-' + visibles[i].id + '-container');
			if(temp != null) {
				temp.zIndex = '0';
				Element.hide(temp);
			}
			visibles.pop();
		}	
	}
}

function hideMenus2(startLevel, stopLevel) {
	if(stopLevel < startLevel) {
		var temp = startLevel;
		startLevel = stopLevel;
		stopLevel = temp;
	}
	try {
		for(var i = startLevel; i <= stopLevel; i++) {
			var temp = $(visibles[i].uniqueId + '-' + visibles[i].id + '-container');
			if(temp != null) {
				temp.zIndex = '0';
				Element.hide(temp);
			}
		}
	}
	catch(e) { }
}

function hideAll() {
	mouseOuts.push(1);
	isOver = false;
	var time = 2000;
	setTimeout("hideAllTimer()", time);
}

function hideAllTimer() {
	mouseOuts.pop();
	if(!isOver && mouseOuts.length == 0 && !isScrolling) {
		for(var i = visibles.length-1; i >= 0; i--) {
			Element.hide($(visibles[i].uniqueId + '-' + visibles[i].id + '-container'));
			visibles.pop();
		}
	}
}

function fromLeft(o) {
	if(o.style.position == 'absolute') {
		return parseInt(o.style.left);
	}

	else {
		var left = o.offsetLeft;
		if(o.offsetParent) {
			left += fromLeft(o.offsetParent);
		}
		return left;
	}
}

function fromTop(o) {
	if(o.style.position == 'absolute') {
		return parseInt(o.style.top);
	}
	
	else {
		var top = o.offsetTop;
		if(o.offsetParent) {
			top += fromTop(o.offsetParent);
		}
		return top;
	}
}

function netscape() {
	return navigator.appName == 'Netscape';
}