var mousex, mousey;
var visibleMenue = null, menue_x, menue_y, menue_w, menue_h;
var Fenster;

document.onmousemove = handleMouseMove;

function handleMouseMove(Ereignis) {
	rememberMousePosition(Ereignis);
	reactOnMousePosition();
}

function reactOnMousePosition() {
	var tollerance = 20;
	if (visibleMenue == null || visibleMenue.style.display == 'none') return;

	if (navigator.appName == "Microsoft Internet Explorer") {
		var scrolltop = document.body.scrollTop;
		if (!((mousex + tollerance > menue_x && mousex - tollerance < menue_x + menue_w) && (mousey + scrolltop + tollerance> menue_y && mousey - tollerance < menue_y + menue_h))) {
			toggleVisibility(visibleMenue.id, false);
		}
	} else {
		if (!((mousex + tollerance > menue_x && mousex - tollerance < menue_x + menue_w) && (mousey + tollerance> menue_y && mousey - tollerance < menue_y + menue_h))) {
			toggleVisibility(visibleMenue.id, false);
		}
	}
}

function rememberMousePosition(Ereignis) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		mousex = window.event.clientX;
		mousey = window.event.clientY;
	} else {
		mousex = Ereignis.pageX;
		mousey = Ereignis.pageY;
	}
}

function open_dialog_window(command) {
	open_window(command, 500, 300, 'dialog_window');
}

function open_smalldialog_window(command) {
	open_window(command, 400, 250, 'dialog_window');
}


function open_application_window(command, relpath) {
	open_window(command, 700, 500, 'implaceapplication_window', relpath);
}

function open_width_window(command, relpath) {
	open_window(command, 700, 250, 'implaceapplication_window', relpath);
}



function open_window(command, width, height, name, relpath) {
	if (relpath == undefined) relpath = '';

	if (Fenster) Fenster.close();
	Fenster = window.open(relpath + 'littleadmin/interfaces/exec.php?' + unescape(command), name, 'width='+width+',height='+height+',left='+(screen.width/2-width/2)+',top='+(screen.height/2-height/2)+',location=no,dependent=no,toolbar=no,status=no');
}

function reload_site(get) {
	document.location.replace(document.URL.substring(0, document.URL.lastIndexOf('?')) + '?' + unescape(get));
}

function exec_cms_commands(get) {
	document.cookie = getScrollPos();
	document.location.replace('./littleadmin/interfaces/exec.php?' + unescape(get));
}

function hover(Element, vis) {
	if (vis) {
		Element.style.backgroundColor = '#000080';
		Element.style.color = '#FFFFFF';
	} else {
		Element.style.backgroundColor = '#F0F0F0';
		Element.style.color = '#000000';
	}
}

function toggleVisibility(id, vis) {
	Element = document.getElementById(id);

	if (vis) {
		if (visibleMenue != null) return;
		moveToMousePos(Element);
		Element.style.display = 'block';
		visibleMenue = Element;
	} else {
		Element.style.display = 'none';
		visibleMenue = null;
	}

}

function toggleElementVisibility(id, icon) {
	Element = document.getElementById(id);

	var vis = Element.style.display != "block";

	if (vis) {
		Element.style.display = 'block';
		icon.src = "./littleadmin/interfaces/templates/images/inplace/icon_minus.gif";
	} else {
		Element.style.display = 'none';
		icon.src = "./littleadmin/interfaces/templates/images/inplace/icon_plus.gif";
	}
}

function getScrollPos() {
	var innerWidth = document.body.offsetWidth;
	var innerHeight = document.body.offsetHeight;
	var scrolltop = document.body.scrollTop;
	var scrollleft = document.body.scrollLeft;
	return "scrolltop=" + scrolltop + ";scrollleft=" + scrollleft;
}

function moveToMousePos(Element) {
	var offset = 10;

	// Breite und Höhe des Fenster
	// ermitteln.

	if (navigator.appName == "Microsoft Internet Explorer") {
		var innerWidth = document.body.offsetWidth - 2*offset;
		var innerHeight = document.body.offsetHeight - 2*offset;
		var scrolltop = document.body.scrollTop;
		var scrollOffset = scrolltop;
		var scrollleft = document.body.scrollLeft;
	} else {
		var isIE = false;
		var innerWidth = window.innerWidth;
		var innerHeight = window.innerHeight;
		var scrolltop = window.pageYOffset;
		var scrollleft = window.pageXOffset;
		var scrollOffset = 0;
	}

	var width 	= parseInt(Element.style.width);
	var height 	= parseInt(Element.style.height);

	var menue_right = mousex + width;
	var menue_bott 	= mousey + height;

	// X-Werte setzen.
	if ((menue_right - scrollleft) > innerWidth) {
		Element.style.left 	= mousex - ((menue_right - scrollleft) - innerWidth) - offset;

	} else {
		Element.style.left 	= mousex - offset;

	}

	//alert(menue_bott - scrolltop + " > " + innerHeight);

	// Y-Werte setzen.
	if ((menue_bott - scrolltop) > innerHeight) {
		Element.style.top 	= mousey - ((menue_bott - scrolltop)- innerHeight) - offset + scrollOffset;

	} else {
		Element.style.top 	= mousey - offset + scrollOffset;
		//alert(menue_bott - scrolltop);
	}

	// Menü global melden.
	menue_x			= parseInt(Element.style.left);
	menue_y			= parseInt(Element.style.top);
	menue_w			= width;
	menue_h			= height;

}