
var origlat = 0;
var origlon = 0;

var infoOpen;
var currentcat;
var markers=[];
var map;
var poiType = 1;
var sMaxZoom = 18;

window.onload = function()
{
	mainOnLoad();
	if($('showMap').value == "1" && GBrowserIsCompatible())
	{
		$('msgPI').hide();
		origlon = parseFloat($('lon').value);
		origlat = parseFloat($('lat').value);

		initMap_situation(origlat, origlon);
	}
	else addZipAutoCompletion('city', null, 1);
}

function changeType(newType)
{
	poiType = newType;
	refreshPoints();
	if(poiType != "1")
	{
		$('tdpoi').hide();
		//$('poiListDiv').hide();
		sMaxZoom = 14;
		//new Effect.BlindUp('poiListDiv');
	}
	else
	{
		$('tdpoi').show();
		//$('poiListDiv').show();
		sMaxZoom = 18;
		//new Effect.BlindDown('poiListDiv');
	}

	if(map.getZoom() > sMaxZoom) map.setZoom(sMaxZoom);
}

function changeLonLat(lonlat)
{
	var tab = lonlat.split(";");
	$('lon').value = tab[0];
	$('lat').value = tab[1];
}

function initMap_situation(lat, lng, LimitZoom)
{
	icon = new GIcon();
	icon.image = "images/icons/test.png";
	icon.iconSize = new GSize(24, 24);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);

	map = new GMap2(document.getElementById("map"),{mapTypes : [G_HYBRID_MAP,G_SATELLITE_MAP,G_NORMAL_MAP]});
	map.addControl(new GSmallMapControl());
	map.continuousZoomEnabled();
	map.enableContinuousZoom();
	map.enableDoubleClickZoom();
	map.addControl(new GMapTypeControl());

	//map.addControl(new GOverviewMapControl());

	startZoom = 14 ;

	map.setCenter(new GLatLng(lat, lng), startZoom);

	origlat=lat;
	origlon=lng;

	iconOverlay=new GMarker(new GLatLng(origlat, origlon), icon);
	map.addOverlay(iconOverlay);


	GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
		if(newLevel > sMaxZoom) map.setZoom(oldLevel);
		else map.setZoom(newLevel);
	});

	GEvent.addListener(map, "click", function(overlay, point) {
		if ((overlay)&&(overlay.infowindow))
		{
				overlay.openInfoWindowHtml(overlay.infowindow);
				currentOverlay=true;
				overlay.opened=true;
		}
	});

	GEvent.addListener(map, "infowindowopen", function() {
		infoOpen=true;
	});

	GEvent.addListener(map, "infowindowclose", function() {
		if (infoOpen)
		{
			currentOverlay=false;
				infoOpen=false;
				refreshPoints();
		}
	});

	GEvent.addListener(map, "moveend", function() {
		refreshPoints();
	});

	infoOpen=false;
	currentcat=-1;
	refreshPoints();
	currentOverlay=false;


	if (($('pizip'))&&($('picity'))) addZipAutoCompletion('pizip', 'picity');
}

function showWaitMessage() {
	var message_div = document.getElementById("message_div");
	var google_map = document.getElementById("map");
	if (message_div != null && google_map != null) {
		message_div.innerHTML = "<img src=\"images/loading.gif\"/> Veuillez patienter";
		google_map.appendChild(message_div);
		message_div.style.zIndex = parseInt(google_map.style.zIndex + 1);
		message_div.style.position = 'absolute';
		message_div.style.top = ((google_map.offsetHeight - message_div.offsetHeight) / 2) + "px";
		message_div.style.left = ((google_map.offsetWidth - message_div.offsetWidth) / 2) + "px";
		message_div.style.visibility = 'visible';
	}
}
function hideWaitMessage() {
	var message_div = document.getElementById("message_div");
	if (message_div != null) {
		message_div.style.zIndex = 0;
		message_div.style.visibility = 'hidden';
	}
}

function changeCateg()
{
	map.closeInfoWindow();
	infoOpen=false;
	refreshPoints();
}

function refreshPoints()
{
	currentcat = document.getElementById('idpicat').value;
	if (infoOpen==false)
	{
			showWaitMessage();

		var bounds = map.getBounds();
		//$('debug').innerHTML = '?ajax&type='+poiType+'&idpicat='+currentcat+'&minlat='+bounds.getSouthWest().lat()+'&minlon='+bounds.getSouthWest().lng()+'&maxlat='+bounds.getNorthEast().lat()+'&maxlon='+bounds.getNorthEast().lng()+'&lat='+origlat+'&lon='+origlon;
		var params = {
				parameters: '?ajax&type='+poiType+'&idpicat='+currentcat+'&minlat='+bounds.getSouthWest().lat()+'&minlon='+bounds.getSouthWest().lng()+'&maxlat='+bounds.getNorthEast().lat()+'&maxlon='+bounds.getNorthEast().lng()+'&lat='+origlat+'&lon='+origlon,
					asynchronous: true,
					onSuccess: function(request, json)
					{
						//alert(request.responseText);
						json = eval('(' + request.responseText + ')');
						if (json.errormsg)
							document.getElementById('msg').innerHTML=json.errormsg;
						else
							document.getElementById('msg').innerHTML='';

						document.getElementById('nbr').innerHTML=json.pois.length;

					for (var i=0; i<markers.length; i++) if (currentOverlay!=markers[i]) map.removeOverlay(markers[i]);
						markers = [];

						if(json.pois.length > 0)
						for(var i=0; i<json.pois.length; i++)
						{
							if(poiType == "1")
							{
								var html = "<p><strong>" + json.pois[i].label + "</strong></p><p>Distance par rapport au bien: <b>"+Math.round(json.pois[i].dist*10)/10+"km</b></p><p style='color: #0000FF;'>" + json.pois[i].tel+"</p><br /><p>" + json.pois[i].address1+"</p><p>" + json.pois[i].address2+"</p>";
								icon2 = new GIcon();
								icon2.image = "images/icons/pi.png";
								icon2.iconSize = new GSize(15, 22);
								icon2.shadowSize = new GSize(22, 20);
								icon2.iconAnchor = new GPoint(6, 20);
								icon2.infoWindowAnchor = new GPoint(5, 1);
							}
							else
							{
								if(poiType == 2)
								{
									icon2 = new GIcon();
									icon2.image = "images/icons/alouer.png";
									icon2.iconSize = new GSize(15, 15);
									icon2.iconAnchor = new GPoint(6, 20);
									icon2.infoWindowAnchor = new GPoint(5, 1);
								}
								else
								{
									icon2 = new GIcon();
									icon2.image = "images/icons/avendre.png";
									icon2.iconSize = new GSize(15, 15);
									icon2.iconAnchor = new GPoint(6, 20);
									icon2.infoWindowAnchor = new GPoint(5, 1);
								}

								var html = "<table class=\"infoWindow\"><tr>";
								html += "<td><a href=\"view.php?id="+json.pois[i].id+"\" target=\"_blank\"><img src=\""+json.pois[i].imagePath+"\" height=\"75\"/></a>&nbsp;</td>";
								html += "<td><b>"+json.pois[i].propertyType+"</b><br />"+json.pois[i].mandate+"<br />"+json.pois[i].city+"<br /><b>"+json.pois[i].price+" &euro;</b></td>";

    html += "<td><a href=\"view.php?id="+json.pois[i].id+"\" target=\"_blank\"><img src=\"images/template/images/bt_voir_fr.jpg\" /></a></td>";
								html += "</tr></table>";
							}

							markers[i] = new GMarker(new GLatLng(parseFloat(json.pois[i].lat), parseFloat(json.pois[i].lon)), icon2);
							markers[i].infowindow = html;
							markers[i].opened=false;
							map.addOverlay(markers[i]);
						}
						hideWaitMessage();
					}
			};
			new Ajax.Request('?ajax', params);
	}
}

function showHideMsg()
{
	if($('msgPI').visible())
	{
		new Effect.BlindUp('msgPI');
	}
	else
	{
		new Effect.BlindDown('msgPI');
	}
}

function showHideSendToFriend()
{
	if($('sendToFriend').visible())
	{
		new Effect.BlindUp('sendToFriend');
	}
	else
	{
		new Effect.BlindDown('sendToFriend');
	}
}

function showHidePTypeMsg()
{
	if($('ptype1').checked==true)
	{
		new Effect.BlindDown ('terror');
		new Effect.BlindUp('tmissing');
	} else if ($('ptype2').checked==true)
	{
		new Effect.BlindUp('terror');
		new Effect.BlindDown('tmissing');
	}
}

function checkPIMsg()
{
	if (($('ptype1').checked==false)&&($('ptype2').checked==false))
	{
		alert('Veuillez choisir un type de problème');
		return false;
	}
	if ($('name2').value == "")
	{
		alert('Veuillez entrer votre nom');
		return false;
	}
	return true;
}

function setSearchType(num)
{
	for(var i=1; i<5; i++)
	{
		if(i != num) $('search' + i).hide();
		else $('search' + i).show();
	}
}