function initialize_map(address) {
    var geocoder = new google.maps.Geocoder();
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latlng = results[0].geometry.location;
            var myOptions = {
              zoom: 7,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP,
              scrollwheel: false
            };

            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
 }

   function addMarkers(offers, region, image_url, image_url2) {
       // window.i = 0; //tiganie
       offers = eval('(' + offers +')');
	   var zoom_value=7; // Display region 100 Km
	   if (region==undefined) {
		   zoom_value=10; // 10 km
	   }
	    var geocoder = new google.maps.Geocoder();
	    var latlng = new google.maps.LatLng(46.227638, 2.213749); // France
		// coords
			var myOptions = {
			zoom: zoom_value, 
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scaleControl: false,
			scrollwheel: false
			};

    if(region) {
    	if (region=='France') {
    		zoom_value=5;
    	} else if (region=='Suisse') {
    		zoom_value=4;
    	}
    	
    	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
         geocoder.geocode( { 'address': region}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latlng = results[0].geometry.location;
               var myOptions = {
         	          zoom: zoom_value, 
        	          center: latlng,
        	          mapTypeId: google.maps.MapTypeId.ROADMAP,
        	          scaleControl: false
                };
               map.setCenter(latlng);
            } else {
              alert("Geocode was not successful for the following reason: " + status);
            }
        });
         map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    } else {
    	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }

     google.maps.event.addListener(map, 'zoom_changed', function() {
         zoomChangeBoundsListener =
             google.maps.event.addListener(map, 'bounds_changed', function(event) {
                 if (this.getZoom() > 10) {
                     // Change max/min zoom here
                     this.setZoom(10);

                 }
             google.maps.event.removeListener(zoomChangeBoundsListener);
         });
     });

        var coords = new Array();
        for(var i=0; i < offers.length; i++) {
            var offer = offers[i];

            if(offer.GoogleMapLat !=0 && offer.GoogleMapLng != 0) {
                var latlng = new google.maps.LatLng(offer.GoogleMapLat, offer.GoogleMapLng);
                    coords.each(function(item) {
                        if(latlng.lat() == item.lat() && latlng.lng() == item.lng()) {
                            sign = Math.random() < 0.5 ? '-'  : '+' ;
                            var x = eval((Math.random() < 0.5 ? '-'  : '+')  + '0.01');
                            var y = eval((Math.random() < 0.5 ? '-'  : '+')  + '0.01');
                            // console.log(n);
                            latlng = new google.maps.LatLng(item.lat() + x, item.lng() + y);
                        }
                    });

                    coords[i] = latlng;

// var myOptions = {
// zoom: 7,
// center: latlng,
// mapTypeId: google.maps.MapTypeId.ROADMAP
// };

                    // if(i==0) map = new
					// google.maps.Map(document.getElementById("map_canvas"),
					// myOptions);

                    //map.setCenter(latlng);

    

                    // var image = '/images/google.png';
                    var contentString = '<a href="./o/'+offer.Id+'/'+offer.Name+'.html">'+offer.Name+'</a><br /><br />'+offer.Price+' &euro;<br />Temps de vol: '+offer.Duration+'<br /><br /><a href="#offers_list" onclick="updateOffers(\''+offer.Address+'\')">voir toutes les offres au m&ecirc;me endroit</a>';

                    if(offer.orange == true){
                    	var marker = new google.maps.Marker({
                            map: map,
                            position: latlng,
                            icon: image_url2,
                            html: contentString
                    	});

                    } else {
                    var marker = new google.maps.Marker({
                              map: map,
                              position: latlng,
                              icon: image_url,
                              html: contentString
                     });
            			}


                     var infowindow = new google.maps.InfoWindow({
                        // content: contentString
                    });

                    // http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/
                     google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent(this.html);
                        infowindow.open(map, this);
                    });
                     
                   



// geocoder.geocode( { 'address': address}, function(results, status) {
// var i = window.i;
// var offer = offers[i];
// window.i++;
//
// //console.log(this);
// if (status == google.maps.GeocoderStatus.OK) {
// } else {
// //alert("Geocode was not successful for the following reason: " + status);
// }
// });

            }
        }
}

function setLatLng(address)
{
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': address}, function(results, status) {
	    if (status == google.maps.GeocoderStatus.OK) {
	    	var latlng = results[0].geometry.location;
	    	$('offer_google_map_lat').value = latlng.lat();
	    	$('offer_google_map_lng').value = latlng.lng();
	    }
    });
}
