/* Hotspot */

function Hotspot(target) {
	this.target = target;
	this.source = null;
	this.textElm = null;
	this.starElm = null;
	this.offsetX = 0;
	this.offsetY = 0;
	this.x = 0;
	this.y = 0;
	this.text = 'Text';
	this.textColor = '#000000';
	this.textColorHover = '#000000';
	this.starColor = '#000000';
	this.starColorHover = '#000000';
	this.icon = '';
	this.iconHover = '';
	this.link = '';
	this.description = '';
	this.descriptionElm = null;

	this.setOffset();
}

Hotspot.prototype.setXY = function(x, y) {
	this.x = x;
	this.y = y;
}

Hotspot.prototype.setText = function(text, color, hcolor) {
	if(text) this.text = text;
	if(color) this.textColor = color;
	if(hcolor) this.textColorHover = hcolor;
}

Hotspot.prototype.setStar = function(color, hcolor) {
	if(color) this.starColor = color;
	if(hcolor) this.starColorHover = hcolor;
}

Hotspot.prototype.setIcon = function(icon, hicon) {
	if(icon) this.icon = icon;
	if(hicon) this.iconHover = hicon;
}

Hotspot.prototype.setLink = function(link) {
	if(link) this.link = link;
}

Hotspot.prototype.setDescription = function(description) {
	if(description) this.description = description;
}

Hotspot.prototype.addOnClick = function() {
	var h = this;

	this.source.onclick = function(event) {
		event = enhanceEvent(event);
		event.stopBubble();

		if(h.link != '')
			window.open(h.link);
	}
}

Hotspot.prototype.create = function() {
	var b = document.getElementsByTagName('body')[0];

	var h = this;

	var textColor = this.textColor;
	var textColorHover = this.textColorHover;
	var starColor = this.starColor;
	var starColorHover = this.starColorHover;
	var icon = this.icon;
	var iconHover = this.iconHover;

	var div = document.createElement('div');
	div.style.zIndex = '50';
	div.style.position = 'absolute';
	div.style.textAlign = 'center';
	div.style.cursor = 'default';
	div.style.left = this.offsetX + this.x + 'px';
	div.style.top = this.offsetY + this.y + 'px';
	div.style.display = 'none';

	var s = null;

	s = document.createElement('div');
	s.style.color = this.textColor;
	s.style.whiteSpace = 'nowrap';
	s.style.padding = '0 0 9px 0';
	s.appendChild(document.createTextNode(this.text));

	this.textElm = s;

	div.appendChild(s);

	if(this.icon != '') {
		s = document.createElement('img');
		s.src = this.icon;
		s.style.marginTop = '-5px';
	}
	else {
		s = document.createElement('div');
		s.style.color = this.starColor;
		s.style.fontFamily = 'Times New Roman';
		s.style.whiteSpace = 'nowrap';
		s.style.fontSize = '4em';
		s.style.lineHeight = '0.4em';
		s.appendChild(document.createTextNode('*'));
	}

	this.starElm = s;

	div.appendChild(s);

	var r = 0;

	div.onmouseover = function(event) {
		event = enhanceEvent(event, h.target);

		h.textElm.style.color = textColorHover;
		if(h.icon == '') {
			h.starElm.style.color = starColorHover;
			h.starElm.style.fontSize = '5em';
		}
		else {
			if(iconHover != '')
				h.starElm.src = iconHover;
		}

		// Extern: Stoppt das scrollen des Bildes
		r = richtung;
		richtung = 0;

		if(h.descriptionElm != null) {
			var pos = event.getAbsoluteXY();
			var rel = event.getRelativeXY();

			var offset = { x: 0, y: 0 };
			if((rel.y + h.descriptionElm.offsetHeight) > (h.target.offsetHeight - 10)) {
				offset.y = -(h.descriptionElm.offsetHeight + 10);
			}

			h.descriptionElm.style.left = (pos.x + offset.x + 10) + 'px';
			h.descriptionElm.style.top = (pos.y + offset.y + 10) + 'px';

			document.onmousemove = function(event) {
				event = enhanceEvent(event);
				event.stopBubble();

				var pos = event.getAbsoluteXY();

				h.descriptionElm.style.left = (pos.x + offset.x + 10) + 'px';
				h.descriptionElm.style.top = (pos.y + offset.y + 10) + 'px';
			}
		}
	}
	div.onmouseout = function() {
		h.textElm.style.color = textColor;
		if(h.icon == '') {
			h.starElm.style.color = starColor;
			h.starElm.style.fontSize = '4em';
		}
		else
			h.starElm.src = icon;

		document.onmousemove = '';

		if(h.descriptionElm != null) {
			h.descriptionElm.style.left = '-1000px';
			h.descriptionElm.style.top = '0px';
		}

		// Extern: Stoppt das scrollen des Bildes
		richtung = r;
	}

	this.source = div;

	if(this.link != '')
		this.source.style.cursor = 'pointer';

	if(this.description != '') {
		div = document.createElement('div');
		div.style.position = 'absolute';
		div.style.left = '-1000px';
		div.style.top = '0px'
		div.style.border = '1px solid black';
		div.style.backgroundColor = 'white';
		div.style.padding = '3px';
		div.style.zIndex = '100';
		div.style.whiteSpace = 'nowrap';
		div.innerHTML = this.description.replace(/(\r\n|\r|\n)/, '<br>');

		this.descriptionElm = div;

		b.appendChild(this.descriptionElm);
	}

	this.addOnClick();

	b.appendChild(this.source);
}

Hotspot.prototype.destroy = function() {
	var b = document.getElementsByTagName('body')[0];

	b.removeChild(this.source);

	this.source = null;
}

Hotspot.prototype.display = function() {
	if(!this.source)
		this.create();

	this.source.style.display = 'inline';
}

Hotspot.prototype.hide = function() {
	if(!this.source)
		return;

	this.source.style.display = 'none';
}

Hotspot.prototype.setOffset = function() {
	if(!this.target)
		return;

	var p = this.getOffset(this.target);

	this.offsetX = p.x;
	this.offsetY = p.y;
}

Hotspot.prototype.getOffset = function(node) {
	var p = { x: 0, y: 0 };
	if(!node)
		return p;

	var n = node;
	p.x = n.offsetLeft;
	p.y = n.offsetTop;

	while((n = n.offsetParent) != null) {
		p.x += n.offsetLeft;
		p.y += n.offsetTop;
	}

	return p;
}
