var geocoder,point, marker, map, addressMarker, gdir, fromAddress, toAddress;

/*
**
* Bootstrap function to setup map and apply
* custom company marker
*/
function initialize(idmap, cords, z) {
   if (GBrowserIsCompatible()) {  
      //settings 
      z = (typeof z == 'undefined') ? 13 : z; 
      var defaultZoomLevel  = z; 
       
      //end settings  
   
      //setup elements  
      map   = new google.maps.Map2(document.getElementById("map_"+idmap));  
      gdir  = new GDirections(map, document.getElementById("directions"));
       var aCoords = cords.split(',');
      point = new google.maps.LatLng(aCoords[0],aCoords[1]); 
      var zz = new GLargeMapControl();
      map.addControl(zz);
      map.addControl(new GOverviewMapControl(new GSize(105,105)), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));
      //error handler  
      GEvent.addListener(gdir, "error", handleErrors);  
      toAddress = point; 
     //set company marker  
      marker = new GMarker(point);
      map.addOverlay(marker); 
      //map.openInfoWindow(point, 'kfsnfksbnfsk');
      marker.openInfoWindowHtml(document.getElementById("marker_map_"+idmap).innerHTML);
      GEvent.addListener(map, "click", function(marker, point) { if (marker && marker.openInfoWindowHtml) marker.openInfoWindowHtml(document.getElementById("marker_map_"+idmap).innerHTML); });   
   
      //set map center  
      map.setCenter(point, defaultZoomLevel);
      map.enableScrollWheelZoom();
      map.enableContinuousZoom();
      map.prototype.wheelZoom = function(event)
          {
              if(event.cancelable)    event.preventDefault();
              return false;
          }
      GEvent.addDomListener(document.getElementById("map_"+idmap), "DOMMouseScroll", map.wheelZoom);
      GEvent.addDomListener(document.getElementById("map_"+idmap), "mousewheel", map.wheelZoom);  
  
   }  
 }  

/*
**
* Looks up the directions, overlays route on map,
* and prints turn-by-turn to #directions.
*/

function overlayDirections()
{   
    
    fromAddress =
      document.getElementById("address").value;
    var language  = document.getElementById("language").value;
    marker.closeInfoWindow();
    gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": language });
    
}


/*
**
* Wrapper function to create/return a marker object
* with custom image
*/
function createMarker(latlng)
{

}

/*
**
* Display error to user
*
*/
function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("Nie odnaleziono podanej lokalizacji. \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_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("Wystąpił błąd podczas wyznaczania trasy. Prosimy spróbować później.");
}