/*
  --------------
  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 aMarkersCommunes  = new Array();
var id_obt_focus  = 0;
var id_menu_focus = 0;



function Config () {
  this.lat  = 0;
  this.lng  = 0;
  this.zoom = 2;
  this.kml  = "";
}
var cConfig = new Config();
var iconeCommune = new GIcon();
    
iconeCommune.image= 'http://www.parc-naturel-pilat.fr/php/carte-interactive/img/marker_commune.png';
//iconeCommune.shadow = cheminSite+'png/map/ombre.png';

iconeCommune.iconSize = new GSize(20, 20);
//iconeCommune.shadowSize = new GSize(22, 20);
iconeCommune.iconAnchor = new GPoint(10, 10);
iconeCommune.infoWindowAnchor = new GPoint(15, 20);


                   

/*
  --------------
  FONCTIONS D'INITIALISATION
  --------------
*/
                   
//initialisation de l'interface et des variables en fonction de l'itineraire, puis initialisation de la carte
function init_interface(id_obt,id_menu) {
  id_obt_focus  = (id_obt !=  undefined) ? id_obt  : id_obt_focus;
  id_menu_focus  = (id_menu !=  undefined) ? id_menu  : id_menu_focus;
  loading(true,"initialisation de la carte interactive");
  set_config(6.167323,45.861525,11);
}//init_interface

function set_config(plat,plng,pzoom) {//initialisation de l'objet de configuration
  cConfig.lat = plat;
  cConfig.lng = plng;
  cConfig.zoom  = pzoom;
  cConfig.kml  = '';
  var bGmap = init_gmap();  //initialisation de la carte
  if(bGmap) {
    var bMenu = init_menu_carte(); //initialisation du click sur les menus, si la carte s'est initialisée
    if(bMenu) {
      loading(false);  
    }
  }
  
}//set_config


//Fonction d'affichage de la div de chargement
function loading(bDisplay,texte) {
  if(texte  !=  '' && texte != null) {
    jQ("#texte_loading").html(texte);
  }
  jQ("#loading_carte").css('top','400px');
	divLoading = document.getElementById("loading_carte");
	if(bDisplay) {
		jQ("#loading_carte").fadeIn("fast");

	}else {
	  jQ("#loading_carte").fadeOut("fast");
	}
}


function init_menu_carte()  {
  
  jQ(".menu_carte_interactive").click(function(){
    var sId = this.id;
    ssMenuId  = sId.split('_')[2];
    jQ('#sous-menu-carto_'+ssMenuId).slideToggle("fast",function(){
      //jQ('#menuCarto').scrollTo(jQ(this),500);
    });

        jQ('.menu_carte_interactive').each(function(){
          jQ(this).removeClass('carto_selectionne');
        });
        
        jQ(this).addClass('carto_selectionne');
        loading(true,"Chargement des données...");
        get_type(ssMenuId);

    return false;
  });
  

  
  
  
  if(id_menu_focus  !=  0)
  {
   //jQ('#sous-menu-carto_'+id_menu_focus).trigger('click');
   get_tag_jumi(id_menu_focus,12,true);
  }
  return true;
}

function get_type(ssMenuId,nouveauZoom,rangerMenu)
{
removeMarkers();
  jQ("#message_carte").css('visibility','hidden');
  jQ.ajax({
               type: "GET",
               url: url_carto + "ajax/get_objets.ajax.php",
               data: "id_m="  +ssMenuId,
               dataType:"script",
               success: function(msg){
                loading(false);
              },
              error:  function() {
                loading(false);
                GLog.write("Erreur dans la récupération des données objets");
              }
      
      });
      if(nouveauZoom  !=  undefined)
      {
       map.setZoom(nouveauZoom);
      }
      
      if(rangerMenu != undefined)
      {
//        jQ('#menuCarto').toggle("fast");
          
      }
}




/*
  Fonctions utiles pour la carte gMap
*/


//initialisation de la gmap
function init_gmap()  {
	
   if (GBrowserIsCompatible()) {
  	 map = new GMap2(document.getElementById('map_carto'));
  	 map.clearOverlays();
  	 //var boundaries = new GLatLngBounds(new GLatLng(44.930315575503254,5.88849306106567), new GLatLng(44.941774318333902,5.90986490249634));
     //var campusmap = new GGroundOverlay("http://graargh.returnstrue.com/rajdeep/maps/iiml/customlayout3/base_combined.jpg", boundaries);
     //map.addOverlay(campusmap);

	   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 + cConfig.kml +"?"+trompeCache.getMilliseconds());
	     map.addOverlay(ggeo);
     } 
     map.setMapType(G_PHYSICAL_MAP);	  
	}	
  return true;
}//init_gmap


function removeMarkers () {
  for(var i=0;i<aMarkers.length;i++) {
    map.removeOverlay(aMarkers[i]);
  }
}

function removeMarkersCommunes () {
  for(var i=0;i<aMarkersCommunes.length;i++) {
    map.removeOverlay(aMarkersCommunes[i]);
  }
}

function addCartoMarker  (id,lat,lng,titre,html,lien_detail,texte_loading,cheminIconePerso) {
  if(texte_loading  !=  '') {
    jQ("#texte_loading").html(texte_loading);
  }
 // iconeDefaut.image= url_carto+'files/menuItineraireIcone/'+icone;
  //var mMarker = new GMarker(new GLatLng(lat,lng),{icon:iconeDefaut});
  
  var markerPerso = {};
  if(cheminIconePerso  !=  "")
  {
    var iconePerso = new GIcon();
    iconePerso.iconSize = new GSize(30, 30);
    iconePerso.iconAnchor = new GPoint(15, 15);
    iconePerso.infoWindowAnchor = new GPoint(15, 20);
    iconePerso.image= cheminIconePerso;
    markerPerso = { icon:iconePerso };
  }
  var mMarker = new GMarker(new GLatLng(lat,lng),markerPerso);
  aMarkers.push(mMarker);
  map.addOverlay(mMarker);
  
  GEvent.addListener(mMarker,"click",function() {
    var rand  = Math.round(Math.random()*100);
    mMarker.openInfoWindowHtml("<div class='infowindow_gmap'>"+html+"</div>");
  });
  
  
  if(id_obt_focus != 0 && id_obt_focus  ==  id)
   {
    GEvent.trigger(mMarker,"click");
    map.setCenter(mMarker.getLatLng().lat(),mMarker.getLatLng().lng());
   }
   
}



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

