$(document).ready(function() {
	// -------------------------------------------------
	// Fonctions pour la googlemap
	// -------------------------------------------------

	// Récupération des paramètres de la googlemap
	var geo_url_img     = $("#geo_url_img").html();
	var geo_region      = $("#geo_region").html().split('|');
	var geo_biens       = $("#geo_biens").html().split('|');
	var geo_biens_total = $("#geo_biens_total").html().split('|');

	// Récupération des coordonnées du bien sélectionné
	var geo_region_lat  = geo_region[0];
	var geo_region_lng  = geo_region[1];
	var geo_region_zoom = geo_region[2];
	var geo_region_type = geo_region[3];
  
	// Récupération des coordonnées des biens à afficher
	var geo_biens_tableau = new Array();
	var cpt=0;
	for (i=0; i<geo_biens.length; i=i+3) {
		geo_biens_tableau[cpt] = new Array();
		geo_biens_tableau[cpt]["geo_biens_lat"]   = geo_biens[i];
		geo_biens_tableau[cpt]["geo_biens_lng"]   = geo_biens[i+1];
		geo_biens_tableau[cpt]["geo_biens_texte"] = geo_biens[i+2];
		cpt++;
	}

	// Récupération des coordonnées des biens aux alentours
	var geo_biens_tableau_total = new Array();
	var cpt=0;
	for (i=0; i<geo_biens_total.length; i=i+3) {
		geo_biens_tableau_total[cpt] = new Array();
		geo_biens_tableau_total[cpt]["geo_biens_lat"]   = geo_biens_total[i];
		geo_biens_tableau_total[cpt]["geo_biens_lng"]   = geo_biens_total[i+1];
		geo_biens_tableau_total[cpt]["geo_biens_texte"] = geo_biens_total[i+2];
		cpt++;
	}

	// Création des points sur la googlemap
	if (GBrowserIsCompatible()) {
		// Récupération du div contenant la googlemap
		var map = new GMap2(document.getElementById("googlemap"));
		if (geo_region_type=='G_NORMAL_MAP') map.setMapType(G_NORMAL_MAP);
		if (geo_region_type=='G_SATELLITE_MAP') map.setMapType(G_SATELLITE_MAP);
		if (geo_region_type=='G_HYBRID_MAP') map.setMapType(G_HYBRID_MAP);
		// Ajout des barres de navigation à la googlemap
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		// Centrage de la googlemap sur le bien sélectionné
		map.setCenter(new GLatLng(geo_region_lat, geo_region_lng), eval(geo_region_zoom));

		// Fonction de création d'un point de marquage
		function createMarker(marker, texte) {
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(texte);
			});
			return marker;
		}

		// Définition du point de marquage des autres biens
		var unselextIcon = new GIcon();
		unselextIcon.image = geo_url_img+"/puce-google-off.jpg";
		unselextIcon.iconSize = new GSize(70, 20);
		unselextIcon.iconAnchor = new GPoint(35, 20);
		unselextIcon.infoWindowAnchor = new GPoint(35, 1);

		// Traitement de tous les biens
		for (i=0; i<geo_biens_tableau.length; i++) {
			// Marquage du bien sélectionné
			var point = new GLatLng(geo_biens_tableau[i]["geo_biens_lat"], geo_biens_tableau[i]["geo_biens_lng"]);
			markerOptions = { icon:unselextIcon };
			var marker = new GMarker(point, markerOptions);
			map.addOverlay(createMarker(marker, geo_biens_tableau[i]["geo_biens_texte"]));
		}

		// Initialisation du marker manager
		var mgr = new MarkerManager(map);
		// Traitement de tous les autres biens
		for (i=0; i<geo_biens_tableau_total.length; i++) {
			// Ajout du bien au marker manager
			var point = new GLatLng(geo_biens_tableau_total[i]["geo_biens_lat"], geo_biens_tableau_total[i]["geo_biens_lng"]);
			var marker = new GMarker(point, { icon:unselextIcon });
			mgr.addMarker(createMarker(marker, geo_biens_tableau_total[i]["geo_biens_texte"]), 10);
		}
		// Affichage des biens du marker manager
		mgr.refresh();
	}

  // -------------------------------------------------
  // Fonctions pour la galerie photo
  // -------------------------------------------------
  $("#galerie > li").each(function(){
    $(this).hide();
  });
  $("#galerie li:first").show();
  
  $("#photos .precedent").click(function() {
    var ok=false;
    $("#galerie").find("li").each(function(i) {
       if (!ok && $(this).is(':visible')) {
//       if ($(this).is(':visible')) {
         $(this).hide();
         if (i==0) {
           $("#galerie li:last").SlideInLeft(500);
         } else {
           $(this).prev().SlideInLeft(500);
         }
         ok=true;
//         return true;
       }
    });
  });

  $("#photos .suivant").click(function() {
    var ok=false;
    $("#galerie").find("li").each(function(i) {
       if (!ok && $(this).is(':visible')) {
//       if ($(this).is(':visible')) {
         $(this).hide();
         if ((i+1)==$("#galerie > li").size()) {
           $("#galerie li:first").SlideInRight(500);
         } else {
           $(this).next().SlideInRight(500);
         }
         ok=true;
//         return true;
       }
    });
  });

  // -------------------------------------------------
  // Fonctions pour jouer les vidéos
  // -------------------------------------------------
  $('.playvideo').click(function() {
    window.open('play.php?videos_fichier='+$(this).children('span').eq(0).html(),'video','menubar=no, status=no, scrollbars=no, width=320, height=240');
//    play_video($(this).children('span').eq(0).html());
//    $('#video').show();
  });

  // -------------------------------------------------
  // Fonctions pour afficher le formulaire de contact
  // -------------------------------------------------
  $('.contact').click(function() {
	  var id= $(this).children('span').html();
		$('.deploiement'+id).slideToggle("slow");
  });
});

function play_video (fichier) {
  $.ajax({
    type: "GET",
    url: "play.php",
    data: "videos_fichier="+fichier,
    error:function(msg){
      alert("Erreur durant la requete");
    },
    success:function(data){
      //affiche le contenu du fichier dans le conteneur dédié
      $('#video').empty();
      $('#video').append(data);
    }
  });
}
