// mapsSearch.js 
// Created on: 14 Nov 2006
// Author: Mauro Ferratello :-Þ - Zero Computing S.r.l.
// Version 0.1

  function loadSearchMap() 
  {
    try
    {
      if (GBrowserIsCompatible()) 
      {  
        mainMap = new GMap2(document.getElementById("mainMap"));
        //mainMarker = new GMarker(new GPoint(0,0), {draggable:true})
        largeControl = new GLargeMapControl();
        smallControl = new GSmallMapControl();    

        //map.addControl(new GSmallMapControl());
        mainMap.addControl(largeControl);
        mainMap.addControl(new GMapTypeControl());

        // default Western Europe center coordinates
        var lat = 45;
        var lon = 12;
        //setTimeout("mainMap.setCenter(new GLatLng("+lat+","+lon+"), 4)",10000);   
        mainMap.setCenter(new GLatLng(lat,lon), 4);
        //mainMap.hideControls();
        //var center = new GLatLng(lat, lon);	             
        //setCityMarker(country, city, latitude, longitude);
        
        //addAlphaTileLayer();
        return;
      }
    }
    catch (ex)
    {
      mainMap = false; 
    }
  }
  // gestisce i controlli per l'effettivo caricamento della mappa.
  function isMapLoaded()
  {
    if (!mainMap)
      return false;
    
    if (!mainMap.isLoaded())
      return false;
    
    return true;
  }

  function refreshCenterFromCountry(countryField)
  {    
    if (!isMapLoaded())
    {
      //alert("Map is not still fully loaded");
      return;
    }
//    alert(countries.IT.id+' - '+countries.IT.latitude+':'+countries.IT.longitude+' - '+countries.IT.zoom);                           
//alert (countries.eval(idCountry).id);
/* // ### debug ###
var idCountry = $F('country');
alert(idCountry);
alert (countries[idCountry].id);
*/
    try 
    {
      //var idCountry = $F('country');
      var idCountry = $F(countryField);      
      //alert (countries.eval(idCountry).id);
      if (idCountry != undefined )
        //centerTo(countries.eval(idCountry).latitude, countries.eval(idCountry).longitude,countries.eval(idCountry).zoom);
        centerTo(countries[idCountry].latitude, countries[idCountry].longitude,countries[idCountry].zoom);
      
    }
    catch (Exception)
    {
      
    }
    return true;
  }  
  function centerTo(lat,lon,zoom)
  {
    mainMap.setCenter(new GLatLng(lat,lon), zoom);
  }
  
  // <@todo:> gestire la mancanza delle coordinate x la citta' ed eventualmente usare il geocoding.
  // @todo: settarlo nel cityObj.
  function setSearchCityMarker(cityObj, setOnMap)
  {
    // se non funziona Google il sito deve funzionare lo stesso,
    // quindi il controllo sul mainMap c vuole.
    if (!mainMap)
      return;
    if (!isMapLoaded())
    {
      setOnMap = false;
      // se non ho finito di caricare la mappa, chiamo la funzione che al termine del caricamento
      // mostra tutti gli stoppini caricati
      //placeAllMarkers();          
    }    
    
    if (cityObj.latitude == 0 || cityObj.longitude == 0)
    {
      cityObj.isMarkerSet = false;      
      return;
    }  
    // serve x dire ke il marker e' settabile.
    cityObj.isMarkerSettable = true;

    var point = new GLatLng(cityObj.latitude,cityObj.longitude);
    var cityIcon = new GIcon(G_DEFAULT_ICON, "imgs/mapicons/center.png");
    cityIcon.iconSize = new GSize(24,34);
    cityIcon.shadowSize = new GSize(0,0);
    var cityMarker = new GMarker(point, cityIcon);
    //cities[cityObj.id].marker = cityMarker;
    //cities[cityObj.id]= cityObj;
    cityMarker.title = cityObj.name;
    var content = "<p class='map_p_description' align='center'> <span style='color:red; font-weight;bold;'>"+cityObj.name+"</span> - <span color='Navy'>"+cityObj.country+"</span></p>"+
                  "<p class='map_p_description'> "+cityObj.hotels+" hotel in questa citt&agrave;</p>";
                  //"<p class='map_p_description'> <a href='#1' onclick='cities_listbox.setValue(cities_listbox.getIndexFromValue("+cityObj.id+"));'>Seleziona come destinazione</a></p>";
    cityObj.marker = cityMarker;
    cityObj.content = content;
    //cities[cityObj.id].content = content;
    GEvent.addListener(cityMarker, "click", function() 
    {
      //cityMarker.showMapBlowup(18,G_SATELLITE_MAP);
      cities_listbox.setValue(cities_listbox.getIndexFromValue(cityObj.id));

      cityMarker.openInfoWindowHtml(content);
    });
    GEvent.addListener(cityMarker, "infowindowclose", function() 
    {
      mainMap.panTo(point);
    }); 
    
    if (setOnMap)
    {   
      mainMap.addOverlay(cityMarker);
      cityObj.isMarkerSet = true;
    }
  }
  
  function openCityMarkerPopup(idCity)
  {    
    //var idCity = $F('');
//alert(idCity+'---');
    var cityObj = cities[idCity];
    if (cityObj.latitude == 0 && cityObj.longitude == 0 ) 
      return;
      
//alert (cityObj.name);
    
    if (!isMapLoaded())
    {
      return;
    }
    if (!cityObj.isMarkerSet)
    {
      mainMap.addOverlay(cityObj.marker);;
      cityObj.isMarkerSet = true;
    }
    cityObj.marker.openInfoWindowHtml(cityObj.content);
  } 
  //###################################################
  //# Aggiunge tutti i marker contenuti in cities[].  #               
  //###################################################
  function placeAllMarkers()
  {    
    if (!cities)
    {
      if (debug)
        alert("cities not loaded!");
      return;    
    }  
    // @Todo: gestire una sorta di controllo di degenerazione del Thread,
    // altrimenti viene richiamato all'infinito fino a quando nn vengono mostrati gli stoppini.
    if (!isMapLoaded())
    {  
      if (debug)
        alert("map not loaded: thread recalled after 500ms");
      setTimeout("placeAllMarkers()", 500);
      return;   
    } 
    //alert("place markers");
    //alert("length:"+cities.getLength());
    
    for (var key in cities)
    {
      
//alert(key + " : " + cities[key]);
      cityObj = cities[key];
//alert("is "+cityObj.name+"  marker set:"+cityObj.isMarkerSet+" is marker settable: "+cityObj.isMarkerSettable);
      if (!cityObj.isMarkerSet && cityObj.isMarkerSettable)
      {
        mainMap.addOverlay(cityObj.marker);
        cityObj.isMarkerSet = true;
      }
    }
    
    return;
  }
  
  function cleanMap()
  {
//    alert("cleaning map");
    try
    {
      if (isMapLoaded())
        mainMap.clearOverlays();
    }
    catch(ex)
    {
      if (debug)
        alert ("Error while cleaning Map: "+ex.message);
    }
    return ;
  }
  
  
  // gestisce l'opacita' x l'effetto disabled, ma solo all'interno della mappa.
  function addAlphaTileLayer()
  {
     var tilelayer = new GTileLayer(new GCopyrightCollection(), 0, 17);
     tilelayer.getTileUrl = function(tile, zoom) {
       // A more interesting tile server would do something with the numbers
       // tile.x, tile.y and zoom.
       //return "http://kml.lover.googlepages.com/white_map_tile.gif";
       //return "http://212.121.66.72:8084/HotelAvailabilitySearch/imgs/white_map_tile.gif";
       return "./imgs/white_map_tile.gif";
     };
     tilelayer.getOpacity = function() {return 0.75;}
     mainMap.addOverlay(new GTileLayerOverlay(tilelayer));
  }

