var submenus = new Array();
var timer;

function init() 
{
	//resizeElements();
	//window.onresize = resizeElements;
}

function getBodySize() 
{
	var w = -1;
	if (window.innerWidth) 
		w = window.innerWidth;
	else 
	{
		if (document.body && document.body.clientWidth) 
			w = document.body.clientWidth;
	}
	var h = -1;
	if (window.innerHeight) 
		h = window.innerHeight;
	else 
	{
		if (document.documentElement) 
			h = document.documentElement.offsetHeight;
		else 
		{
			if (document.body && document.body.offsetHeight) 
				h = document.body.offsetHeight;
		}
	}
	return [w, h];
}

function getElement(elementId) 
{
	return (document.getElementById) ? document.getElementById(elementId) : document.all.elementId;
}

function showMenu(parentObj, id, level) 
{
	clearTimer();
	hideMenus(level);
	
	var submenu = submenus[id];
	if (!submenu)
		submenu = getElement('cid'+id);
	if (!submenu) // || level == 1
		return;
	
	var pos = findPos(parentObj);
	var l = pos[0];
	var t = pos[1];
	if (level == 1)
	{
		t += 32;
		//hideMenus();
	}
	else
		l += parentObj.offsetWidth + 3;
	submenu.style.left = l + "px";
	submenu.style.top = t + "px";
	submenu.style.zIndex = level;
	submenu.style.visibility = "visible";
	
	if (!submenus[id])
		submenus[id] = submenu;
}

function hideMenus(level)
{
	if (!level)
		level = 0;
	for (var id in submenus)
	{
		var submenu =submenus[id];
		var l = (document.all) ? submenu.style.getAttribute('zIndex', 'false') : submenu.style.zIndex;
		if (l > level-1)
			submenu.style.visibility = "hidden";
	}
}

function clearTimer()
{
	window.clearTimeout(timer);
}

function setTimer()
{
	timer = window.setTimeout("hideMenus()", 500);
}

function findPos(obj) 
{
	var left = 0;
	var top = 0;
	if (obj.offsetParent) 
	{
		left = obj.offsetLeft;
		top = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			left += obj.offsetLeft;
			top += obj.offsetTop;
		}
	}
	return [left, top];
}

function toggleVisibility(id, posObj)
{
	var obj = getElement(id);
	if (!obj)
		return;
		
	if (obj.style.visibility == 'hidden')
	{
		if (posObj)
		{
			var pos = findPos(posObj);
			obj.style.left = pos[0] +'px';
			obj.style.top = (pos[1]+14) +'px';
		}
		obj.style.visibility = 'visible';
	}
	else
		obj.style.visibility = 'hidden';
}

// GoogleMap Skripte
var googlemap;
var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;

// A function to create the marker and set up the event window
function createMarker(point,name,html) {
	var marker 		= new GMarker(point);
	var bspAdr 		= 'z.B.: &quot;Jahnstr. 10, 70597 Stuttgart, Germany&quot;<br/>';
	var newWindow = '(&Ouml;ffnet ein neues Fenster)';
	var route 		= 'Route';
	var calcRoute = 'Route berechnen';
	var from 			= 'Von hier';
	var to 				= 'Hierher';
	var start 		= 'Startadresse';
	var end 			= 'Zieladresse';
	
  // The info window version with the "to here" form open
  to_htmls[i] = html + '<br/>'+ route +': <b>'+ to +'</b> - <a href="javascript:fromhere(' + i + ')">'+ from +'</a>' +
       '<br/>'+ start +': <br/>'+ bspAdr +'<form action="http://maps.google.de/maps" method="get" target="_blank">' +
       '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br/>' +
       '<INPUT value="'+ calcRoute +'" TYPE="SUBMIT">' +
       '<input type="hidden" name="daddr" value="fridinger str 20, 70619 stuttgart, germany"/>' +
       //'<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '"/> 
       '<span class="small">'+ newWindow +'</span>';

  // The info window version with the "to here" form open
  from_htmls[i] = html + '<br/>'+ route +': <a href="javascript:tohere(' + i + ')">'+ to +'</a> - <b>'+ from +'</b>' +
       '<br>'+ end +': <br/>'+ bspAdr +'<form action="http://maps.google.de/maps" method="get"" target="_blank">' +
       '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br/>' +
       '<INPUT value="'+ calcRoute +'" TYPE="SUBMIT">' +
       '<input type="hidden" name="saddr" value="fridinger str 20, 70619 stuttgart, germany"/> ' +
       //'<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '"/> ' +
       '<span class="small">'+ newWindow +'</span>';

  // The inactive version of the direction info
  html = html + '<br/>'+ route +': <a href="javascript:tohere('+i+')">'+ to +'</a> - <a href="javascript:fromhere('+i+')">'+ from +'</a>';

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  gmarkers[i] = marker;
  htmls[i] = html;
  i++;
  return marker;
}

// functions that open the directions forms
function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
    
function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function load() {
	if (GBrowserIsCompatible()) {
    var mapcenter = new GLatLng(48.748382, 9.212979);
    var bepixeld = new GLatLng(48.751373, 9.217562);
		var googlemap = new GMap2(document.getElementById("googlemap"));
		googlemap.addControl(new GLargeMapControl());
		googlemap.addControl(new GMapTypeControl());
		googlemap.setCenter(mapcenter, 15);
		googlemap.enableContinuousZoom();
    
    var marker = createMarker(bepixeld,'bepixeld','<b>bepixeld</b><br/>Fridinger Str. 20<br/>70619 Stuttgart<br/>Tel. 0711 / 469 09 77')
    googlemap.addOverlay(marker);	
	}
}

