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: 'b',				// 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: 500,			// 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:	"opent nieuw venster",	// 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
	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;
}

/* 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.setAttribute('href', href);
							}else{
								elem.addClassName('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){
							var len = this.breadCrumbs.length;
							target.appendChild( this.reassemble(this.breadCrumbs.pop(),true));
							while(this.breadCrumbs.length){
									target.appendChild(document.createTextNode(this.breadCrumbSeperator));
									var temp = this.reassemble(this.breadCrumbs.pop(),false);
									target.appendChild(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			:	'b'							,	// 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	:	500							,	// 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	:	"opent nieuw venster"		,	// 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
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].setAttribute("src", this.imageRoot+this.images[num][0] );
						}
					}							,
markCurrent		:	function( nr , par ){
						if(par !== false){
							var len = this.children.length;
							for(var i = 0 ; i < len ; i++){
								if(this.children[i] !== false){
									if(this.children[i].id === nr){
										this.a[i].addClassName("current");
										pg.breadCrumbs[pg.breadCrumbs.length] = [this.a[i].getAttribute('href'), this.a[i].innerHTML];
									}
								}
							}
						}else{
							this.a[nr].addClassName("current");
							pg.breadCrumbs[pg.breadCrumbs.length] = [this.a[nr].getAttribute('href'), this.a[nr].innerHTML];
						}
						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 ){
						position( 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.style.display = 'none';
					}							,
show			:	function(){
						this.container.style.display = 'block';
					}							,
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');
	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);
	}
	// 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);
	(clickab1e === '1')? a.setAttribute("href", node.getAttribute('click') ) : a.style.cursor = "default";
	if(newWindow === '1'){
		a.setAttribute("target" , "_blank");
		a.setAttribute("title" , menu.openNewString);
	}
	if(imageButton === '1'){
		images = [node.getAttribute('image')];
		if(submenu){
			img = $(document.createElement('img'));
			img.setAttribute("src", menu.imageRoot+images[0]);
			a.appendChild(img);	
		}else{
			img = a.firstChild;
		}
		if (node.getAttribute('moimage')){
			images[images.length] = node.getAttribute('moimage');
		}
	}
	if(hideName === '1'){
		a.style.padding = 0;
	}else{
		if(imageButton === '1')
			img.style.marginRight = 10+'px';
		if(submenu)
			a.innerHTML = menu.prefix + node.getAttribute( 'txt' );
	}
	if(tChildren.length > 0){
		a.addClassName("hasSub");
		childOb = createSubMenu(parOb, node);
	}
	var over = function(){
		parOb.over(num);
	}
	var out = function(){
		parOb.out(num);
	}
	a.observe('mouseover' , over );
	a.observe('mouseout' , out );
	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.addClassName("submenu");
		nSub.container = div;
		var tempUl = $(document.createElement('dl'));
		nSub.ul = tempUl;
		div.appendChild(tempUl);
		position( menu.container , div , 'b' );
		nSub.hide();
		document.body.appendChild(div);
	}
	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 position(target , subMenu , dir ){
	getScroll();
	var tC	= target.cumulativeOffset();
	var tS	= target.getDimensions();
	var sS	= subMenu.getDimensions();
	var x	= tC.left;
	var y	= tC.top;
	var w	= tS.width;
	var h	= tS.height;
	var sH	= sS.height;
	var sW	= sS.width;
	switch(dir){
			case 't':
			if( (y - sH) < info.scrollY && (y + h + sH + 20) < (info.scrollY + info.height) )
				dir = 'b';
			break;
		case 'b':
			if( (y + h + sH + 20) > (info.scrollY + info.height) && (y - sH) > info.scrollY )
				dir = 't';
			break;
		case 'r':
			if( (x + sW + w + 20) > (info.scrollX + info.width) && (x - sW) > info.scrollX )
				dir = 'l';
			break;
		case 'l':
			if( (x - sW) < info.scrollX && (x + w + sW + 20) > (info.scrollX  + info.width) )
				dir = 'r';
			break;
	}
	switch(dir){
		case 't':
			subMenu.style.top = (y-sH) +'px';
			subMenu.style.left = x +'px';
			break;
		case 'b':
			subMenu.style.top = (y + h) +'px';
			subMenu.style.left = x +'px';
			break;
		case 'r':
			subMenu.style.top = y +'px';
			subMenu.style.left = (x+w) +'px';
			break;
		case 'l':
			subMenu.style.top = y +'px';
			subMenu.style.left = (x-sW) +'px';
			break;
	}
}
function planHideAll(){
	menu.hideTimer = setInterval(hideAll , menu.hideTimerDur);
}
function hideAll(){
	cancelHideAll();
	var len  = menu.subMenus.length;
	for( var i = 0 ; i < len ; i++){
		menu.subMenus[i].hide();
	}
}
function cancelHideAll(){
	if(menu.hideTimer !== undefined){
		clearInterval(menu.hideTimer);
		menu.hideTimer = undefined;
	}
} */
