var lastMenu;
var lastMenuItem;
var lastSubMenu;
var lastItem;
var lastToolbar;

var clicked;
var subclicked;
clicked = 0;
subclicked = 0;

if (document.layers) {
	visible = 'show';
	hidden = 'hide';
} else if (document.all) {
	visible = 'visible';
	hidden = 'hidden';
}

var message = "Sidan är skyddad av upphovsrättslagen.\nCopyright © " +  new Date().getFullYear();
function click(e) {
  if (document.all) {
    if (event.button==2 || event.button==3) {
		  alert(message);
      return false;
    }
  }
  if (document.layers) {
    if (e.which == 3) {
		  alert(message);
      return false;
    }
  }
}
if (document.layers) {
  document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;


function ToggleMenu(menuitem, menu, offset, on) {
	WriteDbgLn("ToggleMenu(" + menuitem + ", " + menu +  ", " + offset +  ", " + on + ")...", 1);
	if (document.layers) {
		theMenu = document.layers[menu];
		theMenuItem = document.layers[menuitem];
	} else if (document.all && document.all(menu) && document.all(menuitem)) {
		theMenu = document.all(menu).style;
		theMenuItem = document.all(menuitem).style;
		newStyle = (on) ? (clicked ? document.all("mainMenuSunken").currentStyle : document.all("mainMenuRaised").currentStyle ): document.all("mainMenuNormal").currentStyle;
	}

	theMenuItem.backgroundColor = newStyle.backgroundColor;
	theMenuItem.borderColor = newStyle.borderColor;
	theMenuItem.borderStyle = newStyle.borderStyle;
	
	lastMenuItem = theMenuItem;
	
	if ((!clicked || lastMenu != theMenu) && lastSubMenu) {
		WriteDbg("The last submenu, ", 1);
		WriteDbgLn("visibility is set to: " + hidden, 0);
		lastSubMenu.visibility = hidden;
		subclicked = 0;
	}
	if (menu && theMenu.visibility == visible) {
		if (!clicked) {
			WriteDbg("Menu " + menu + ", (offset=" + offset + ") ", 1);
			WriteDbgLn("visibility is set to: " + hidden, 0);
			theMenu.visibility = hidden;
			lastMenu = 0;
		}
	} else {
		if (menu && clicked) {
			WriteDbg("Menu " + menu + ", (offset=" + offset + ") ", 1);
			WriteDbgLn("visibility is set to: " + visible, 0);
			theMenu.left = offset + 10 + "px";
			theMenu.visibility = visible;
		}
		if (lastMenu && lastMenu != theMenu || !menu) {
			WriteDbg("The last menu, ", 1);
			WriteDbgLn("visibility is set to: " + hidden, 0);
			lastMenu.visibility = hidden;
		}
		lastMenu = theMenu;
	}
	
}
function ToggleMenuOnMouse(menuitem, menu, offset, on) {
	ToggleMenu(menuitem, menu, offset, on);
}
function ToggleMenuOnClick(menuitem, menu, offset) {
	if (!menu || clicked) {
		clicked = 0;
	} else {
		clicked = 1;
	}
	ToggleMenu(menuitem, menu, offset, clicked);
}

function ToggleSubMenu(submenu) {
	if (document.layers) {
		theSubMenu = document.layers[submenu];
	} else if (document.all && document.all(submenu)) {
		theSubMenu = document.all(submenu).style;
	}

	index = submenu.substring(submenu.lastIndexOf('_') + 1, submenu.length);
	mtop = 18 * (index) -1;
	mtop += "px";
	
	if (submenu && theSubMenu.visibility == visible) {
		if (!subclicked) {
			WriteDbg("SubMenu " + submenu + ", ", 1);
			WriteDbgLn("visibility is set to: " + hidden, 0);
			theSubMenu.visibility = hidden;
			lastSubMenu = 0;
		}
	} else {
		if (submenu) {
			WriteDbg("SubMenu " + submenu + ", ", 1);
			WriteDbgLn("visibility is set to: " + visible, 0);
			theSubMenu.top = mtop;
			theSubMenu.left = "161px";
			theSubMenu.visibility = visible;
		}
		if (lastSubMenu && lastSubMenu != theSubMenu || !submenu) {
			WriteDbg("The last submenu, ", 1);
			WriteDbgLn("visibility is set to: " + hidden, 0);
			lastSubMenu.visibility = hidden;
		}
		lastSubMenu = theSubMenu;
	}
}
function ToggleSubMenuOnMouse(submenu) {
	if (!submenu || subclicked) {
		subclicked = 0;
	} else {
		subclicked = 1;
	}
	if (subclicked) {
		ToggleSubMenu(submenu);
	}
}
function ToggleSubMenuOnClick(submenu) {
/*
	if (!submenu || subclicked) {
		subclicked = 0;
	} else {
		subclicked = 1;
	}
	ToggleSubMenu(submenu);
*/
}
function ToggleItem(item, status, on) {
	if (document.layers) {
		theItem = document.layers[item];
		newStyle = (on == 1) ? document.layers["menuItemHighlight"] : document.layers["menuItemNormal"];
	} else if (document.all) {
		theItem = document.all(item).style;
		newStyle = (on == 1) ? document.all("menuItemHighlight").currentStyle : document.all("menuItemNormal").currentStyle;
	}

	WriteDbgLn("Toggle item, " + item + ", " + on, 1);
	theItem.backgroundColor = newStyle.backgroundColor;
	theItem.borderColor = newStyle.borderColor;
	theItem.borderStyle = newStyle.borderStyle;
	
	WriteDbgLn("Setting status to: " + status, 1);
	LayerWrite("statusBar", 0, status);
	
	lastItem = theItem;
}
function ToggleImage(image) {
	img = document.images[image];
	if (img) {
		re = /blank\.gif/i;
		if (img.src.search(re) != -1) {
			img.src = "images/checkbox.gif";
		} else {
			img.src = "images/blank.gif";
		}
	}
}
function ToggleToolbar() {
	if (document.layers) {
		theToolbar = document.layers["toolBar"];
		currentToolbar = document.layers["toolBar"];
	} else if (document.all && document.all("toolBar")) {
		theToolbar = document.all("toolBar").style;
		currentToolbar = document.all("toolBar").currentStyle;
	}
	
	if (currentToolbar.visibility == hidden) {
		theToolbar.visibility = visible;
		setCookieAttribute("toolbar", "show");
	} else {
		theToolbar.visibility = hidden;
		setCookieAttribute("toolbar", "hide");
	}
	
	lastToolbar = theToolbar;
}
function InitToolbar() {
	WriteDbgLn("InitToolBar...", 1);
	var img;
	
	checkImage = new Image();
	checkImage.src = "images/checkbox.gif";
	blankImage =  new Image();
	blankImage.src = "images/blank.gif";
	
	if (document.layers) {
		theToolbar = document.layers["toolBar"];
	} else if (document.all && document.all("toolBar")) {
		theToolbar = document.all("toolBar").style;
	}
	
	imageid = getCookieAttribute("toolbartoggleimage");
	if (imageid.length > 0) {
		img = document.images[imageid];
	}
	
	if (getCookieAttribute("toolbar") == "show") {
		theToolbar.visibility = visible;
		if (img) img.src = checkImage.src;
	} else {
		theToolbar.visibility = hidden;
		setCookieAttribute("toolbar", "hide");
		if (img) img.src = blankImage.src;
	}
}
function SetToolbarToggleMenuItem(item) {
	WriteDbgLn("SetToolbarToggleMenuItem...", 1);
	if (item) {
		imageid = item.index;
		root = item.parent;
		while (root && root.index) {
			imageid = root.index + "_" + imageid;
			root = root.parent;
		}
		imageid = "image" + imageid;
		
		setCookieAttribute("toolbartoggleimage", imageid);
	}
}

function LayerWrite(id, nestref, text) {
	WriteDbgLn("LayerWrite(" + id + ", " + nestref + ", " + text + ")", 1);
	if (document.layers) {
		doc = (nestref) ? eval('document.' + nestref + '.document.' + id + '.document') : document.layers[id].document;
		doc.open();
		doc.write(text);
		doc.close();
	} else {
		document.all(id).innerHTML = text;
	}
}

function setCookieAttribute(attribute, value) {
	cookies = document.cookie;
	WriteDbgLn("setCookieAttribute(" + attribute + ", " + value + "): cookie = " + cookies, 1);
	re = new RegExp(attribute + "=[a-z0-9_]*", "i");
	if (cookies.search(re) != -1) {
		cookies = cookies.replace(re, attribute + "=" + value);
	} else {
		if (cookies.length > 0)
			cookies += "&";
		else
			cookies = "@";
		cookies += attribute + "=" + value;
	}
	WriteDbgLn("Setting cookie to: " + cookies, 1);
	document.cookie = cookies;
}
function getCookieAttribute(attribute) {
	cookies = document.cookie;
	WriteDbgLn("getCookieAttribute(" + attribute + "): cookie = " + cookies, 1);
	re = new RegExp(attribute + "=([a-z0-9_]*)", "i");
	values = cookies.match(re);
	if (values == null) {
		WriteDbgLn("Returning: null", 1)
		return "";
	} else {
		WriteDbgLn("Returning: " + values[1], 1)
		return values[1];
	}
}
// -----------------------------------------------------------------------------------
// Menu objects...
// -----------------------------------------------------------------------------------
function Menu(status) {
	this.submenus = new Array;
	this.count = 0;
	this.status = status;
	this.parent = 0;

	// Methods
	this.Add = AddSubMenu;
	this.Write = WriteMenu;
	this.Status = GetStatus;
	this.Root = GetRoot;
	
	return this;
}
function SubMenu(title, href, xpos, status, index, parent) {
	this.title = title;
	if (href) {
		this.href = href;
	} else {
		this.href = 0;
	}
	this.xpos = xpos;
	this.items = new Array;
	this.count = 0;
	if (status) {
		this.status = status;
	} else {
		this.status = 0;
	}
	this.index = index;
	this.parent = parent;
	
	// Methods
	this.Add = AddItem;
	this.AddCheckbox = AddCheckboxItem;
	this.AddSeparator = AddSeparator;
	this.AddMenu = AddItemSubMenu;
	this.Write = WriteSubMenu;
	this.Status = GetStatus;
	this.Root = GetRoot;

	return this;
}
function ItemSubMenu(title, img, href, status, index, parent) {
	this.title = title;
	this.items = new Array;
	this.count = 0;
	if (img) {
		this.img = new Image();
		this.img.src = img;
	} else {
		this.img = 0;
	}
	if (href) {
		this.href = href;
	} else {
		this.href = 0;
	}
	if (status) {
		this.status = status;
	} else {
		this.status = 0;
	}
	this.index = index;
	this.parent = parent;

	// Methods
	this.Add = AddItem;
	this.AddCheckbox = AddCheckboxItem;
	this.AddSeparator = AddSeparator;
	//this.AddMenu = AddItemSubMenu;
	this.Write = WriteItemSubMenu;
	this.Status = GetStatus;
	this.Root = GetRoot;
	
	return this;
}
function Item(title, img, href, status, index, parent) {
	this.title = title;
	if (img) {
		this.img = new Image();
		this.img.src = img;
	} else {
		this.img = 0;
	}
	if (href) {
		this.href = href;
	} else {
		this.href = 0;
	}
	if (status) {
		this.status = status;
	} else {
		this.status = 0;
	}
	this.index = index;
	this.parent = parent;
	this.isCheckbox = 0;
	
	// Methods
	this.Write = WriteItem;
	this.Status = GetStatus;
	this.Root = GetRoot;
	
	return this;
}
// -----------------------------------------------------------------------------------
// Menu functions...
// -----------------------------------------------------------------------------------
function AddSubMenu(title, href, xpos, status) {
	this.submenus[this.count] = new SubMenu(title, href, xpos, status, this.count, this);
	index = this.count++;
	
	return this.submenus[index];
}
function AddItemSubMenu(title, img, href, status) {
	this.items[this.count] = new ItemSubMenu(title, img, href, status, this.count, this);
	index = this.count++;

	return this.items[index];
}
function AddItem(title, img, href, status) {
	this.items[this.count] = new Item(title, img, href, status, this.count, this);
	index = this.count++;
	
	return this.items[index];
}
function AddCheckboxItem(title, href, status) {
	var item = this.Add(title, 0, href, status);
	item.isCheckbox = 1;
	
	return item;
}
function AddSeparator() {
	return this.Add("<hr>", 0, 0, 0);
}

function GetStatus() {
	if (this.status) {
		return this.status;
	} else if (this.Root().status) {
		return this.Root().status;
	} else {
		return "";
	}
}
function GetRoot() {
	if (this.parent) {
		return this.parent.Root();
	} else {
		return this;
	}
}
// -----------------------------------------------------------------------------------
// Menu write functions...
// -----------------------------------------------------------------------------------
function WriteMenu() {
	document.writeln("<div id='mainMenuNormal'>mainMenuNormal</div>");
	document.writeln("<div id='mainMenuRaised'>mainMenuRaised</div>");
	document.writeln("<div id='mainMenuSunken'>mainMenuSunken</div>");
	document.writeln("<div id='menuItemNormal'>menuItemNormal</div>");
	document.writeln("<div id='menuItemHighlight'>menuItemHighlight</div>");
	document.writeln("<div id='menuBar'> </div>");
	
	i = 0;
	for (i = 0; i < this.count; i++) {
		this.submenus[i].Write()
	}
	WriteDbgLn("<span id='statusBar'>" + this.status + "</span>");
	document.writeln("<span id='statusBar'>" + this.status + "</span>");
}
function WriteSubMenu() {
	id = "main" + this.index;
	subid = "sub" + this.index;
	document.writeln("<span id='" + id + "' class='mainMenu' style='left: " + (this.xpos + 10) + "'>");
	if (this.count > 0) {
		if (this.href) {
			document.write("<a href='" + this.href + "' style='left: " + this.xpos + "' ");
			document.write("onclick='javascript: ToggleMenuOnClick(\"\", \"\", " + this.xpos + "); return false;' ");
			document.write("onmouseover='javascript: ToggleMenuOnMouse(\"\", \"\", " + this.xpos + ", 1); return false;' ");
			document.writeln("onmouseout='javascript: ToggleMenuOnMouse(\"\", \"\", " + this.xpos + ", 0); return false;'>");
		} else {
			document.write("<a href='#' style='left: " + this.xpos + "'");
			document.write("onclick='javascript: ToggleMenuOnClick(\"" + id + "\", \"" + subid + "\", " + this.xpos + "); return false;' ");
			document.write("onmouseover='javascript: ToggleMenuOnMouse(\"" + id + "\", \"" + subid + "\", " + this.xpos + ", 1); return false;' ");
			document.writeln("onmouseout='javascript: ToggleMenuOnMouse(\"" + id + "\", \"" + subid + "\", " + this.xpos + ", 0); return false;'>");
		}
		document.writeln(this.title);
		document.writeln("</a>");
	}
	else
		document.writeln(this.title);
	document.writeln("</span>");
	if (!this.href) {
		document.writeln("<div id='" + subid + "' class='subMenu'>");
		var i = 0;
		for (i = 0; i < this.count; i++) {
			this.items[i].Write(this.index)
		}
		document.writeln("<img src='images/blank.gif' height='3'></div>");
	}
}
function WriteItemSubMenu(subindex) {
	id = "item" + subindex;
	id +="_" + this.index;
	subid = "sub" + subindex;
	subid += "_" + this.index;
	root = this.Root();
	document.writeln("<nobr><span id='" + id + "' class='menuItem'>");
	if (this.count > 0) {
		document.write("<a href='#' ");
		document.write("onclick='javascript: ToggleSubMenuOnClick(\"" + subid + "\"); return false;' ");
		document.write("onmouseover='javascript: ToggleSubMenuOnMouse(\"" + subid + "\"); return false;' ");
		document.writeln("onmouseout='javascript: ToggleSubMenuOnMouse(\"" + subid + "\"); return false;'>");
		if (this.img) {
			document.writeln("<img src='" + this.img.src + "' width='16' height='16' border='0' align='top'>");
		} else {
			document.writeln("<img src='images/blank.gif' width='16' height='16' border='0' align='top'>");
		}
		document.writeln(this.title);
		document.writeln("<img style='position: absolute; right: 5px' src='images/submenu.gif' width='16' height='16' border='0' align='top'>");
		document.writeln("</a>");
	} else {
		if (this.href) {
			re = new RegExp("\"", "g");
			statustext = this.Status();
			if (statustext.search(re) != -1) {
				statustext = statustext.replace(re, "\\\"");
			}
			defaulttext = root.Status();
			if (defaulttext.search(re) != -1) {
				defaulttext = defaulttext.replace(re, "\\\"");
			}
			document.write("<a href='" + this.href + "' ");
			document.write("onmouseover='javascript: ToggleItem(\"" + id + "\", \"" + statustext + "\", 1); return false;' ");
			document.writeln("onmouseout='javascript: ToggleItem(\"" + id + "\", \"" + defaulttext + "\", 0); return false;'>");
		}
		if (this.img) {
			document.writeln("<img src='" + this.img.src + "' width='16' height='16' border='0' align='top'>");
		} else if (this.count == 0) {
			document.writeln("<img src='images/blank.gif' width='16' height='16' border='0' align='top'>");
		}
		document.writeln(this.title);
		if (this.href) {
			document.writeln("</a>");
		}
	}
	document.writeln("</span></nobr><br>");

	document.writeln("<div id='" + subid + "' class='subMenu'>");
	var i = 0;
	for (i = 0; i < this.count; i++) {
		this.items[i].Write(subindex + "_" + this.index)
	}
	document.writeln("<img src='images/blank.gif' height='3'></div>");
}
function WriteItem(subindex) {
	id = "item" + subindex;
	id += "_" + this.index;
	imageid = "image" + subindex;
	imageid += "_" + this.index;
	root = this.Root();
	document.writeln("<nobr><span id='" + id + "' class='menuItem'>");
	if (this.title != "<hr>") {
		if (this.href) {
			re = new RegExp("javascript:", "i");
			if (this.href.search(re) != -1) {
				index = ("" + subindex).replace(/_\d*/g, "");
				mainid = "main" + root.submenus[index].index;
				subid = "sub" + root.submenus[index].index;
				xpos = root.submenus[index].xpos;
				href = this.href.replace(re, "javascript: ToggleMenuOnClick(\"" + mainid + "\", \"" + subid + "\", " + xpos + ");");
			} else {
				href = this.href;
			}
			re = new RegExp("\"", "g");
			statustext = this.Status();
			if (statustext.search(re) != -1) {
				statustext = statustext.replace(re, "\\\"");
			}
			defaulttext = root.Status();
			if (defaulttext.search(re) != -1) {
				defaulttext = defaulttext.replace(re, "\\\"");
			}
			document.write("<a href='" + href + "' ");
			document.write("onmouseover='javascript: ToggleItem(\"" + id + "\", \"" + statustext + "\", 1); return false;' ");
			document.write("onmouseout='javascript: ToggleItem(\"" + id + "\", \"" + defaulttext + "\", 0); return false;'");
			if (this.isCheckbox) {
				document.write(" onclick='javascript: ToggleImage(\"" + imageid + "\");'");
			}
			document.writeln(">");
		}
		if (this.img) {
			document.writeln("<img id='" + imageid + "' src='" + this.img.src + "' width='16' height='16' border='0' align='top'>");
		} else {
			document.writeln("<img id='" + imageid + "' src='images/blank.gif' width='16' height='16' border='0' align='top'>");
		}
		document.writeln(this.title);
		if (this.href) {
			document.writeln("</a>");
		}
		document.writeln("<br>");
	} else {
		document.writeln(this.title);
	}
	document.writeln("</span></nobr>");
}

// -----------------------------------------------------------------------------------
// Toolbar objects...
// -----------------------------------------------------------------------------------
function Toolbar() {
	this.tools = new Array;
	this.count = 0;
	
	// Methods
	this.AddTool = AddToolbarTool;
	this.AddMenuItem = AddToolbarMenuItem;
	this.AddSeparator = AddToolbarSeparator;
	this.Write = WriteToolbar;
	
	return this;
}
function Tool(img, href, status, index) {
	if (img) {
		this.img = new Image();
		this.img.src = img;
	} else {
		this.img = 0;
	}
	if (href) {
		this.href = href;
	} else {
		this.href = 0;
	}
	if (status) {
		this.status = status;
	} else {
		this.status = "";
	}
	this.index = index;
	
	// Methods
	this.Write = WriteTool;

	return this;
}
// -----------------------------------------------------------------------------------
// Toolbar functions...
// -----------------------------------------------------------------------------------
function AddToolbarTool(img, href, status) {
	this.tools[this.count] = new Tool(img, href, status, this.count);
	index = this.count++;
	
	return this.tools[index];
}
function AddToolbarMenuItem(item) {
	return this.AddTool(item.img.src, item.href, item.status);
}
function AddToolbarSeparator() {
	return this.AddTool(0, 0, 0);
}
// -----------------------------------------------------------------------------------
// Toolbar write functions...
// -----------------------------------------------------------------------------------
function WriteToolbar() {
	document.writeln("<div id='toolBar'>");
	i = 0;
	for (i = 0; i < this.count; i++) {
		this.tools[i].Write()
	}
	document.writeln("</div><br>");
}
function WriteTool() {
	if (this.href) {
		document.writeln("<a href='" + this.href + "'>");
	}
	if (this.img) {
		document.writeln("<img src='" + this.img.src + "' width='16' height='16' vspace='1' border='0' align='top' alt='" + this.status + "'>");
	} else {
		//document.writeln("<img src='images/blank.gif' width='16' height='16' border='0' align='top'>");
		document.writeln("&nbsp;|&nbsp;");
	}
	if (this.href) {
		document.writeln("</a>");
	}
}

// -----------------------------------------------------------------------------------
// Main functions...
// -----------------------------------------------------------------------------------
function GenerateMenu(menu) {
	if(browser.isIE) {
		menu.Write();
	} else if (location.pathname.indexOf("contents.html") > 0){
		ListMenu(menu);
	}
}
function ListMenu(menu) {
	document.writeln("<nobr>");
	for (i = 0; i < menu.count; i++) {
		for (j = 0; j < menu.submenus[i].count; j++) {
			if (menu.submenus[i].items[j].title == "<hr>") continue;
			if (menu.submenus[i].items[j].isCheckbox) continue;
			if (menu.submenus[i].items[j].img) {
				document.writeln("<img src='" + menu.submenus[i].items[j].img.src + "' width='16' height='16'>");
			} else {
				document.writeln("<img src='images/blank.gif' width='16' height='16'>");
			}
			if (menu.submenus[i].items[j].count) {
				document.writeln("<span class='menuItem'>" + menu.submenus[i].items[j].title + "</span>");
				document.writeln("<br>");
				for (k = 0; k < menu.submenus[i].items[j].count; k++) {
					if (!menu.submenus[i].items[j].items[k].href) continue;
					document.writeln("<img src='images/blank.gif' width='16' height='16'>");
					if (menu.submenus[i].items[j].items[k].img) {
						document.writeln("<img src='" + menu.submenus[i].items[j].items[k].img.src + "' width='16' height='16'>");
					} else {
						document.writeln("<img src='images/blank.gif' width='16' height='16'>");
					}
					document.writeln("<a class='menuItem' href='" + menu.submenus[i].items[j].items[k].href + "' target='body'>");
					document.writeln(menu.submenus[i].items[j].items[k].title);
					document.writeln("</a>");
					document.writeln("<br>");
				}
			} else if (menu.submenus[i].items[j].href) {
				document.writeln("<a class='menuItem' href='" + menu.submenus[i].items[j].href + "' target='body'>");
				document.writeln(menu.submenus[i].items[j].title);
				document.writeln("</a>");
			} else {
				document.writeln(menu.submenus[i].items[j].title);
			}
			document.writeln("<br>");
		}
		document.writeln("<hr>");
	}
	document.writeln("</nobr>");
}
function GenerateToolbar(toolbar) {
	if(browser.isIE) {
		toolbar.Write();
		InitToolbar();
	}
}
