/***************************


Java-Script für Bilder-Overlays

Wenn Man mit der Maus über ein Bild fährt,
dann wird der Alt tag des Bildes angezeigt.


****************************/


/*  weitere Style-Angaben möglich, oder einfach eine Klasse bestimmen  */
var temptext = "";

/* wird aufgerufen, wenn man über das Bild fährt */
function show_alt(img)
{
	if(img.alt != "")
	{
		temptext = img.alt;
		img.alt = "";
		img.title = "";
		tooltip_show( temptext, "", 1 );
	}
}


/* wird aufgerufen, wenn man vom Bild herunterfährt */
function hide_alt(img)
{
	img.alt = temptext;
	temptext = "";
	tooltip_hide();
}

var tooltip_x = 0;
var tooltip_y = 0;
var tooltipDialog;
var tooltip_is_hide = 0;
var tooltip_align = 0;


function tooltip_show(title,text,align)
{
	tooltip_align = align;
	tooltip_is_hide = false;
	tooltipDialog.setContent(text);
	tooltipDialog.setTitle(title);
	tooltip_movetip();
	tooltipDialog.openDialog();
}


function tooltip_movetip()
{
	var x = tooltip_x;
	var y = tooltip_y;
	y = y + 14;
	switch(tooltip_align)
	{
		case 2: // Center
			x = x - (tooltipDialog.element.offsetWidth/2) + 1;
			break;
		case 0: // Right
			x = x - tooltipDialog.element.offsetWidth - 6;
			break;
		case 1: // Left
		default:
			break;
	}
	tooltipDialog.moveTo(x,y);
}

function tooltip_movetooltip(e)
{
	var cx = 0;
	var cy = 0;
	
	if(document.all) {
		cx = event.x + document.body.scrollLeft;
		cy = event.y + document.body.scrollTop;
		
		//alert('document.all');
	}
	else {
		cx = e.clientX + window.pageXOffset;
		cy = e.clientY + window.pageYOffset;
		
		//alert('else');
	}	
	
	tooltip_x = cx;
	tooltip_y = cy;
	if( tooltipDialog.isOpen() )  
		tooltip_movetip();
}


function tooltip_install_global_events()
{
	tooltipDialog = getDialog("tooltip")
	tooltipDialog.setCloseVisible(false);



	var oldMouseMove = document.onmousemove;
	document.onmousemove = function(e) {
		tooltip_movetooltip(e);
		if( oldMouseMove )
			oldMouseMove(e);
	};
}


function tooltip_hide()
{
	tooltip_is_hide = true;
	//window.setTimeout(function() { if( tooltip_is_hide ) tooltipDialog.closeDialog();  }, 1);
	tooltipDialog.closeDialog();
}


//window.setTimeout(function() { tooltip_install_global_events(); }, 100);
var dialog_control = null;
var dialog_zIndex = 1;

function dialog_install_global_events()
{
	var oldMouseUp = document.onmouseup;
	var oldMouseMove = document.onmousemove;
	var oldSelect = document.onselect;
	var oldSelectStart = document.onselectstart;
	document.onmouseup = function(e) { 
		if( dialog_control ) {
			dialog_control.stopMove(e);
			return;
		}

		if( oldMouseUp )
			eval(oldMouseUp);
	};
	document.onmousemove = function(e) {
		if( dialog_control ) {
			dialog_control.moveWindow(e);
			return;
		}

		if( oldMouseMove )
			oldMouseMove(e);
	};
	document.onselect = function(e) {
		if( dialog_control ) {
			dialog_cancel_event(e);
			return;
		}

		if( oldSelect )
			eval(oldSelect);
	};
	document.onselectstart = function(e) {
		if( dialog_control ) {
			dialog_cancel_event(e);
			return;
		}

		if( oldSelectStart )
			eval(oldSelectStart);
	};
}

function dialog_cancel_event(e)
{
	if( !e )
		e = window.event;
	e.cancel = true;
	e.returnValue = false;
	if( e.stopPropagation)
		e.stopPropagation();
	else
		e.cancelBubble = true;
	return false;
}

function dialog_init(id)
{
	this.id = id;
	this.element = document.getElementById(id);
	this.element.obj = this;
	var titleBar = document.getElementById(id + "_title");
	this.titleBar = titleBar;
	this.titleBar_text = document.getElementById(id + "_tt");
	this.closeX = document.getElementById(id + "_x");
	// Events die man selber einhängen kann
	this.closeHandler = function() {}; // Macht nix
	// Offset zum verschieben vom Top beim Center
	this.topAddOffset = 200;

	// Variable für die Funktionen zur Verfügung stellen
	var element = this.element;
	var thisControl = this;

	this.getOffsetTop = function (obj)
	{
		var o = obj.offsetParent;
		var t = obj.offsetTop;
		while( o ) {
			t += o.offsetTop;
			o = o.offsetParent;
		}
		return t;
	};

	this.setCloseVisible = function(visible)
	{
		if( visible ) {
			thisControl.closeX.style.visibility = "visible";
			thisControl.closeX.style.display = "inline";
		}
		else {
			thisControl.closeX.style.visibility = "hidden";
			thisControl.closeX.style.display = "none";
		}
	}

	this.getOffsetLeft = function(obj)
	{
		var o = obj.offsetParent;
		var t = obj.offsetLeft;
		while( o ) {
			t += o.offsetLeft;
			o = o.offsetParent;
		}
		return t;
	};

	this.startMove = function (e)
	{
		if( !e )
			e = window.event;

		dialog_control = thisControl;
		thisControl.x = e.screenX;
		thisControl.y = e.screenY;
		thisControl.bringIntoFront();
		titleBar.style.cursor = "move";
		if( document.body.style.mozUserSelect)
			document.body.style.mozUserSelect = "none";
		if( document.body.style.userSelect )
			document.body.style.userSelect = "none";
		dialog_cancel_event(e);
	};

	this.moveWindow = function(e)
	{
		if( !e )
			e = window.event;
		if( typeof(dialog_control) == undefined || !dialog_control )
			return;
		if( dialog_control != thisControl ) {
			// Events forwarden
			dialog_control.moveWindow(e);
			return;
		}
		var xdelta = e.screenX - thisControl.x;
		var ydelta = e.screenY - thisControl.y;
		thisControl.x = e.screenX;
		thisControl.y = e.screenY;
		element.style.left = element.offsetLeft + xdelta;
		element.style.top = element.offsetTop + ydelta;
		dialog_cancel_event(e);
	};

	this.stopMove = function(e)
	{
		if( !e )
			e = window.event;
		if( dialog_control != thisControl ) {
			// Events forwarden
			dialog_control.moveWindow(e);
			return;
		}
		titleBar.style.cursor = "auto";
	 	dialog_control = null;
		if( document.body.style.mozUserSelect)
			document.body.style.mozUserSelect = "normal";
		if( document.body.style.userSelect )
			document.body.style.userSelect = "normal";
	};

	this.toggleVisibility = function()
	{
		if(element.style.visibility == "visible")
			thisControl.closeDialog();
		else {
			thisControl.openDialog();
		}
	};

	this.isOpen = function()
	{
		return element.style.visibility == "visible";
	};

	this.onOpen = function() { return 0; };

	this.openDialog = function () 
	{
		element.style.visibility = "visible";
		thisControl.onOpen();
		thisControl.bringIntoFront();
	};

	this.bringIntoFront = function() {
		element.style.zIndex = ++dialog_zIndex;
	}

	this.closeDialog = function()
	{
		thisControl.closeHandler();
		element.style.visibility = "hidden";
	};

	this.setTitle = function(title)
	{
		thisControl.titleBar_text.innerHTML = title;
	};

	this.setContent = function(content_)
	{
		thisControl.getContentCtrl().innerHTML = content_;
	};

	this.getContentCtrl = function()
	{
		return document.getElementById(thisControl.id + "_c");
	};

	this.centerDialog = function()
	{
		var maincontent = document.getElementById("maincontent");
		//element.style.top = this.getOffsetTop(maincontent) + this.topAddOffset; 
		var myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myHeight = document.body.clientHeight;
		}

		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
		}

		var topOff = (myHeight - element.offsetHeight) / 2;
		if( topOff < 0 )
			topOff = 0;
		topOff = topOff + scrOfY;

		element.style.top = topOff;
		element.style.left = this.getOffsetLeft(maincontent) + ((maincontent.offsetWidth - element.offsetWidth)/2);
	}
	this.openAndCenter = function() 
	{
		thisControl.centerDialog();
		thisControl.openDialog();
		thisControl.centerDialog();
	}
	this.moveTo = function(x,y)
	{
		//alert( y );
		element.style.top = y;
		element.style.left = x;
	}
	this.moveRel = function(xdelta,ydelta)
	{
		element.style.top = element.offsetY + ydelta;
		element.style.left = element.offsetX + xdelta;
	}

	titleBar.onmousedown = function(event) { thisControl.startMove(event); };
	titleBar.onmouseup = function(event) { thisControl.stopMove(event); };
	titleBar.onmousemove = function(event) { thisControl.moveWindow(event); };
	titleBar.onselect = dialog_cancel_event 
	titleBar.ondrag = dialog_cancel_event;
	titleBar.onselectstart = dialog_cancel_event;
}

function getDialog(id)
{
	return document.getElementById(id).obj;
}
