﻿var geocoder = null;
var map = null;
var mapTable = null

var curAddress = "";
var intervalId = null;
var opacity = 0;

//IDs
var mapDivId = "map";
var mapTableId = "tblMap";
var mapsAPIKey = "";

//var mapsAPIKey = "ABQIAAAApm5KuNrwc-N5oMvJLLmRNhSMhnr0ZMsH4TRE-JHDdMQzHMwqGxQNQDJuty-fc6MIoAGMkRvLjGtX8A" // Our default key

if (mapsAPIKey != "")
    document.write('<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' + mapsAPIKey + '" type="text/javascript"></script>');
 
//include Google Maps API
       
function loadMap() {
    
    if (mapsAPIKey == "") return;
    
    if (GBrowserIsCompatible()) 
    {
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById(mapDivId));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        map.removeMapType(G_HYBRID_MAP);

        var mapControl = new GMapTypeControl();
        map.addControl(mapControl);
        map.addControl(new GLargeMapControl());

        map.enableScrollWheelZoom();
        map.enableContinuousZoom();
        
        mapTable = document.getElementById(mapTableId); 
    }
}

function showAddress(address, xPos, yPos) 
{ 
    if (mapsAPIKey == "") return;
    
    geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                alert(address + " not found");
            } 
            else {
                curAddress = address;
                map.setCenter(point, 13);
                var marker = new GMarker(point);
               
                GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml('<b>' + curAddress + '</b>', {maxWidth: 50}); });

                map.clearOverlays();
                map.addOverlay(marker);
                //marker.openInfoWindowHtml(address);
                
                fadeMapIn(xPos, yPos);

            }
        }
    );
}

function setOpacity(val) {
    mapTable.style.MozOpacity = val;
    mapTable.KhtmlOpacity = val/10; 
    mapTable.style.opacity = val/10;
    mapTable.style.filter = 'alpha(opacity=' + val*10 + ')';
    
}

function resetOpacity() {
    mapTable.style.opacity = undefined;
    mapTable.style.filter = "";
}

function fadeMapOut() {
    setOpacity(10);
    opacity = 10;
    
    intervalId = setInterval('fadeMapOutInterval()', 30);      
}

function fadeMapOutInterval()
{
    opacity--;
    
    if (opacity < 0)
    {
        mapTable.style.visibility = 'hidden';
        clearInterval(intervalId);
        return;
    }
    
    setOpacity(opacity);
}

function fadeMapIn(xPos, yPos) {      
    if (mapTable.style.visibility == 'visible')
        return;
        
    setOpacity(0);     
    opacity = 0;
       
    mapTable.style.top = yPos;
    mapTable.style.left = xPos;
    mapTable.style.visibility = 'visible';
     
    intervalId = setInterval('fadeMapInInterval()', 30);    
}

function fadeMapInInterval()
{
    opacity++;
    
    if (opacity > 10)
    {
        resetOpacity(); 
        clearInterval(intervalId);
        return;
    }
    
    setOpacity(opacity);
}
