// herold bubble
// (c)2005-2006 Lennart Pegel, neofonie GmbH

// struct TPosition
function TPosition(xx,yy) {
	this.x = xx;
	this.y = yy;
}

function Bubble() {

	// construction part..
	
	document.write('<div id="bub" onmouseover="bub.startShowTimer()" onmouseout="bub.off()"><div id="btop"></div><div id="bbody" onmouseover="bub.startShowTimer()" onmouseout="bub.off()"><div class="bclose"><!--<a href="javascript:void(0)" onclick="bub.off(1)">schließen</a>--></div><div class="bdata" id="bdata" onmouseover="bub.startShowTimer()" onmouseout="bub.off()"></div></div><div id="bbot"></div></div>');

	this.DELAY_SHOW = 350;
	this.DELAY_HIDE = 6000;
	this.XOFFSET_STD = -120; //-63; // -76 - 63; // -35;
	this.XOFFSET_MIRROR = -222;
	
	this.xoffset = this.XOFFSET_STD;
	
	this.container = document.getElementById("bub");
	this.bdata = document.getElementById("bdata");
	this.bbot = document.getElementById("bbot");

	this.dataID = "";
	this.aTag = null;
	this.timer = null;
	
	// trivial on/off
	this.off = function(immed) {
		if (immed!=null) {
			if (this.timer!=null) this.resetTimer();
			this.container.style.display = "none";
			this.isVisible = false;
		} else {
			this.resetTimer();
			this.timer = setTimeout("bub.off(true)",50);
		}
	}

	// resets the timeout
	this.resetTimer = function() {
		if (this.timer!=null) {
			clearTimeout(this.timer);
			this.timer = null;
		}
	}

	this.startHideTimer = function () {
		this.resetTimer();
		this.timer = setTimeout("bub.off(true)",this.DELAY_HIDE);
	}

	this.startShowTimer = function() {
		this.resetTimer();
		this.timer = setTimeout("bub.show(bub.dataID,bub.aTag,true)",this.DELAY_SHOW);
	}

	// dataSrc = id of container to pick data from
	// aTag = a object to position bubble at
	this.show = function (dataSrc,aTag,doit) {
		if (doit==null) {
			if (this.isVisible) {
				this.off(true);
			}
			this.dataID = dataSrc;
			this.aTag = aTag;
			this.startShowTimer();
			return;
		}

		if (this.isVisible) return;

		this.leechData(dataSrc);
		this.resetContainer();
		var bubbleHeight = this.bbot.offsetTop + 8; //54;

		var aTagPos = this.getPosOf(aTag);
		var newX = aTagPos.x + this.xoffset;
		if (newX<0) newX = 4;
		var newY = aTagPos.y - bubbleHeight + 5;

		// alert(newX + " : " + newY);
		
		this.container.style.left = newX+"px";
		this.container.style.top = newY+"px";
		this.isVisible = true;
	}

	// puts the container to (0;0)
	this.resetContainer = function() {
		if (parseInt(this.container.style.left)<0) this.container.style.left = "0px";
		if (parseInt(this.container.style.top)<0) this.container.style.top = "0px";
		if (this.container.style.display!="block") this.container.style.display="block";
	}
	
	// returns the coordinate of object obj as TPosition object
	this.getPosOf = function(obj) {
		var pos = new TPosition(0,0);
		if (obj!=null) {
			var tmp = document.createElement("SPAN");
			tmp.style.position = "absolute";
			obj.parentNode.appendChild(tmp);
			
			if (document.all) {
				var el = tmp;
				for (var lx=0,ly=0;el!=null;lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
				pos.x = lx; 
				pos.y = ly;
			} else {
				pos.x = tmp.offsetLeft;
				pos.y = tmp.offsetTop;
			}
			tmp.parentNode.removeChild(tmp);
		}
		return pos;			
	}
	
	// copies html from object defined by srcID  into bdata
	this.leechData = function(srcID) {
		var s = document.getElementById(srcID);
		s.innerHTML = s.innerHTML.replace(/^\r\n/,"");
		if (s==null) {
			this.bdata.innerHTML = "invalid srcID";
		} else if (this.bdata.innerHTML!=s.innerHTML) {
			this.bdata.innerHTML = s.innerHTML;
		}
	}

	// mirrors the lower part of the keys on true
	this.mirror = function(doit) {
		if (doit) {
			this.xoffset = this.XOFFSET_MIRROR;
			if (this.bbot.className.length==0) this.bbot.className = "mir";
		} else {
			this.xoffset = this.XOFFSET_STD;
			if (this.bbot.className.length!=0) this.bbot.className = "";
		}
	}
	
	// if container unavailable (document.write failed), set off() + show() back to prototype status
	if (this.container==null) {
		this.mirror = function(){};
		this.off = function(){};
		this.show = function(){};
	}
	
} // end class

var bub = new Bubble();

