// $Id: common.js 4342 2010-06-28 07:53:52Z k.zhukov $

function gebi(id) {
	return document.getElementById(id);
}

function showMsg(url, title, w, h) {
	try {
		w=w||480;
		h=h||300;
		var win = top.window;
		var doc = top.document;
		var width = doc.body.clientWidth;
		var height = doc.body.clientHeight;
		var divWidth = Math.max(doc.compatMode != 'CSS1Compat' ? doc.body.scrollWidth : doc.documentElement.scrollWidth,width);
		var divHeight = Math.max(doc.compatMode != 'CSS1Compat' ? doc.body.scrollHeight : doc.documentElement.scrollHeight,height);
		var obj = top.gebi('errorFrame');
		var div = top.gebi('errorDiv');
		var divTitle = top.gebi('errorTitle');
		if (!obj || !div) return false;
		obj.src=url;
		div.style.left = ((width-w)/2);
		div.style.top = ((height-h)/2);
		div.style.display = 'block';
		obj.style.display = 'block';
		divTitle.innerHTML = title;
		win.scrollTo(0,0);
	} catch (e) {}
}

function showArtifactInfo(artifactId, artikulId, setId) {
	var url = "/artifact_info.php";
	if (artifactId) url += "?artifactId="+artifactId;
	else if (artikulId) url += "?artikulId="+artikulId;
	else if (setId) url += "?setId="+setId;
	else return;
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showUserInfo(nick) {
	var url = "/user_info.php?nick="+nick;
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function artifactAlt(obj, evnt, show) {
	var div = gebi(obj.getAttribute('div_id'));
	if (!div)
		return;
	var act1 = obj.getAttribute('act1');
	var act2 = obj.getAttribute('act2');
	if (show == 2) {
		document.onmousemove = function(e) {
			artifactAlt(obj, e || event, 1)
		}
		div.style.display = 'block';
		if (act1 || act2) {
			if (obj.tagName == 'IMAGE') {
				obj.src = ("images/itemact-" + act1) + (act2 + ".gif")
			} else {
				obj.style.backgroundImage = 'url(' + ("images/itemact-" + act1) + (act2 + ".gif") + ')'
			}
		}
	}
	if (!show) {
		if (act1 || act2) {
			if (obj.tagName == 'IMAGE') {
				obj.src = "images/d.gif"
			} else {
				obj.style.backgroundImage = 'url(' + "images/d.gif" + ')'
			}
		}
		div.style.display = 'none';
		document.onmousemove = function() {
		}
		return;
	}

	var ex = evnt.clientX + document.body.scrollLeft;
	var ey = evnt.clientY + document.body.scrollTop;

	if (act1 || act2) {
		obj.style.cursor = 'default'
		obj.onclick = function() {
			showArtifactInfo(obj.getAttribute('aid'))
		}
		var coord = getCoords(obj)
		var cont = gebi("item_list")
		var rel_x = (ex + cont.scrollLeft - coord.l)
		if (rel_x >= 40) {
			var rel_y = (ey + cont.scrollTop - coord.t)
			if (act1 != 0 && rel_y < 20) {
				obj.onclick = function() {
					try {
						artifactAct(obj, act1)
					} catch (e) {
					}
				}
				try {
					obj.style.cursor = 'hand'
				} catch (e) {
				}
				try {
					obj.style.cursor = 'pointer'
				} catch (e) {
				}
			}
			if (act2 != 0 && rel_y >= 40) {
				obj.onclick = function() {
					try {
						artifactAct(obj, act2)
					} catch (e) {
					}
				}
				try {
					obj.style.cursor = 'hand'
				} catch (e) {
				}
				try {
					obj.style.cursor = 'pointer'
				} catch (e) {
				}
			}
		}
	}
	var x = evnt.clientX + div.offsetWidth > document.body.clientWidth - 7 ? ex - div.offsetWidth - 10 : ex + 10;
	var y = evnt.clientY + div.offsetHeight > document.body.clientHeight - 7 ? ey - div.offsetHeight - 10
			: ey + 10;

	if (x < 0) {
		x = ex - div.offsetWidth / 2
	}
	if (x < 7) {
		x = 7
	}
	if (x > document.body.clientWidth - div.offsetWidth - 7) {
		x = document.body.clientWidth - div.offsetWidth - 7
	}

	div.style.left = x;
	div.style.top = y;
}
