var mapArr = new Array();
var firstMarkerArr = new Array();
var markerArr = new Array();
var directionArr = new Array();

function pointKeyPress(e, inp)
{
	if ((e.keyCode && e.keyCode==13) || 
		(e.which && e.which==13))
	{
		createPoint(inp);
	}
	return true;
}

function onLoadGooglemap(widget)
{
	var map = widget.gmap;
	
	map.setUIToDefault();
	map.disableScrollWheelZoom();

	if (widget.item.name.indexOf("ExpatCenterBrabant") > -1)
	{
		var marker = widget.item.markers[0];
		
		var infoWindow = document.createElement("div");
		marker.titleNode = document.createElement("span");
		marker.titleNode.style.fontWeight = "bold"; 
		marker.titleNode.appendChild(document.createTextNode(marker.title));
		infoWindow.appendChild(marker.titleNode);
		
		marker.contentNode = document.createElement("div");
		marker.contentNode.innerHTML = marker.content;	
		infoWindow.appendChild(marker.contentNode);

		map.openInfoWindow(marker.gmarker.getLatLng(), infoWindow);
		
		
		var par = widget.divObject.nextSibling;
		var routeDiv = document.createElement("div");
		
		xajax.call("xajax_getHTML", {parameters:["route", widget.item.name], context: {
			callback: function(html)
			{
				routeDiv.innerHTML = html;
				document.getElementById("content").insertBefore(routeDiv, par);
			}
		}});
		
		firstMarkerArr[widget.item.name] = marker.gmarker;
		mapArr[widget.item.name] = map;
		markerArr[widget.item.name] = null;
		directionArr[widget.item.name] = null;
	}
}

function createPoint(inp)
{
	if (markerArr[inp] != null)
	{
		mapArr[inp].removeOverlay(markerArr[inp]);
	}
	var x = document.getElementById('point' + inp).value;
	var str = x.replace(/ /g,"+");
	var geocoder = new GClientGeocoder();
	
	geocoder.getLatLng(str, function(point)
	{
		if (!point)
		{
			window.alert("The address was not found");
		}
		else
		{
			firstMarkerArr[inp].hide();
			
		    var icon = new GIcon();
		    icon.image = "images/layout/flag.png";
		    icon.iconSize = new GSize(37, 31);
		    icon.iconAnchor = new GPoint(18, 31);
		    icon.infoWindowAnchor = new GPoint(18, 31);
			
		    markerArr[inp] = new GMarker(point,
			{
				title: x,
				icon: icon,
				clickable: false,
				draggable: false,
				hide: true
			});
		    mapArr[inp].addOverlay(markerArr[inp]);

			directionsPanel = document.getElementById("routeDiv" + inp);
			if (directionArr[inp] == null)
			{
				directionArr[inp] = new GDirections(mapArr[inp], directionsPanel);
			}
			
			var firstLocation = new GLatLng();
			firstLocation = firstMarkerArr[inp].getLatLng();
				
			directionArr[inp].load("from: "+point.lat()+","+point.lng()+" to: "+firstLocation.lat()+","+firstLocation.lng(),
  				{
					"locale": "en_UK"
				}
			);
			document.getElementById('printP' + inp).href = "http://maps.google.com/maps?f=d&hl=en&geocode=" + 
													 "&saddr=" + str +
													 "&daddr=" + firstMarkerArr[inp].getLatLng() + "&ie=UTF8&z=13&pw=2";
			
			document.getElementById('printP' + inp).style.display = "block";
			document.getElementById("routeDiv" + inp).style.display = "block";
		}
	});
}
