var gdir;
var geocoder = null;
var addressMarker;

var LAT = 45.68957;
var LONG = 13.14436;

var point = new GLatLng(LAT, LONG);
var map = null;
var marker = new GMarker( point, { } );


function setupMap() {
      setSize();
      tagetBlank();


      // elimino la mappa statica
      //document.getElementById('static-map').style.display='none';

      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));
        //gdir = new GDirections(map, document.getElementById("directions"));
        //GEvent.addListener(gdir, "load", onGDirectionsLoad);
        //GEvent.addListener(gdir, "error", handleErrors);

	//setDirections("Venezia", "33 via carnia, lignano sabbiadoro", "it_IT");

	// mapp di default
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		// 45.68978360899995, 13.144176006317139
		map.setCenter(new GLatLng(LAT, LONG), 14);


		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		//baseIcon.iconAnchor = new GPoint(9, 34);
		//baseIcon.infoWindowAnchor = new GPoint(9, 2);
		//baseIcon.infoShadowAnchor = new GPoint(18, 25);


		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();


		//map.addOverlay(createMarker(point));
		//if (document.getAttribute("class").value == "googledir")
		//alert("ciao");
		//alert(document.getAttribute("class").value);
		// dopo il submit non sta pių a sentire
		//if (document.getAttribute("class").value == "googledir") {

		map.addOverlay(marker);

		//}
		
		//se č settato il percorso
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		//setDirections("Venezia", "33 via carnia, lignano sabbiadoro", "it_IT");

      }
}
    
function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
}

function createMarker(point) {
	  // Set up our GMarkerOptions object
	  markerOptions = {  };
	  var marker = new GMarker(point, markerOptions);
	  return marker;
}

function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
}

function setSize() {
  var myWidth = 0, myHeight = 0;
  var add = 13; // per IE
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth + add;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth + add;
    myHeight = document.body.clientHeight;
  }
	// attivo le parti con display none
	// document.getElementById('percorsi').style.display='inherit';

	// imposto gli stili secondo la dimensione della finestra del browser

	//document.getElementById('percorsi').style.position='absolute';
	//document.getElementById('percorsi').style.left=((myWidth - 800)/2 + 234) + 'px';

}

function tagetBlank() {
    var links = document.getElementsByTagName('a');
    for (var i=0;i < links.length;i++) {
        if (links[i].className == 'new-window') {
            links[i].onclick = function() {
                window.open(this.href);
                return false;
            };
        }
    }
};

