$(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_bien    = $("#geo_bien").html().split('|');
	var geo_biens   = $("#geo_biens").html().split('|');

	// Récupération des coordonnées du bien sélectionné
	var geo_bien_lat     = geo_bien[0];
	var geo_bien_lng     = geo_bien[1];
	var geo_bien_zoom    = geo_bien[2];
	var geo_bien_type    = geo_bien[3];
	var geo_bien_texte   = geo_bien[4];

	// Récupération des coordonnées des biens aux alentours
	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++;
	}

	// 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_bien_type=='G_NORMAL_MAP') map.setMapType(G_NORMAL_MAP);
		if (geo_bien_type=='G_SATELLITE_MAP') map.setMapType(G_SATELLITE_MAP);
		if (geo_bien_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_bien_lat, geo_bien_lng), eval(geo_bien_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 du bien sélectionné
		var selextIcon = new GIcon();
		selextIcon.image = geo_url_img+"/puce-google-on.jpg";
		selextIcon.iconSize = new GSize(70, 20);
		selextIcon.iconAnchor = new GPoint(35, 20);
		selextIcon.infoWindowAnchor = new GPoint(35, 1);

		// 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);

		// Marquage du bien sélectionné
		var point = new GLatLng(geo_bien_lat, geo_bien_lng);
		var marker = new GMarker(point, { icon:selextIcon });
		map.addOverlay(createMarker(marker, geo_bien_texte));

		// Teste s'il existe d'autres points à afficher
		if (geo_biens!="") {
			// Traitement de tous les autres biens
			for (i=0; i<geo_biens_tableau.length; i++) {
				// Marquage du bien de la liste
				var point = new GLatLng(geo_biens_tableau[i]["geo_biens_lat"], geo_biens_tableau[i]["geo_biens_lng"]);
				var marker = new GMarker(point, { icon:unselextIcon });
				map.addOverlay(createMarker(marker, geo_biens_tableau[i]["geo_biens_texte"]));
			}
		}
	}

  // -------------------------------------------------
  // Fonctions pour la galerie photo
  // -------------------------------------------------
  $("#galerie > li").each(function(){
    $(this).hide();
  });
  $("#galerie li:first").show();
  timer = window.setTimeout(function() {defilement($("#galerie li:first"))},4000);
  
  $("#galerie > li").mouseover(function() {
    defilementStop();
  });

  $("#galerie > li").mouseout(function() {
    defilementStart();
  });
  
  $("#photos .precedent").click(function() {
    var ok=false;
    $("#galerie").find("li").each(function(i) {
      if (!ok && $(this).is(':visible')) {
        $(this).hide();
        if (i==0) {
          $("#galerie li:last").SlideInLeft(500);
        } else {
          $(this).prev().SlideInLeft(500);
        }
        ok=true;
      }
    });
  });

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

  // -------------------------------------------------
  // 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=640, height=480');
//    play_video($(this).children('span').eq(0).html());
//    $('#video').show();
  });

  // -------------------------------------------------
  // Fonctions pour afficher le formulaire de contact
  // -------------------------------------------------
  $('#contact').click(function() {
	  $('.deploiement').slideToggle("slow");
  });
});

function defilement(photo) {
  photo.hide();
  if (photo.next().html()==null) {
    $("#galerie li:first").SlideInRight(500);
    timer = window.setTimeout(function() {defilement($("#galerie li:first"))},4000);
  } else {
    photo.next().SlideInRight(500);
    timer = window.setTimeout(function() {defilement(photo.next())},4000);
  }
}
function defilementStop() {
	window.clearTimeout(timer);
}
function defilementStart() {
  $("#galerie").find("li").each(function(i) {
    if ($(this).is(':visible')) {
      var photo = $(this);
      timer = window.setTimeout(function() {defilement(photo)},500);
      return;
    }
  });
}

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);
    }
  });
}
