var maps;

function addMarker(result)
{
   var location = new GLatLng(result.location_lon, result.location_lat);
   // create new gicon to use with the marker
   var customIcon = new GIcon();
   
   
   // put in random image
   var pos = Math.floor(Math.random()*4);
   var images = new Array("theme/img/markers/ico_marker_tha.png","theme/img/markers/ico_marker_thg.png","theme/img/markers/ico_marker_tka.png","theme/img/markers/ico_marker_tkg.png");
   if($chk(result.sell_types[1]) && result.type == 1)
   {
		customIcon.image = images[2];
   } else if($chk(result.sell_types[1]) && result.type == 2)
   {
		customIcon.image = images[3];
   } else if($chk(result.sell_types[2]) && result.type == 1)
   {
		customIcon.image = images[0];
   } else if($chk(result.sell_types[2]) && result.type == 2)
   {
		customIcon.image = images[1];
   }
   
   customIcon.iconSize = new GSize(62,50);
   customIcon.iconAnchor = new GPoint(10, 34);
   customIcon.infoWindowAnchor = new GPoint(10, 0);
   
   

   // create new marker
   var marker = new GMarker(location, customIcon);
   GEvent.addListener(marker, 'click',
      function() 
      {
         var htmlString = "<a style='text-decoration: none;' href='/nl_/advertentie/id/"+result.estate_id+"/'><font color=\"#6387A5\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
         
         if(result.type == 1)
         {
         	htmlString = htmlString + "<strong>"+result.address1+" "+result.address2+", "+result.city+"</strong><br /><i>";
         }
         else
         {
         	htmlString = htmlString + "<strong>"+result.city+" < "+result.straal+"km</strong><br /><i>";
         }
         
         if($chk(result.filename))
         {
         	htmlString = '<img style="float: left; margin-right: 10px;" height="50" src="downloads/'+result.estate_id+'/thumb_'+result.filename+'" alt="'+result.address1+' '+ result.address2 +'" />' + htmlString;
         }
         
         
         if($chk(result.sell_types[1]) && $chk(result.sell_types[2]))
         {
         	htmlString = htmlString + "Te koop / Te huur ";
         }
         else if($chk(result.sell_types[1]))
         {
         	htmlString = htmlString + "Te koop ";
         }
         else if($chk(result.sell_types[2]))
         {
         	htmlString = htmlString + "Te huur ";
         }
         
         if(result.type == 1)
         {
         	htmlString = htmlString + "aangeboden";
         }
         else if(result.type == 2)
         {
         	htmlString = htmlString + "gevraagd";
         }
         htmlString = htmlString + "</i><br /><span style='width: 225px; display: block;'>" + result.description.substr(0, 180)+"...</span></font></a>";
         marker.openInfoWindowHtml(htmlString);
      }
   );
   
   // place new marker in the map   
   maps.addOverlay(marker); 
   
}

window.addEvent('load', function(e) {
if (GBrowserIsCompatible() && $('map')) {
		var geocoder = new GClientGeocoder();
		var markers = {};
		var size = new GSize(664,400);
		
		maps = new GMap2(document.getElementById("map"), {'size': size});

        var latLon = $('map').get('title');
        if($chk(latLon))
        {
        	var latLong = latLon.split(',');
        	var lat = latLong[0];
        	var lon = latLong[1];
        }
        
        $('map').set('title', '');
        
        maps.setCenter(new GLatLng(lon, lat), 13);
		maps.addControl(new GSmallMapControl());
     	maps.addControl(new GMapTypeControl());
     	new Request.JSON({
     		'url': 'nl_/complete/',
     	  	onComplete: function(res) {
     	  		res.each(function(result){
     	  			addMarker(result);
     	  		});
     	  	}
     	}).post({'point_x':lat, 'point_y':lon});

    }
});