function FindElement(sId) {
	var o = null;
	if(document.getElementById)
		o = document.getElementById(sId);
	if(!o && document.all)
		o = document.all[sId];
	return o;
}
function openForm(url, nimi) {
	var handle = window.open(url, nimi, "width=750,height=450,resizable=no,toolbar=no,status=no,location=no,titlebar=no");
	if (!handle.opener) handle.opener = self;
	childHandle = handle;
	handle.focus();
	return false;
}

function openResizableForm(url, nimi) {
	var handle = window.open(url, nimi, "width=750,height=450,resizable=yes,toolbar=no,status=no,location=no,titlebar=no");
	if (!handle.opener) handle.opener = self;
	childHandle = handle;
	handle.focus();
	return false;
}

function AvaaGalleria() {
	var handle = window.open("http://kehittamiskeskus.salonseutu.fi/valokuvat.aspx", "Kuvagalleria", "width=1000,height=750,resizable=yes,scrollbars=yes,toolbar=no,status=no,location=no,titlebar=no");
	handle.focus();
	return false;
}

function GetElementX(o) {
	var iX = 0;
	var oTemp = null;
	if (document.getElementById) {
		oTemp = o.offsetParent;
		iX = o.offsetLeft;
		while (oTemp != null) {
			iX += oTemp.offsetLeft;
			oTemp = oTemp.offsetParent;
		}
	}
	return iX;
}
function GetElementY(o) {
	var iY = 0;
	var oTemp = null;
	if (document.getElementById) {
		oTemp = o.offsetParent;
		iY = o.offsetTop;
		while (oTemp != null) {
			iY += oTemp.offsetTop;
			oTemp = oTemp.offsetParent;
		}
	}
	return iY;
}
/* IMAGE POPUPS */
var g_oImgDiv    = null;
var g_oImgDivSdw = null;
var g_bImgDrag   = false;
var g_iMPX       = 0;
var g_iMPY       = 0;
function GetElements() {
	g_oImgDiv    = FindElement("imgDiv");
	g_oImgDivSdw = FindElement("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;
}
document.onmousemove = DragImage;
document.onmouseup   = new Function("g_bImgDrag=false");
