var map = null;
var defaultzoom = 15;
var widezoom = 7;
var closezoom = 19;
var mapsize = {};

window.addEvent('unload', function()
{
  GUnload();
});

function create_map(element, center, zoom, enabled, controls_navigation, controls_maptype, maptype)
{
  // options = {};
  var obj = new GMap2(element);
  zoom = zoom || defaultzoom;
  enabled = enabled || false;
  controls_navigation = controls_navigation || false;
  controls_maptype = controls_maptype || false;
  maptype = maptype || '';

  if (enabled == true)
  {
    obj.enableScrollWheelZoom();
  } 
  else
  {
    obj.disableDoubleClickZoom();
    obj.disableDragging();
    obj.disableInfoWindow();
  }
  
  if (controls_navigation == true)
  {
    obj.addControl(new GSmallMapControl());
  }
  
  if (controls_maptype == true)
  {
    obj.addControl(new GMapTypeControl());
  }
  
  if (maptype == 'satellite')
  {
    obj.setMapType(G_SATELLITE_MAP);
  }
  else if (maptype == 'hybrid')
  {
    obj.setMapType(G_HYBRID_MAP);
  }

  obj.setCenter(center, zoom);
 
  mapsize = obj.getContainer().getDimensions();
 
  return obj;
}

function create_default_marker(latlng, enabled)
{
    enabled = enabled || false;
    
    var marker = new GMarker(
        latlng
       ,{
       
        clickable   : false
       ,draggable   : enabled
       ,bouncy      : enabled
       ,autoPan     : enabled
       
       }
    );

    return marker;
}

function create_marker(latlng, icon, clickable, draggable)
{
    clickable = clickable || false;
    draggable = draggable || false;
    
    var marker = new GMarker(
        latlng
       ,{
       
        icon        : icon
       ,clickable   : clickable
       ,draggable   : draggable
       
       }
    );

    return marker;
}

function create_infomarker(data)
{
    /*
        0 = latitude
        1 = longitude
        2 = id
        3 = name
		4 = urlencode(name)
        5 = description
        6 = avatar width
        7 = avatar height
        8 = gender
    */
	
	var multiple = (data.length > 1);

    var latlng  = new GLatLng(data[0][0], data[0][1]);
    var icon = create_icon_user(multiple);
    var marker = create_marker(latlng, icon, true, false);
	
	var info = '';
	var url = '';

	data.each(function(user, index)
	{
		url = site_base+"/profielen/"+user[2]+"/"+user[4];
		
		if (multiple)
		{
			info += "<a class='avatar avatar_small' href='"+url+"' title='"+user[3]+"'>";
			if (user[6] > 0)
			{
				info += "<img class='photo' src='"+site_base+"/resources/uploads/core/user/small/"+user[2]+"_present.jpg' width='"+user[6]+"' height='"+user[7]+"' alt='"+user[3]+"' /><img class='shadow' src='"+site_base+"/resources/images/layout/shadow_bottom_1.png' width='"+user[6]+"' height='5' alt='' />";
			}
			else
			{
				info += "<img class='photo' src='"+site_base+"/resources/images/styles/avatar_small_"+user[8]+".jpg' width='60' height='60' alt='"+user[3]+"' /><img class='shadow' src='"+site_base+"/resources/images/layout/shadow_bottom_1.png' width='60' height='5' alt='' />";
			}
			info += "</a>";
			
			
			if ((index+1)%4==0)
			{
				info += "<div class='layoutfix'>&nbsp;</div>";
			}
		}
		else
		{
			info += "<div class='item_user_small'>";
			info += "<a class='avatar avatar_small' href='"+url+"' title='"+user[3]+"'>";
			
			if (user[6] > 0)
			{
				info += "<img class='photo' src='"+site_base+"/resources/uploads/core/user/small/"+user[2]+"_present.jpg' width='"+user[6]+"' height='"+user[7]+"' alt='"+user[3]+"' /><img class='shadow' src='"+site_base+"/resources/images/layout/shadow_bottom_1.png' width='"+user[6]+"' height='5' alt='' />";
			}
			else
			{
				info += "<img class='photo' src='"+site_base+"/resources/images/styles/avatar_small_"+user[8]+".jpg' width='60' height='60' alt='"+user[3]+"' /><img class='shadow' src='"+site_base+"/resources/images/layout/shadow_bottom_1.png' width='60' height='5' alt='' />";
			}
			
			info += "</a>";

			info += "<div class='content'>";
			info += "<a href='"+url+"'>"+user[3]+"</a>";
			if (user[5].length > 0)
			{
				info += "<p>"+user[5]+"</p>";
			}
			info += "</div><div class='layoutfix'>&nbsp;</div></div>";
		}

	});
	
  
    GEvent.addListener(marker, 'click', function()
    {
        marker.openInfoWindowHtml(info);
    });
    
    return marker;
}

function create_tooltipmarker(data)
{    
    var latlng  = new GLatLng(data[0], data[1]);
    var icon = create_icon_user();
    var marker = create_marker(latlng, icon, true, false);
    var url = site_base+"/profielen/"+data[2]+"/"+data[4];

    marker.tooltip = new Tooltip(marker, data[3]); 
    
    map.addOverlay(marker.tooltip); 
    GEvent.addListener(marker, 'mouseover', function()
    {
        this.tooltip.show();
    });
    GEvent.addListener(marker, 'mouseout', function()
    {
        this.tooltip.hide();
    });
    
    GEvent.addListener(marker, 'click', function()
    {
        window.location = url;
    });
    
    return marker;
}

function create_icon()
{
    var icon = new GIcon();
    icon.image = site_base+'/resources/images/styles/mapmarker.png';
    icon.shadow = site_base+'/resources/images/styles/mapmarker_shadow.png';
    icon.iconSize = new GSize(20, 34);
    icon.shadowSize = new GSize(37, 34);
    icon.iconAnchor = new GPoint(9, 34);
    icon.infoWindowAnchor = new GPoint(9, 2);
    return icon;
}
function create_icon_user(multiple)
{
	var multiple = multiple || false;

    var icon = new GIcon();
	
	if (multiple)
	{
		icon.image = site_base+'/resources/images/styles/mapmarker_user_multiple.png';
		icon.shadow = site_base+'/resources/images/styles/mapmarker_user_multiple_shadow.png';
		icon.iconSize = new GSize(17, 15);
		icon.shadowSize = new GSize(23, 16);
		icon.iconAnchor = new GPoint(8, 15);
		icon.infoWindowAnchor = new GPoint(9, 3);
	}
	else
	{
		icon.image = site_base+'/resources/images/styles/mapmarker_user.png';
		icon.shadow = site_base+'/resources/images/styles/mapmarker_user_shadow.png';
		icon.iconSize = new GSize(12, 14);
		icon.shadowSize = new GSize(18, 15);
		icon.iconAnchor = new GPoint(6, 14);
		icon.infoWindowAnchor = new GPoint(7, 3);
	}
	
    return icon;
}

function draw(point, zoom)
{
  zoom = zoom || false;

  if (circle) map.removeOverlay(circle);
  var givenRad = radius;
  var givenQuality = 30;
  var center = point || map.getCenter()

  circle = drawCircle(center, givenRad, givenQuality, "#fff", 2, 0.4, "#c1c6e6", 0.4);
  map.addOverlay(circle);

  if (zoom == true)
  {
    map.setCenter(point, map.getBoundsZoomLevel(bounds));
  }
}

function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa)
{
  // Esa 2006
  // calculating km/degree
  var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
  var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

  var points = [];
  var step = parseInt(360/nodes)||10;
  for(var i=0; i<=360; i+=step)
  {
    var point = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + (radius/lngConv * Math.sin(i * Math.PI/180)));
    points.push(point);
    bounds.extend(point);
  }
  fillColor = fillColor||liColor||"#30b3ff";
  liWidth = liWidth||2;
  return new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa, {clickable:false});
}

function distance(point1, point2)
{
  var R = 6371; // earth's mean radius in km
  var lon1 = point1.lng()* Math.PI / 180;
  var lat1 = point1.lat() * Math.PI / 180;
  var lon2 = point2.lng() * Math.PI / 180;
  var lat2 = point2.lat() * Math.PI / 180;

  var deltaLat = lat1 - lat2
  var deltaLon = lon1 - lon2

  var step1 = Math.pow(Math.sin(deltaLat/2), 2) + Math.cos(lat2) * Math.cos(lat1) * Math.pow(Math.sin(deltaLon/2), 2);
  var step2 = 2 * Math.atan2(Math.sqrt(step1), Math.sqrt(1 - step1));
  return step2 * R;
}