var map;
var directions;
var startZoom = 13;

// Creates a marker at the given point
// Clicking the marker will hide it

var myaddr;
var marker;
var tabhtml;
function initMap(id, n_tabhtml, n_myaddr, lat, lng) 
{
	//alert(id);
	//alert(n_myaddr);
	directions = undefined;
	myaddr = n_myaddr;
	tabhtml = n_tabhtml;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(id));
		var location = new GLatLng(lat, lng);
		map.setCenter(location, startZoom);
		marker = new GMarker(location, {title: 'Click for directions'});
		map.addOverlay(marker);
		marker.bindInfoWindowHtml(tabhtml);
		marker.openInfoWindowHtml(tabhtml);		
		map.addControl(new GScaleControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new GMapTypeControl());
	} else {
		alert('Your browser is not compatible with Google maps');
	}
}

// Get directions function

var googletimer;
function checkStatus(){
	if(directions.getStatus()){
		clearInterval(googletimer);
		googletimer=undefined;
		//alert(directions.getStatus().code);
		if(directions.getStatus().code!=200){
			alert('The address entered was not recognized by Google. Check spelling and that city and state are included.');
		} else {
			var a =0;
			//map.closeInfoWindow();
			marker.bindInfoWindowHtml(tabhtml);
			marker.openInfoWindowHtml(tabhtml);		
			$('printbutton').show();
	
			//var iw = map.getInfoWindow();
			//iw.hide();
			//iw.show();
		//	marker.closeInfoWindow();
		}
	}
}
var mycuraddress;
function doDir(obj) {
	mycuraddress = obj.addr.value;
	// dirPanel.innerHtml = ""; //clear the div before filling it with the directions
	var dirPanel = document.getElementById("directions");
	if(typeof directions=='undefined'){
		directions = new GDirections(map, dirPanel);	
	} else {
		directions.clear();
	}
	
	directions.load('from: ' + mycuraddress + ' to: ' + myaddr);

	if(typeof googletimer!='undefined'){
		clearInterval(googletimer);
	}
	googletimer = setInterval(checkStatus, 500);
}

		function getAddress(){
			return document.getElementById("addr").value;
		}

	function printDir(gigaddress){
		url = 'http://maps.google.com/maps?f=d&saddr=' + escape(getAddress()) + '&daddr=' + escape(gigaddress) + '&hl=en&geocode=&mra=ls&ie=UTF8&z=19&pw=2';
		window.open(url, '_blank', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=700, height=450');
	}


window.onunload=GUnload;