//
//
// Simple Popup
// coded by Nikola Toshkov
// version 0.8
//

var popupgi, popupcls, popupact, popupdeact, popupreact, popupopn, sx, sy, dx, dy, cs, ms;
var popupcgi, popupcact, csx, csy, cdx, cdy, ccs;
var popupr = false;
var popupcl = false;

function gettop(obj) {
	try {
		return (obj.offsetParent != undefined) ? parseInt(obj.offsetTop)  + gettop(obj.offsetParent) : 0;
	} catch (e) {}
}

function getleft(obj) {
	try {
		return (obj.offsetParent != undefined) ? parseInt(obj.offsetLeft) + getleft(obj.offsetParent) : 0;
	} catch (e) {}
}

function registerPopup(name, ppdiv, content, backgroundColor, width, height,  ofleft, oftop, borderColor, borderWidth, waitTime, showTime, holdTime, hideTime, showSteps, hideSteps, className) {
	handle = document.getElementById(name);
	
	if (ppdiv != "") {
		content = document.getElementById(ppdiv).innerHTML;
	}

	var oDiv = document.createElement("DIV");

	oDiv.style.borderWidth = borderWidth + "px";
	oDiv.style.borderStyle = "solid";
	oDiv.style.borderColor = borderColor;
	oDiv.style.width = width + "px";
	oDiv.style.height = width + "px";

	oDiv.style.display = "none";
	oDiv.style.position = "absolute";
				
	oDiv.id = name + "|popup";


	var hx = getleft(handle);
	var hy = gettop(handle);


	oDiv.show = function(hleft, htop, dleft, dtop) {
		popupr = true;
		popupreact = "";
		var _showTime = showTime;
		var _showSteps = showSteps;
		var _mwidth = width;
		var _mheight = height;

		this.style.position = "absolute";
		this.style.width = "30px";
		this.style.height = "15px";

		this.style.left = hleft;
		this.style.top = htop;
		this.style.display = "block";

		sx = Math.round(dleft / _showSteps);
		sy = Math.round(dtop / _showSteps);
		dx = Math.round((_mwidth - 30) / _showSteps);
		dy = Math.round((_mheight - 30) / _showSteps);
		cs = 0;
		ms = _showSteps;
		popupact = this.id;

		popupgi = setInterval("showPopup()", Math.round(_showTime / _showSteps));
	}

	oDiv.hide = function(hleft, htop) {
		popupcl = true;
		popupdeact = "";
		var _hideTime = hideTime;
		var _hideSteps = hideSteps;

		this.style.backgroundColor = "";
		this.className = "";
		this.innerHTML = "";

		csx = Math.round((hleft - getleft(this)) / _hideSteps);
		csy = Math.round((htop - gettop(this)) / _hideSteps);
		cdx = Math.round(parseInt(this.offsetWidth) / _hideSteps);
		cdy = Math.round(parseInt(this.offsetHeight) / _hideSteps);
		ccs = _hideSteps;
		popupcact = this.id;

		popupcgi = setInterval("closePopup()", Math.round(_hideTime / _hideSteps));
	}

	oDiv.render = function() {
		var _bc = backgroundColor;
		var _content = content;
		var _className = className;

		if (!_className) this.style.backgroundColor = _bc;
		this.className = _className;
		
		this.innerHTML = _content;
	}

	oDiv.unRender = function() {
		this.style.display = "none";
	}

	oDiv.onmouseleave = function() {
		var _holdTime = holdTime;
		var _hx = hx;
		var _hy = hy;

		popupdeact = this.id;
		popupcls = setTimeout("document.getElementById('" + this.id + "').hide(" + _hx + "," + _hy + ")", _holdTime);
	}

	oDiv.onmouseenter = function() {
		if (popupdeact == this.id && !popupcl) clearTimeout(popupcls);
	}

	document.body.appendChild(oDiv);

	handle.onmouseover = function() {
		var _id = oDiv.id;
		var _ol = ofleft;
		var _ot = oftop;
		var _holdTime = holdTime;

		if (popupr) return false;

		popupreact = _id;
		popupopn = setTimeout("document.getElementById('" + _id + "').show(" + getleft(this) + ", " + gettop(this) + ", " + _ol + ", " + _ot + ")", _holdTime);
	}

	handle.onmouseout = function() {
		var _id = oDiv.id;
		var _holdTime = holdTime;

		if (popupreact == _id) {
			clearTimeout(popupopn);
			return false;
		}

		if (popupcl) return false;

		vvx = getleft(this);
		vvy = gettop(this);

		popupdeact = _id;
		popupcls = setTimeout("document.getElementById('" + _id + "').hide(" + vvx + ", " + vvy + ")", _holdTime);
	}
}

function closePopup() {
	ccs--;
	ppu = document.getElementById(popupcact);
	ppu.style.width = ccs * cdx + "px";
	ppu.style.height = ccs * cdy + "px";
	ppu.style.top = gettop(ppu) + csy + "px";
	ppu.style.left = getleft(ppu) + csx + "px";
	if (ccs <= 0) {
		clearInterval(popupcgi);
		ppu.unRender();
		popupcl = false;
	}
}

function showPopup(tx,ty) {
	cs++;
	ppu = document.getElementById(popupact);
	ppu.style.width = 30 + cs * dx + "px";
	ppu.style.height = 30 + cs * dy + "px";
	ppu.style.top = gettop(ppu) + sy + "px";
	ppu.style.left = getleft(ppu) + sx + "px";
	if (cs >= ms) {
		clearInterval(popupgi);
		ppu.render();
		popupr = false;
	}
}
