
var globParpmElement;
var rTimer2 = new rTimer2Class();
// cv = 0; // nicht ref
function fnToolTip(pmElement, pmArguments, pmE) {
	new rToolTipClass(pmElement, pmArguments, pmE);
}

function rToolTipClass(pmElement, pmArguments, pmE) {

	// konfiguriert Darstellung
	this.delayDisplay = 400; // i:300
	this.delayHide = 200; // mind. 100ms kleiner als delayDisplay
	
	this.displayOffsetX = 10; // i:10, vgn:-2
	this.displayOffsetY = 10; // i:10, vgn:-2
	this.defaultHeight = 40;
	this.defaultWidth = 260; // i:180
	this.maxHeight = 400; // i:300
	this.maxWidth = 400; // v:360
	
	globParpmElement = pmElement;
	
	// initialisiert Tooltipp
	this.delayPrepareDisplay = this.delayDisplay - 100; // v:-300
	this.delayHide = this.delayPrepareDisplay<this.delayHide?this.delayPrepareDisplay:this.delayHide;

	this.element = pmElement;
	for(var arg in pmArguments) {
		this[arg] = pmArguments[arg];
	}
	pmElement.tooltip = this;
	pmElement.onmouseover = fnToolTip_TriggerDisplay;
	var el = $(pmElement);
	if(el.onmouseover && !pmArguments.notrigger) {
		if(pmE)
			el.onmouseover(pmE);
		else if(typeof(window.event) != "undefined")
			el.onmouseover(window.event);

	}
	//pmElement.style.cursor = 'help';
	fnRemoveTitle(pmElement);
}
rToolTipClass.prototype.fnPrepareDisplay1 = function(e) {

	this.element.onmouseout = fnToolTip_CancelDisplay;
	
	var div = document.getElementById("roccasTooltip");
	if(div.tooltip && false)
		div.tooltip.fnHide();
   
   	ausgleich = 0;
	if(e == null && window.Anwesenheit && window.Anwesenheit.event) { // holt Ereignis aus Iframe (nur IE)
		e =  window.Anwesenheit.event;
		ausgleich = document.getElementById("Anwesenheit").style.left.replace("px", "");
	}

	this.mouseX = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) + parseInt(ausgleich));
	this.mouseY = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop));

	if(this.id || this.short) {
		var params = {
			Id: typeof(this.id)=="undefined"?"":this.id,
			Short: typeof(this.short)=="undefined"?"":this.short,
			Extra: typeof(this.extra)=="undefined"?"":this.extra,
			Box: typeof(this.box)=="undefined"?"":this.box};
		params = $H(params).toQueryString();

		var _ajax = new Ajax.Request('/ib/site/generators/rToolTip.php', { method: 'get', asynchronous: false, parameters: params});
		var responseText_hex = fnTrim( _ajax.transport.responseText);
		var responseText = ""
/*
		for(i = 0; i < responseText_hex.length; i=i+2){
			var responseTextsubstring = responseText_hex.substring(i, i+2)
			if(eval("0x" + responseTextsubstring) >= 0x80 && eval("0x" + responseTextsubstring) < 0xa0)
				responseText += "&#" + eval("0x" + responseTextsubstring) + ";"	
			else
				responseText += String.fromCharCode("0x" + responseTextsubstring);
		}
*/		
		this.html =  responseText_hex; //_ajax.transport.responseText;
		//alert(this.html);
	}
	else if(this.url) {
		var _ajax = new Ajax.Request(this.url, { method: 'get', asynchronous: false});
		this.html = _ajax.transport.responseText;
	}
}
rToolTipClass.prototype.fnPrepareDisplay2 = function(e) {
	var div_content = document.getElementById("roccasTooltip_Content");
	var div_content2 = document.getElementById("roccasTooltip_Content2");
	div_content2.innerHTML = this.html;
	div_content.innerHTML = this.html;
}
rToolTipClass.prototype.fnDisplay = function() {
	var div = document.getElementById("roccasTooltip");

	var obj = this.element;

	div.tooltip = this;
	div.onmouseover = fnToolTip_CancelHide;
	div.onmouseout = fnToolTip_TriggerHide;
	obj.onmouseover = fnToolTip_CancelHide;
	obj.onmouseout = fnToolTip_TriggerHide;
	obj.onkeyup = fnToolTip_Hide;
	obj.onmouseup = fnToolTip_Hide;

	div.style.visibility = "hidden";

	var div_content = document.getElementById("roccasTooltip_Content");
	var div_foreground = document.getElementById("roccasTooltip_Foreground");
	var div_content2 = document.getElementById("roccasTooltip_Content2");
	var div_foreground2 = document.getElementById("roccasTooltip_Foreground2");
	
	div_foreground.style.display = "block";

	var pluswidth = 0;
	var pwv = 17;
	// legt Groesse des Darstellungsbereiches fest
	if(parseInt(div_foreground2.offsetHeight) >= this.maxHeight) {
		div_foreground.style.height = this.maxHeight + "px";
		div_foreground.style.minHeight = "";
		pluswidth = pwv;
		div_foreground.style.paddingRight = pluswidth + "px";
	}
	else {
		div_foreground.style.paddingRight = "0px";
		if(parseInt(div_foreground2.offsetHeight) >= this.defaultHeight) {
			div_foreground.style.height = "auto";
			div_foreground.style.minHeight = this.defaultHeight + "px";
		}
		else {
			div_foreground.style.height = this.defaultHeight + "px";
			div_foreground.style.minHeight = "";
		}
	}

	if(parseInt(div_foreground2.offsetWidth) > this.maxWidth) {
		div_foreground.style.width = this.maxWidth + "px"; // abgeschnitten
		div_foreground.style.minWidth = "";
	}
	else if(parseInt(div_foreground2.offsetWidth) > this.defaultWidth) {
		div_foreground.style.width = "auto";
		div_foreground.style.minWidth = this.defaultWidth + "px";
	}
	else {
		div_foreground.style.width = this.defaultWidth + "px";
		div_foreground.style.minWidth = "";
	}

	var pluswidth = 0;
	if(parseInt(div_foreground.offsetHeight) >= this.maxHeight) {
		div_foreground.style.height = this.maxHeight + "px";
		div_foreground.style.minHeight = "";
		pluswidth = pwv;
		div_foreground.style.paddingRight = pluswidth + "px";
	}
	else {
		div_foreground.style.paddingRight = "0px";
		if(parseInt(div_foreground.offsetHeight) >= this.defaultHeight) {
			div_foreground.style.height = "auto";
			div_foreground.style.minHeight = this.defaultHeight + "px";
		}
		else {
			div_foreground.style.height = this.defaultHeight + "px";
			div_foreground.style.minHeight = "";
		}
	}
	
	// positioniert Hintergrund/Schatten
	var innerWidth = Math.max((div.offsetWidth - 28), this.defaultWidth);
	innerWidth = Math.min(innerWidth, this.maxWidth+pluswidth) ;
	var innerHeight = Math.max((div.offsetHeight - 28), this.defaultHeight);
	var left = 0;
	var top = 0;
	var t = document.getElementById("roccasTooltip_Background_LO");
	t.style.left = left + "px";
	t.style.top = top + "px";
	left = left + parseInt(t.style.width);
	var t = document.getElementById("roccasTooltip_Background_O");
	t.style.left = left + "px";
	t.style.top = top + "px";
	t.style.width =  innerWidth + "px";
	left = left + parseInt(t.style.width);
	var t = document.getElementById("roccasTooltip_Background_RO");
	t.style.left = left + "px";
	t.style.top = top + "px";
	left = 0;
	top = top + parseInt(t.style.height);
	var t = document.getElementById("roccasTooltip_Background_L");
	t.style.left = left + "px";
	t.style.top = top + "px";
	t.style.height = innerHeight + "px";
	left = left + parseInt(t.style.width);
	var t = document.getElementById("roccasTooltip_Background_M");
	t.style.left = left + "px";
	t.style.top = top + "px";
	t.style.width = innerWidth + "px";
	t.style.height = innerHeight + "px";
	left = left + parseInt(t.style.width);
	var t = document.getElementById("roccasTooltip_Background_R");
	t.style.left = left + "px";
	t.style.top = top + "px";
	t.style.height = innerHeight + "px";
	left = 0;
	top = top + parseInt(t.style.height);
	var t = document.getElementById("roccasTooltip_Background_LU");
	t.style.left = left + "px";
	t.style.top = top + "px";
	left = left + parseInt(t.style.width);
	var t = document.getElementById("roccasTooltip_Background_U");
	t.style.left = left + "px";
	t.style.top = top + "px";
	t.style.width = innerWidth + "px";
	left = left + parseInt(t.style.width);
	var t = document.getElementById("roccasTooltip_Background_RU");
	t.style.left = left + "px";
	t.style.top = top + "px";
	
	// ermittelt Scrollposition und Fenstergroesse
	var scrollTop, scrollLeft;
	if (typeof window.pageYOffset != 'undefined') {
		scrollTop = window.pageYOffset;
		scrollLeft = window.pageXOffset;
	}
	else if (typeof document.compatMode != 'undefined' &&
		document.compatMode != 'BackCompat') {
		scrollTop = document.documentElement.scrollTop;
		scrollLeft = document.documentElement.scrollLeft;
	}
	else if (typeof document.body != 'undefined') {
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;
	}
	var wh = document.viewport.getHeight();
	var ww = document.viewport.getWidth();
/*
Im Internet Explorer koennen Sie mit document.body.clientHeight die innere Fensterhoehe bestimmen. 
Wenn Sie eine  Dokumenttyp-Deklaration einsetzen, die im Internet Explorer 6 den  standardkonformen Modus
 ausloest, muessen Sie document.documentElement.clientHeight statt document.body.clientHeight verwenden.
 Zu diesem Zeitpunkt muss jedoch das body- bzw. html-Element bereits vorhanden sein, d.h. es ist sinnvoll,
 die Ermittlung dieser Eigenschaften vom Event-Handler onload abhaengig zu machen.
*/
/*
	var offsetLeft = 0;
	var offsetTop = 0;
	while(obj) {
		if (obj.offsetLeft || obj.offsetTop) {
			offsetLeft += obj.offsetLeft;
			offsetTop += obj.offsetTop;
		}
		obj = obj.offsetParent;
	}
	if(h < offsetTop - scrollTop) {
		div.style.top = offsetTop - h - 0;
	}
	else if(h < wh - (offsetTop - scrollTop - this.element.offsetHeight)) {
		div.style.top = offsetTop + this.element.offsetHeight;
	}
	else {
		div.style.top = offsetTop + this.element.offsetHeight;
	}
	if(w < ww - (offsetLeft - scrollLeft)) {
		div.style.left = offsetLeft;
	}
	else if(w < offsetLeft - scrollLeft) {
		div.style.left = offsetLeft - w;
	}
	else {
		div.style.left = offsetLeft - w;
	}
*/
	// positioniert Tooltipp
	$(div);
	var h = div.getHeight();
	var w = div.getWidth();
	if(h < wh - (this.mouseY - scrollTop + this.displayOffsetY)) { // unterhalb
		div.style.top = this.mouseY + this.displayOffsetY + "px";
	}
	else if(h < this.mouseY - scrollTop - this.displayOffsetY) { // oberhalb
		div.style.top = this.mouseY - h - this.displayOffsetY + "px";
	}
	else { // unterhalb (abgeschnitten)
		div.style.top = this.mouseY + this.displayOffsetY + "px";
	}
	
	if(w < ww-20 - (this.mouseX - scrollLeft + this.displayOffsetX)) { // rechts
		div.style.left = this.mouseX +this.displayOffsetX + "px";
	}
	else if(w < ww-20) { // rechts, am rechten Seitenrand
		div.style.left = ww-20 + scrollLeft - w + "px";
	}
	else { // am linken Seitenrand (abgeschnitten)
		div.style.left = scrollLeft + "px";
	}
	div.style.visibility = "visible";
/*
	var iframe = document.getElementById("roccasTooltip2");
	var ndiv = iframe.contentWindow.document.createElement('div');
	ndiv.innerHTML = div.parentNode.innerHTML;
	iframe.contentWindow.document.body.appendChild(ndiv);
*/
}
rToolTipClass.prototype.fnHide = function() {
	if(rTimer2.lock)
		return;
	var div = document.getElementById("roccasTooltip");
	var div_foreground = document.getElementById("roccasTooltip_Foreground");
	div.style.visibility = "hidden";
	div_foreground.style.height = "auto";
	div_foreground.style.width = "auto";
	div_foreground.style.display = "none";
	div.tooltip = null;
	this.element.onmouseover = fnToolTip_TriggerDisplay;
}
rToolTipClass.prototype.fnCancelDisplay = function() {
	rTimer2.fnCancel(this.displayTimer1);
	rTimer2.fnCancel(this.displayTimer2);
}



function fnToolTip_TriggerDisplay(e) {
	e = e ? e : window.event;
	this.tooltip.displayTimer1 = rTimer2.fnAdd(this.tooltip, "fnDisplay", this.tooltip.delayDisplay);
	this.tooltip.displayTimer2 = rTimer2.fnAdd(this.tooltip, "fnPrepareDisplay2", this.tooltip.delayPrepareDisplay);
	this.tooltip.fnPrepareDisplay1(e);
}
function fnToolTip_CancelDisplay() {
	this.tooltip.fnCancelDisplay();
}
function fnToolTip_TriggerHide() {
	this.tooltip.hideTimer = rTimer2.fnAdd(this.tooltip, "fnHide", this.tooltip.delayHide);
}
function fnToolTip_CancelHide() {
	try {
		if(this.tooltip.hideTimer)
			rTimer2.fnCancel(this.tooltip.hideTimer);
	}
	catch(e) {
	}
}
function fnToolTip_Hide() {
	//console.log("hide", this);
	this.tooltip.fnHide();
}


function fnRemoveTitle(pmObj) {
	if(pmObj.title) {
		pmObj.title = "";
	}
	if(pmObj.alt) {
		pmObj.alt = "";
	}
	var children = pmObj.childNodes;
	if(children) {
		for(var i=0; i < children.length; i++) {
			fnRemoveTitle(children[i]);
		}
	}
}


function rTimer2Class() {
	this.timers = new Array();
	this.id = 0;
	this.lock = false;
}
rTimer2Class.prototype.fnAdd = function(pmObject, pmFunction, pmTimeout) {
	var id = this.id++;
	
	this.timers[id] = new Array();
	this.timers[id]["class"] = pmObject;
	
	this.timers[id]["function"] = pmFunction;
	this.timers[id]["timeoutId"] = window.setTimeout("fnTimer2('" + id + "')", pmTimeout);
	return id;
}
rTimer2Class.prototype.fnCancel = function(pmId) {
	if(navigator.family == "gecko") {
		//return;// firefox
	}
	window.clearTimeout(this.timers[pmId]["timeoutId"]);
}
rTimer2Class.prototype.fnExec = function(pmId) {
	var obj = this.timers[pmId]["class"];
	eval("obj." + this.timers[pmId]["function"] + "()");
}

function fnTimer2(pmId) {
	if(typeof(rTimer2) == 'undefined') {
		return;
	}
	rTimer2.fnExec(pmId);
}

function fnTrim(s) {
  s = s.replace(/[\s]*(.*?)[\s]*$/g, '$1');
  return s;
}

