var pg = {
	breadCrumbSeperator: " > ",
	breadCrumbs: [],
	reassemble: function (vars , main) {
		var elem = document.createElement('a');
		if (vars) {
			var href = vars[0];
			var text = vars[1];

			if (this.breadCrumbs.length > 0) {
				if (href) {
					$(elem).attr('href', href);
				}
			} else {
				$(elem).addClass('current');
			}
			if (!main) {
				if (menu.prefixLen !== 0) {
					text = text.substr(menu.prefixLen);
				}
			}
			elem.appendChild(document.createTextNode(text));
		}
		return elem;
	},
	makeBreadCrumbs: function () {
		var target = $('#breadCrumbs');
		if (target.length) {
			target.append(this.reassemble(this.breadCrumbs.pop(), true));
			while (this.breadCrumbs.length) {
				target.append(document.createTextNode(this.breadCrumbSeperator));
				var temp = this.reassemble(this.breadCrumbs.pop(), false);
				target.append(temp);
			}
		}
	}
};
var menu = {
	id: undefined,
	xmlNode: undefined,
	nodes: undefined,
	parent: undefined,
	children: [],
	childrenCount: 0,
	container: undefined,
	ul: undefined,
	a: undefined,
	li: undefined,
	img: [],
	images: [],
	subMenus: [],
	menuDir: 'b',
	subMenuDir: 'r',
	hideTimer: undefined,
	hideTimerDur: 500,
	childrenTimer: undefined,
	childrenTimerDur: 100,
	openNewString:	"opent nieuw venster",
	imageRoot:	"userdata/menuimages/",
	doCurrent:	false,
	prefix:	"",
	prefixLen:	0,
	over: function (num) {
		this.cancelHide();
		cancelHideAll();
		this.hideChildren();
		if (this.children[num] !== false) {
			var dir = (this.id === 'PGMenu') ?  this.menuDir : this.subMenuDir;
			this.children[num].positionMe(this.a[num], dir);
		}
		if (this.img[num] !== undefined) {
			if (this.images[num].length > 1) {
				this.img[num].setAttribute("src", this.imageRoot + this.images[num][1]);
			}
		}
	},
	out: function (num) {
		this.planHide(false);
		planHideAll();
		if (this.img[num] !== undefined) {
			if (this.images[num].length > 1) {
				$(this.img[num]).attr("src", this.imageRoot + this.images[num][0]);
			}
		}
	},
	markCurrent: function (nr, par) {
		if (par !== false) {
			for (var i = 0, len = this.children.length ; i < len ; i++) {
				if (this.children[i] !== false) {
					if (this.children[i].id === nr) {
						this.a[i].addClass("current");
						pg.breadCrumbs[pg.breadCrumbs.length] = [this.a[i].attr('href'), this.nodes[i].getAttribute("txt")];
					}
				}
			}
		} else {
			this.a[nr].addClass("current");
			pg.breadCrumbs[pg.breadCrumbs.length] = [this.a[nr].attr('href'), this.nodes[nr].getAttribute("txt")];
		}
		if (this.id !== "PGMenu") {
			this.parent.markCurrent(this.id);
		} else {
			pg.makeBreadCrumbs();
		}
		if (menu.visibleSubItem === undefined &&  this.children[nr]) {
			menu.visibleSubItem = this.children[nr];
		}
	},
	positionMe:	function (origin, dir) {
		positionMenuElem(origin, this.container, dir);
		this.show();
	},
	addA: function (node, liItem, aItem, subMenuOb, imgItem, imagesItem) {
		this.nodes[this.nodes.length] = node;
		this.a[this.a.length] = aItem;
		this.children[this.children.length] = subMenuOb;
		this.img[this.img.length] = imgItem;
		this.images[this.images.length] = imagesItem;
		if (liItem) {
			this.ul.appendChild(liItem);
			this.li[this.li.length] = liItem;
		}
		if (subMenuOb !== false) {
			this.subMenus[this.subMenus.length] = subMenuOb;
		}
		this.childrenCount++;
	},
	hide: function () {
		$(this.container).hide();
	},
	show: function () {
		$(this.container).show();
	},
	planHide: function () {
		this.childrenTimer = setInterval(this.hideChildren, this.childrenTimerDur , false);
	},
	cancelHide:	function () {
		if (this.childrenTimer !== undefined) {
			clearInterval(this.childrenTimer);
			this.childrenTimer = undefined;
		}
	},
	hideChildren: function (thisAlso) {
		if (this.children !== undefined) {
			var len = this.children.length;
			for (var i = 0 ; i < len ; i++) {
				if (this.children[i] !== false) {
					this.children[i].hideChildren(true);
				}
			}
			if (thisAlso) {
				this.hide();
			}
		}
	}
};
function getChildren(xmlNode) {
	var nodes = xmlNode.childNodes;
	var nChildren = [];
	for (var i = 0, len = nodes.length ; i < len ; i++) {
		var tempNode = nodes[i];
		if (tempNode.nodeType === 1) {
			if (tempNode.nodeName === "menuitem" || tempNode.nodeName === "PGMenu") {
				nChildren[nChildren.length] = tempNode;
			}
		}
	}
	return nChildren;
}
function initMenu() {
	getDimensions();
	menu.container = $('#MainMenu');
	var xml = getXMLData(MenuXML);
	menu.xml = xml.firstChild;
	if (menu.container && menu.xml) {
		var curNode = xml.getElementsByTagName("currentId")[0];
		if (curNode.getAttribute('ParentId')) {
			menu.currentItem = curNode.getAttribute('ParentId');
			var curNodeInfo = ['', curNode.getAttribute('TabName')];
			pg.breadCrumbs[pg.breadCrumbs.length] = curNodeInfo;
			menu.currentNode = curNode;
		} else {
			menu.currentItem = curNode.getAttribute('id');
		}
		createSubMenu(menu , menu.xml);
		if (menu.doCurrent) {
			menu.currentItemOb.markCurrent(menu.currentItemCount, false);
		} else {
			pg.makeBreadCrumbs();
		}
	}
}
function createMenuItem(parOb, node , mainMenuId) {
	var a, submenu = true, childOb = false, tChildren = getChildren(node), newLi = false,
	num = parOb.childrenCount, img, images;
	var id = node.getAttribute('id');
	var type = node.getAttribute('Type');

	if (parOb.id === "PGMenu") {
		a = $("#" + id);
		submenu = false;
	} else {
		newLi = document.createElement('dt');
		a = document.createElement('a');
		newLi.appendChild(a);
		a = $(a);
	}
	// clickabb1e newWindow image hideName
	var clickab1e	= type.substr(0, 1);
	var newWindow	= type.substr(1, 1);
	var imageButton	= type.substr(2, 1);
	var hideName	= type.substr(3, 1);
	if (clickab1e === '1') {
		a.attr("href", node.getAttribute('click'));
	} else {
		a.css("cursor", "default");
	}
	if (newWindow === '1') {
		a.attr("target" , "_blank");
		a.attr("title" , menu.openNewString);
	}
	if (imageButton === '1') {
		images = [node.getAttribute('image')];
		if (submenu) {
			img = document.createElement('img');
			a.append(img);
			$(img).attr("src", menu.imageRoot + images[0]);
		} else {
			img = a.firstChild;
		}
		if (node.getAttribute('moimage')) {
			images[images.length] = node.getAttribute('moimage');
		}
	}
	if (hideName === '1') {
		a.css("padding", 0);
	} else {
		if (imageButton === '1') {
			img.style.marginRight = 10 + 'px';
		}
		if (submenu) {
			a.html(menu.prefix + node.getAttribute('txt'));
		}
	}
	if (tChildren.length > 0) {
		a.addClass("hasSub");
		childOb = createSubMenu(parOb, node);
	}
	a.bind('mouseover', function () {
		parOb.over(num);
	});
	a.bind('mouseout', function () {
		parOb.out(num);
	});
	if (id === menu.currentItem) {
		menu.doCurrent = true;
		menu.currentItemCount = parOb.childrenCount;
		menu.currentItemOb = parOb;
	}
	parOb.addA(node, newLi, a, childOb, img, images);
}
function createSubMenu(pParent, pNode) {
	var nSub = object(pParent);
	var mainMenu = false;
	nSub.children = [];
	nSub.childrenCount = 0;
	nSub.nodes = [];
	nSub.a = [];
	nSub.li = [];
	nSub.img	= [];
	nSub.images = [];
	nSub.xmlNode = pNode;
	if (pNode.nodeName === 'PGMenu') {
		mainMenu = true;
		nSub.id = "PGMenu";
	} else {
		nSub.parent = pParent;
		nSub.id = pNode.getAttribute('id');
		var div = document.createElement('div');
		$(div).addClass("submenu");
		$(div).addClass("s" + howManyParents(nSub));
		nSub.container = div;
		var tempUl = document.createElement('dl');
		nSub.ul = tempUl;
		div.appendChild(tempUl);
		positionMenuElem(menu.container, div , 'b');
		document.body.appendChild(div);
		$(nSub.container).hide();
	}
	var nChildren = getChildren(pNode);
	if (nChildren.length > 0) {
		for (var i = 0, len = nChildren.length ; i < len ; i++) {
			var tempId = false;
			if (mainMenu) {
				tempId = nChildren[i].getAttribute('id');
			}
			createMenuItem(nSub, nChildren[i], tempId);
		}
	}
    return nSub;
}
