/*
  --------------
  Module Cartographie -- v 0.2 -- Copyright Altimax F.R
  --------------
*/


/*
  --------------
  VARIABLES
  --------------
*/
var jQ  = jQuery.noConflict();
var url_carto = "";
var map1;
var ZOOM_MAX = 12;
var ZOOM_MIN = 6;
var aMarkers  = new Array();
var bChoixVisite = false;  //variable qui dit si on choisit ou pas d'afficher uniquement les producteurs qui font visiter la ferme
var bChoixVente = false;  //variable qui dit si on choisit ou pas d'afficher uniquement les producteurs qui vendent a la ferme
function Config () {
  this.lat  = 0;
  this.lng  = 0;
  this.zoom = 2;
  this.kml  = "hs.kml";
}
var cConfig = new Config();


/*
  --------------
  FONCTIONS D'INITIALISATION
  --------------
*/
                   
//initialisation de l'interface et des variables en fonction de l'itineraire, puis initialisation de la carte
function init_interface() {
  loading(true);
  jQ.ajax({
           type: "GET",
           url: url_carto + "frontend/ajax/donnees_itineraire.ajax.php",
           data: "",
           dataType:"script",
           success: function(msg){//le résultat de cet appel est la configuration des coordonnées de l'itinéraire set_config
           
          },
          error:  function() {
            alert("Erreur dans l'initialisation du module de Cartographie");
          }
  });
}//init_interface

function set_config(plat,plng,pzoom,pkml) {//initialisation de l'objet de configuration
  cConfig.lat = plat;
  cConfig.lng = plng;
  cConfig.zoom  = pzoom;
  cConfig.kml = pkml;
  var bGmap = init_gmap();  //initialisation de la carte
  if(bGmap) {
    var bMenu = init_menu_itineraire(); //initialisation du click sur les menus, si la carte s'est initialisée
    if(bMenu) {
      generation_markers();
      loading(false);  
    }
  }
  
}//set_config

//Initialisation du menu propre à l'itinéraire
function init_menu_itineraire() {
  
    /*
    //Comportements des panneaux
    jQ("#panelProfil").toggle(function(){jQ("#panelProfil").animate({left: -getWidth}, 0);}, function(){jQ("#panelProfil").animate({left: +getWidth}, 0);});
      //survol     
      jQ("#btnCacherPanelProfil").hover(function () {
          jQ("#btnCacherPanelProfil").css("background-image","url("+url_carto+"frontend/img/btnProfil_hover.jpg)");
        },function () {
          jQ("#btnCacherPanelProfil").css("background-image","url("+url_carto+"frontend/img/btnProfil.jpg)");
      });
      
    
    //click
    jQ("#btnCacherPanelProfil").click(function () {
         jQ("#panelProfil").toggle(750); 
    });

*/
   
   

  return true;
}//init_menu_itineraire

function generation_markers() {
  loading(true,'...');
  removeMarkers();

  jQ.ajax({
           type: "GET",
           url: url_carto + "frontend/generation-markers/markers_zones.php",
           data: "rand="  + (Math.random()*(50*Math.random())),
           dataType:"script",
           success: function(msg){
           loading(false);
          }
  });
}

//Fonction d'affichage de la div de chargement
function loading(bDisplay,texte) {
  if(texte  !=  '' && texte != null) {
    jQ("#texte_loading").html(texte);
  }
	divLoading = document.getElementById("loading");
	if(bDisplay) {
		jQ("#loading").fadeIn("fast");

	}else {
	  jQ("#loading").fadeOut("fast");
	}
}

/*
  Fonctions utiles pour la carte gMap
*/


//initialisation de la gmap
function init_gmap()  {
	jQ('containerDesc').html('');

  jQ("#panelProfil").css("display","none");		
   if (GBrowserIsCompatible()) {
  	 map = new GMap2(document.getElementById('map_carto'));
  	 map.clearOverlays();
	   map.getContainer().style.overflow='hidden';
	   map.setCenter(new GLatLng(cConfig.lng,cConfig.lat), cConfig.zoom);
	   //map.addControl(new GLargeMapControl());
	   if(cConfig.kml != '') {
	     var trompeCache = new Date();
	     var ggeo =new GGeoXml(url_carto + 'files/hs/' + cConfig.kml +"?"+trompeCache.getMilliseconds());
	     map.addOverlay(ggeo);
     } 
     map.addControl(new GLargeMapControl());
     map.addControl(new GMapTypeControl());
     map.setMapType(G_PHYSICAL_MAP);	  
	}	
  return true;
}//init_gmap


function removeMarkers () {
  


  for(var i=0;i<aMarkers.length;i++) {
    map.removeOverlay(aMarkers[i]);
  }
}

function addMarker  (id_mi,lat,lng,titre,html,lien_detail,icone,texte_loading) {
  if(texte_loading  !=  '') {
    jQ("#texte_loading").html(texte_loading);
  }
  if(aMarkers  ==  undefined) {
    aMarkers = new Array();
  }
  
  
  var mMarker = new GMarker(new GLatLng(lat,lng));
  aMarkers.push(mMarker);
  map.addOverlay(mMarker);
  GEvent.addListener(mMarker,"click",function() {
    mMarker.openInfoWindowHtml(html);
  });
  
   
}

function openTb(elt,url,title) {
  var htmlelt = document.getElementById(elt); 
	var t = title
	var a = url
	var g = false;
	tb_show(t,a,g);
	jQ("#"+elt).blur();
	return false;
}

function switchMap(val) {//Switch d'un etat de gMap a l'autre
	switch(val) {
		case 1 : 	mapTypeCourant = G_PHYSICAL_MAP;map.setMapType(G_PHYSICAL_MAP);break;
		case 2 :	mapTypeCourant = G_SATELLITE_3D_MAP;map.setMapType(G_SATELLITE_3D_MAP);break;
		default:	mapTypeCourant = G_PHYSICAL_MAP;map.setMapType(G_PHYSICAL_MAP);break;
	}
	return false;
}

