// JavaScript Document

var mapO
var gdir
var gDivName

function showGoogleMap( divName ){
	gDivName = divName;
	renderMap();
	
	var point = new GLatLng(loclat, loclon);
	mapO.addOverlay(new GMarker(point));
}

function renderMap(){
	if (GBrowserIsCompatible()) {
		var text;
		
		text = companyName + "<br/>\n";
		text += ppaPhone + "<br/>";
		
		var map = new GMap2(document.getElementById(gDivName));
		map.addControl(new GSmallMapControl());
		//		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(loclat,loclon), loczoom);
		
		var node;
		node = document.createElement('div');
		node.setAttribute('class','gmapBubble');
		node.appendChild(document.createTextNode(companyName));
		node.appendChild(document.createElement('br'));
		
		node.appendChild(document.createTextNode(formatPhone(ppaPhone)));
		node.appendChild(document.createElement('br'));

		var link = document.createElement('a');
		link.setAttribute('href','javascript:showEmailForm()');
		link.appendChild(document.createTextNode("Send us an email."));
		node.appendChild(link);


		//node.createTextNode(ppaPhone) ;
		
		map.openInfoWindow(map.getCenter(), node );
		
		//var point = new GLatLng(loclat, loclon);
		//map.addOverlay(new GMarker(point));


		mapO = map
	}
	
}

function showDirectionsFrom(divName, address){
	showDirections(divName, address + " to " + loclat + " , " + loclon );
}

function showDirections(divName, full){		
		//clear any old directions
		var dirDiv = document.getElementById(divName)
		if (dirDiv .hasChildNodes()) while (dirDiv .childNodes.length >= 1) dirDiv .removeChild(dirDiv .firstChild);
		
		renderMap();
		directions = new GDirections( mapO, dirDiv );
		//GEvent.addListener(gdir, "load", onGDirectionsLoad);
		//GEvent.addListener(directions, "error", handleErrors);
		directions.load(full);

		gdir = directions
		//GDirections.getStatus()
}

function formatPhone(num){ 
  var _return=false;
  if(num.length == 10){ 
	_return="(";
	var ini = num.substring(0,3);
	_return+=ini+") ";
	var st = num.substring(3,6);
	_return+=st+"-";
	var end = num.substring(6,10);
	_return+=end;
  }else if(num.length == 7){ 
	_return="";
	var st = num.substring(3,6);
	_return+=st+"-";
	var end = num.substring(6,10);
	_return+=end;	
  }else{
	_return=num;  
  }
  return _return; 
} 

 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...
	}