/*  Copyright (C) 2009 - Gianluca Sabena - www.intertesto.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License  
    http://www.gnu.org/licenses
*/

var map=0;

function setOptions(opt) {
  var option = new Object();
  if (opt != 'null' ) {
    try {
      option = YAHOO.lang.JSON.parse(opt);
    }
    catch (e) {
      alert("Error reading options! "+e);
      return;
    }
  } 
  if (typeof(option.clat) == 'undefined') {
    option.clat='41.87194';
  }
  if (typeof(option.clon) == 'undefined') {
    option.clon='12.56738';
  }
  if (typeof(option.maptype) == 'undefined') {
    option.maptype='G_HYBRID_MAP';
  }
  if (typeof(option.czoom) == 'undefined') {
    option.czoom='3';
  }
  if (typeof(option.idlat) == 'undefined') {
    option.idlat='latitude';
  }
  if (typeof(option.idlon) == 'undefined') {
    option.idlon='longitude';
  }
  if (typeof(option.gmapcontrol1) == 'undefined') {
    option.gmapcontrol1='GSmallMapControl()';
  }
  if (typeof(option.gmapcontrol2) == 'undefined') {
    option.gmapcontrol2='GMapTypeControl()';
  }
  if (typeof(option.markeraction) == 'undefined') {
    option.markeraction='popup';
  }
  if (typeof(option.markerwidth) == 'undefined') {
    option.markerwidth=30;
  }
  if (typeof(option.markerheight) == 'undefined') {
    option.markerheight=30;
  }
  return option;
}


function gmapCoordinatesInit() {
  if (GBrowserIsCompatible()) {
    var option = optionsCoordinates;
    var center;
    var latlonempty=true;
    center = new GLatLng(option.clat,option.clon);
    if (map == 0) {
      map = new GMap2(document.getElementById("gmapCoordinates"));
      eval('map.setMapType('+option.maptype+')');
      if (option.gmapcontrol1.length > 0) eval('map.addControl(new '+option.gmapcontrol1+')');
      if (option.gmapcontrol2.length > 0) eval('map.addControl(new '+option.gmapcontrol2+')');
    }
    var lat = document.getElementById(option.idlat).value;
    var lon = document.getElementById(option.idlon).value;
    if (lat.length > 0 && lon.length > 0) {
      center = new GLatLng(lat,lon);
      latlonempty=false;
    }
    geocoder = new GClientGeocoder();
    map.setCenter(center , 1*option.czoom);  
    var marker = new GMarker(center, {draggable: true});  
    map.addOverlay(marker);
    GEvent.addListener(marker, "dragend", function() {
        var point = marker.getPoint();
        map.panTo(point);
        document.getElementById(option.idlat).value = point.lat().toFixed(5);
        document.getElementById(option.idlon).value = point.lng().toFixed(5);
        });
    GEvent.addListener(map, "moveend", function() {
        map.clearOverlays();
        var center = map.getCenter();
        var marker = new GMarker(center, {draggable: true});
        map.addOverlay(marker);
        document.getElementById(option.idlat).value = center.lat().toFixed(5);
        document.getElementById(option.idlon).value = center.lng().toFixed(5);
        GEvent.addListener(marker, "dragend", function() {
          var point =marker.getPoint();
          map.panTo(point);
          document.getElementById(option.idlat).value = point.lat().toFixed(5);
          document.getElementById(option.idlon).value = point.lng().toFixed(5);
          });
        });
    if (latlonempty == true ) {
        document.getElementById(option.idlat).value = "";
        document.getElementById(option.idlon).value = "";
    }
  }
}

function showAddress(address) {
  var option = optionsCoordinates;
  if (map == 0) {
    map = new GMap2(document.getElementById("gmapCoordinates"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
  }
  if (geocoder) {
    geocoder.getLatLng(address, function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          document.getElementById(option.idlat).value = point.lat().toFixed(5);
          document.getElementById(option.idlon).value = point.lng().toFixed(5);
          map.clearOverlays()
          map.setCenter(point, 14);
          var marker = new GMarker(point, {draggable: true});  
          map.addOverlay(marker);
          GEvent.addListener( 
          marker, "dragend", function() {
            var pt = marker.getPoint();
            map.panTo(pt);
            document.getElementById(option.idlat).value = pt.lat().toFixed(5);
            document.getElementById(option.idlon).value = pt.lng().toFixed(5);
        });
        GEvent.addListener(map, "moveend", function() {
          map.clearOverlays();
          var center = map.getCenter();
          var marker = new GMarker(center, {draggable: true});
          map.addOverlay(marker);
          document.getElementById(option.idlat).value = center.lat().toFixed(5);
          document.getElementById(option.idlon).value = center.lng().toFixed(5);
          GEvent.addListener(marker, "dragend", function() {
            var pt = marker.getPoint();
            map.panTo(pt);
            document.getElementById(option.idlat).value = pt.lat().toFixed(5);
            document.getElementById(option.idlon).value = pt.lng().toFixed(5);
            });
          });
        }
        }
    );
  }
}

/* ---------- GmapPlot ---------------------------- */
var tileLayerOverlay;
var groundOverlay;

function gmapPlotInit(dd) {  
  var option = optionsPlot;
  try {
    var dd = YAHOO.lang.JSON.parse(dd);
  }
  catch (e) {
    alert(e);
  }
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("gmapplot"));  
    map.setCenter(new GLatLng(option.clat,option.clon), 1*option.czoom);  
    eval('map.setMapType('+option.maptype+')');
    if (option.gmapcontrol1.length > 0) eval('map.addControl(new '+option.gmapcontrol1+')');
    if (option.gmapcontrol2.length > 0) eval('map.addControl(new '+option.gmapcontrol2+')');
    if(typeof(option.overlay) != 'undefined') {
        var tilelayer = new GTileLayer();
        tilelayer.getTileUrl = function() { return option.overlay; };
        tilelayer.isPng = function() { return true;};
        tilelayer.getOpacity = function() { return 1.0; }
        tileLayerOverlay = new GTileLayerOverlay(tilelayer);
        map.addOverlay(tileLayerOverlay);
    }
    if(typeof(option.groundoverlayimage) != 'undefined' && typeof(option.groundoverlaybounds) != 'undefined') {
      var b = option.groundoverlaybounds.split(",");
      var boundaries = new GLatLngBounds(new GLatLng(b[0],b[1]), new GLatLng(b[2],b[3]));
      groundOverlay = new GGroundOverlay(option.groundoverlayimage, boundaries);
      map.addOverlay(groundOverlay); 
    }

    for ( var ii=0; ii < dd.length; ii++ ) {
      if(typeof(dd[ii].latitude) != 'undefined'  && typeof(dd[ii].longitude)!='undefined') {
        if(dd[ii].iscenter == true)
          map.setCenter(new GLatLng(dd[ii].latitude,dd[ii].longitude), 1*option.czoom);  
          var markerslist = new Array();
          var latlng = new GLatLng(dd[ii].latitude,dd[ii].longitude);
          var marker = createMarkerUrl(latlng,option.urlpopup+dd[ii].pageid,option.markeraction,dd[ii].markericofront, dd[ii].markericoback,option.markerwidth,option.markerheight);
          map.addOverlay(marker);
      }
      if(typeof(dd[ii].polyline)!='undefined') {
        //var p = "37.4419,-122.1419;37.4519,-122.1519";
        c = dd[ii].polyline.split(";");
        var a = new Array();
        for (var i=0; i<c.length;i++) {
          var t = c[i].split(",");
          if (t[0].length>0 && t[1].length>0) a.push(new GLatLng(t[0],t[1]));
        }
        var color = "";
        var size = "";
        if(typeof(dd[ii].color)=='undefined') color = "#ffffff";
        else color = dd[ii].color;
        if(typeof(dd[ii].size)=='undefined') size = "1";
        else size = dd[ii].size;
 //alert(dd[ii].polyline+" -- "+a);
        //var polyline = new GPolyline(a, color, size, 1);
        if (a.length>0) map.addOverlay( new GPolyline(a, color, size, 1));
        //map.addOverlay(polyline);
      }
    } /* end dd loop */
  } /* close if GBrowserIsCompatible */
} /* close initialize() */



function createMarkerUrl(point,url,action,ico1,ico2,w,h) { 
  //alert(ico1+ico2);
  var option = optionsPlot;
  var icon = new GIcon();
  if (typeof(ico1) != 'undefined') icon.image = ico1;
  else if (typeof(option.markericofront) != 'undefined') icon.image = option.markericofront;
  else icon.image =  "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
  if (typeof(ico2) != 'undefined') icon.shadow = ico2;
  else if (typeof(option.markericoback) != 'undefined') icon.shadow = option.markericoback;
  else icon.shadow = "http://maps.gstatic.com/intl/en_ALL/mapfiles/shadow50.png";
  icon.iconSize = new GSize(w, h);
  icon.shadowSize = new GSize(w,h);
  icon.iconAnchor = new GPoint(15, 15);
  icon.infoWindowAnchor = new GPoint(15, 15);
  var marker = new GMarker(point,icon); 
  if (url.length > 0 && action == 'popup') {
    GEvent.addListener(marker, "mousedown", function() { 
      GDownloadUrl(url, function(data, responseCode) {
        //restringiamo la risposta @api di deki
        var patt = /<body[^>]*>[\s\S]*?<\/body>/i;
        var t = data.match(patt);
        marker.openInfoWindowHtml(t[0]);
        });
      });
  } 
  if (url.length > 0 && action=='redirect') {
    GEvent.addListener(marker, "mousedown", function() {
      window.location=url;
      //alert(url);
    });
  } 
  return marker;
}


