document.write('<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>');


// global vars
var map;
var geocoder;
var latInp;
var lngInp;
var addressInp;
var marker;
var cicon;
var streetViewService;

function BaseInit()
{
    var mapDiv = document.getElementById("map");
	// if we can't find the map div, return.
    if( !mapDiv )
		return;
	map = new google.maps.Map(mapDiv, {zoom:15, scaleControl: true, streetViewControl: false});
	map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
    cicon = new google.maps.MarkerImage('../images/house_icon.png',
		new google.maps.Size(14, 15), new google.maps.Point(0,0), new google.maps.Point(7, 15));
}

// alternative load method, the parameters are actual lng lat values
function InitWithPoint(lat, lng)
{
    if( lat == null || lat == '' || lng == null || lng == '' )
        return;
    
    BaseInit();
    var point = new google.maps.LatLng(lat, lng);
    map.setCenter(point);
	markGeoCode(point);
}

// the initial load method, the parameters are client ids so that we know how to get data
function Init(lat, lng, add) 
{
    BaseInit();
	map.setCenter(new google.maps.LatLng(0,0));
	geocoder = new google.maps.Geocoder();
	loadData(lat, lng, add);
}

// alternative load method, parameters are actual lng, lat values and the address is an textual address
function InitMapWithDirections(lat, lng, address)
{
	if( lat == null || lat == '' || lng == null || lng == '' )
        return;

    BaseInit();
    var point = new google.maps.LatLng(lat,lng);
    map.setCenter(point);
    markGeoCode(point);
    setHome(address, 'mapDirections');        
}

function processPanorama(data, status)
{
    if( status == google.maps.StreetViewStatus.OK )
    {
        var panorama = new google.maps.StreetViewPanorama(document.getElementById("mapStreetView"));
        panorama.setPano(data.location.pano);
        panorama.setVisible(true);
    }
    else
    {
        document.getElementById("mapStreetView").innerHTML = "Sorry!  Street View is not available at this location.";
    }
}

function InitStreetview(lat, lng)
{
    
    var point = new google.maps.LatLng(lat,lng);
    streetViewService = new google.maps.StreetViewService();
    streetViewService.getPanoramaByLocation(point, 50, processPanorama);
}

function loadData(lat, lng, add)
{
	var point = null;
	latInp = document.getElementById(lat);
	lngInp = document.getElementById(lng);
	addressInp = document.getElementById(add);
	
	if( latInp.value == "" || lngInp.value == "" )
	{
		geoCodeAddress();
	}
	else
	{
		point = new google.maps.LatLng(latInp.value, lngInp.value);
		
		// this will cause our variables to overwrite each other,
		// but this is OK because it will be the same values.
		markGeoCode(point);
	}
}

function geoCodeAddress()
{
    var address = addressInp.value;
   
	if (address=='')
	{
	    address = "Boise, ID";
	}
	geocoder.geocode( {address: address},
	function(results, status)
	{
	    if( status == google.maps.GeocoderStatus.OK)
		{
			markGeoCode(results[0].geometry.location);
		}
		else
		{
		    // TODO: tell the user we couldn't find the address???
		}
	});
}



function markGeoCode(point)
{
	marker = new google.maps.Marker({position: point, icon: cicon, draggable: false, flat: true, bouncy: false});
	marker.setMap(map);
	map.panTo(point);
}

///// stuff for directions

var directionsPanel;
var directionsDisplay;
var directionsService;

function setHome(srcaddr, dirPanelID)
{
    directionsPanel = document.getElementById(dirPanelID);
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsService = new google.maps.DirectionsService();
    directionsDisplay.setMap(map);

    directionsDisplay.setPanel(directionsPanel);
    
    var request = {origin: srcaddr, destination: marker.getPosition(), travelMode: google.maps.DirectionsTravelMode.DRIVING};
    
    directionsService.route(request, function(result, status){
        if( status == google.maps.DirectionsStatus.OK) {
    
            directionsDisplay.setDirections(result);
        }
    });
}

