// VARIABLEN
var mousedistance = [15,15]; // Vertikaler und horizontaler Abstand vom Mauszeiger
var scrollbarSize = [20,20]; // Höhe des horizontalen und Breite des vertikalen Scrollbalkens
var chaserW;
var chaserH;
var chaserID = 'chaser';

if (document.getElementById || document.all)
document.write('<div id="chaser"></div>');

// NEUEN CHASER ANLEGEN -------------------------------------------------------------------------------------------------------------------
function newelement() {

	if (document.createElement) {

		var el = document.createElement('div');
		el.id = chaserID;

		with(el.style) {
			display = 'none';
			position = 'absolute';
			left = '-1000px';
		}

		document.body.appendChild(el);

	}

}

// MITZUBEWEGENDES OBJEKT ZURÜCKGEBEN -----------------------------------------------------------------------------------------------------
function getchaser (withstyle) {
	if (document.getElementById) {
		if (withstyle) {
			return document.getElementById(chaserID).style;
		} else {
			return document.getElementById(chaserID);
		}
	} else if (document.all) {
		if (withstyle) {
			return document.all.chaserID.style;
		} else {
			return document.all.chaserID;
		}
	}
}

// ERMITTELN DES ROOT-ELEMENTS ------------------------------------------------------------------------------------------------------------
function realroot () {
	return (!window.opera && document.compatMode && document.compatMode!='BackCompat')? document.documentElement : document.body
}


// TOOLTIP ANZEIGEN -----------------------------------------------------------------------------------------------------------------------
function showChaser (html, width, cssClass) {

	if (cssClass == 'notepad_add') {
		var inner_html = '<p>Produkt zum Merkzettel/Warenkorb hinzuf&uuml;gen/entfernen.</p>';
	} else {
		inner_html = html;
	}

	if(!getchaser(0)) newelement();

	getchaser(0).innerHTML = inner_html;
	getchaser(1).display = 'block';
	getchaser(1).width = width + 'px';
	getchaser(0).className = cssClass;

	chaserH = getchaser(0).offsetHeight;
	chaserW = getchaser(0).offsetWidth;

	document.onmousemove = chaseMouse;

}

// TOOLTIP VERSTECKEN ---------------------------------------------------------------------------------------------------------------------
function hideChaser () {
	getchaser(0).innerHTML = ' ';
	getchaser(1).display = 'none';
	document.onmousemove = '';
	getchaser(1).left = '-1000px';
}

// MAUS-VERFOLGER -------------------------------------------------------------------------------------------------------------------------
function chaseMouse (e) {

	var xcoord = mousedistance[0];
	var ycoord = mousedistance[1];

	var documentW = document.all? realroot().scrollLeft+realroot().clientWidth : pageXOffset + window.innerWidth - scrollbarSize[1];
	var documentH = document.all? Math.min(realroot().scrollHeight, realroot().clientHeight) : Math.min(window.innerHeight - scrollbarSize[0]);

	if (typeof e != 'undefined'){

		if (documentW - e.pageX < chaserW + mousedistance[0]){
			xcoord = e.pageX - xcoord - chaserW; // Auf die linke Seite des Mauszeigers wechseln
		} else {
			xcoord += e.pageX;
		}

		if (documentH - e.pageY < (chaserH + mousedistance[1])){
			ycoord += e.pageY - Math.max(0,(mousedistance[1] + chaserH + e.pageY - documentH - realroot().scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != 'undefined'){

		if (documentW - event.clientX < chaserW + mousedistance[0]){
			xcoord = event.clientX + realroot().scrollLeft - xcoord - chaserW; // Auf die linke Seite des Mauszeigers wechseln
		} else {
			xcoord += realroot().scrollLeft + event.clientX;
		}

		if (documentH - event.clientY < (chaserH + mousedistance[1])){
			ycoord += event.clientY + realroot().scrollTop - Math.max(0,(mousedistance[1] + chaserH + event.clientY - documentH));
		} else {
			ycoord += realroot().scrollTop + event.clientY;
		}

	}

	var documentW = document.all? realroot().scrollLeft + realroot().clientWidth : pageXOffset + window.innerWidth - scrollbarSize[0];
	var documentH = document.all? Math.max(realroot().scrollHeight, realroot().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight - scrollbarSize[1]);

	if(ycoord < 0) { ycoord = ycoord*-1; }

	getchaser(1).left = xcoord + 'px'
	getchaser(1).top = ycoord + 'px';

}