
var popupMenus = {
	menus: {},
	nextPopupMenuID: null,
	currentPopupMenu: null,
	currentPopupMenuRef: 0,
	currentPopupMenuNo: 0,
	subMenu: null,
	subMenuRef: 0,
	subMenuN: 0,
	itemDefault: {
		margin : {
			top: 2
		}
	},
	popupLinkClass: "popuplink",
	popupClass: "popup",
	
	newMenu: function (id, content) {
		var menu = {
			id: id,
			content: content,
			items: [],
			marked: false,
			node: null,
			popupLinkClass: "popuplink",
			popupClass: "popup",			
			margin: {
				top: this.itemDefault.margin.top
			},
			addItem: function(text, href) {
				var item = this.newItem(text,href);
				this.items.push(item);
				return item;
			},
			newItem: function(text, href) {
				return {
					parent: this,
					text: text,
					href: href,
					target: null,
					items: [],
					newItem: this.newItem,
					addItem: this.addItem
				};
			},
			
			onHide: function() {
			},
			onShow: function() {
			},
			print: function() {
				var subCount = 0;
				var html = "<span class=\"" + this.popupClass +	"\" id=\"popup_"+this.id+"\" onMouseOver=\"popupMenus.show('"+this.id+"')\" onMouseOut=\"popupMenus.hide('"+this.id+"')\"><ul class=\"popup\">";				
				for (var i = 0; i < this.items.length; i++) {
					var item = this.items[i];
					if (item.text == null)
						html += ("<li class=\"popupSep\">&nbsp;</li>");
					else if (item.items.length == 0) {
						html += "<li class=\"popup\"><a class=\"subpopup\" href=\""+item.href+"\"";
						if (item.target) {
							html += " target=\""+item.target+"\"";
						}
						html += ">"+item.text+"</a></li>";
					}
					else if (item.items.length > 0) {
						html += ("<li class=\"popup\"><a class=\"popupWithSub\" href=\""+item.href+"\" onMouseOver=\"popupMenus.showSub('"+this.id+"', "+i+", this);\" onMouseOut=\"popupMenus.hideSub('"+this.id+"',"+i+")\">"+item.text+"</a></li>");
						subCount++;
					}
				}
				html += "</ul></span><span class=\"";
				html += this.popupLinkClass;				
				if (this.marked) html += " active";
				html += "\" id=\"popuplink_" + this.id + "\" onMouseOver=\"popupMenus.show('" + this.id + "', this)\" onMouseOut=\"popupMenus.hide('" + this.id + "')\">" + this.content + "</span>";	
				if (subCount > 0) {
					for (i = 0; i < this.items.length; i++) {
						if (this.items[i] != null && this.items[i].items) {
							html += "<span class=\"subpopup\" id=\"popup_"+this.id+"_"+i+"\"  onMouseOver=\"popupMenus.showSub('"+this.id+"',"+i+")\" onMouseOut=\"popupMenus.hideSub('"+this.id+"',"+i+")\"><ul class=\"subpopup\">";
							var subItems = this.items[i].items;
							for (var j = 0; j < subItems.length; j++) {
								html += "<li class=\"subpopup\"><a class=\"subpopup\" href=\""+subItems[j].href+"\"";
								if (subItems[j].target) {
									html += " target=\"" + subItems[j].target + "\"";
								}
								html += ">"+subItems[j].text+"</a></li>";
							}
							html += "</ul></span>";
						}
					}
				}
				document.write(html);
			}
		};
		this.menus[id] = menu;
		return menu;
	},
	debug: function() {
		if (console) console.log("nextPopupMenuID: " + this.nextPopupMenuID + ", currentPopupMenu: " + this.currentPopupMenu + ", currentPopupMenuRef: " + this.currentPopupMenuRef);
	},
	show: function(id, link) {
		if (console) console.log("show(" + id + "," + link + ")");
		this.debug();
		this.nextPopupMenuID = id;
		if (link != null) {
			var popup = document.getElementById("popup_" + id);
			popup.style.left = findX(link) + 'px';
			popup.style.top = (findBottomY(link) + this.menus[id].margin.top) + 'px';
		}				
		setTimeout('popupMenus.showPopupMenuImpl("'+id+'")', 100);	
	},
	hide: function(id) {
		if (console) console.log("hide(" + id + ")");
		this.debug();
		if (this.nextPopupMenuID == id)
			this.nextPopupMenuID = null;
		else
			setTimeout('popupMenus.hidePopupMenuImpl("'+id+'", ' + this.currentPopupMenuNo + ')', 400);	
	},
	
	showPopupMenuImpl: function (name) {
		if (console) console.log("showPopupMenuImpl(" + name + ")");
		this.debug();
		if (name == this.nextPopupMenuID) {
			this.nextPopupMenuID = null;
			if(this.currentPopupMenu != name && this.currentPopupMenu != null) {		
				var currentMenu = document.getElementById("popup_" + this.currentPopupMenu);
				if (currentMenu.style.visibility != 'hidden') {
					currentMenu.style.visibility = 'hidden';
					this.menus[this.currentPopupMenu].onHide();
				}				
				this.currentPopupMenuRef = 0;
				this.currentPopupMenu = null;
			}
			if (this.currentPopupMenu == null) {
				this.currentPopupMenuNo = this.currentPopupMenuNo+1;
			}
			var menu = document.getElementById("popup_" + name);
			if (menu.style.visibility != 'visible') {
				menu.style.visibility = 'visible';
				this.menus[name].onShow();
			}
			showIEMenuFix(menu);
			
			this.currentPopupMenu = name;
			this.currentPopupMenuRef++;	
		}
	},

	hidePopupMenuImpl: function (name, no) {
		if (console) console.log("hidePopupMenuImpl(" + name + ", " + no +")");
		this.debug();
		if (name == this.currentPopupMenu && this.currentPopupMenuNo == no) {
			this.currentPopupMenuRef--;
			if (this.currentPopupMenuRef <= 0) {
				var menu = document.getElementById("popup_" + name);
				hideIEMenuFix(menu);
				if (menu.style.visibility != 'hidden') {
					menu.style.visibility = 'hidden';
					this.menus[name].onHide();
				}
						
				this.currentPopupMenu = null;
				this.currentPopupMenuRef = 0;			
			}
		}
	},
	
	showSub: function (id, sub, item) {
		this.nextPopupMenuID = id;
		this.showPopupMenuImpl(id);
		
		if (item != null) {
			var popup = document.getElementById("popup_" + id + "_" + sub);
			popup.style.top = (findY(item)-1) + 'px';
			popup.style.left = (findX(item) + item.offsetWidth - 1) + 'px';
		}
		if ((this.subMenu != id  || this.subMenuN != sub) && this.subMenu != null) {
			document.getElementById("popup_" + this.subMenu + "_" + this.subMenuN).style.visibility = 'hidden';
			this.subMenuRef = 0;		
		}
		var menu = document.getElementById("popup_" + id + "_" + sub);		
		menu.style.visibility = 'visible';
		showIEMenuFixSub(menu);

		this.subMenu = id;
		this.subMenuN = sub;
		this.subMenuRef++;
	},

	hideSub: function (id) {
		setTimeout('popupMenus.hideSubMenuImpl("' + id + '", ' + this.subMenuN + ')', 400);
		this.hide(id);
	},

	hideSubMenuImpl: function (id, n) {
		if (id == this.subMenu && this.subMenuN == n) {
			this.subMenuRef--;
			if (this.subMenuRef <= 0) {
				var menu = document.getElementById("popup_" + id + "_" + n);
				hideIEMenuFixSub(menu);
				menu.style.visibility = 'hidden';
				this.subMenu = null;
				this.subMenuRef = 0;
			}
		}
	}

};

function initHeadMenu(menu) {
	menu.popupClass = "popup";
	menu.margin.top = 0;
	menu.onShow = function() {
		var link = document.getElementById("popuplink_" + this.id);
		if (link) {
			link.style.backgroundColor = "#ED7700";
		}
	};
	menu.onHide = function() {
		var link = document.getElementById("popuplink_" + this.id);
		if (link) {
			link.style.backgroundColor = "";
		}
	};
}

/*
 * SLUT på JavaScript för expanderingsknapparna
 */


function showIEMenuFix(theDiv) {
	var iefix = document.getElementById("iefix");
	if (iefix) {
		iefix.style.width = theDiv.offsetWidth;
		iefix.style.height = theDiv.offsetHeight - 15;		
		iefix.style.top = (findY(theDiv) + 15) + 'px';
		iefix.style.left = findX(theDiv) + 'px';
		iefix.style.display = "block";
		ieFixedFor = theDiv;
	}	
}

function showIEMenuFixSub(theDiv) {
	var iefix = document.getElementById("iefixsub");
	if (iefix) {
		iefix.style.width = theDiv.offsetWidth;
		iefix.style.height = theDiv.offsetHeight;		
		iefix.style.top = findY(theDiv) + 'px';
		iefix.style.left = findX(theDiv) + 'px';
		iefix.style.display = "block";
		iesubFixedFor = theDiv;
	}	
}

function hideIEMenuFix(theDiv) {
	if (theDiv == ieFixedFor) {
		var iefix = document.getElementById("iefix");
		if (iefix) {
			iefix.style.display = "none";
			ieFixedFor = null;
		}
	}
}

function hideIEMenuFixSub(theDiv) {
	if (theDiv == iesubFixedFor) {
		var iefix = document.getElementById("iefixsub");
		if (iefix) {
			iefix.style.display = "none";
			iesubFixedFor = null;
		}
	}
}

function printMenu(popupid, popupcontent, aItems, marked) {
	var menu = popupMenus.newMenu(popupid, popupcontent);
	menu.marked = marked;
	for (var i = 0; i < aItems.length; i++) {
		if (aItems[i] != null) {
			var item = menu.addItem(aItems[i][0], aItems[i][1]);
			if (aItems[i].length > 2) {
				for (var j = 0; j < aItems[i][2].length; j++) {
					if (aItems[i][2][j] != null) {
						item.addItem(aItems[i][2][j][0], aItems[i][2][j][1]);
					}
					else {
						item.addItem();
					}
				}
			}
		}
		else {
			menu.addItem();
		}
	}
	menu.print();
}
// Kopierat in från infotorg3.js
var ieFixedFor;
var iesubFixedFor;

function findBottomY(item) {
	return item.offsetHeight + findY(item);
}

function findY(item) {
	var y = 0;
	while (item != null) {
		y += item.offsetTop;
		item = item.offsetParent;
	}
	return y;
}
function findX(item) {
	var x = 0;
	while (item != null) {
		x += item.offsetLeft;
		item = item.offsetParent;
	}
	return x;
}

