function createMarker(point, image) {
	var icon = new GIcon();
	icon.image = image[0];
	icon.iconSize = new GSize(image[1], image[2]);
	subt = image[2] / 100 * 11;
	subt = Math.ceil(subt);
	var anchor = image[2] - subt;
	icon.iconAnchor= new GPoint(10, anchor);
	//{icon: icon, draggable: true}
	var marker = new GMarker(point,icon);
	/*var tooltip = document.createElement("div");
      		map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
      		tooltip.style.visibility="hidden";  */
	return marker;
}

function createMapMarker(point,image) {
	var icon = new GIcon();
	icon.image = image[0];
	icon.iconSize = new GSize(image[1], image[2]);
	subt = image[2] / 100 * 11;
	subt = Math.ceil(subt);
	var anchor = image[2] - subt;
	icon.iconAnchor= new GPoint(10, anchor);
	var infoanchor = image[2] / 3;
	infoanchor = Math.floor(infoanchor);
	icon.infoWindowAnchor = new GPoint(infoanchor, 2);
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {map.showMapBlowup(marker.getPoint());});
	return marker;
}


function createTabMarker(point, tabs,image) {
	var icon = new GIcon();
	icon.image = image[0];
	icon.iconSize = new GSize(image[1], image[2]);
	subt = image[2] / 100 * 11;
	subt = Math.ceil(subt);
	var anchor = image[2] - subt;
	icon.iconAnchor= new GPoint(10, anchor);
	var infoanchor = image[2] / 3;
	icon.infoWindowAnchor = new GPoint(infoanchor, 2);
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowTabsHtml(tabs);
	});
	return marker;
}


function createOtherMarker(point, number,image) {
	var icon = new GIcon();
	icon.image = image[0];
	icon.iconSize = new GSize(image[1], image[2]);
	subt = image[2] / 100 * 11;
	subt = Math.ceil(subt);
	var anchor = image[2] - subt;
	icon.iconAnchor= new GPoint(10, anchor);
	var infoanchor = image[2] / 3;
	infoanchor = Math.floor(infoanchor);
	icon.infoWindowAnchor = new GPoint(infoanchor, 2);
	var marker = new GMarker(point,icon);
	var html = number;
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	return marker;
}

	var arrowIcon = new GIcon();
	arrowIcon.iconSize = new GSize(24,24);
	arrowIcon.shadowSize = new GSize(1,1);
	arrowIcon.iconAnchor = new GPoint(12,12);
	arrowIcon.infoWindowAnchor = new GPoint(0,0);
	var degreesPerRadian = 180.0 / Math.PI;
	function bearing( from, to ) {
		var lat1 = from.latRadians();
		var lon1 = from.lngRadians();
		var lat2 = to.latRadians();
		var lon2 = to.lngRadians();
		var angle = - Math.atan2( Math.sin( lon1 - lon2 ) * Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) - Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
		if ( angle < 0.0 )
		angle  += Math.PI * 2.0;
		angle = angle * degreesPerRadian;
		angle = angle.toFixed(1);
		return angle;
	}

	function arrowHead(points) {
		var p1=points[points.length-1];
		var p2=points[points.length-2];
		var dir = bearing(p2,p1);
		var dir = Math.round(dir/3) * 3;
		while (dir >= 120) {dir -= 120;}
		arrowIcon.image = "/img/dir_"+dir+".png";
		map.addOverlay(new GMarker(p1, arrowIcon));
	}

	function midArrows(points) {
		for (var i=1; i < points.length-1; i++) {
			var p1=points[i-1];
			var p2=points[i+1];
			var dir = bearing(p1,p2);

			var dir = Math.round(dir/3) * 3;
			while (dir >= 120) {dir -= 120;}
			arrowIcon.image = "/img/dir_"+dir+".png";
			map.addOverlay(new GMarker(points[i], arrowIcon));
		}
	}

	function geocodeAddress(address,typ,marker,zoom) {
		geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				document.getElementById('googlemap').innerHTML = '<div class="mapMsg"><img src="/images/map/map_address_not_found.png" width="350" height="170" alt="'+T_('Unfortunately there are no map data available at the moment. Thank you for your understanding.')+'" /><p class="imgTxt">'+T_('Unfortunately there are no map data available at the moment. Thank you for your understanding.')+'</p>';
			} else {
				var lat = point.lat();
				var lng = point.lng();
				map.setCenter(point,zoom);
				//document.getElementById('lat').value = lat;
				//document.getElementById('lng').value = lng;
				if(typ == "otherMarker") {
					marker = createOtherMarker(point,address,marker);
				}
				else if(typ == "marker") {
					marker = createMarker(point,marker);
				}
				else if(typ == "mapMarker")	{
					marker = createMapMarker(point,marker);
				}
				map.addOverlay(marker);
			}
		}
		);
	}

	function getAdress() {
		var adress = document.getElementById('adresse').value;
		return adress;
	}
	
	function createMarker2(point, name, html, url, image, j) {
        var icon = new GIcon();
		icon.image = image[0];
		icon.iconSize = new GSize(image[1], image[2]);
		subt = image[2] / 100 * 11;
		subt = Math.ceil(subt);
		var anchor = image[2] - subt;
		icon.iconAnchor= new GPoint(10, anchor);
		var infoanchor = image[2] / 3;
		infoanchor = Math.floor(infoanchor);
		icon.infoWindowAnchor = new GPoint(infoanchor, 2);
        var marker = new GMarker(point, {title:name, icon:icon});
		GEvent.addListener(marker, "mouseover", function() {showTooltip(marker, j);});
		GEvent.addListener(marker, "mouseout", function() {tooltip[j].style.display="none";});
		GEvent.addListener(marker, "click", function() {
		  window.location.href = url;
		});
		gmarkers.push(marker);
		return marker;
    }
	
	function calculateMarkerPosition(map, markerLatLngPoint) {
		gmapTopLeftLatLng = map.fromContainerPixelToLatLng(new GPoint(0,0));
		gmapTopLeftPixel = map.fromLatLngToDivPixel(gmapTopLeftLatLng);
		myMarkerDivPixel = map.fromLatLngToDivPixel(markerLatLngPoint);
		markerTop = getAbsoluteTop(map.getContainer()) - gmapTopLeftPixel.y + myMarkerDivPixel.y;
		markerLeft = getAbsoluteLeft(map.getContainer()) - gmapTopLeftPixel.x + myMarkerDivPixel.x;
		return {top:markerTop, left:markerLeft};
	}
	
	function setupOfficeMarkers() {
		var mgr = new GMarkerManager(map);
		gmarkers = [];	
		//for (var i in officeLayer) {
		for(var i = 0; i < officeLayer.length; i++) {
			var markers = [];
			var layer = officeLayer[i];
			tooltip = [];
			//for (var j in layer["places"]) {
			for(var j = 0; j < layer["places"].length - 1; j++) {
			
				tooltip[j] = document.createElement("div");
	      		map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip[j]);
	      		tooltip[j].className = 'tooltip';	
				tooltip[j].style.display = "none";
				tooltip[j].style.position = "absolute";
				//tooltip[j].style.background = "#fff";
				//tooltip[j].style.padding = "0";
				//tooltip[j].style.margin = "0";
				tooltip[j].style.zIndex = 50000;
				var place = layer["places"][j];
				//var icon = getIcon(place["icon"]);
				tooltip[j].innerHTML = layer["places"][j]["html"];
				var posn = new GLatLng(place["posn"][0], place["posn"][1]);
				//var tmp = new GMarker(posn, { title: place["name"], icon: icon, 'clickable': true, 'draggable': true });
				var tmp = createMarker2(posn, place["name"], place["html"], place["url"], place["icon"], j);
			
				markers.push(tmp);
				//mgr.addMarker(tmp, layer["zoom"][0], layer["zoom"][1]);
			}
			mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
			
		}
		mgr.refresh();
	}
	var iconData = {
		"a": { width: 25, height: 38 },
		"b": { width: 25, height: 38 }
	}
	function getIcon(images) {
		var icon = null;
		if (images) {
			if (icons[images[0]]) {
				icon = icons[images[0]];
			} else {
				icon = new GIcon();
				icon.image = images[0];
				var size = { width: 20, height: 31 };
				icon.iconSize = new GSize(size.width, size.height);
				icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
				if (images[1]) {	
					icon.shadow = images[1];
					size = iconData[images[1]];
					icon.shadowSize = new GSize(size.width, size.height);
				}
				icons[images[0]] = icon;
			}
		}
		return icon;
	}
	
	function showTooltip(marker, j) {
	    var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var height=tooltip[j].clientHeight;
		markerPosition = calculateMarkerPosition(map, marker.getPoint());
		try {
			tooltip[j].style.left = markerPosition.left + ( marker.getIcon().iconSize.width - marker.getIcon().iconAnchor.x - 5 ) + "px";
			tooltip[j].style.top = markerPosition.top  + "px";
			tooltip[j].style.display = "block";
			document.body.appendChild(tooltip[j]);
			
		} catch(e) {
			console.error(e);
		}	
    }

	function getAbsoluteLeft(o) {
		var oLeft = o.offsetLeft; // Get left position from the parent object
		var oParent;
		var o;
		while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent // Get parent object reference
			oLeft += oParent.offsetLeft // Add parent left position
			o = oParent
		}
		return oLeft
	}

	function getAbsoluteTop(o) {
		var oTop = o.offsetTop; // Get top position from the parent object
		var oParent;
		var o;
		while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent // Get parent object reference
			oTop += oParent.offsetTop // Add parent top position
			o = oParent
		}
		return oTop
	}
