/**
 * Allgemeine Funktionen für Formulare etc.
 * @author ag
 * @version 2007-07-04
 */
function ly_htmlforms()
{
	var app = this;
	app.functions = new ly_functions();
	app.backgroundColorFocus = '#fffddf';
	app.backgroundColorNormal = '#ffffff';
	app.currentBubbleHelpText = null;
	app.imageDir = '/kredite/baufinanzierung/eigentuemer/include';
	app.lastTitle = null;
	app.lastAlt = null;
	app.offsetX = 5;
	app.offsetY = 20;
	app.interval = null;

	app.setBackgroundColorFocus = function(newColor)
	{
		return(app.backgroundColorFocus = newColor);
	}

	app.setBackgroundColorNormal = function(newColor)
	{
		return(app.backgroundColorNormal = newColor);
	}

	app.getBackgroundColorFocus = function()
	{
		return(app.backgroundColorFocus);
	}

	app.getBackgroundColorNormal = function()
	{
		return(app.backgroundColorNormal);
	}

	app.setEventHandlers = function()
	{
		app.setFocusEventHandlers();
		app.setBubbleHelpEventHandlers();
		app.setTooltipEventHandlers();
		return(true);
	}

	app.setTooltipEventHandlers = function(cName)
	{
		var elems;
		var i;

		if (!cName) cName = 'tooltip';

		elems = document.getElementsByTagName('IMG');
		for (i = 0; i < elems.length; i++)
		{
			if (app.functions.hasClass(cName, elems[i].className))
			{
				app.functions.addEvent(elems[i], 'mouseover', function(ev) { app.showTooltip(ev); } );
				app.functions.addEvent(elems[i], 'mousemove', function(ev) { app.moveTooltip(ev); } );
				app.functions.addEvent(elems[i], 'mouseout', function(ev) { app.closeTooltip(ev); } );
			}
		}
	}

	app.setFocusEventHandlers = function()
	{
		var nodes;
		var i;

		if (document.getElementsByTagName)
		{
			nodes = document.getElementsByTagName('input');
			for (i = 0; i < nodes.length; i++)
			{
				if (app.functions.hasClass('text', nodes[i].className)
					&& !app.functions.hasClass('readonly', nodes[i].className))
				{
					//app.functions.addEvent(nodes[i], 'focus', function() { this.style['backgroundColor'] = String(app.backgroundColorFocus); this.select(); return(true); } );
					//app.functions.addEvent(nodes[i], 'blur', function() { this.style['backgroundColor'] = String(app.backgroundColorNormal); return(true); } );
					app.functions.addEvent(nodes[i], 'focus', function() { this.className = app.functions.addClass(this.className, 'focus'); this.select(); return(true); } );
					app.functions.addEvent(nodes[i], 'blur', function() { this.className = app.functions.removeClass(this.className, 'focus'); return(true); } );
				}
			}

			nodes = document.getElementsByTagName('textarea');
			for (i = 0; i < nodes.length; i++)
			{
				if (app.functions.hasClass('text', nodes[i].className)
					&& !app.functions.hasClass('readonly', nodes[i].className))
				{
					//app.functions.addEvent(nodes[i], 'focus', function() { this.style['backgroundColor'] = String(app.backgroundColorFocus); this.select(); return(true); } );
					//app.functions.addEvent(nodes[i], 'blur', function() { this.style['backgroundColor'] = String(app.backgroundColorNormal); return(true); } );
					app.functions.addEvent(nodes[i], 'focus', function() { this.className = app.functions.addClass(this.className, 'focus'); this.select(); return(true); } );
					app.functions.addEvent(nodes[i], 'blur', function() { this.className = app.functions.removeClass(this.className, 'focus'); return(true); } );
				}
			}
		}
	}

	app.setBubbleHelpEventHandlers = function()
	{
		var nodes;
		var i;

		if (document.getElementsByTagName)
		{
			nodes = document.getElementsByTagName('input');
			for (i = 0; i < nodes.length; i++)
			{
				if (app.functions.hasClass('tooltipleft', nodes[i].className))
				{
					app.functions.addEvent(nodes[i], 'focus', function(ev)
																			{
																				app.showBubbleHelp(ev, 'left');
																				this.style['backgroundColor'] = String(app.backgroundColorFocus);
																			});
					app.functions.addEvent(nodes[i], 'blur', function(ev)
																			{
																				app.hideBubbleHelp(ev);
																				this.style['backgroundColor'] = String(app.backgroundColorNormal);
																			});
				}
				else if (app.functions.hasClass('tooltip', nodes[i].className))
				{
					app.functions.addEvent(nodes[i], 'focus', function(ev)
																			{
																				app.showBubbleHelp(ev);
																				this.style['backgroundColor'] = String(app.backgroundColorFocus);
																			});
					app.functions.addEvent(nodes[i], 'blur', function(ev)
																			{
																				app.hideBubbleHelp(ev);
																				this.style['backgroundColor'] = String(app.backgroundColorNormal);
																			});
				}
			}
		}
	}

	app.showBubbleHelp = function(ev, direction)
	{
		if (!direction) direction = 'right';
		ev = (ev)? ev: ((window.event)? window.event : "");
		var elem = (ev.target)? ev.target : ev.srcElement;

		if (direction == 'right')
		{
			document.getElementById('bubbleHelp').style['top'] = String(app.functions.getOffsetTop(elem) +5) + 'px';
			document.getElementById('bubbleHelp').style['left'] = String(app.functions.getOffsetLeft(elem) +145) + 'px';
			document.getElementById('bubbleHelp').style['backgroundImage'] = 'url("' + app.imageDir + '/background-bubble-right.gif")';
			document.getElementById('helptext').className = 'right';
			document.getElementById('bubbleHelp').style['opacity'] = '0';
			document.getElementById('bubbleHelp').style['filter'] = 'Alpha(opacity=0)';
			document.getElementById('bubbleHelp').style['display'] = 'block';
			app.fadeBubbleHelp(0.1);
		}
		else
		{
			document.getElementById('bubbleHelp').style['top'] = String(app.functions.getOffsetTop(elem) +5) + 'px';
			document.getElementById('bubbleHelp').style['left'] = String(app.functions.getOffsetLeft(elem) -200) + 'px';
			document.getElementById('bubbleHelp').style['backgroundImage'] = 'url("' + app.imageDir + '/background-bubble-left.gif")';
			document.getElementById('helptext').className = 'left';
			document.getElementById('bubbleHelp').style['opacity'] = '0';
			document.getElementById('bubbleHelp').style['filter'] = 'Alpha(opacity=0)';
			document.getElementById('bubbleHelp').style['display'] = 'block';
			app.fadeBubbleHelp(0.1);
		}

		app.currentBubbleHelpText = String(elem.getAttribute('title'));
		document.getElementById('helptext').innerHTML = app.currentBubbleHelpText;
		elem.setAttribute('title', '');
		elem.setAttribute('alt', '');
	}

	app.hideBubbleHelp = function(ev)
	{
		ev = (ev)? ev: ((window.event)? window.event : "");
		var elem = (ev.target)? ev.target : ev.srcElement;

		document.getElementById('bubbleHelp').style['display'] = 'none';
		elem.setAttribute('title', app.currentBubbleHelpText);
		app.currentBubbleHelpText = null;
	}

	app.fadeBubbleHelp = function(currentAlpha)
	{
		var newAlpha;
		currentAlpha = parseFloat(currentAlpha);

		document.getElementById('bubbleHelp').style['opacity'] = String(currentAlpha);
		document.getElementById('bubbleHelp').style['filter'] = 'Alpha(opacity=' + String(currentAlpha *100) + ')';
		newAlpha = currentAlpha +0.1;

		if (currentAlpha < 1) window.setTimeout( function() { app.fadeBubbleHelp(newAlpha); }, 25);
	}

	app.showTooltip = function(ev)
	{
		var tooltip;
		var text;
		var tt;
		ev = (ev)? ev: ((window.event)? window.event : "");
		var elem = (ev.target)? ev.target : ev.srcElement;

		text = elem.getAttribute('title');
		app.lastTitle = text;
		app.lastAlt = elem.getAttribute('alt');

		/*elem.removeAttribute('title');
		elem.removeAttribute('alt');*/

		elem.setAttribute('title', '');
		elem.setAttribute('alt', '');

		tooltip = document.getElementById('lytooltip');
		if (tooltip)
		{
			app.moveTooltip(ev);
			tt = tooltip.contentWindow.document.getElementById('tooltip');
			if (tt) tt.innerHTML = app.lastTitle;
			tooltip.style['display'] = 'block';
			if (tt) tooltip.style['height'] = String(tt.offsetHeight +6) + 'px';
		}

		return(true);
	}

	app.moveTooltip = function(ev)
	{
		var tooltip;
		var x, y;
		ev = (ev)? ev : event;
		tooltip = document.getElementById('lytooltip');

		if (tooltip)
		{
			if (ev.pageX)
			{
				x = ev.pageX;
				y = ev.pageY;
			}
			else
			{
				x = window.event.clientX +document.body.scrollLeft;
				y = window.event.clientY +document.body.scrollTop;
			}

			tooltip.style['left'] = String(x +app.offsetX) + 'px';
			tooltip.style['top'] = String(y +app.offsetY) + 'px';

			width = tooltip.offsetWidth;
			height = tooltip.offsetHeight;
			if (y -document.body.scrollTop +height >= document.body.clientHeight)
			{
				y = y -height -app.offsetY;
				tooltip.style['top'] = String(y) + 'px';
			}
			if (x -document.body.scrollLeft + width >= document.body.clientWidth)
			{
				x = document.body.clientWidth -width +document.body.scrollLeft -2;
				tooltip.style['left'] = String(x) + 'px';
			}
		}
		return(true);
	}

	app.closeTooltip = function(ev)
	{
		var tooltip;
		ev = (ev)? ev: ((window.event)? window.event : "");
		var elem = (ev.target)? ev.target : ev.srcElement;

		tooltip = document.getElementById('lytooltip');
		if (tooltip) tooltip.style['display'] = 'none';

		elem.setAttribute('title', app.lastTitle);
		elem.setAttribute('alt', app.lastAlt);

		app.lastTitle = null;
		app.lastAlt = null;
		return(true);
	}
}

var htmlformsApp;
function initLyHtmlforms()
{
	htmlformsApp = new ly_htmlforms();
	htmlformsApp.setEventHandlers();
}