google.load('maps', '2.x');
var map;
var directions;
var marker;
var window_print_it;

function loadMap()
{
	var icon = new google.maps.Icon();
	icon.image = 'http://www.google.com/intl/en_ALL/mapfiles/arrow.png';
	icon.shadow = 'http://www.google.com/intl/en_ALL/mapfiles/arrowshadow.png';
	icon.transparent = 'http://www.google.com/intl/en_ALL/mapfiles/arrowtransparent.png';
	icon.printImage = 'http://www.google.com/intl/en_ALL/mapfiles/arrowie.gif';
	icon.mozPrintImage = 'http://www.google.com/intl/en_ALL/mapfiles/arrowff.gif';
	icon.iconSize = new google.maps.Size(39, 34);
	icon.shadowSize = new google.maps.Size(39, 34);
	icon.iconAnchor = new google.maps.Point(11, 34);
	icon.infoWindowAnchor = new google.maps.Point(13, 2);

	map = new google.maps.Map2(document.getElementById('map'));
	map.addControl(new google.maps.SmallMapControl());
	map.addControl(new google.maps.ScaleControl());

	var geocoder = new google.maps.ClientGeocoder();
	var mgr;
	
	var callBackPoint = {
		setPointAddress:
			function(point){
				if (point) {
					map.setCenter(point, 12);
					marker = new google.maps.Marker(point, icon);
					google.maps.Event.addListener(marker, 'click', function() {
						var div = document.createElement('DIV');
						var a = document.createElement('A');
						var br = document.createElement('BR');
						var span = document.createElement('SPAN');
						a.setAttribute('href',internalurl);
						a.appendChild(document.createTextNode(fullname));
						div.appendChild(a);
						div.appendChild(br);
						span.style.fontSize = '12px';
						span.appendChild(document.createTextNode(specialty));
						div.appendChild(span);
						marker.openInfoWindowHtml(div);
					});
					map.addOverlay(marker);
					if (shoDesc) google.maps.Event.trigger(marker, "click");
					if (document.getElementById('DirectoryPrintMap') != null)
					document.getElementById('DirectoryPrintMap').style.display = 'block';
				}else{
					geocoder.getLatLng(cityname+','+countryname, callBackPoint.setPointCity);
				}
			},
		setPointCity:
			function(point){
				if (point) {
					map.setCenter(point, 12);
					marker = new google.maps.Marker(point, icon);
					google.maps.Event.addListener(marker, 'click', function() {
						var div = document.createElement('DIV');
						var a = document.createElement('A');
						var br = document.createElement('BR');
						var span = document.createElement('SPAN');
						a.setAttribute('href',internalurl);
						a.appendChild(document.createTextNode(fullname));
						div.appendChild(a);
						div.appendChild(br);
						span.style.fontSize = '12px';
						span.appendChild(document.createTextNode(specialty));
						div.appendChild(span);
						marker.openInfoWindowHtml(div);
					});
					map.addOverlay(marker);
					if (shoDesc) google.maps.Event.trigger(marker, "click");
					if (document.getElementById('DirectoryPrintMap') != null)
					document.getElementById('DirectoryPrintMap').style.display = 'block';
				}
			}
	}
	
	geocoder.getLatLng(address+','+cityname+','+statename+' '+zipcode, callBackPoint.setPointAddress);
	directions = new google.maps.Directions(map, document.getElementById('DirectoryDrivingDirections'));
}

google.setOnLoadCallback(loadMap);

function getDirections()
{
	var input = document.getElementById('DirectoryYourAddress');
	if (input.value != '') {		
		directions.load('from: ' + input.value + ' to: '+address+', '+cityname+', '+statename+' '+zipcode,{ "locale": "en" });
		GEvent.addListener(directions, "load", handleCodes);
		GEvent.addListener(directions, "error", handleErrors); 
	}
}

function handleCodes(){
	if (directions.getStatus().code == 200){
		map.clearOverlays();
		document.getElementById('DirectoryDrivingDirections').style.display = 'block';
		document.getElementById('DirectoryPrintDirections').style.display = 'block';
	}
}

function handleErrors(){
   if (directions.getStatus().code != 500){
	   document.getElementById('DirectoryPrintDirections').style.display = 'none';
	   alert("No driving directions found, please check your start address.");
	   loadMap();
   }
}

function getDirectionsFromBeginning(location_ip)
{
	var icon = new google.maps.Icon();
	icon.image = 'http://www.google.com/intl/en_ALL/mapfiles/arrow.png';
	icon.shadow = 'http://www.google.com/intl/en_ALL/mapfiles/arrowshadow.png';
	icon.transparent = 'http://www.google.com/intl/en_ALL/mapfiles/arrowtransparent.png';
	icon.printImage = 'http://www.google.com/intl/en_ALL/mapfiles/arrowie.gif';
	icon.mozPrintImage = 'http://www.google.com/intl/en_ALL/mapfiles/arrowff.gif';
	icon.iconSize = new google.maps.Size(39, 34);
	icon.shadowSize = new google.maps.Size(39, 34);
	icon.iconAnchor = new google.maps.Point(11, 34);
	icon.infoWindowAnchor = new google.maps.Point(13, 2);

	map = new google.maps.Map2(document.getElementById('map'));
	map.addControl(new google.maps.SmallMapControl());
	map.addControl(new google.maps.ScaleControl());

	directions = new google.maps.Directions(map, document.getElementById('DirectoryDrivingDirections'));

	map.clearOverlays();
	directions.load('from: ' + input.value + ' to: '+address+', '+cityname+', '+statename+' '+zipcode,{ "locale": "en" });
	document.getElementById('DirectoryDrivingDirections').parentNode.parentNode.style.display = 'block';
	document.getElementById('DirectoryPrintDirections').style.display = 'block';
}

function printMap()
{
	window_print_it = window.open('print-map.php?idprovider='+idproviderforprint+idcobranderforprint, 'print_it', 'width=420, height=420, scrollbars=0');
}

function printDirections()
{
	window_print_it = window.open('print-directions.php?idprovider='+idproviderforprint+idcobranderforprint+'&from=' + document.getElementById('DirectoryYourAddress').value, 'print_it', 'width=436, height=440, scrollbars=1');
}