// JavaScript Document
var map = null;      
var pinid = 0;      
var shape;
var layerid=1;
            var _hide;
        var _currentShape = null;
        var _isPanning = false;                
 function GetMap()         {            
 map = new VEMap('myMap');   
// map.SetDashboardSize(VEDashboardSize.Tiny);         
 map.LoadMap(new VELatLong(40,10), 2 ,'h' ,false);
 map.Find("", "France");
 map.ShowDisambiguationDialog(false);
  map.SetMouseWheelZoomToCenter(true);
  map.SetMapStyle(VEMapStyle.Road); 
 map.AttachEvent("onclick", bulle);
// map.AttachEvent("onclick", OnClick);
  map.AttachEvent("onmousedown", OnMouseDown);
            map.AttachEvent("onmousemove", OnMouseMove);
            map.AttachEvent("onmouseup", OnMouseUp);
           // map.AttachEvent("onmouseover", OnMouseOver);
            map.AttachEvent("onmouseover", OnClick);
            map.AttachEvent("onstartpan", OnStartPan);
            map.AttachEvent("onstartzoom", OnStartZoom);
            map.AttachEvent("onmouseout", OnMouseOver);
        }

        //////////////////////////////////////////////////
        function AddPushpin()
        {
            var latlong = map.GetCenter();
            var shape = new VEShape(VEShapeType.Pushpin, latlong);
            shape.SetTitle('My pushpin');
            shape.SetDescription('This pushpin is at:<br>Latitude: ' + latlong.Latitude.toFixed(5) + '<br>Longitude: ' + latlong.Longitude.toFixed(5));
            map.AddShape(shape);       
        }

        //////////////////////////////////////////////////
        function OnMouseDown(e)
        {
            if (e.leftMouseButton)
                _isPanning = true;
        }

        //////////////////////////////////////////////////
        function OnMouseMove(e)
        {
            if (_isPanning)
                CustomHideInfoBox(_currentShape);
        }

        //////////////////////////////////////////////////
        function OnMouseUp(e)
        {
            _isPanning = false;
        }
       
        //////////////////////////////////////////////////
        function OnMouseOver(e)
        {
            return true;
        }

        //////////////////////////////////////////////////
        function OnClick(e)
        {
            if (e.elementID)
            {
                var clickedShape = map.GetShapeByID(e.elementID);

                if (clickedShape)
                {
                    if (window.ero.isVisible() && (clickedShape == _currentShape) )
                    {
                        // We clicked on the shape which has an infobox open.
                        CustomHideInfoBox(_currentShape);
                    }
                    else if (window.ero.isVisible())
                    {
                        // We clicked on a different shape. Close the infobox first,
                        // then show it for the new shape.
                        CustomHideInfoBox(_currentShape);
                        CustomShowInfoBox(clickedShape);
                    }
                  /*  else
                    {
                        CustomShowInfoBox(clickedShape);
                    }*/
                }
            }
        }

        //////////////////////////////////////////////////
        function OnStartPan(e)
        {
            if (e.leftMouseButton)
            {
                // We don't want to handle this here, because a simple mouse click on a shape,
                // could be interpreted as a "start pan" action. return;
                CustomHideInfoBox(_currentShape);
            }

           
        }

        //////////////////////////////////////////////////
        function OnStartZoom(e)
        {
            CustomHideInfoBox(_currentShape);
        }

        //////////////////////////////////////////////////
        function DisableEroHide()
        {
            _hide = window.ero.hide;
            window.ero.hide = function(a)
            {
                return;
            }       
        }
       
        //////////////////////////////////////////////////
        function EnableEroHide()
        {
            window.ero.hide = _hide;
        }

        //////////////////////////////////////////////////
        function CustomShowInfoBox(shape)
        {
            if (!shape)
                return;
               
            DisableEroHide();
            map.ShowInfoBox(shape);
            _currentShape = shape;
        }

        //////////////////////////////////////////////////
        function CustomHideInfoBox(shape)
        {
            if (!shape)
                return;
               
            // Before actually calling the hide method, we need to restore the ero hide function,
            // otherwise we will spend half an hour saying "Why it doesn't hide? I am calling
            // HideInfoBox..." as I did :)
            EnableEroHide();
            map.HideInfoBox(shape);
            _currentShape = null;
        }
 function bulle(e){
  if(e.elementID != null)
            {
            shape = map.GetShapeByID(e.elementID);
var pts=shape.GetPoints();
var coordx=pts[0].Latitude;
var coordy=pts[0].Longitude;
var title=shape.GetTitle(); 
//keepPushpin(e);
xajax_infobulle('supprimer selection');
//document.getElementById('bulle').innerHTML="Cliquez sur >> pour afficher votre selection.<br />Pour supprimer un marqueur, cliquez sur l'image dans le panier.";
            xajax_addPushpin(title,coordx,coordy)
           
            }
 }
 function PixelClick(e)
         {
         
           var x = e.mapX;
              var y = e.mapY;
              pixel = new VEPixel(x, y);
              LL = map.PixelToLatLong(pixel);
if(e.rightMouseButton == 1)
              {
            map.FindLocations(LL, Push);
}
         }
 function Push(locations){
var options = new VERouteOptions;

            // Otherwise what's the point?
            options.DrawRoute      = true;

            // So the map doesn't change:
            options.SetBestMapView = false;

            // Call this function when map route is determined:
           // options.RouteCallback  = ShowTurns;

            // Show as miles
            options.DistanceUnit   = VERouteDistanceUnit.Kilometer;

            // Show the disambiguation dialog
            options.ShowDisambiguation = true;
            
shape = new VEShape(VEShapeType.Pushpin,new VELatLong(LL.Latitude,LL.Longitude));
var layer=map.GetShapeLayerByIndex(1);
              var test=layer.GetShapeCount();
              if(test==1){
              var lecalque=layer.GetShapeByIndex(0);
              var pts = lecalque.GetPoints();
              var origin=new VELatLong(LL.Latitude,LL.Longitude);
              var fin=new VELatLong(pts[0].Latitude,pts[0].Longitude);
              map.GetDirections([origin,fin],options);
map.AddShape(shape);}
}
  function ShapeInfo(e)
         {
            if(e.elementID != null)
            {
               shape = map.GetShapeByID(e.elementID);
            
               //Respond to ctrl-click event (toggle polygon icon visibility).
               //Note that pushpin shapes ignore ShowIcon and HideIcon.
            //   if(e.ctrlKey == 1)
             //  {
               var layer=map.GetShapeLayerByIndex(1);
               var count=layer.GetShapeCount();
              count+=1;
              alert(count);
               for(i=0;i<=count;i++)
               {
               
              var lecalque=layer.GetShapeByIndex(i);
              
               if(lecalque.GetID()!=shape.GetID()){
               layer.DeleteShape(lecalque);
               }
               }
               
                  /*map.HideAllShapeLayers();
                   shape.Show();*/
               //}shape.GetID()map.GetShapeLayerByIndex(0);
               }
               
               }
function affiche(lang,categorie){
 deleteRoute()
if(categorie!="no"){
map.HideInfoBox();
xajax_infobulle('selection');
//document.getElementById('bulle').innerHTML="Cliquez sur les images pour cr&eacute;er votre selection.";
 var layers =  map.GetShapeLayerCount() ;
if (layers!=0){ map.DeleteAllShapeLayers();}

var l = new VEShapeLayer();            
/*var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, url+'/'+categorie+'.xml', l);     */    
var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, 'xml/'+lang+'/'+categorie+'.xml', l);   
map.ImportShapeLayerData(veLayerSpec,DoZoom,0);
}
 }
 
function deleteRoute()
         {
            
                map.DeleteRoute();
            }
            
 
function pays(lepays){
if(lepays!="no"){
var terre=document.getElementById('earth');
var distance=document.getElementById('distance');
 // if(distance.style.display=="none"){distance.style.display="";}
if(terre.style.display==""){document.getElementById('earth').style.display="none";}

xajax_infobulle('quoi');
//document.getElementById('bulle').innerHTML="Veuillez maintenant s&eacute;l&eacute;ctionner une cat&eacute;gorie \"Quoi ?\".";
var what = "";
  var where =lepays;
  var zoom="";
  if(lepays=="guadeloupe"){

  //map.Find(what, where, null, null, null, null, true,true,true,true, monZoom("9"));
    var origin=new VELatLong("16.274664678160836","-61.55914306640625");
  map.SetCenter(origin);
  setTimeout("monZoom('9')",1000);
  }else{
  map.Find(what, where);
  }
  if(lepays=="france"){
  //map.Find(what, where, null, null, null, null, true,true,true,true, monZoom("4"));
  setTimeout("monZoom('5')",1000);
  }
  if(lepays=="martinique"){
  //map.Find(what, where, null, null, null, null, true,true,true,true, monZoom("9"));
  setTimeout("monZoom('9')",1000);
  }
 
  
  }else{
  var distance=document.getElementById('distance');
  if(distance.style.display==""){distance.style.display="none";}
  document.getElementById('earth').style.display="";}
}
function DoFind()
{
  var what = "";
  var where = document.getElementById('txtWhere').value;
  map.Find(what, where);
}
function monZoom(leZoom)      {
//alert(leZoom);
  map.SetZoomLevel(leZoom);
  
}
function DoZoom(layer)      {         
//var zoom = "3";         
// 
if(layer!=""&&layer!="undefined"){

var count = layer.GetShapeCount();
            for (var i = 0; i < count; i++)
            {
              var shape = layer.GetShapeByIndex(i);
              //test.value = shape.IconId;
              shape.SetCustomIcon("<img src='" + shape.IconId + "'/>");}
  }
  }
  function myclose(){
 
   window.document.getElementById('test').src=" ";
  window.document.getElementById('video').style.display="none";
  } 
  function video(url){
  
  window.document.getElementById('test').src=url;
    window.document.getElementById('video').style.display="";
  }
  function getId(idselect)
{   
    alert(idselect);
}
function ClearAll(){
var layers =  map.GetShapeLayerCount() ;
if (layers!=0){ map.DeleteAllShapeLayers();}
}
function seePushpin(coordx,coordy,title,desc,icon){

shape = new VEShape(VEShapeType.Pushpin,new VELatLong(coordx,coordy));

map.AddShape(shape);
shape.SetTitle(title);
shape.SetDescription(desc);
if(icon!=""){
shape.SetCustomIcon("<img src='pict/" + icon + "'/>");}


}

function keepPushpin(e){
if(e.elementID != null)
            {
shape = map.GetShapeByID(e.elementID);
var pts=shape.GetPoints();
var coordx=pts[0].Latitude;
var coordy=pts[0].Longitude;
var title=shape.GetTitle();
var desc=shape.GetDescription();
var icon=shape.GetCustomIcon();

document.getElementById('panier').innerHTML+=icon+"<br /><br />";

//alert(coordx+' '+coordy+' '+title+' '+desc+' '+icon);
}
}
function closeError(){
document.getElementById('error').style.display="none";
}
function openError(){
document.getElementById('error').style.display="";
}
function GetRouteMap(test)
         {
    var point="";
    var route=new Array;
    var reg2=new RegExp("[ ,]+", "g");
    var reg=new RegExp("[ ;]+", "g");
    var tableau=test.split(reg);
    for (var i=0; i<tableau.length; i++) {
    var tableau2=tableau[i].split(reg2);
    route[i]=(new VELatLong(tableau2[0],tableau2[1]));
}
            var options = new VERouteOptions;

            // Otherwise what's the point?
            options.DrawRoute      = true;

            // So the map doesn't change:
            options.SetBestMapView = false;

            // Call this function when map route is determined:
            options.RouteCallback  = ShowDistance;
            options.RouteColor=new VEColor(203,61,0,0.7);
            // Show as miles
            options.DistanceUnit   = VERouteDistanceUnit.Kilometer;

            // Show the disambiguation dialog
            options.ShowDisambiguation = false;

            map.GetDirections(route, options);
         }
         function ShowDistance(route){
         document.getElementById('distance').innerHTML='<strong>Distance :</strong> '+route.Distance.toFixed(1)+" Km";
         document.getElementById('distance').style.display="";
		 xajax_infobulle('bienvenue');
         //document.getElementById('bulle').innerHTML="Bienvenue sur Visit'monde.com. Veuillez selectionner un pays.";
         }
