var fCom = {
	obj: undefined,
	slideShowStopped: false,
	init:	function(s){
		fCom.obj = document[s] || window[s];
		if(fCom.obj.length)
			fCom.obj = fCom.obj[1];
	},
	reset:	function(){
		if(fCom.obj)
			fCom.obj.resetSlideShow();
	},
	jumpSlide: function(nr){
		if(fCom.obj)
			fCom.obj.jumpTo(nr);
	}
};
function isReady(s){
	fCom.init(s);
	return true;
}
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 () {
		if(menu.currentItem !== "20"){
			var t = $("#tdTopPane");
			if(t.length){
				if($('#breadCrumbs').length === 0){
					var bc = document.createElement("div");
					bc.id = "breadCrumbs";
					t.append(bc);
				}
				var target = $('#breadCrumbs');
				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] );
						}
						if(this.id === "20"){
							fCom.jumpSlide(num);
							fCom.slideShowStopped = true;
						}
					}							,
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 ){
						if(this.id !== "20" && this.parent.id !== "20") 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(this.id === "20"){
							$("#m_"+node.getAttribute("id")).prepend(aItem);
						}else{
							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(){ 
						if(this.id !== "20") 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).attr("href", node.getAttribute('click') ) : a.style.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');
			$(img).attr("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).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), mainMenu = false, normal = true;
	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');
		nSub.container = div;
		if(pParent.id === "20"){
			$("#m_"+nSub.id).prepend(div);
		}else{
			$(div).addClass("submenu");
			document.body.appendChild(div);
			positionMenuElem( nSub.container , div , 'b' );
		}
		nSub.hide();
		var tempUl = document.createElement('dl');
		nSub.ul = tempUl;
		div.appendChild(tempUl);
	}
	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;
}
