/* Animation contact */
var lang         = 'fr';
var base_href    = '/';
var timer = '';
var diapo_img_current = 0;
var newTop_diapo_on  = 187;
var newTop_diapo_off = 222;
var newTop_act_on  = 298;
var newTop_act_off = 333;
var newTop_fct_on  = 0;
var newTop_fct_off = 0;
var current_acces = 1
var anim_encours = 0;

function initialize(lat,lng,contenu_bulle) {

	if (GBrowserIsCompatible()) {
      // define the crosshair tile layer and its required functions
      var crossLayer = new GTileLayer(new GCopyrightCollection(""), 0, 15);
      crossLayer.getTileUrl =  function(tile, zoom) {
        return "./include/tile_crosshairs.png";
      }
      crossLayer.isPng = function() {return true;}

      // Create a new map type incorporating the tile layer
      var layerTerCross = [ G_PHYSICAL_MAP.getTileLayers()[0],
                            crossLayer ];
      var mtTerCross = new GMapType(layerTerCross,
                                    G_PHYSICAL_MAP.getProjection(), "Ter+");

      var map = new GMap2(document.getElementById("carte"),
          { size: new GSize(525,510) } );
      map.addMapType(G_PHYSICAL_MAP);
	  map.setMapType(G_SATELLITE_MAP);
      map.addMapType(mtTerCross);
	  var point = new GLatLng(lat, lng);
      map.setCenter(point, 16);
	  var marker = new GMarker(point);
	  map.addOverlay(marker);

	  marker.openInfoWindowHtml(contenu_bulle);
      map.addControl(new GLargeMapControl())

      var mapControl = new GHierarchicalMapTypeControl();

      // Set up map type menu relationships
      mapControl.clearRelationships();
      mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
      mapControl.addRelationship(G_PHYSICAL_MAP, mtTerCross, "Crosshairs");

      // Add control after you've specified the relationships
      map.addControl(mapControl);

    }
}

function envoyer(id,ch_nom,ch_email,ch_objet){

	if(ById("anti_spam").value == 'no-spam' ){
		var erreur = '';
		if(ById("nom").value == '' || !ById("nom")){
			erreur += ch_nom;
		}
		if(ById("email").value == '' || !ById("email")){
			if(erreur != ''){
				erreur += ', ';
			}
			erreur += ch_email;

		}else{
			if(!verifEmail(ById("email").value)){
				if(erreur != ''){
					erreur += ', ';
				}
				erreur += ch_email;
			}
		}
		if(ById("objet").value == '' || !ById("objet")){
			if(erreur != ''){
				erreur += ', ';
			}
			erreur += ch_objet;
		}
		if(erreur != ''){
			$("#champ_erreur").html(erreur);
			$("#form-error").fadeIn({ duration: 200, easing: 'easeOutQuad', complete:function(){
				$("#form-error").effect("bounce", { times:3,direction:'left',distance:30 }, 300);
			}});
		}else{
			ById(id).submit();
		}
	}else{
		alert('spam');
	}
}

function ById(id){
	return document.getElementById(id);
}

function verifEmail(email) {
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
   return (reg.exec(email)!=null);
}

	function showById (id) {

		if(timer){
			window.clearTimeout(timer);
		}
		timer = window.setTimeout(function(){
			if($("#ss_menu_"+id).length>0){
				$("#a"+id).css('background-color','#ea2c7f');
			}
			if($("#ss_menu_"+currentMenu).length>0){
				$("#a"+currentMenu).css('background-color','#fd3f92');
			}
			if (animEnCours == 0) {
				if($("#sous_menu").width() == 0 || $("#sous_menu").css('display') == 'none' ){

					$("#sous_menu").width(0);
					$("#sous_menu").css('display','block');
					if($("#ss_menu_"+id).length > 0){
						$("#sous_menu").animate({"width":"210px"}, {duration: 200, easing: 'easeOutElastic', complete: function(){
							fadeMenu(id);
						}});
					}
				}else{
					if(currentMenu > 0 && $("#ss_menu_"+currentMenu).length>0){
						$("#ss_menu_"+currentMenu).fadeOut({ duration: 75, complete:function(){
							fadeMenu(id);
						}});
					}else{
						fadeMenu(id);
					}
				}
				if($("#ss_menu_"+id).length > 0){
				//animEnCours = animEnCours + 1;
				}
			}else {
				return false;
			}
		},250);
	}

	function fadeMenu (id) {

		if ($("#ss_menu_"+id).length > 0) {
			$("#ss_menu_"+id).fadeIn({ duration: 100, easing: 'easeOutQuad', complete:function(){
				currentMenu    = id;
				animEnCours    = 0;

			}});
		}else {
			$("#ss_menu_"+currentMenu).fadeOut({ duration: 75, complete:function(){
				currentMenu = 0;
				$("#sous_menu").animate({"width":"0px"}, {duration: 200, easing: 'easeInBack', complete: function(){
					animEnCours    = 0;
				}});
			}});
			return false;
		}
	}

	function nextImg(taille,nb,newTop_off,newTop_on){
		if(nb>1){
			newTop_fct_on  = newTop_on;
			newTop_fct_off = newTop_off;

			var maxLeft = -(nb-1)*taille;
			var timer_diapo = setTimeout(function(){

				$("#diapo"+diapo_img_current).animate({"top": newTop_fct_off+"px"}, {duration: 300, easing: 'easeInBack', complete: function(){

					diapo_img_current = diapo_img_current +1;
					var newLeft = -(diapo_img_current-1)*taille;
					if(maxLeft>newLeft){
						$("#images").fadeOut('300',function(){
							diapo_img_current = 1;
							$("#images").css('left',0);
							$("#images").fadeIn('300',function(){
								$("#diapo"+diapo_img_current).animate({"top": newTop_fct_on+"px"}, {duration: 300, easing: 'easeInBack', complete: function(){
									nextImg(taille,nb,newTop_fct_off,newTop_fct_on);
								}});
							});
						});
					}else{

						$("#images").animate({"left": newLeft+"px"}, {duration: 300, easing: 'easeInBack', complete: function(){
							$("#diapo"+diapo_img_current).animate({"top": newTop_fct_on+"px"}, {duration: 300, easing: 'easeInBack', complete: function(){
								nextImg(taille,nb,newTop_fct_off,newTop_fct_on);
							}});
						}});
					}
				}});

			},5000);
		}
	}

	function stopInfoImgDiapo(idBloc){
		$("#diapo"+idBloc).stop(true,true);
	}

$(document).ready(function () {

	//initialisation base href
	if($("#base_href").val() != ''){
		base_href = $("#base_href").val();
	}
	//initialisation langue
	if($("#langue").val() != ''){
		lang = $("#langue").val();
	}

	//diaporama
	if($("#diaporama").length>0){
		$.get(base_href+"include/"+lang+"/images_diaporama.php?id_piece="+$("#id_piece").val()+"&nocache="+Math.random(), function(data, textStatus){
			if(textStatus == 'success'){

				$("#load").fadeOut('500',function(){
					$("#diaporama").html(data);
					diapo_img_current = 1;
					setTimeout(function(){
						$("#images").fadeIn('800',function(){
							$("#images").width($("#width_img").val()*$("#nb_img_diapo").val());
							$("#diapo"+diapo_img_current).animate({"top": newTop_diapo_on+"px"}, {duration: 300, easing: 'easeInBack', complete: function(){
								nextImg($("#width_img").val(),$("#nb_img_diapo").val(),newTop_diapo_off,newTop_diapo_on);
							}});
						});
					},1000);
				});
			}
		});
	}
	if($("#diaporama_activites").length>0){
		$.get(base_href+"include/"+lang+"/images_activites.php?nocache="+Math.random(), function(data, textStatus){
			if(textStatus == 'success'){

				$("#load").fadeOut('500',function(){
					$("#diaporama_activites").html(data);
					diapo_img_current = 1;
					setTimeout(function(){
						$("#images").fadeIn('800',function(){
							$("#images").width($("#width_img").val()*$("#nb_img_diapo").val());
							$("#diapo"+diapo_img_current).animate({"top": newTop_act_on+"px"}, {duration: 300, easing: 'easeInBack', complete: function(){
								nextImg($("#width_img").val(),$("#nb_img_diapo").val(),newTop_act_off,newTop_act_on);
							}});
						});
					},1000);
				});
			}
		});
	}
	if($("#diaporama_gite").length>0){
		$.get(base_href+"include/"+lang+"/images_gite.php?id_piece="+$("#id_piece").val()+"&nocache="+Math.random(), function(data, textStatus){
			if(textStatus == 'success'){

				$("#load").fadeOut('500',function(){
					$("#diaporama_gite").html(data);
					diapo_img_current = 1;
					setTimeout(function(){
						$("#images").fadeIn('800',function(){
							$("#images").width($("#width_img").val()*$("#nb_img_diapo").val());
							$("#diapo"+diapo_img_current).animate({"top": newTop_act_on+"px"}, {duration: 300, easing: 'easeInBack', complete: function(){
								nextImg($("#width_img").val(),$("#nb_img_diapo").val(),newTop_act_off,newTop_act_on);
							}});
						});
					},1000);
				});
			}
		});
	}
});

