google.load("maps", "3", {other_params:"sensor=false"});

var cache = [];
var $j = jQuery.noConflict();

$j(document).ready(function($)
{
	try
	{
		$j("#nav li").hover(
			function()
			{
				var childUl = $j(this).find("ul");
				childUl[0].show(1);
			},
			function()
			{
				var childUl = $j(this).find("ul");
				childUl[0].hide(1);
			}
		);
	}
	catch(e){}
	
	//Build list items and points
	var directoryItem;
	var i=0;
	var highlightCircle = null;
	var currentMarker = null;
	
	// Check to see if this browser can run the Google API
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET",'lease.xml',false);
	xmlhttp.send();
	xmlDoc = xmlhttp.responseXML;
	
	//Prepare the map
	$("#map-col").html('<div id="map-area" style="position: relative;"><div id="map-canvas" style="width: 500px; height: 400px"></div></div>');
	var bounds = new google.maps.LatLngBounds();
	var geocoder = new google.maps.Geocoder();
	var mapOptions = 
	{
zoom: 13,
		mapTypeControl: false,
		navigationControl: true,
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
		mapTypeId: google.maps.MapTypeId.ROADMAP      
	};
	var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
	google.maps.event.addListener(map, 'click', function(){
		infoWindow.close();
	});		
	
	var resultsets = xmlDoc.getElementsByTagName("resultset");
	for(var count=0;count<resultsets.length;count++)
	{
		var results = resultsets[count].getElementsByTagName("result");
		for(var counted=0; counted<results.length; counted++)
		{
			directoryItem = new DirectoryItem();
			
			if (results[counted].getElementsByTagName("address")[0].childNodes[0] != null)
			{
				directoryItem.name = results[counted].getElementsByTagName("title")[0].childNodes[0].nodeValue;
				directoryItem.address = results[counted].getElementsByTagName("address")[0].childNodes[0].nodeValue + ' ';
				directoryItem.street = results[counted].getElementsByTagName("address")[0].childNodes[0].nodeValue;
				if (results[counted].getElementsByTagName("city")[0].childNodes[0] != null)
				{
					directoryItem.address += results[counted].getElementsByTagName("city")[0].childNodes[0].nodeValue + ', ';
					directoryItem.city = results[counted].getElementsByTagName("city")[0].childNodes[0].nodeValue;
				}
				directoryItem.address += results[counted].getElementsByTagName("state")[0].childNodes[0].nodeValue + ' ';
				directoryItem.state = results[counted].getElementsByTagName("state")[0].childNodes[0].nodeValue;
				directoryItem.address += results[counted].getElementsByTagName("zipcode")[0].childNodes[0].nodeValue;
				directoryItem.zip += results[counted].getElementsByTagName("zipcode")[0].childNodes[0].nodeValue;
				directoryItem.phone = results[counted].getElementsByTagName("phone")[0].childNodes[0].nodeValue;
				directoryItem.buildMarker(map,bounds,geocoder,counted+1);
			}
		}
	}
});

//DirectoryItem object
function DirectoryItem()
{
	var prevmark = null;
	this.name = '';
	this.address = '';
	this.phone = '';
	this.html = '';
	this.street = '';
	this.city = '';
	this.state = '';
	this.zip = ''
	this.buildMarker = function(map,bounds,geocoder,num)
	{
		var infowindowLevel = 0;
		var name = this.name;
		var phone = this.phone;
		var address = this.address;
		var street = this.street;
		var city = this.city;
		var state = this.state;
		var zip = this.zip;
		geocoder.geocode(
		{'address': this.address},
		function(results, status)
		{
			if (status != google.maps.GeocoderStatus.OK) 
			{
				//alert('Sorry, we were unable to geocode that address.');
			} 
			else 
			{
				point = results[0].geometry.location;
				var image = 'images/map marker/map-marker-' + num + '.png';
				var marker = new google.maps.Marker({
					position: point, 
					map: map,
					icon: image
				});
				bounds.extend(point);
				map.setCenter(bounds.getCenter());
				//map.fitBounds(bounds);
				var contentString = 
					'<div id="mapcontent">' +
					name + '<br />' +
					street + '<br />' +
					city + ', ' +
					state + ' ' +
					zip + '<br />' +
					'P: ' + phone.substr(0,3) + '-' + phone.substr(3,3) + '-' + phone.substr(6,4) +
					'<br>Directions: <b>To here</b><br />Start address:<form action="directions.php" method="post">' + '<input type="text" SIZE=20 name="saddr" id="saddr" value="" /><br>' + '<INPUT value="Get Directions" TYPE="SUBMIT">' + '<input type="hidden" name="daddr" value="' + address + '"/>' +
					'</div>';
				var infowindow = new google.maps.InfoWindow({
					content: contentString
				});
				google.maps.event.addListener(marker, 'click', function() {
					infowindow.setZIndex(++infowindowLevel);
					infowindow.open(map,marker);
				});
			}
		});
	};
}
