/**
 * Prototype pour le portail SDCH
 * 
 * @author Olivier
 * @version 2.2
 */

/**
 * Prototype des fonctions de la recherche par destination avec la carte gmaps
 */
var pSearchCarte = {

	isForm: false,
	
	markers: [],
	infowindow: null,

	init: function() {
		// Test si le navigateur est compatible
		if ($.browser.msie && parseInt($.browser.version) <= 6) {
			//alert('Nous sommes désolés, mais votre navigateur est incompatible avec notre carte intéractive.');
			$("#mapq").html('Nous sommes désolés, mais votre navigateur est incompatible avec notre carte intéractive.');
		} else {
			oMap = new google.maps.Map(document.getElementById("mapq"), {
	    		zoom: 3,
	    		center: new google.maps.LatLng(49.837982, 14.0625),
	    		mapTypeId: google.maps.MapTypeId.ROADMAP,
	    		panControl: false,
	    		mapTypeControl: false,
	    		rotateControl: false,
	    		zoomControl: true,
	    	    zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL },
	    		scaleControl: false,
	    		scrollwheel: false,
	    		streetViewControl: false
	    	});
			this.infowindow = new google.maps.InfoWindow();
			google.maps.event.addListener(oMap, "dragend", function() {
				pSearchCarte.isForm = false;
				zone = oMap.getBounds();
				sw = zone.getSouthWest();
				ne = zone.getNorthEast();
				$.getJSON("/home.ajax.php", {select: 'carte', code: $("#fType").val(), zoom: oMap.getZoom(), coord: sw.lat()+'x'+sw.lng()+'x'+ne.lat()+'x'+ne.lng()}, function (retour) {
					//alert(retour.length);
					pSearchCarte.deleteMarkers();
					for (var i = 0; i < retour.length; i++) {
						pSearchCarte.createMarker(retour[i].lat, retour[i].lon, retour[i].lib, retour[i].code);
					}
				});
			});
			google.maps.event.addListener(oMap, "zoom_changed", function() {
				if (pSearchCarte.isForm) return;
				zone = oMap.getBounds();
				sw = zone.getSouthWest();
				ne = zone.getNorthEast();
				$.getJSON("/home.ajax.php", {select: 'carte', code: $("#fType").val(), zoom: oMap.getZoom(), coord: sw.lat()+'x'+sw.lng()+'x'+ne.lat()+'x'+ne.lng()}, function (retour) {
					//alert(retour.length);
					pSearchCarte.deleteMarkers();
					for (var i = 0; i < retour.length; i++) {
						pSearchCarte.createMarker(retour[i].lat, retour[i].lon, retour[i].lib, retour[i].code);
					}
				});
			});
		}
	},

	changePays: function () {
		$.getJSON("/home.ajax.php", {select: 'pays', code: $("#fType").val(), subdiv: $("#fPays").val()}, function (retour) {
			var option = ''; //alert(retour);
			if (retour.region.length > 1) {
				for (var i = 0; i < retour.region.length; i++) {
					option+= '<option value="'+retour.region[i].val+'">'+retour.region[i].lib+'</option>';
				}
				$("#fRegion").attr("disabled", "");
				$("#fProvince").attr("disabled", "true"); $("#fProvince").html('<option value="">&nbsp;</option>');
				$("#fRegion").html(option);
			}
			pSearchCarte.moveMap(retour.latitude, retour.longitude, 5, retour.libelle, $("#fPays").val());
		});
	},

	changeRegion: function () {
		$.getJSON("/home.ajax.php", {select: 'region', code: $("#fType").val(), subdiv: $("#fRegion").val()}, function (retour) {
			var option = '';
			if (retour.province.length > 1) {
				for (var i = 0; i < retour.province.length; i++) {
					option+= '<option value="'+retour.province[i].val+'">'+retour.province[i].lib+'</option>';
				}
				$("#fProvince").attr("disabled", "");
				$("#fProvince").html(option);
			}
			pSearchCarte.moveMap(retour.latitude, retour.longitude, 7, retour.libelle, $("#fRegion").val());
		});
	},

	changeProvince: function () {
		$.getJSON("/home.ajax.php", {select: 'province', code: $("#fType").val(), subdiv: $("#fProvince").val()}, function (retour) {
			pSearchCarte.moveMap(retour.latitude, retour.longitude, 9, retour.libelle, $("#fProvince").val());
		});
	},

	// v3
	moveMap: function (latitude, longitude, zoom, libelle, subdiv) {
		pSearchCarte.isForm = true;
		oMap.setCenter(new google.maps.LatLng(latitude, longitude));
		oMap.setZoom(zoom);
		this.deleteMarkers();
		var marker = this.createMarker(latitude, longitude, libelle, subdiv);
		var content = pSearchCarte.getInfoWindows($("#fType").val(), latitude, longitude, libelle, subdiv);
		pSearchCarte.infowindow.setContent(content);
		pSearchCarte.infowindow.open(oMap,marker);
	},

	// v3
	zoomIn: function (latitude, longitude) {
		pSearchCarte.infowindow.oMap.close();
		oMap.setCenter(new google.maps.LatLng(latitude, longitude));
		oMap.setZoom(oMap.getZoom() + 2);
	},

	// v3
	createMarker: function (latitude, longitude, libelle, subdiv) {
		var marker = new google.maps.Marker({
		    position: new google.maps.LatLng(latitude, longitude),
		    map: oMap
		});
		pSearchCarte.markers.push(marker);
		google.maps.event.addListener(marker, "click", function() {
			var content = pSearchCarte.getInfoWindows($("#fType").val(), latitude, longitude, libelle, subdiv);
			pSearchCarte.infowindow.setContent(content);
			pSearchCarte.infowindow.open(oMap, marker);
		});
		return marker;
	},
	
	// v3
	deleteMarkers: function() {
		if (this.markers) {
			for (i in this.markers) {
				this.markers[i].setMap(null);
		    }
			this.markers.length = 0;
		}
	},

	getInfoWindows: function (type, latitude, longitude, libelle, subdiv) {
		var message = '<div class="gmaphome">Annonces <b>'+libelle+'</b><br/>';
		message+= '<a href="/carte.html?type='+type+'&subdiv='+subdiv+'">Visualisez toutes les annonces<br/>sur la carte en cliquant sur ce lien.</a>';
		if (oMap.getZoom() < 8)
			message+= '<br/><small>ou bien zoomez en double cliquant</small>';
			//message+= '<br/>ou bien <a href="javascript:pSearchCarte.zoomIn('+latitude+','+longitude+')">Zoomez</a>';
		message+= '</div>';
		return message;
	},

	submitForm: function () {
		// Validation du formulaire de la page d'accueil
		if ($("#fPays").val() == '') {
			alert('Veuillez sélectionner un pays.');
			return false;
		}
		return true;
	}

};
