// google map support scripts
//
// Copyright (c) 2006-2007 San Jacinto High Rollers
// You may copy this script as long as you include attribution in your script.

// keys for directories
// for this site, each directory, "dir", can have one of the following variations:
//
// http://sjhr.org/dir/
// http://www.sjhr.org/dir/
// http://sjhr.net/dir/
// http://www.sjhr.net/dir/
//
var googleKeys = new Array();
// home directory
googleKeys['http://sjhr.org/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthSo0Tbs44aVVrAukM1yz8LfLlalfRTumQgUbB33nVFMYTUuXXEKXzSZhg';
googleKeys['http://www.sjhr.org/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthQtRn3eymzWQqhHeJ_jAjTdkvRJdRTkSk4RRJPXQxGHC41cr3eA27U38A';
googleKeys['http://sjhr.net/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthSbam3Ry_px7KH0mOjHd2jeFTl1vRTSvHfWvt08cllHPLA63mcwLo9UzA';
googleKeys['http://www.sjhr.net/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthRhVIBS13KskweWOlQciirhkFhzbxTMtoK-Vnl7EyCZ3Wdlkq0bstwzRg';
// events directory
googleKeys['http://sjhr.org/events/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthQ284paK8eyslK4HrKOGMABQNn3KxSySNSGEBinrTDbvr3R_bNOWcG7xw';
googleKeys['http://www.sjhr.org/events/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthTAUs9wtYQ0XNmYXEFWEUWeAfVSsRRq7Kf9MO_EmSwDSOPeRgZsGNkn8w';
googleKeys['http://sjhr.net/events/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthRs-NZP07oC5LdZiIsyKEtjZ6bzAxRYQGPk62PVDEGZv0cEIgqZEwbYUA';
googleKeys['http://www.sjhr.net/events/'] = 'ABQIAAAA4FB0kZ-lxrCo5vxr7S5tthRnZmwkrzzwe32Q4FUEz11bXlxQ4hQDZUpn1VG0-djzHkk15kG3J6zn9g';

// info window states
var INFO_NEUTRAL = 0;
var INFO_TO = 1;
var INFO_FROM = 2;

// determine directory for the current page
// returns null if unable to determine it
function getDirectory() {
    // determine current directory for page
    var path = window.location.href;
    var i = path.lastIndexOf('/');
    if (i < 0) {
        // can't determine directory
        return null;
    }
    return path.substring(0, i+1);
}

// get the key for the current directory
// returns null if there isn't one
function getGoogleKey() {
    var key = null;
    var dir = getDirectory();
    if (dir != null) {
        key = googleKeys[dir];
    }
    return key ? key : null;
}

// output the include line for google maps
// outputs nothing if no key is available
function includeGoogle() {
    var key = getGoogleKey();
    if (key) {
        document.writeln('<script src="http://maps.google.com/maps?file=api' + 
            '&v=1&key=' + key + '" type="text/javascript"></script>');
    }
}

// create a map
// returns map reference or null if not created
function createMapCase(id, longtitude, latitude, zoom) {
    // find id to display at
    var element = document.getElementById(id);
    if (!element) {
        alert('Unable to locate map id "' + id + '".');
        return null;
    }
    
    // is a key available for mapping?
    if (!getGoogleKey()) {
        element.innerHTML = 'Missing key for this map.';
        return null;
    }
    
    // is this browser supported?
    if (!GBrowserIsCompatible()) {
        element.innerHTML = 'The map feature does not support this browser.';
        return null;
    }

    // create the map
    return new MapCase(element, longtitude, latitude, zoom);
}

//
// MapCase
//
// map container
//
function MapCase(element, longtitude, latitude, zoom) {
    this.center = new GPoint(longtitude, latitude);
    this.zoom = zoom;
    
    // create google map
    this.map = new GMap(element);
    this.map.addControl(new GLargeMapControl());
    this.map.addControl(new GMapTypeControl());
    this.map.centerAndZoom(this.center, zoom);
}

//
// MapLocation
//
// Provides a marker with info window
//
function MapLocation(map, longtitude, latitude, zoom, name, info) {
    this.map = map;
    this.point = new GPoint(longtitude, latitude);
    this.zoom = zoom;
    this.name = name;
    this.info = info;
    
    this.infoOpen = false;
    // (used to suppress close processing during reopen)
    this.suppressInfoClose = false;
    
    // add a marker to the map for this location
    var point = new GPoint(longtitude, latitude);
    this.marker = new GMarker(point);
    GEvent.bind(this.marker, 'click', this, this.onMarkerClick);
    GEvent.bind(this.marker, 'infowindowopen', this, this.onInfoWindowOpen);
    GEvent.bind(this.marker, 'infowindowclose', this, this.onInfoWindowClose);
    map.map.addOverlay(this.marker);
}

// handle click on the marker
// note: since this bound to an event and is overridden, the implementation
// is split. override implementation, not this method.
function MapLocation_onMarkerClick() {
    // all functionality should be in implementation!
    this.onMarkerClickImpl();
}
MapLocation.prototype.onMarkerClick = MapLocation_onMarkerClick;

function MapLocation_onMarkerClickImpl() {
    if (!this.infoOpen) {
        this.infoOpen = true;
        
        // close info window if already open
        this.map.map.closeInfoWindow();
        
        // set state for this location
        this.map.map.centerAndZoom(this.point, this.zoom);
        
        // open the info window
        this.openInfoWindow(INFO_NEUTRAL, '');
    }
}
MapLocation.prototype.onMarkerClickImpl = MapLocation_onMarkerClickImpl;

// infowindowopen event handler
// note: since this bound to an event and is overridden, the implementation
// is split. override implementation, not this method.
function MapLocation_onInfoWindowOpen() {
    // all functionality should be in implementation!
    this.onInfoWindowOpenImpl();
}
MapLocation.prototype.onInfoWindowOpen = MapLocation_onInfoWindowOpen;

function MapLocation_onInfoWindowOpenImpl() {
	// does nothing
}
MapLocation.prototype.onInfoWindowOpenImpl = MapLocation_onInfoWindowOpenImpl;

// infowindowclose event handler
// note: since this bound to an event and is overridden, the implementation
// is split. override implementation, not this method.
function MapLocation_onInfoWindowClose() {
    // all functionality should be in implementation!
    if (!this.suppressInfoClose) {
    	this.onInfoWindowCloseImpl();
   	}
}
MapLocation.prototype.onInfoWindowClose = MapLocation_onInfoWindowClose;

function MapLocation_onInfoWindowCloseImpl() {
    if (this.infoOpen) {
        this.infoOpen = false;
        this.map.map.centerAndZoom(this.map.center, this.map.zoom);
    }
}
MapLocation.prototype.onInfoWindowCloseImpl = MapLocation_onInfoWindowCloseImpl;

// handles click on from/to links in info window
var infoWindowLocation = null;

function infoWindowClick(oldState, newState) {
	// get the user's entered address from the form
	var address = '';
	if (oldState == INFO_TO) {
		address = document.directions.saddr.value;
	} else if (oldState == INFO_FROM) {
		address = document.directions.daddr.value;
	}
	
	infoWindowLocation.openInfoWindow(newState, address);
}

// open the info window, reflecting the current state
function MapLocation_openInfoWindow(state, address) {
    var html = '<div style="font-family: Arial; font-size: small">' + 
        this.info + '<br><br>Directions:&nbsp;';
        
    infoWindowLocation = this;
    
    var label;
    var locValue;
    var userValue;
    if (state == INFO_TO) {
        html += '<b>To&nbsp;here</b>';
        label = 'Start';
        locValue = 'daddr';
        userValue = 'saddr';
    } else {
        html += '<a href="javascript:;" onclick="infoWindowClick(' + state + ',' + INFO_TO + 
        	'); return false;">To&nbsp;here</a>';
    }
    html += '&nbsp;&#8209;&nbsp;';
    
    if (state == INFO_FROM) {
        html += '<b>From&nbsp;here</b>';
        label = 'End';
        locValue = 'saddr';
        userValue = 'daddr';
    } else {
        html += '<a href="javascript:;" onclick="infoWindowClick(' + state + ',' + INFO_FROM + 
        	'); return false;">From&nbsp;here</a>';
    }
    
    if (state != INFO_NEUTRAL) {
    	html += '<br>' + 
    		'<form name="directions" class="directions" method="GET" ' + 
    		'action="http://maps.google.com/maps" target="_blank">' + 
    		label + ' address:<br>' + 
    		'<input name="' + userValue + '" value="' + address + 
    		'" style="width: 20em">' + 
    		'<input name="' + locValue + '" value="' + this.point.y + ',' + 
    		this.point.x + ' (' + this.name + ')" type="hidden">' + 
    		'<input name="hl" value="en" type="hidden"><br>' + 
			'<small>Example: 1400 Shepherd Dr, Houston, TX</small><br>' + 
    		'<input type="submit" value="Get Directions"></form>';
    }
    
    html += '</div>';
    this.suppressInfoClose = true;
    this.marker.openInfoWindowHtml(html);
    this.suppressInfoClose = false;
}
MapLocation.prototype.openInfoWindow = MapLocation_openInfoWindow;

