var visibles = new Array();
var currentItem = null;
var allItems = new Object();
var tsOver = 0;
var tsOut = 0;
var timeoutMs = 1000;
var timeout = null;
var spacing = 5;

function cc() {
	$('visibleslength').value = visibles.length;
	if (visibles.length)
		$('lastlevel').value = end(visibles).level;
	if (currentItem != null) {
		$('level').value = currentItem.level;
		
		$('iid').value = currentItem.item.id;
		if (currentItem.container != null) {
			$('cid').value = currentItem.container.id;
		}
		else {
			$('cid').value = 'null';
		}
		if (currentItem.parentContainer != null) {
			$('pid').value = currentItem.parentContainer.id;
		}
		else {
			$('pid').value = 'null';
		}
	}
	setTimeout('cc()', 50);
}

function MenuItem(id, trackId, level, parentId, uniqueId, direction, subDirectionH, subDirectionV) {
	this.id = id;
	this.trackId = trackId;
	this.level = level;
	this.parentId = parentId;
	this.uniqueId = uniqueId;
	this.key = uniqueId + '-' + id + '-item';
	this.value = id;
	this.direction = direction;
	this.subDirectionH = subDirectionH;
	this.subDirectionV = subDirectionV;	
	this.parentContainer = $(this.uniqueId + '-' + this.parentId + '-container');
	this.container = $(this.uniqueId + '-' + this.id + '-container');
	this.item = $(this.uniqueId + '-' + this.id + '-item');
	
	clearTimeout(timeout);
	
	this.position = function() {
		if (this.container != null) {
			switch (this.level) {
				case 0:
					switch (this.direction) {
						case 'down':
							this.container.style.left = from_left(this.item) + 'px';
							this.container.style.top = from_top(this.item) + this.item.offsetHeight + spacing + 'px';
						break;
					}
				break;
				
				default:
					switch (this.subDirectionH) {
						case 'right':
							this.container.style.left = from_left(this.parentContainer) + this.parentContainer.offsetWidth + spacing + 'px';
						break;
					}
					switch (this.subDirectionV) {
						case 'down':
							this.container.style.top = from_top(this.item) - 1 + 'px';
						break;
					}
				break;				
			}
						
			
			this.container.style.display = '';
			this.container.style.position = 'absolute';
			this.container.zIndex = this.level + 10;	
		}
		this.highlight();
	}
	
	this.highlight = function() {
		var tds = this.item.getElementsByTagName('td');
		for (var i = 0; i < tds.length; i++) {
			tds[i].className = tds[i].className.replace(/-hover/, '') + '-hover';
		}
	}
	
	this.unhighlight = function() {
		var tds = this.item.getElementsByTagName('td');
		for (var i = 0; i < tds.length; i++) {
			tds[i].className = tds[i].className.replace(/-hover/, '');
		}
	}
}

function show_menu(id, trackId, level, parentId, uniqueId, direction, subDirectionH, subDirectionV) {
	set_time_over();
	var key = uniqueId + '-' + id + '-item';
	var menuItem = new MenuItem(id, trackId, level, parentId, uniqueId, direction, subDirectionH, subDirectionV);
	allItems[key] = menuItem;
	currentItem = menuItem;

	if (visibles[level] != menuItem) {
		hide_menus(level);
		visibles.push(menuItem);
	}
	menuItem.position();
}

function set_time_over() {
	var tsNow = new Date();
	tsOver = tsNow.getTime();
}

function set_time_out() {
	var tsNow = new Date();
	tsOut = tsNow.getTime();
}

function hide_timer() {
	set_time_out();
	clearTimeout(timeout);
	timeout = setTimeout('hide_all_menus()', timeoutMs);
	
}

function hide_menus(stopLevel) {
	for (var i = visibles.length-1; i >= stopLevel; i--) {
		visibles[i].unhighlight();
		if (visibles[i].container != null) {
			visibles[i].container.style.display = 'none';
		}
		visibles.pop();
	}
}

function hide_all_menus() {
	hide_menus(0);
}