/*
* public vars
*/
var addresses = [];
var map;
var geocoder;
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var currentMarker;
var lastMarker;
var gmarkers = [];


/*
* Initialize google maps API
*
*/
function initialize_googlemaps()
{
//	geocoder = new google.maps.Geocoder();
	
	var latlng = new google.maps.LatLng(37.937834452806605, -107.81149843103026);
	directionsDisplay = new google.maps.DirectionsRenderer();
	
	var myOptions = {
		zoom: 8,
		center: latlng,
		mapTypeControl: true,
		mapTypeControlOptions:{style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
		navigationControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
		
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);					directionsDisplay.setPanel(document.getElementById("directionsPanel"));



}


function plotAddressArray( addresses_to_add )
{
	addresses = addresses_to_add;
	
	/* get longitude, latitude for locations */
	for( var i = 0; i < addresses_to_add.length; i++ ) {
	geocoder.geocode( { address:addresses[ i ].location }, geoResults );
	}
}


function plotSingleAddress( address )
{
	plotAddressArray( [address] );
}

/*
* Callback function for geocode calls
*
* @param results returned by geocoder.geocode()
* @param status returned by geocoder.geocode()
*/
function geoResults(results, status) {
	if (status == google.maps.GeocoderStatus.OK) {
//		map.setCenter(results[0].geometry.location);
		plotLocation(results[0].geometry.location,results[0].formatted_address);
	} else {
		alert("Geocode was not successful for the following reason: " + status);
	}
}

function latlngResults(results) {
	addresses = results;
	for( var i = 0; i < results.length; i++ ) {
		var mylatlng = results[i].lat+", "+results[i].lng
//		map.setCenter(mylatlng);
		plotLocation(mylatlng,results[i].name);
	}
}

/*
* Parse through all addresses to find correct object
*
* @param address is the formatted address returned by geocode results
* @returns address
*/
function getAddressObject( address )
{
var latlng_address = address.slice( 0, address.indexOf( "," ) );
//var latlng_address = address;
for( var i = 0; i < addresses.length; i++ ) { 
if( addresses[ i ].lat.indexOf( latlng_address ) > -1 ) {
return addresses[ i ];
}
}
}

/*
* Plot location and infowindow
*
* @param location is latitude, longitude returned by geocode results
* @param formatted_address returned by from geocode results
*/
function plotLocation( location, formatted_address )
{
	/* grab the correct address object */
	var addressObject = getAddressObject( location );

	var myLatlng = new google.maps.LatLng(addressObject.lat, addressObject.lng)
	
	/* set infoContent */
	var infoContent = '<table width=225 border=0 cellpadding=1 cellspacing=1>'
		+ '<tr>'
		+ '<td rowspan=4 valign=top>' + addressObject.link + '<img src="' + addressObject.photo + '" width=75 height=50 border=0 valign=top></a></td>'
		+ '</tr>'
		+ '<tr>'
		+ '<td class=sbc><b>' + addressObject.broker + '</b><br>' + addressObject.name + '<br>Price: ' + addressObject.price + '<br>' + addressObject.link + '<span class=sbc>More Info</span></a></td>'
		+ '</tr>'
		+ '<tr>'
		+ '<td class=inputgray>Find your way here from: Address, City, State</td>'
		+ '</tr>'
		+ '<tr>'
		+ '<td class=inputgray><form action="about.asp" onsubmit="calcRoute();return false;">'
		+ '<input class=inputgray type="text" size=45 id="start" value="">'
		+ '<input type=hidden id="end" value="'+formatted_address+'"><input class=inputgray type="submit" value="GO">'
		+ '</form>'
		+ '</td>'
		+ '</tr>'
		+ '</table>';

	
	/* create the marker */
var marker = new google.maps.Marker({ 
	map:map,
	position:myLatlng,
	title:addressObject.name
});
	marker.mycategory = addressObject.category;
	gmarkers.push(marker);
	
	/* create the infowindow & give the marker a reference to its infowindow */
	var infowindow = new google.maps.InfoWindow({ content:infoContent, position:myLatlng, size:new google.maps.Size(200,100)});
	marker.infowindow = infowindow;
	
	/* add event listener and callback function passing the event's target which is the clicked marker */
	google.maps.event.addListener(marker, 'click', function() { openMarker( marker ) } );
}

/*
* opens infowindow of marker
*
* @param marker is the target of the click event
*/
function openMarker( marker )
{
	if( currentMarker != null ) {
		lastMarker = currentMarker;
		closeMarker( lastMarker );
	}
	
	currentMarker = marker;
	
	/* using the marker's reference to its infowindow, open infowindow */
	marker.infowindow.open(map,currentMarker);
}

/*
* closes infowindow of the marker
*
* @param marker is the target of the click event
*/
function closeMarker( marker )
{
	marker.infowindow.close();
}

/*
* Calculates route
*
*/
function calcRoute() {
	var start = document.getElementById("start").value;
	var end = document.getElementById("end").value;
	var request = {
		origin:start,
		destination:end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};

	directionsService.route(request, directionsResults);
}

/*
* Directions results callback
*
* @param response returned by directionsService.route()
* @param status returned by directionsService.route()
*/
function directionsResults(response, status) {
	if (status == google.maps.DirectionsStatus.OK) {
		directionsDisplay.setDirections(response);
	}
}
