var firstMarker = null;
var marker = null;
var directions = null;
var map = null;

function onLoadProfileMap(widget)
{
	map = widget.gmap;
	map.setMapType(G_PHYSICAL_MAP);
	map.addMapType(G_PHYSICAL_MAP);
	map.disableScrollWheelZoom();
	
	if (userLocation.indexOf(";") > -1)
	{
		var locArr = userLocation.split(";");
		var point = new GLatLng(locArr[0], locArr[1]);
		
	    var icon = new GIcon();
	    icon.image = "images/site_ebc/gmap/blue_marker.gif";
	    icon.iconSize = new GSize(24, 33);
	    icon.iconAnchor = new GPoint(15, 33);
	    icon.infoWindowAnchor = new GPoint(15, 33);
		
	    firstMarker = new GMarker(point,
		{
			title: '',
			icon: icon,
			clickable: false,
			draggable: false,
			hide: false
		});
		map.addOverlay(firstMarker);
		
		map.setCenter(point, 13);
	}
}

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

function createPoint(inp)
{
	if (marker != null)
	{
		map.removeOverlay(marker);
	}
	
	var x = document.getElementById(inp).value;
	var str = x.replace(/ /g,"+");
	var geocoder = new GClientGeocoder();

	geocoder.getLatLng(str, function(point){
		if (!point)
			{ window.alert("Er is geen geldig adres ingevoerd"); }
		else
		{
			firstMarker.hide();
			
		    var icon = new GIcon();
		    icon.image = "images/site_ebc/gmap/blue_marker.png";
		    icon.iconSize = new GSize(24, 33);
		    icon.iconAnchor = new GPoint(15, 33);
		    icon.infoWindowAnchor = new GPoint(15, 33);
			
			marker = new GMarker(point,
			{
				title: x,
				icon: icon,
				clickable: false,
				draggable: false,
				hide: true
			});
			map.addOverlay(marker);

			if (directions == null)
				directions = new GDirections(map);
			
			var firstLocation = new GLatLng();
			firstLocation = firstMarker.getLatLng();
				
  			directions.load("from: "+point.lat()+","+point.lng()+" to: "+firstLocation.lat()+","+firstLocation.lng(),
  				{
					"locale": "nl_NL"
				}
			);
			document.getElementById('printP').href = "http://maps.google.com/maps?f=d&hl=nl&geocode=" + 
													"&saddr=" + str +
													"&daddr=" + firstMarker.getLatLng() + "&ie=UTF8&z=13&pw=2";
			
			document.getElementById('printP').style.display = "block";
		}
	});
}

var markers = new Array();
var labels = new Array();
var overlays = new Array();
var isOver = new Array();
var overlayTimers = new Array();
var currentLabel = null;
var polys = new Array();

function createMarker(point, i)
{
    var icon = new GIcon();
    icon.image = "images/site_ebc/gmap/grey_marker.png";
    icon.iconSize = new GSize(32, 40);
    icon.iconAnchor = new GPoint(19, 38);
    icon.infoWindowAnchor = new GPoint(19, 38);
	
	marker = new GMarker(point,
	{
		title: "",
		icon: icon,
		clickable: true,
		draggable: false,
		hide: false
	});
	
	GEvent.addListener(marker, "mouseover", function()
	{
		overlays[i].setPoint(markers[i].getLatLng());
		labels[i].setPoint(markers[i].getLatLng());
		
		var callback = function() { checkOverlay(i); }
		overlayTimers[i] = window.setTimeout(callback, 200);
		
		overlays[i].show();
		labels[i].show();
	});
	
	return marker;
}

function checkOverlay(i)
{
	if (isOver[i] == false)
	{
		hideLabel(i);
	}
}

function checkCurrentLabel()
{
	if (currentLabel != null)
	{
		labels[currentLabel].hide();
		overlays[currentLabel].hide();
	}
}

function hideLabel(i)
{
	if (!markers[i].isHidden() && currentLabel != i)
	{
		labels[i].hide();
		overlays[i].hide();
		isOver[i] = false;
	}
}

function createLabel(point, html, isTable)
{
	var size = isTable ? new GSize(-16, 0) : new GSize(-19, 2);
	
	var label = new ELabel(point,
						   html,
						   "",
						   size);
	
	return label;
}

function getUserContent(id, index)
{
	checkCurrentLabel();
	
	currentLabel = index;
	xajax.call("xajax_getUserContent", {parameters:[id]});
}

function onLoadClubMap(widget)
{
	var map = widget.gmap;
	map.setUIToDefault();
	map.disableInfoWindow();
	map.disableScrollWheelZoom();
	
	if (userLocations.length > 0)
	{
		var zoomIndex = -1;
		var markerClusterer = null;
		
		for (var i = 0; i < userLocations.length; i++)
		{
			var point = new GLatLng(userLocations[i]["lat"], userLocations[i]["lng"]);
			
			var titleLength = userLocations[i]["company"].length * 6 + 1;
			var labelHTML =	'<table id="label_'+i+'" class="mapLabel" style="height:33px;width:' + (titleLength + 19) + 'px"><tr>' +
								'<td class="labelLeft"><img style="float:left" src="images/site_ebc/gmap/blue_marker_left.gif"></td>' +
								'<td class="labelBg" style="width:' + titleLength + 'px;">' + userLocations[i]["company"] + '</td>' +
								'<td class="labelRight"><img style="float:right" src="images/site_ebc/gmap/blue_marker_right.gif"></td>' +
							'</tr></table>';

			var label = createLabel(point, labelHTML, true);
			label.hide();
			labels.push(label);
			map.addOverlay(label);
			
			var overlayHTML =  '<div id="labelOverlay_'+i+'" style="cursor:pointer;background-color:#FFFFFF;filter:alpha(opacity=1);-moz-opacity:0.01;-khtml-opacity:0.01;opacity:0.01;height:40px;width:' + (titleLength + 22) + 'px;" onclick="getUserContent('+userLocations[i]["id"]+','+i+')" onmouseover="isOver['+i+'] = true" onmouseout="hideLabel('+i+')"></div>';
			var overlay = createLabel(point, overlayHTML, false);			
			overlay.hide();
			overlays.push(overlay);
			map.addOverlay(overlay);
			
			isOver.push(false);
			overlayTimers.push(0);
			
			var marker = createMarker(point, i);
			markers.push(marker);
			
			var paramId = _GET("zId");
			if (paramId != null && paramId == userLocations[i]["id"])
			{
				zoomIndex = i;
			}
		}

		var customIcon = [{
	        url: g_baseDir + 'images/site_ebc/gmap/grey_cluster.png',
	        height: 40,
	        width: 55,
	        opt_anchor: [7, 27],
	        opt_textColor: '#FFFFFF'
	      }];
		
		var markerCluster = new MarkerClusterer(map, markers, {styles: customIcon, maxZoom: 17});
		
		if (zoomIndex > -1)
		{
			map.setCenter(markers[zoomIndex].getLatLng());
			getUserContent(userLocations[zoomIndex]["id"], zoomIndex);
			labels[zoomIndex].show();
		}
	}
}
