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, isFindPos)
{
	var obj = getElement(id);
	if (!obj)
		return;

	if (obj.style.visibility == 'hidden') {
		if (isFindPos == 'undefined') {
			isFindPos = true;
		}
		if (isFindPos && 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';
}

/* Google Map functions */
var bmap;
var bmapInfoWindow;
var bmapConf;
var bmapDservice;
var bmapDdisplay;
var isTo = true;

function loadBmap(conf)
{
	bmapConf = conf;
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&language=" + bmapConf.lang + "&callback=initBmap";
	document.body.appendChild(script);
}

function initBmap()
{
	var opt = {
		zoom : 15,
		mapTypeId : google.maps.MapTypeId.ROADMAP
	};
	bmap = new google.maps.Map(document.getElementById(bmapConf.divId), opt);

	var gc = new google.maps.Geocoder();
	gc.geocode( {
		'address' : bmapConf.adr
	}, initMarker);
}

function initMarker(results, status)
{
	if (status == google.maps.GeocoderStatus.OK) {
		bmap.setCenter(results[0].geometry.location);
		var mOpt = {
			map : bmap,
			zIndex : 500,
			position : results[0].geometry.location
		};
		if (bmapConf.icon.img) {
			mOpt.icon = getIcon(bmapConf.icon.img, bmapConf.icon.w, bmapConf.icon.h, bmapConf.icon.x, bmapConf.icon.y);
			mOpt.shadow = getIcon(bmapConf.icon.shadow, bmapConf.icon.w, bmapConf.icon.h, bmapConf.icon.x,
					bmapConf.icon.y);
		}

		var marker = new google.maps.Marker(mOpt);

		var n = document.createElement("div");
		n.innerHTML = bmapConf.infoWindowHtml;

		bmapInfoWindow = new google.maps.InfoWindow( {
			content : n
		});

		google.maps.event.addListener(marker, 'click', function()
		{
			bmapInfoWindow.open(bmap, marker);
		});

		initRoute(results[0].geometry.location);
	} else {
		alert("Address not found. Reason: " + status);
	}
}

function initRoute(target)
{
	bmapDservice = new google.maps.DirectionsService();

	var dOpts = {
		suppressMarkers : ((bmapConf.route.suppressMarker == '1') ? true : false),
		polylineOptions : {
			strokeColor : bmapConf.route.color,
			strokeOpacity : 0.7,
			strokeWeight : bmapConf.route.size
		}
	};
	if (bmapConf.route.panel) {
		dOpts.panel = document.getElementById(bmapConf.route.panel);
	}

	bmapDdisplay = new google.maps.DirectionsRenderer(dOpts);

	if (bmapConf.route.startAdr) {
		showRoute(bmapConf.route.startAdr, target);
	}
}

function showRoute(start, target)
{
	var sOpts = {
		origin : start,
		destination : target,
		unitSystem : google.maps.DirectionsUnitSystem.METRIC
	};
	if (bmapConf.route.type == 1) {
		sOpts.travelMode = google.maps.DirectionsTravelMode.WALKING;
	} else {
		sOpts.travelMode = google.maps.DirectionsTravelMode.DRIVING;
	}

	bmapDservice.route(sOpts, function(result, status)
	{
		var p = bmapDdisplay.getPanel();
		if (status == google.maps.DirectionsStatus.OK) {
			if (p) {
				p.innerHTML = "";
			}
			bmapDdisplay.setMap(bmap);
			bmapDdisplay.setDirections(result);
		} else {
			bmapDdisplay.setMap(null);
			bmapConf.route.startAdr = '';
			if (p) {
				p.innerHTML = bmapConf.route.noRouteTxt;
			}
			alert(bmapConf.route.errorTxt + ' Code: ' + status);
		}
	});
}

function getIcon(img, width, height, anchX, anchY)
{
	return new google.maps.MarkerImage(img, new google.maps.Size(width, height), new google.maps.Point(0, 0),
			new google.maps.Point(anchX, anchY));
}

function setRoute(to)
{
	var n = bmapInfoWindow.getContent();
	getElement('mapRouteForm').style.display = 'block';
	getElement('toText').style.display = ((to) ? 'block' : 'none');
	getElement('fromText').style.display = ((!to) ? 'block' : 'none');
	bmapInfoWindow.setContent(n);
	isTo = to;
}

function submitRoute()
{
	// Startadress in config setzen und Berechnungstyp zurücksetzen
	bmapConf.route.startAdr = getElement('adr').value;
	bmapConf.route.type = 0;
	if (isTo) {
		showRoute(bmapConf.route.startAdr, bmapConf.adr);
	} else {
		showRoute(bmapConf.adr, bmapConf.route.startAdr);
	}
	bmapInfoWindow.close();
}

function printRoute()
{
	if (!bmapConf.route.startAdr) {
		alert(bmapConf.route.noRouteTxt);
		return;
	}
	var start = encodeURIComponent((isTo) ? bmapConf.route.startAdr : bmapConf.adr);
	var target = encodeURIComponent((isTo) ? bmapConf.adr : bmapConf.route.startAdr);
	var type = '';
	if (bmapConf.route.type == 1) {
		type = '&dirflg=w';
	}

	window.open('http://maps.google.de/maps?pw=2&saddr=' + start + '&daddr=' + target + type, '_blank');
}
/* END Google Map functions */


