// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var map;
var directionsPanel;
var directions;
var geocoder = null;
var locale = null;
function initialize(address,loc) {
	map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
	directionsPanel = document.getElementById("route");
	directions = new GDirections(map, directionsPanel);
	geocoder = new GClientGeocoder();
	GEvent.addListener(directions, "error", handleDirectionsErrors);
	locale = loc;
	if(address != "")
	{
		showAddress(address);	
	}	
}

function handleDirectionsErrors()
{
	$('route_error').show();
}

function compute_route()
{
	map.clearOverlays();
	if ($('route').childNodes[0] != null) {
		$('route').innerHTML = '';//apichildNodes[0].remove();
	}
	
	var directionFrom = $('source_string').value;
	var directionTo = $('direction_title').value;
	directions.clear();
	$('route_error').hide();
	directions.load("from: "+directionFrom+" to: "+directionTo,
 		{ "locale": locale });
	
	
	
}

function validate_address(address,locale,test_from)
{
	map.clearOverlays();
	directions.clear();
	$('route_error').hide();
	directions.load("from: "+test_from+" to: "+address,
 		{ "locale": locale });
}

function showAddress(address) {
if (geocoder) {
 geocoder.getLatLng(
 address,
 function(point) {
 if (!point) {
 alert(address + " not found");
 } else {
 map.setCenter(point, 13);
 var marker = new GMarker(point);
 map.clearOverlays();
 map.addOverlay(marker);
 marker.openInfoWindowHtml(address);
 }
 }
 );
 }
 }
 
function fj_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=fj_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function fj_showHideLayers() {
  var i,p,v,obj,args=fj_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=fj_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
} 