(function($) {
    $.fn.jQGmap = function(options) {
    	
    		var MarkerArray = Array();
    		var bounds = new google.maps.LatLngBounds();
    		
			var defaults = {
				MAPwidth: "600px",
				MAPheight: "450px",
				CenterLAT: "45.325667",
				CenterLNG: "6.537917",
				zoomBar: "small",
				type_map:"location"
			};
					
			var opts = $.extend(defaults, options);
			
			$(this).width(opts.MAPwidth);
			$(this).height(opts.MAPheight);
			
			var m = $(this)[0];
			var start = new google.maps.LatLng(opts.CenterLAT,opts.CenterLNG);
			var zoomLevel = 11;
			
			if(opts.zoomBar=='small')
			{
				var MyToolBar = 'SMALL';
			}
			else
			{
				var MyToolBar = 'ZOOM_PAN';
			}
			
			var baseOptions = { 
				zoom: zoomLevel, 
				center: start, 
				navigationControl: true, 
				navigationControlOptions: {style: google.maps.NavigationControlStyle.MyToolBar},
				mapTypeId: google.maps.MapTypeId.ROADMAP 
			}; 
			
			var map = new google.maps.Map(m, baseOptions);
			
			$("#AddMarkerV").click(function() {
				CreateClouds(MarkerArray, bounds, map, 'vente', $("#bien_zgeo").val(), $("#bien_wtype").val(), $("#budget").val());
			});
			
			$("#AddMarkerL").click(function() {
				if(document.getElementById('lot_crit1')) var lotCrit1 = $("#lot_crit1").val(); else var lotCrit1 = '';
				CreateClouds(MarkerArray, bounds, map, 'location', $("#type_lot").val(), $("#lot_crit2").val(), lotCrit1, $("#lot_pax").val());
			});
			
		return map;
	};	
})(jQuery);



function clearMarkers(markerTab) {
  if (markerTab) {
    for (i in markerTab) {
      markerTab[i].setMap(null);
    }
  }
}


function array_search(tab,val) {
	for (var i = 0; i < tab.length; i++) {
		if (tab[i] == val) {
			return true;
		}
	}
	return false;
}

function CreateClouds(MarkerArray, bounds, map, type_map, crit1, crit2, crit3, crit4)
{
	var MyPopup = new google.maps.InfoWindow({maxWidth:"200"});
	// Map Partie Vente
	if(type_map=='vente')
	{
		$.ajax({
			type: "POST",
			dataType: "xml",
			cache: "false",
			url: "/gmap.req.php?id=GmapT",
			data: {"bien_zgeo": crit1, "bien_wtype": crit2, "budget": crit3 },					
				
			beforeSend: function() {
				$("#mapLoading").show();
			},
			
			success: function(myData) {
				clearMarkers(MarkerArray);
				MarkerArray.length = 0;
	
				$(myData).find('bien').each(function() {
					var bien_gps_long = $(this).find('bien_gps_long').text();
					var bien_gps_latt = $(this).find('bien_gps_latt').text();
					var type_libelle = $(this).find('type_libelle').text();
					var zgeo_nom_ache = $(this).find('zgeo_nom_ache').text();
					var photo = $(this).find('photo').text();
					var icones = $(this).find('Icone').text();
					var LienFiche = $(this).find('LienFiche').text();
					var LienIntitule = $(this).find('LienIntitule').text();
					var photo_width = $(this).find('photo_width').text();
					var photo_height = $(this).find('photo_height').text();
					var bien_desc = $(this).find('bien_desc').text();
					var bien_prix = $(this).find('bien_prix').text();
					var myPoint = new google.maps.LatLng(bien_gps_latt,bien_gps_long);
	
					// Définition de la popup HTML
					var myHtml = "<div class='PopupGmap'>";
					
					myHtml += "<div class='GmapTitre'>"+type_libelle+" - "+zgeo_nom_ache+"</div>";
					if(photo!='') myHtml += '<p><a href="'+LienFiche+'" title="Consultez la ficher détaillée : '+type_libelle+'" target="_blank"><img src="'+photo+'" width="'+photo_width+'" height="'+photo_height+'" /></a></p>';
					myHtml += "<p style='font-size:1em;'><b>"+bien_prix+"</b></p>";
					myHtml += "<p style='font-size:0.90em;'>"+bien_desc+"</p>";
					myHtml += '<p style="font-size:0.90em;"><b><img src="/images/icones/zoom.png" class="icones" /><a href="'+LienFiche+'" title="Consultez la ficher détaillée : '+type_libelle+'" target="_blank">'+LienIntitule+'</a></b></p>';
					myHtml += "</div>";
	
					// Personnalisation des icones
					var myIcon = new google.maps.MarkerImage("/data/cms/icones_gmap/"+icones, new google.maps.Size(32, 24));		
	
					// Ajout du marker sur la map
					var marker = createMarker(map, myPoint, myHtml, myIcon, MyPopup, type_libelle);
					marker.setMap(map);
					MarkerArray.push(marker);
					bounds.extend(myPoint);
					return MarkerArray;
				});
			},
			complete: function() {
				$("#mapLoading").hide();
				map.fitBounds(bounds);
			}
		});	
	}
	// Map Partie Location
	if(type_map=='location')
	{
		$.ajax({
			type: "POST",
			dataType: "xml",
			cache: "false",
			url: "/gmap.req.php?id=GmapL",
			data: {"type_lot": crit1, "lot_crit2": crit2, "lot_crit1": crit3, "lot_pax": crit4 },					
				
			beforeSend: function() {
				$("#mapLoading").show();
			},
			
			success: function(myData) {
				clearMarkers(MarkerArray);
				MarkerArray.length = 0;
	
				$(myData).find('bien').each(function() {
					
					var bien_gps_long = $(this).find('bien_gps_long').text();
					var bien_gps_latt = $(this).find('bien_gps_latt').text();
					var crit1_libelle = $(this).find('crit1_libelle').text();			
					var crit2_libelle = $(this).find('crit2_libelle').text();				
					var type_lot = $(this).find('type_lot').text();
					var imme_nom = $(this).find('imme_nom').text();
					var lot_cp = $(this).find('lot_cp').text();
					var lot_ville = $(this).find('lot_ville').text();			
					var bien_desc = $(this).find('bien_desc').text();
					var photo = $(this).find('photo').text();
					var photo_width = $(this).find('photo_width').text();			
					var photo_height = $(this).find('photo_height').text();			
					var LienFiche = $(this).find('LienFiche').text();			
					var LienIntitule = $(this).find('LienIntitule').text();			
					var icones = $(this).find('Icone').text();			
					var myPoint = new google.maps.LatLng(bien_gps_latt,bien_gps_long);
	
					// Définition de la popup HTML
					var myHtml = "<div class='PopupGmap'>";
					
					myHtml += "<div class='GmapTitre'>"+imme_nom+" - "+type_lot+"</div>";
					if(photo!='') myHtml += '<p><a href="'+LienFiche+'" title="Consultez la ficher détaillée : '+crit2_libelle+'" target="_blank"><img src="'+photo+'" width="'+photo_width+'" height="'+photo_height+'" /></a></p>';
					myHtml += "<p style='font-size:0.90em;'>"+bien_desc+"</p>";
					myHtml += '<p style="font-size:0.90em;"><b><img src="/images/icones/zoom.png" class="icones" /><a href="'+LienFiche+'" title="Consultez la ficher détaillée : '+crit2_libelle+'" target="_blank">'+LienIntitule+'</a></b></p>';
					myHtml += "</div>";
	
					// Personnalisation des icones
					var myIcon = new google.maps.MarkerImage("/data/cms/icones_gmap/"+icones, new google.maps.Size(32, 24));		
	
					// Ajout du marker sur la map
					var marker = createMarker(map, myPoint, myHtml, myIcon, MyPopup, crit2_libelle);
					marker.setMap(map);
					MarkerArray.push(marker);
					bounds.extend(myPoint);
					return MarkerArray;
				});
			},
			complete: function() {
				$("#mapLoading").hide();
				map.fitBounds(bounds);
			}
		});	
	}
}

function getMap(MAPwidth, MAPheight, zoomBar, type_map)
{
	$(document).ready(function() {
		var map = $('#map').jQGmap({
			MAPwidth: ""+MAPwidth+"",
			MAPheight: ""+MAPheight+"",
			zoomBar: ""+zoomBar+"",
			type_map: ""+type_map+""
		});
		return map;	
	});
}

function createMarker(map, point, html, myicon,MyPopup, type_libelle) 
{
	var markerOptions = { 
		position: point,
		icon: myicon, 
		clickable: true, 
		title: type_libelle,
		optimized:false
	}; 
			
	var marker = new google.maps.Marker(markerOptions);
	
	if(html != null) 
	{
		google.maps.event.addListener(marker, 'click', function() {

			MyPopup.setContent(html);
			MyPopup.open(map,marker);
		});
		
		google.maps.event.addListener(marker, 'mouseover', function() {
			MyPopup.setContent(html);
			MyPopup.open(map,marker);
		});		
	}
	return marker; 
}


