function FindElement(sId) {
	var o = null;
	if (document.getElementById)
		o = document.getElementById(sId);
	if (!o && document.all)
		o = document.all[sId];
	if (!o && document.layers)
		o = document.layers[sId];
	return o;
}
function OpenMailPopup(sUrl) {
	window.open(sUrl, "kk", "width=600,height=720,scrollbars=1,menubar=0");
	return false
}
function openEventEdit(url, nimi) {
	var handle = window.open(url, nimi, "width=850,height=545,resizable=0,menubar=0,toolbar=0,status=0,location=0,titlebar=0");
	if (!handle.opener)
		handle.opener = self;
	childHandle = handle;
	handle.focus();
	return false;
}
var g_oImgDiv = null;
var g_oImgDivSdw = null;
var g_bImgDrag = false;
var g_iMPX = 0;
var g_iMPY = 0;
function GetElements() {
	if (document.getElementById) {
		g_oImgDiv = document.getElementById("imgDiv");
		g_oImgDivSdw = document.getElementById("imgDivSdw");
	} else if (document.all) {
		g_oImgDiv = document.all["imgDiv"];
		g_oImgDivSdw = document.all["imgDivSdw"];
	}
}
function PositionImage(o) {
	if (g_oImgDiv) {
		var iWindowWidth = -1;
		var iTop = 60;
		if (window.outerWidth)
			iWindowWidth = window.outerWidth;
		else
			iWindowWidth = document.body.clientWidth;
		if (document.body && (document.body.scrollLeft || document.body.scrollTop))
			iTop += document.body.scrollTop;
		else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
			iTop += document.documentElement.scrollTop;
		g_oImgDiv.style.display = "block";
		g_oImgDiv.style.top = iTop + "px";
		g_oImgDiv.style.left = Math.round((iWindowWidth / 2) - (o.width / 2)) + "px";
		PositionShadow();
	}
	g_bImgDrag = false;
}
function PositionShadow() {
	if (g_oImgDiv && g_oImgDivSdw) {
		var iTop = parseInt(g_oImgDiv.style.top) + 5;
		var iLeft = parseInt(g_oImgDiv.style.left) + 5;
		var iWidth = g_oImgDiv.offsetWidth;
		var iHeight = g_oImgDiv.offsetHeight;
		if (iWidth > 0 && iHeight > 0) {
			g_oImgDivSdw.style.display = "block";
			g_oImgDivSdw.style.top = iTop + "px";
			g_oImgDivSdw.style.left = iLeft + "px";
			g_oImgDivSdw.style.width = iWidth + "px";
			g_oImgDivSdw.style.height = iHeight + "px";
		}
	}
}
function StartImageDrag() {
	g_iMPX = 0;
	g_iMPY = 0;
	g_bImgDrag = true;
}
function DragImage(e) {
	if (g_oImgDiv && g_bImgDrag) {
		var iX = parseInt(g_oImgDiv.style.left);
		var iY = parseInt(g_oImgDiv.style.top);
		var iMX = (e) ? e.clientX : event.clientX;
		var iMY = (e) ? e.clientY : event.clientY;
		if (g_iMPX > 0 || g_iMPY > 0) {
			g_oImgDiv.style.left = (iX + (iMX - g_iMPX)) + "px";
			g_oImgDiv.style.top = (iY + (iMY - g_iMPY)) + "px";
			PositionShadow();
		}
		g_iMPX = iMX;
		g_iMPY = iMY;
		return false;
	}
}
function ShowImage(sImgUrl, sAlt) {
	GetElements();
	if (g_oImgDiv) {
		g_oImgDiv.style.display = "none";
		g_oImgDiv.innerHTML = "<div class=\"title\" onmousedown=\"StartImageDrag();\" onmouseup=\"g_bImgDrag=false;\"><div class=\"titleCnt\">" + sAlt + "</div><div onClick=\"HideImage();\" class=\"close\" title=\"Sulje kuva\"></div></div><div class=\"image\"><img src=\"" + sImgUrl + "\" onload=\"if(PositionImage) PositionImage(this)\" onClick=\"return HideImage();\" alt=\"" + sAlt + "\"/></div>";
	}
	return false;
}
function HideImage() {
	GetElements();
	if (g_oImgDiv)
		g_oImgDiv.style.display = "none";
	if (g_oImgDivSdw)
		g_oImgDivSdw.style.display = "none";
	g_bImgDrag = false;
	return false;
}

var g_oSubMenuTimer = null;
var g_sSubMenuId    = "";
var g_iSubMenuDelay = 500;
function ToggleSubMenu(oMenu, sId, b) {
	if (document.getElementById) {
		var o = document.getElementById(sId);
		if (o)
			o.style.display = (b) ? "block" : "none";
		if (oMenu && oMenu.className) {
			//if (oMenu.className.indexOf('Active') < 0)
				oMenu.className = (b) ? oMenu.className + " Over" : oMenu.className.replace(' Over', '');
		}
	}
}
function ShowSubMenu(sId) {
	HideSubMenu();
	var oMenu     = FindElement("sm" + sId);
	var oMenuRoot = FindElement("smroot" + sId);
	if (oMenu) {
		g_sSubMenuId    = sId;
		oMenu.style.display = "block";
	}
	if (oMenuRoot && oMenuRoot.className)
		oMenuRoot.className = oMenuRoot.className + " Over";
}
function HideSubMenu() {
	if (g_oSubMenuTimer)
		clearTimeout(g_oSubMenuTimer);
	if (g_sSubMenuId) {
		var oMenu     = FindElement("sm" + g_sSubMenuId);
		var oMenuRoot = FindElement("smroot" + g_sSubMenuId);
		if (oMenu)
			oMenu.style.display = "none";
		if (oMenuRoot && oMenuRoot.className)
			oMenuRoot.className = oMenuRoot.className.replace(' Over', '');
	}
}
function InitHideSubMenu() {
	if (g_oSubMenuTimer)
		clearTimeout(g_oSubMenuTimer);
	g_oSubMenuTimer = setTimeout("HideSubMenu()", g_iSubMenuDelay);
}

var g_oSubPopTimer = null;
var g_sSubPopId    = "";
var g_iSubPopDelay = 500;
function GetTreeParentY(o) {
	var y     = -1;
	var oTemp = o.offsetParent;
	if (oTemp)
		y += oTemp.offsetTop;
	while (oTemp != null) {
		if (oTemp.id == "tree" || oTemp.id == "treeLinks") {
			y += oTemp.offsetParent.offsetTop;
			break;
		}
		oTemp = oTemp.offsetParent;
	}
	return y;
}
function ShowSubPopup(sId) {
	HideSubPopup();
	var o = FindElement("subpop_" + sId);
	if (o) {
		g_sSubPopId = sId;
		o.style.display = "block";
		var oLink = FindElement("sublnk_" + sId);
		if (oLink) {
			if (oLink.offsetParent)
				o.style.top = GetTreeParentY(oLink) + "px";
			oLink.className += " Over";
		}
	}
}
function HideSubPopup() {
	if (g_oSubPopTimer)
		clearTimeout(g_oSubPopTimer);
	if (g_sSubPopId) {
		var o = FindElement("subpop_" + g_sSubPopId);
		if (o) {
			o.style.display = "none";
			var oLink = FindElement("sublnk_" + g_sSubPopId);
			if (oLink)
				oLink.className = oLink.className.replace(" Over", "");
		}
	}
}
function InitHideSubPopup() {
	if (g_oSubPopTimer)
		clearTimeout(g_oSubPopTimer);
	g_oSubPopTimer = setTimeout("HideSubPopup()", g_iSubPopDelay);
}

document.onmousemove = DragImage;
document.onmouseup = new Function("g_bImgDrag=false");
