var square_config = {
	stroke: {
		width: 2,
		color: "#e94ed1",
		colorloc: "#ff9000",
		opacity: 0.9
	},
	fill: {
		color: "#000000",
		opacity: 0.1
	}
};

var square_config_distances = Array()
square_config_distances[2] = 819.2;
square_config_distances[3] = 409.6;
square_config_distances[4] = 204.8;
square_config_distances[5] = 102.4;
square_config_distances[6] = 51.2;
square_config_distances[7] = 25.6;
square_config_distances[8] = 12.8;
square_config_distances[9] = 6.4;
square_config_distances[10] = 3.2;
square_config_distances[11] = 1.6;
square_config_distances[12] = 0.8;
square_config_distances[13] = 0.4;
square_config_distances[14] = 0.20;

var cachedSquarePoints = [];

function degToRad(deg) {
	return deg * Math.PI / 180;
}
function radToDeg(rad) {
	return rad * 180 / Math.PI;
}

function destinationPoint(lat, lng, distance, bearing) {
	var lat1 = degToRad(lat);
	var lng1 = degToRad(lng);
	distance = distance/6371;
	bearing = degToRad(bearing);

	var lat2 = Math.asin( Math.sin(lat1)*Math.cos(distance) + Math.cos(lat1)*Math.sin(distance)*Math.cos(bearing) );
	var lng2 = lng1 + Math.atan2(Math.sin(bearing)*Math.sin(distance)*Math.cos(lat1), Math.cos(distance)-Math.sin(lat1)*Math.sin(lat2));

	lng2 = (lng2+3*Math.PI)%(2*Math.PI) - Math.PI;

	if (isNaN(lat2) || isNaN(lng2)) {
		return false;
	}
	return {
		lat: radToDeg(lat2),
		lng: radToDeg(lng2)
	};
}

function drawSquare(center,distance,loc){
	var isloc = (loc === true)?true:false;
	var squarePoints = Array();
	var bearings = [45,135,225,315,45];
	with (Math) {

		if (typeof(cachedSquarePoints[center.lat()+"_"+center.lng()+"_"+distance]) == "undefined") {
			for(var i=0; i < bearings.length; i++) {

				var latlng = destinationPoint(center.lat(),center.lng(),distance,bearings[i]);
				var point = new google.maps.LatLng(latlng.lat,latlng.lng);

				squarePoints.push(point);


			}
			cachedSquarePoints[center.lat()+"_"+center.lng()+"_"+distance] = squarePoints;
		} else {
			squarePoints = cachedSquarePoints[center.lat()+"_"+center.lng()+"_"+distance];
		}

		var stroke_color = (isloc === true)?square_config.stroke.colorloc:square_config.stroke.color;
		square = new google.maps.Polygon(
						{
						paths : squarePoints, 
						strokeColor : stroke_color, 
						strokeWeight : square_config.stroke.width, 
						strokeOpacity : square_config.stroke.opacity, 
						fillColor : square_config.fill.color, 
						fillOpacity : square_config.fill.opacity
						}
					);
		return square;
	}
	return false;
}
	
function mapInitialize() {
		
		var myOptions = {
			zoom: 9,
			mapTypeId: google.maps.MapTypeId.ROADMAP,

			// Add controls
			mapTypeControl: true,
			scaleControl: true,
			maxZoom:14,
			scrollwheel:false,
			streetViewControl:false,
			overviewMapControl: true,
			overviewMapControlOptions: {
			  opened: true
			}
		};
	
		map = new google.maps.Map(document.getElementById('map_annonce'),myOptions);
		var bounds = new google.maps.LatLngBounds();
		var image = new google.maps.MarkerImage(debuturl+"img/site/gmap/picto_location_32.png");
		var shadow = new google.maps.MarkerImage(debuturl+"img/site/gmap/picto_shadow_32.png");
		var bbLatLng = new google.maps.LatLng(annonce_lat,annonce_lng);

		point = new google.maps.LatLng(annonce_lat,annonce_lng);
		map.setCenter(point, 15);

		locmarker = new google.maps.Marker({draggable: false, icon: image, shadow: shadow,  map:map, position: bbLatLng});	

		bounds.extend(bbLatLng);

		google.maps.event.addListener(locmarker, 'click', function() {
			map.setCenter(new google.maps.LatLng(annonce_lat,annonce_lng));
		});

		var infowindow = new google.maps.InfoWindow();

		for (var i=0; i<carto_cit.length; i++) {
			var item = carto_cit[i];
			item.point = new google.maps.LatLng(item.lat,item.lng);
			
			var baseIcon_image = new google.maps.MarkerImage(debuturl+"img/site/gmap/gmap_dotmarker_pink.png");
			var baseIcon_shadow = new google.maps.MarkerImage(debuturl+"img/site/gmap/gmap_dotmarker_shadow.png");

			var citIcon_image = new google.maps.MarkerImage(debuturl+"img/site/gmap/picto_cit_32.png");
			var citIcon_shadow = new google.maps.MarkerImage(debuturl+"img/site/gmap/picto_shadow_32.png");
			
			
			
			carto_cit[i].marker = new google.maps.Marker({draggable: false, icon: baseIcon_image, shadow: baseIcon_shadow,  map:map, position: item.point});	
			carto_cit[i].marker2 = new google.maps.Marker({draggable: false, icon: citIcon_image, shadow: citIcon_shadow,  map:null, position: item.point});	
			
			item.marker.data_id = "map_marker_"+i;

			var infoWindowHtml = "<table style='font-size:12px;'><tr><td><img src='"+debuturl+"img/site/gmap/picto_cit_32.png' /></td><td style='text-align:left;padding-left:10px;'><b>"+item.nom+"</b><br/>"+item.categorie+((item.distance_reelle != "0")?("<br/>"+item.distance_reelle):"")+"</td></tr></table>";
			carto_cit[i].infoWindowHtml = infoWindowHtml;

			google.maps.event.addListener(item.marker, 'click', function() {
				var index = this.data_id.substr(11);
				infowindow.setContent(carto_cit[index].infoWindowHtml);
				infowindow.open(map, carto_cit[index].marker);
				
			});
			google.maps.event.addListener(item.marker2, 'click', function() {
				var index = this.data_id.substr(11);
				infowindow.setContent(carto_cit[index].infoWindowHtml);
				infowindow.open(map, carto_cit[index].marker);
			});
			google.maps.event.addListener(item.marker, 'mouseover', function() {
				var index = this.data_id.substr(11);
				overLegendItem(index);
			});
			google.maps.event.addListener(item.marker, 'mouseout', function() {
				var index = this.data_id.substr(11);
				outLegendItem(index);
			});

			bounds.extend(item.point);
			
		}

		map.fitBounds(bounds);
		

		for (var i=0; i<carto_cit.length; i++) {
			var item = carto_cit[i];
			if (item.affichage == 1) {
				setTimeout(function() {
					overLegendItem(i);
					clickLegendItem(i);
				},700);
			}
		}

}

function overLegendItem(param) {
	
	for (var i=0; i<carto_cit.length; i++) {
		var item = carto_cit[i];
		if(item.over === true) {
			outLegendItem(i);
		}
	}

	var index = param;
	current_index = index;

	var marker = carto_cit[index].marker;
	var marker2 = carto_cit[index].marker2;

	carto_cit[index].over = true;

	var radius = square_config_distances[map.getZoom()];

	var circle = drawSquare(marker.getPosition(),radius);
	carto_cit[index].circle = circle;

	marker2.setMap(map) ;
	circle.setMap(map) ;
	
	/* Retrait des eventuelles légendes mises en avant */
	$('#googlemap #poi li').removeClass('actif');

	/* Mise en avant de la légende pour l'item surligné */
	$('#googlemap #poi #'+index).addClass('actif');
}

function outLegendItem(param) {
	var index = param;
	var marker = carto_cit[index].marker;
	var marker2 = carto_cit[index].marker2;
	var circle = carto_cit[index].circle;

	carto_cit[index].over = false;

	marker2.setMap(null) ;
	if (typeof(circle) != "undefined") {
		circle.setMap(null) ;
	}
	
	/* Retrait des eventuelles légendes mises en avant */
	$('#googlemap #poi li').removeClass('actif');

}

function clickLegendItem(param) {

	alert("oto");
	var index = (isNaN(param))?this.id.substr(11):param;
	var marker = carto_cit[index].marker;

	
	var marker_loc = locmarker;

	
	marker_loc.getPosition()
	var poi_bounds = new google.maps.LatLngBounds();
	poi_bounds.extend(marker_loc.getPosition() );
	poi_bounds.extend(marker.getPosition() );

	map.fitBounds(poi_bounds);

}
