	$(document).ready(function() {

		$("a.popup").click(	function (e) {
			if (!e.ctrlKey && !e.shiftKey) {
				openBrWindow($(this).attr("href"), '', '');
				return false;
			}
		});

		$("a.pdflink").each(function() {
			if (this.href.toLowerCase().indexOf(".pdf") != -1) {
				this.target = "_blank";	
			}
		});
	});

	var defaultWidth = 460;

	function getPopupSizeFilename(url) {
		var fileName = url;	
		if (fileName.indexOf("/") != -1) fileName = fileName.slice(fileName.lastIndexOf("/") + 1, fileName.length);

		var width;
		var height;
		var scrollbars;

		if ( document.createElement ) {
			if (fileName.indexOf("x") != -1 && fileName.indexOf("_") != -1 && (fileName.indexOf("x") < fileName.indexOf("_")) ) {
				width = fileName.slice(0, fileName.indexOf("x"));
				height = fileName.slice(fileName.indexOf("x") + 1, fileName.indexOf("_"));
				scrollbars = ((fileName.slice(fileName.indexOf("_") + 1, fileName.indexOf("_") + 3) == "s_") ? true : false);
			}
			if ( isNaN(width) ) width = defaultWidth;
			if ( isNaN(height) ) height = 0;
		}
		
		return { 
			width: width, 
			height: height,
			scrollbars: scrollbars
		};
	}

	function verifyPopupSize(dim) {
		var maxHeight = 360;
		
		if(dim.width > defaultWidth) {
			dim.width = 660;
		} else {
			dim.width = defaultWidth;
		}
		
		if (dim.height != 0) {
			if(dim.height < 200) {
				dim.height = 200;
			} else if(dim.height > (maxHeight + 40)) {

				dim.scrollbars = true;
				dim.height = maxHeight;

			}
		} else {
			dim.height = 300;
		}
		return dim;
	}


	function popupLoaded(doc) {
		var popup = document.getElementById("PopupBox");
		var popupFrame = document.getElementById("PopupFrame");
		var dim = getPopupSizeFilename(popupFrame.src);

		// Om popupstorlek redan satt med filnamn avbryt
		if ( dim.width != defaultWidth || dim.height != 0) {
			if (!dim.scrollbars) {
				doc.body.scroll = 'no';
			}
			return;
		}

		if (!doc.body) {
			return;
		}
		
		
		dim.width = $(doc).width();
		dim.height = $(doc).height();
		dim.scrollbars = false;
		
		$("#CloseButton.loading").addClass("closeButton");//.removeClass("loading")
		
		//alert(dim.width);
		
		if (dim.width == 0 || dim.height == 0 ) {
			return;
		}
		
		if ( popupFrame && popup ) {
			verifyPopupSize(dim);
			
			$(popupFrame).animate({width: dim.width, height: dim.height, queue: true}, 400);
			$(popup).animate({width: dim.width, queue: true}, 400);
			
			/*
			popup.style.width = dim.width + "px";
			popupFrame.style.width = dim.width + "px";
			popupFrame.style.height = dim.height+"px";
			*/
			if (dim.scrollbars) {
				popupFrame.scrolling = "yes";
				popupFrame.style.overflow = "auto";
			}
			else {
				popupFrame.scrolling = "no";
				popupFrame.style.overflow = "hidden";
				doc.body.scroll = 'no';
			}
		}
		
		//$(popup).slideDown('slow');
		
		
	}

	function closePopup() {
		var obj = document.getElementById("PopupBox");
		
		if ( obj.parentNode ) {
			obj.parentNode.removeChild(obj);
		}
		
	}

	function openBrWindow(url, name, props) {
		openPopup(url);	
	}
	
	function openPopup(url) {
		var dim = getPopupSizeFilename(url);

		if ( document.createElement ) {
			var popupObj = document.createElement("DIV");
			popupObj.id = "PopupBox";
			popupObj.innerHTML = "<a href=\"javascript: closePopup();\" id=\"CloseButton\" class=\"loading\"><span>Close</span></a>";  //<img src=\"" + relPath + "images/closepopup_button.gif\" width=\"16\" height=\"16\" border=\"0\" />
			//popupObj.innerHTML += "<iframe id=\"PopupFrame\" name=\"PopupFrame\" src=\"" + url + "\" scrolling=\"" + ((scrollbars) ? "yes" : "no") + "\" frameborder=\"0\"></iframe>";
			popupObj.innerHTML += "<iframe id=\"PopupFrame\" name=\"PopupFrame\" src=\"" + url + "\" scrolling=\"yes\" frameborder=\"0\"></iframe>";

			var popupContainer = document.getElementById("Popup");
			if (popupContainer) {
				if ( popupContainer.childNodes.length > 0 ) {
					popupContainer.removeChild(popupContainer.childNodes[0]);
				}
				popupContainer.appendChild(popupObj);
			}
		}

		var popup = document.getElementById("PopupBox");
		var popupFrame = document.getElementById("PopupFrame");
		
		if ( popupFrame && popup ) {
			if (dim.height == 0) {
				verifyPopupSize(dim);
				dim.height = 1;
				popup.style.width = dim.width + "px";
				popupFrame.style.width = dim.width + "px";
				popupFrame.style.height = dim.height+"px";
				scroll(0,0);
				$(popup).show();
			} else {
				verifyPopupSize(dim);
				popup.style.width = dim.width + "px";
				popupFrame.style.width = dim.width + "px";
				popupFrame.style.height = dim.height+"px";
				scroll(0,0);
				$(popup).slideDown(400);
			}


		}
	}
	
	
	/// Sitemap expand function
	function expand(id,linkobj) {
		linkobj.blur();
		var menu = document.getElementById(id);

		if ( menu ) {
			if ( menu.style.display == "none" ) {
				menu.style.display = "";
			} else {
				menu.style.display = "none";
			}
		}
		
		var imgobj = linkobj.getElementsByTagName("IMG");
		if ( imgobj.length > 0 ) {
			var imgpath = imgobj[0].src.slice(0,imgobj[0].src.lastIndexOf("/")+1);
			var imgfile = imgobj[0].src.slice(imgobj[0].src.lastIndexOf("/")+1);

			if ( imgfile == "plus_first.gif" )
				imgobj[0].src = imgpath + "minus_first.gif";
			else if ( imgfile == "plus_last.gif" )
				imgobj[0].src = imgpath + "minus_last.gif";
			else if ( imgfile == "plus_mitt.gif" )
				imgobj[0].src = imgpath + "minus_mitt.gif";
			else if ( imgfile == "minus_first.gif" )
				imgobj[0].src = imgpath + "plus_first.gif";
			else if ( imgfile == "minus_last.gif" )
				imgobj[0].src = imgpath + "plus_last.gif";
			else if ( imgfile == "minus_mitt.gif" )
				imgobj[0].src = imgpath + "plus_mitt.gif";
		}
	}

	


