var id_image_map;
var id_tooltip_area;
var sccwebmap_map_element;
var sccwebmap_image_map;
var sccwebmap_tooltip_area;
var sccwebmap_tooltip_clicked = false;
var sccwebmap_map_element_cursor;
var sccwebmap_map_element_cursor_x;
var sccwebmap_map_element_cursor_y;


function sccwebmapSetNodeVisible(id, visible)
{
	var element = document.getElementById(id);
	if (element)
	{
		if(visible)
			element.style.visibility = 'visible';
		else element.style.visibility = 'hidden';
	}
}


function sccwebmapSetNodeDisplay(id, visible, event, anchor_bottom)
{
	var x = 0;
	var y = 0;	
	if (event)
	{		
		if (event.layerX)
		{
			x = event.layerX;
			y = event.layerY;
		}
		else {
			x = event.x;
			y = event.y;
		}
	}	
	
	var element = document.getElementById(id);
	if (element)
	{
		if(visible)
		{
			if (event)
			{
				element.style.left = '' + (x + 5) + 'px';
				if (anchor_bottom)
					element.style.bottom = '' + (y + 10) + 'px';
				else element.style.top = '' + (y + 10) + 'px';
			}			
			element.style.display = '';
		}
		else element.style.display = 'none';
	}
}



function sccwebmapGetImagemapAreas(image_map, x, y)
{
	var tmp;
	var ret = new Array();
	for (var i=0; i < image_map.areas.length; ++i)
	{
		tmp = image_map.areas[i].coords.split(',');
		if (image_map.areas[i].shape.toLowerCase() == 'circle')
		{
			// Punkt-in-Umkreis-Test
			if (tmp.length >= 3)
			{
				var dx, dy, r;
				dx = tmp[0] - x;
				dy = tmp[1] - y;
				dx = dx * dx;
				dy = dy * dy;
				r = tmp[2] * tmp[2];
				if ((dx + dy) <= r)
				{
					ret.push(image_map.areas[i]);
				}
			}
		}
		else if (image_map.areas[i].shape.toLowerCase() == 'rect')
		{
			// Punkt-in-Rechteck-Test
			if (tmp.length >= 4  &&  tmp[0] <= x  &&  x <= tmp[2]  &&  tmp[1] <= y  &&  y <= tmp[3])
			{
				ret.push(image_map.areas[i]);
			}
		}
		else if (image_map.areas[i].shape.toLowerCase() == 'poly')
		{
			// Punkt-in-Polygon-Test
			var x0, y0, x1, y1, det, edge_count = 0, horiz = 0;
			for (var j=0; (j+1) < tmp.length; j+=2)
			{
				// zwei Punkte der Geraden bestimmen
				x0 = parseFloat(tmp[j]);
				y0 = parseFloat(tmp[j+1]);
				if ((j+3) < tmp.length)
				{
					x1 = parseFloat(tmp[j+2]);
					y1 = parseFloat(tmp[j+3]);
				}
				else {
					x1 = parseFloat(tmp[0]);
					y1 = parseFloat(tmp[1]);
				}
				
				// überspringen, wenn Kante auf horizontaler Linie vom übergebenen Punkt liegt
				if (y0 == y  &&  y1 == y)
					continue;
				
				// wenn Kante horizontaler Linie vom übergebenen Punkt berühert
				// -> erweiterte Tests zum Überspringen
				if (y0 == y  ||  y1 == y)
				{
					// überspringen, wenn Kante in dieselbe Richtung wie vorherige zeigt 
					if (y0 > y1  &&  horiz > 0)
						continue;
					if (y0 < y1  &&  horiz < 0)
						continue;
					
					// Richtung der Kante merken
					if (y0 > y1)
						horiz = 1;
					else horiz = -1;
				}
				else horiz = 0;
				
				// immer so drehen, dass Kanten von unten nach oben gehen
				if (y0 > y1)
				{
					det = x0;
					x0 = x1;
					x1 = det;
					det = y0;
					y0 = y1;
					y1 = det;
				}
				
				// Bereich abschätzen, wo Berechnung Sinn macht
				if (y0 <= y  &&  y <= y1)
				{
					det = x * (y0 - y1) + y * (x1 - x0) + x0 * y1 - y0 * x1;
					if (det >= 0)
						++edge_count;
				}
			}
			
			if ((edge_count % 2) == 1)
			{
				ret.push(image_map.areas[i]);
			}
		}
	}
	
	return ret;
}

function sccwebmapShowTooltip(event, fixed)
{
	var ret = false;
	if ( sccwebmap_tooltip_clicked != true  &&  sccwebmap_map_element
			&&  sccwebmap_image_map  &&  sccwebmap_tooltip_area )
	{
		var visible = false;
		var tooltip_index = 2;
		var x,y, areas;
		if (event.layerX)
		{
			x = event.layerX;
			y = event.layerY;
		}
		else {
			x = event.x;
			y = event.y;
		}
		areas = sccwebmapGetImagemapAreas(sccwebmap_image_map, x, y);
		
		// Link nur bei eindeutigem Tooltip anzeigen
		if (areas.length == 1  &&  areas[0].href != '')
		{
			window.status = areas[0].href;
			sccwebmap_map_element.style.cursor = 'pointer';
			
			ret = true;
		}
		else {
			window.status = '';
			if (sccwebmap_map_element.nodeName.toLowerCase() == 'input')
			{
				sccwebmap_map_element.style.cursor = 'crosshair';
			}
		}
		
		// Tooltip-Texte anzeigen
		if (areas.length > 0)
		{
			var tmp, tmp2;
			for (var i = areas.length - 1; i >= 0; --i)
			{
				if (areas[i].title != '')
				{
					if (tooltip_index < sccwebmap_tooltip_area.childNodes.length)
					{
						tmp = sccwebmap_tooltip_area.childNodes[tooltip_index];
						tmp.removeChild(tmp.firstChild);
					}
					else {
						tmp = document.createElement('div');
						sccwebmap_tooltip_area.appendChild(tmp);
					}
					
					tmp2 = tmp;
					if (areas[i].href != ''  ||  areas[i].onclick != '')
					{
						if (areas[i].href != '')
						{
							tmp2 = document.createElement('a');
							tmp2.href = areas[i].href;
							
							if (areas.length > 2  &&  fixed)
							{
								sccwebmap_tooltip_clicked = true;
							}
						}
						else {
							tmp2 = document.createElement('span');
						}
						if (areas[i].target != '')
						{
							tmp2.target = areas[i].target;
						}
						if ((typeof areas[i].onclick) == 'function')
						{
							eval('tmp2.onclick = function() '
									+ '{ '
									+ '  areas = sccwebmapGetImagemapAreas(sccwebmap_image_map, ' + x + ', ' + y + '); '
									+ '  sccwebmapLeaveTooltip(event);'
									+ '  sccwebmapHideTooltip(event);'
									+ '  return areas[' + i + '].onclick(event);'
									+ '}');
						}
						else {
							tmp2.onclick = function() {
								sccwebmapLeaveTooltip(event);
								sccwebmapHideTooltip(event);
							}
						}
						tmp.appendChild(tmp2);
					}
					tmp2.appendChild(document.createTextNode(areas[i].title));
					
					tmp.style.display = '';
					tmp.style.paddingBottom = sccwebmap_tooltip_area.style.paddingTop;
					tmp.style.marginBottom = sccwebmap_tooltip_area.style.paddingTop;
					tmp.style.borderBottom = sccwebmap_tooltip_area.style.border;
					tmp.style.color = sccwebmap_tooltip_area.style.color;
					tmp.style.fontSize = sccwebmap_tooltip_area.style.fontSize;
					tmp.style.fontStyle = sccwebmap_tooltip_area.style.fontStyle;
					tmp.style.fontWeight = sccwebmap_tooltip_area.style.fontWeight;
					tmp.style.fontFamily = sccwebmap_tooltip_area.style.fontFamily;
					tmp.style.textDecoration = sccwebmap_tooltip_area.style.textDecoration;
					
					++tooltip_index;
					ret = true;
					visible = true;
				}
			}
			
			// letzten Tooltip-Trenner entfernen
			if (tmp)
			{
				tmp.style.paddingBottom = '';
				tmp.style.borderBottom = '';
			}
			
			// Überschriftenblock anzeigen
			if (areas.length == 1)
				sccwebmap_tooltip_area.childNodes[0].style.display = '';
			else sccwebmap_tooltip_area.childNodes[0].style.display = 'none';
			if (areas.length > 1)
				sccwebmap_tooltip_area.childNodes[1].style.display = '';
			else sccwebmap_tooltip_area.childNodes[1].style.display = 'none';
			
			// weitere Tooltip-Bereiche ausblenden
			while (tooltip_index < sccwebmap_tooltip_area.childNodes.length)
			{
				sccwebmap_tooltip_area.childNodes[tooltip_index].style.display = 'none';
				++tooltip_index;
			}
		}
		
		if (visible)
		{
			sccwebmap_tooltip_area.style.position = 'absolute';
			sccwebmap_tooltip_area.style.left = '' + (x + 10) + 'px';
			sccwebmap_tooltip_area.style.top = '' + (y + 5) + 'px';
			sccwebmap_tooltip_area.style.paddingBottom = '0px';
			sccwebmap_tooltip_area.style.display = '';
		}
		else {
			sccwebmap_tooltip_area.style.display = 'none';
		}
	}
	
	return ret;
}

function sccwebmapHideTooltip(event)
{
	window.status = '';
	if (sccwebmap_map_element  &&  sccwebmap_map_element.nodeName.toLowerCase() == 'input')
	{
		sccwebmap_map_element.style.cursor = 'crosshair';
	}
	if (sccwebmap_tooltip_clicked != true  &&  sccwebmap_tooltip_area)
	{
		sccwebmap_tooltip_area.style.display = 'none';
	}
}

function sccwebmapLeaveTooltip(event)
{
	sccwebmap_tooltip_clicked = false;
}

function sccwebmapClickTooltip(event)
{
	var ret = true;
	
	if (sccwebmap_tooltip_clicked == true)
	{
		sccwebmap_tooltip_clicked = false;
		ret = false;
	}
	
	if (sccwebmap_map_element  &&  sccwebmap_image_map)
	{
		var x,y, areas;
		if (event.layerX)
		{
			x = event.layerX;
			y = event.layerY;
		}
		else {
			x = event.x;
			y = event.y;
		}
		areas = sccwebmapGetImagemapAreas(sccwebmap_image_map, x, y);
		
		// Link nur bei eindeutigem Tooltip verarbeiten
		if (areas.length == 1  &&  areas[0].href != '')
		{
			if (areas[0].onclick != '')
			{
				try { ret = areas[0].onclick(event); } catch(e) {}
			}
			if (ret)
			{
				if (areas[0].target == '')
				{
					location.href = areas[0].href;
				}
				else {
					var tmp = window.open(areas[0].href, areas[0].target);
					if (tmp)
						tmp.focus();
				}
				ret = false;
			}
		}
		else if (areas.length > 1)
		{
			sccwebmapShowTooltip(event, true);
			return !sccwebmap_tooltip_clicked;
		}
	}
	
	return ret;
}

function sccwebmapMouseMove(event)
{
	if (sccwebmap_map_element_cursor)
	{
		var x,y;
		if (event.layerX)
		{
			x = event.layerX;
			y = event.layerY;
		}
		else {
			x = event.x;
			y = event.y;
		}
		sccwebmap_map_element_cursor.style.left = '' + (parseInt(x) + sccwebmap_map_element_cursor_x) + 'px';
		sccwebmap_map_element_cursor.style.top = '' + (parseInt(y) + sccwebmap_map_element_cursor_y) + 'px';
	}
	
	var show_tooltip = sccwebmapShowTooltip(event);
	
	if (sccwebmap_map_element_cursor)
	{
		if (show_tooltip)
			sccwebmap_map_element_cursor.style.display = 'none';
		else sccwebmap_map_element_cursor.style.display = '';
	}
}

function sccwebmapMouseOut(event)
{
	if (sccwebmap_map_element_cursor)
	{
		sccwebmap_map_element_cursor.style.display = 'none';
	}
	
	sccwebmapHideTooltip(event);
}

function sccwebmapMouseClick(event)
{
	return sccwebmapClickTooltip(event);
}

function sccwebmapSwitchVisible(id1, id2)
{	
	element1 = document.getElementById(id1);
	element2 = document.getElementById(id2);
	if (element1  &&  element2)
	{
		element2.style.display = '';
		element1.style.display = 'none';
	}
}

