﻿var names = new Object();
var messages = new Object();
var markers = new Object();
var currentMarker;
var currentName;
var map;
var gdir;
var directionMode = false;


function markerClick(marker, message, name)
{
    marker.openInfoWindowHtml(message); 
    currentMarker = marker; 
    document.getElementById('directionsLabel').innerHTML="<strong class='pageHeadline'>Directions to: "+ name +"</strong>";
    currentName = name;
    if(!directionMode)
        document.getElementById('dirButton1').style.display="inline";
}


function drawMap(lat, lng, divID)
{
    map = new GMap2(document.getElementById(divID));
    map.setCenter(new GLatLng(lat,lng),12);
    map.addControl(new GSmallZoomControl3D());
}

function insertMarker(id,lat,lng,name,message,externalListID)
{
    
    names[id] = name;
    messages[id] = message;
    
    var marker = new GMarker(new GLatLng(lat,lng));
    markers[id] = marker;
    
    map.addOverlay(marker);
    
    GEvent.addListener(marker, 'click', function() { markerClick(marker, message, name); });
    GEvent.addListener(marker, 'dblclick', function() { window.open('http://maps.google.com/maps?q='+ lat + "," + lng +' ('+ name +')', name.replace(' ', '') ); });

    var li = document.createElement('li');
    var link = document.createElement('a');
    link.href="javascript:void(0);";
    link.id=id;
    link.onclick=function() { externalClick(id); markerClick(markers[id], message, name); };
    link.innerHTML=name;

    li.appendChild(link);                         
    document.getElementById(externalListID).appendChild(li);
    

}

function externalClick(id)
{
    var marker = markers[id];
    marker.openInfoWindowHtml(messages[id]);
}

function displayDirectionControls()
{
    directionMode = true;
    document.getElementById('dirButton1').style.display="none";
    document.getElementById('listDiv').style.display="none";
    document.getElementById('dirDiv').style.display="block";
}

function getDirections(startAddress)
{
    document.getElementById('headlineP').innerHTML="Directions from "+ startAddress +" to "+ currentName +".";
    document.getElementById('dirDiv').style.display="none";
    gdir = new GDirections(map, document.getElementById('gdirDiv'));
    GEvent.addListener(gdir, 'error', handleErrors);
    gdir.load("from: " + startAddress + "(Start Address) to: "+ currentMarker.getLatLng().lat() + "," + currentMarker.getLatLng().lng() +"("+ currentName +")");
}

function handleErrors()
{
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert('No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.');
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert('A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.');
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert('The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.');
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert('The given key is either invalid or does not match the domain for which it was given.');
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert('A directions request could not be successfully parsed.');
    else 
        alert('An unknown error occurred.');
    
    window.location='map.aspx';
}
