//Dclaration de variables globale pour qu'elles soient visibles dans toutes les fonctions
var map = null;
var geocoder = null;
var marker = null;

	  


//Charge la google map
function loadMap(user) {
	
	if (GBrowserIsCompatible()) {
		
		var zoom = 1;
		var latlng = new google.maps.LatLng(30, -3);

		if (google.loader.ClientLocation) {
		  zoom = 7;
		  latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);

		}

		//alert(google.loader.ClientLocation.address.country_code);
		map = new GMap2(document.getElementById("map-search"), [G_SATELLITE_MAP,G_NORMAL_MAP,G_HYBRID_MAP]);
      	map.setCenter(latlng, zoom, G_HYBRID_MAP);
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new ZoomControlImg()); //Control de la map (zoom, type de map
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		map.savePosition();
		map.enableDragging();
	
		geocoder = new GClientGeocoder();
		
		if(user > 0) { //Visualise la page d'un user donc on veut voir tous ses albums et pas seulement les albums upload il y a 3 jours
			kml = new GGeoXml("http://www.shootsurf.com/searchKml.php?&id="+user+"&s=oui&p=oui&dateMin=1&dateMax=100&az=510");
		}
		else if (user ==-1) {
			//Charge tous les albums (dans les 3jours prcdents)
			kml = new GGeoXml("http://www.shootsurf.com/searchKml.php?s=oui&p=oui&id=-1&az=510&dateMin=-1");
		}
		else if (user ==-2) {
			//Charge tous les albums (Aujourd'hui)
			kml = new GGeoXml("http://www.shootsurf.com/searchKml.php?s=oui&p=oui&id=-2&az=510&dateMin=-2");
		}
		else if (user ==-3) {
			//Charge tous les albums (Hier)
			kml = new GGeoXml("http://www.shootsurf.com/searchKml.php?s=oui&p=oui&id=-3&az=510&dateMin=-3");
		}
		else if (user ==-4) {
			//Charge tous les albums (Avant-Hier)
			kml = new GGeoXml("http://www.shootsurf.com/searchKml.php?s=oui&p=oui&id=-4&az=510&dateMin=-4");
		}
		//Charger le fichier Kml sur la Map

    	map.addOverlay(kml);

	}
}

function getFormattedLocation() {
if (google.loader.ClientLocation.address.country_code == "US" &&
  google.loader.ClientLocation.address.region) {
  return google.loader.ClientLocation.address.city + ", " 
	  + google.loader.ClientLocation.address.region.toUpperCase();
} else {
  return  google.loader.ClientLocation.address.city + ", "
	  + google.loader.ClientLocation.address.country_code;
}
}


var typeAlbum = new Array('oui', 'oui');
function getTypeAlbumSelection(type) {
	var checkSurfeur = document.getElementById('checkSurfeur');
	var checkPhotographe = document.getElementById('checkPhotographe');

	if(type == 'surfeur') {
		if(checkSurfeur.checked) {
			typeAlbum[0] = 'oui';
		}
		else {
			typeAlbum[0] = 'non';
		}	
	}
	
	if(type == 'photographe') {
		if(checkPhotographe.checked) {
			typeAlbum[1] = 'oui';
		}
		else {
			typeAlbum[1] = 'non';
		}
	}
	
	//Mettre toujours coché Surfeur et Photographe
	typeAlbum[0] = 'oui';
	typeAlbum[1] = 'oui';
	return typeAlbum;
}

function changerTypeAlbum(type) {
	var tab = getTypeAlbumSelection(type);
	var dateMin = $("#slider").slider("value", 0);
	var dateMax = $("#slider").slider("value", 1);
	
	//Pas de variable user car le changement d'album n'est pas authorise quand on visualise la liste des albums d'un user
	var kml = new GGeoXml("http://www.shootsurf.com/searchKml.php?s="+tab[0]+"&p="+tab[1]+"&dateMin="+dateMin+"&dateMax="+dateMax);
  	map.clearOverlays(kml);
  	map.addOverlay(kml);
}


function showAddress(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (point) {
        map.setCenter(point, 12);
      }
    }
  );
}

function changeDate(dateMin, dateMax, user) {
	var tab = getTypeAlbumSelection();
	var kml = new GGeoXml("http://www.shootsurf.com/searchKml.php?&id="+user+"&s="+tab[0]+"&p="+tab[1]+"&dateMin="+dateMin+"&dateMax="+dateMax);
  	map.clearOverlays(kml);
  	map.addOverlay(kml);
}

function reCentrerMap(point) {
	var zoom; //valeur du nouveau zoom
	if(map.getZoom() <= 2) {
		zoom=8;
	}
	else {
		zoom = map.getZoom() + 3;
	}
	map.setCenter(point, zoom);
}

