//-- GOOGLE MAPS initialize
var map;
    
function initializeGmap() {
		  map = new GMap2(document.getElementById("map_canvas"));
		  //-- set center point
		  map.setCenter(new GLatLng(60.178456, 24.780928), 13);
		 //-- add controls
		
			map.addControl(new GSmallMapControl());
			var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
			 map.addControl(new GMapTypeControl(), bottomRight);
		
		 //-- ADD MARKER
		 // Create our "tiny" marker icon
   		 var baronaIcon = new GIcon(G_DEFAULT_ICON);
   		baronaIcon.image = "http://lansiauto-areena.com/20090824_uudistus/barona/wp-content/themes/barona/style/images/web/marker_barona.png";
		// Set up our GMarkerOptions object
		var markerOptions = { icon:baronaIcon };
		//-- create point
	    var point = new GLatLng(60.178456,24.780928);
	    //--add to map
	   // map.addOverlay(new GMarker(point, markerOptions)); //add the point/location AND the new icon as option
	    
	    //-- marker event
		
		  
		  // Creates a marker at the given point
		
		function createMarker(latlng, html_msg,markerOptions) {
		      var marker = new GMarker(latlng,markerOptions);
		      	// Clicking the marker will show/hide it
		      GEvent.addListener(marker,"click", function() {
			        map.openInfoWindowHtml(latlng, html_msg);
		      });
		      return marker;
		}
		
		var myHtml = "<b>Barona Areena</b><br/> Urheilupuistontie 3,<br/> 02200 Espoo,<br/> 09 85650200";
		
		//--create marker using the function
		map.addOverlay(createMarker(map.getCenter(),myHtml,markerOptions));
		  map.openInfoWindowHtml(map.getCenter(),myHtml);
		  /*
			//-- show bubble
		  map.openInfoWindowHtml(map.getCenter(),
               document.createTextNode("Barona Oy,<br/> Urheilupuistontie 3, 02200 Espoo, 09 85650200")
          );
		*/

	}//--end initialize
	

	//-- ROUTE
 	
	var gdir;
	var geocoder = null;
	var addressMarker;
	
	function doroute(){
	  if (GBrowserIsCompatible()) {    
	  	var oldroute = document.getElementById("route");
  		oldroute.innerHTML = "";
	  	  
	    map = new GMap2(document.getElementById("map_canvas"));
	    gdir = new GDirections(map, document.getElementById("route"));
	    var startpoint = document.getElementById("route_address").value;
	    
	    map.addControl(new GSmallMapControl());
			var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
			 map.addControl(new GMapTypeControl(), bottomRight);
	    
	    GEvent.addListener(gdir, "load", onGDirectionsLoad);
	    GEvent.addListener(gdir, "error", handleErrors);
	
		//-- call the directions function
	    setDirections(startpoint, "Urheilupuistontie 3, 02200 Espoo", "fi_FI");
	  }
	}
	
	//-- get the directions
	function setDirections(fromAddress, toAddress, locale) {
	  gdir.load("from: " + fromAddress + " to: " + toAddress,
	            { "locale": locale });
	}


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

 		//alert(gdir.getStatus().code);
 	}

	/* This will handle the errors might happen while retrieving the directions */
	function handleErrors(){
	 var ohje = "\n Tarkista että osoite on muodossa: \n Kadunnimi (kadunnumero), kaupunki.\n Esim. Annankatu 28, helsinki";
	
		if( gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS ){
			alert("error_invalid_address. "+ohje);
		}else if( gdir.getStatus().code == G_GEO_SERVER_ERROR ){
			alert("error_google_error. "+ohje);
		}else if( gdir.getStatus().code == G_GEO_MISSING_QUERY ){
			alert("error_address_empty. "+ohje);
		}else {
			alert("error_invalid_address. "+ohje);
		}
		initializeGmap();
	}
