var currItem;

function setMenu(id) {
	var target = document.getElementById(id);
	target.style.color = '#ffffff';
	target.style.backgroundColor = '#1dbcf8';
	var neighbours = getNeighbours(target);	
	for(var i=0; i<neighbours.length;i++) {
		neighbours[i].style.backgroundColor = '#1dbcf8';
	}
	currItem = target;
}
function colorMenu(event) {
	if(!event.target) {
		var event = window.event;
		event.target = window.event.srcElement;
	}
	var target = event.target;
	if(!target.className) {
		target.style.color = '#ffffff';
		target.style.backgroundColor = '#1dbcf8';
	}
	var neighbours = getNeighbours(target);
	for(var i=0; i<neighbours.length;i++) {
		neighbours[i].style.backgroundColor = '#1dbcf8';
	}
	return target;
}
function pickItem(event) {
	var target = colorMenu(event);
	if(target.className != 'sep') {
		currItem = target;
	}
}	
function uncolorMenu() {
	var lis = document.getElementById('menuList').childNodes;	
	for(var i=0;i<lis.length;i++) {
		if(lis[i].nodeType == 1) {
			var li = lis[i];
			if(!currItem || (li != currItem && !inArray(li, getNeighbours(currItem)))) {
				li.style.color = '#000000';
				li.style.backgroundColor = '#ffffff';
			}
		}
	}
}
function getNeighbours(target) {
	var neighbours = Array();
	if(target.previousSibling) {
		var prev = target.previousSibling;
		if(prev.nodeType == 3) {
			prev = prev.previousSibling;	
		}
	}
	if(prev && prev.className == 'sep') {
		neighbours.push(prev);
	}
	if(target.nextSibling) {
		var next = target.nextSibling;
		if(next.nodeType == 3) {
			next = next.nextSibling;	
		}
	}
	if(next && next.className == 'sep') {
		neighbours.push(next);
	}	
	return neighbours;
}
function inArray(needle, haystack) {
	var found=0;
	for(var i=0;i<haystack.length;i++) {
		if(haystack[i] == needle) {
			found=1;	
		}
	}
	return found;
}