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,
	showSubOnLoad: true,
	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, passedIndex) {
		if (passedIndex === 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")];
						nr = i;
						break;
					}
				}
			}
		} 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, false);
		} 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');
	menu.subMenuContainer = $('#subMenuContainer');
	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, true);
		} else {
			pg.makeBreadCrumbs();
		}
		if(menu.showSubOnLoad && menu.visibleSubItem)
			menu.visibleSubItem.show();
	}
}
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');
		menu.subMenuContainer.append(div);
		nSub.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;
}
/*function cancelHideAll() {
	if (menu.hideTimer !== undefined) {
		clearInterval(menu.hideTimer);
		menu.hideTimer = undefined;
	}
}
function hideAll() {
	cancelHideAll();
	var len  = menu.subMenus.length;
	for (var i = 0 ; i < len ; i++) {
		menu.subMenus[i].hide();
	}
}
function planHideAll() {
	menu.hideTimer = setInterval(hideAll , menu.hideTimerDur);
}
function howManyParents(ob) {
	var count = 0;
	while (ob.parent) {
		ob = ob.parent;
		count++;
	}
	return count;
}
function positionElem(button, subMenu, dir) {
	$(button).show();
	var buttonPos	= button.offset();
	var buttonX		= buttonPos.left;
	var buttonY		= buttonPos.top;
	var buttonW		= button.outerWidth(true);
	var buttonH		= button.outerHeight(true);
	var subH		= $(subMenu).outerHeight(true);
	var subW		= $(subMenu).outerWidth(true);
	switch (dir) {
	case 't':
		if ((buttonY - subH) < info.scrollY && (buttonY + buttonH + subH + 20) < (info.scrollY + info.height)) {
			dir = 'b';
		}
		break;
	case 'b':
		if ((buttonY + buttonH + subH + 20) > (info.scrollY + info.height) && (buttonY - subH) > info.scrollY) {
			dir = 't';
		}
		break;
	case 'r':
		if ((buttonX + subW + buttonW + 20) > (info.scrollX + info.width) && (buttonX - subW) > info.scrollX) {
			dir = 'l';
		}
		break;
	case 'l':
		if ((buttonX - subW) < info.scrollX && (buttonX + buttonW + subW + 20) > (info.scrollX  + info.width)) {
			dir = 'r';
		}
		break;
	}
	switch (dir) {
	case 't':
		subMenu.style.top = (buttonY - subH) + 'px';
		subMenu.style.left = buttonX + 'px';
		break;
	case 'b':
		subMenu.style.top = (buttonY + buttonH) + 'px';
		subMenu.style.left = buttonX + 'px';
		break;
	case 'r':
		subMenu.style.top = buttonY + 'px';
		subMenu.style.left = (buttonX + buttonW) + 'px';
		break;
	case 'l':
		subMenu.style.top = buttonY + 'px';
		subMenu.style.left = (buttonX - subW) + 'px';
		break;
	}
}
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					,	// this Objects id
xmlNode			:	undefined					,	// XMLNode from which this Object is made
nodes			:	undefined					,	// Array that holds the nodes that this menus items are made of
parent			:	undefined					,	// Parent Object
children		:	[]							,	// Children Objects, if an A item has no submenu this is undefined
childrenCount	:	0							,	// counts the li's in this menu
container		:	undefined					,	// stores the div this.that holds this menus items
ul				:	undefined					,	// stores the ul in which the li's with thet buttons are stored 
a				:	undefined					,	// stores the a's in this menu
li				:	undefined					,	// array of li's
img				:	[]							,	// array that holds vars to see if a button has an image
images			:	[]							,	// array that holds the mouseover and mouseout images for a link, otherwise holds undefined
subMenus		:	[]							,	// array of submenus,array that holds ALL the subMenus this array is stored in the original object (menu)
menuDir			:	'r'							,	// default foldout direction for mainmenu
subMenuDir		:	'r'							,	// default foldout direction for submenus
hideTimer		:	undefined					,	// if this menu is timed out to be hid this is a number else it is undefined
hideTimerDur	:	800							,	// default value for hide() timeout
childrenTimer	:	undefined					,	// if children are timed out to be hid this is a number else it is undefined
childrenTimerDur:	100							,	// default value for planHide() timeout
openNewString	:	"opens in a new window"		,	// string to show on mouseover when a link opens a new window
imageRoot		:	"userdata/menuimages/"		,	// folder that holds uploaded menuimages
doCurrent		:	false						,	// if currentitem is set in the menuxml this var will be set to true so the markCurrent function gets called
prefix			:	""							,	// prefix for submenuitems
prefixLen		:	0							,	// the length of the submenuprefix in characters since this is hard to determin for JS from the string
visibleSubItem	:	undefined					,	// contains the current submenuitem to make it visible
	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]);
			}
		}
	},
	positionMe:	function( origin , dir ){
		//position( origin , this.container , dir );
		this.show();
	},
	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];
		}
	},
	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 = [];
	var len = nodes.length;
	for (var i = 0 ; 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');
	menu.subMenuContainer = $('#subMenuContainer');
	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);
			if(menu.visibleSubItem)
				menu.visibleSubItem.show();
		}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);
		menu.subMenuContainer.append(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;
}*/