// ==UserScript==
// @name           onNYTurf Subway Map Butler
// @namespace      http://arantius.com/misc/greasemonkey/
// @description    Improve the behavior of the Subway Map provided by onNYTurf.
// @include        http://www.onnyturf.com/subway*
// ==/UserScript==

var mapEl=document.getElementById('map');

// Step one: Reformat, no nags.
document.getElementById('bitright').style.display='none';
mapEl.style.margin='6px';
mapEl.style.minHeight='200px';

// In content scope ...
window.addEventListener('load', function() {
	document.location.assign('javascript:('+function() {
		// Step two: make the map powerful.
		NYCSubway.map.addControl(new GOverviewMapControl());
		NYCSubway.map.enableScrollWheelZoom();

		// Step three: Wire up a new geocoder.
		var geocoder=new GClientGeocoder();
		geocoder.setViewport(NYCSubway.bounds);

		window.showAddress=function(event) {
			if (event) event.preventDefault();

			var address=$('Address').value;

			geocoder.getLatLng(
				address,
				function(point) {
					if (!point) {
						$('loading').innerHTML = "<p><b>We're Sorry!</b> <i>The address \""
							+address.replace(/</g, '&lt;');
							+"\" could not be found in the NYC area.</i> Please try again.</p>";
						$('loading').style.display='block';
					} else {
						$('loading').style.display='none';
						NYCSubway.plotAdd(point.x, point.y, address);
					}
				}
			);
		}

		// Step four: Create an input form that works with pressing ENTER.
		var addr=$('Address');
		var addrP=addr.parentNode;
		$('notice').style.display='none';

		var form=document.createElement('form');
		form.appendChild(addr.previousSibling);
		form.appendChild(addr);
		form.appendChild(document.createTextNode(' '));
		form.addEventListener('submit', window.showAddress, true);

		var input=document.createElement('input');
		input.value='Plot It!';
		input.type='submit';
		form.appendChild(input);

		addrP.appendChild(form);

		// Step five: search in query string from load?  Run it!
		if (document.location.search) {
			var match=document.location.search.match(/address=([^&]+)/);
			if (match[1]) {
				var addr=match[1].replace(/\+/g, '%20');
				addr=unescape(addr);
				$('Address').value=addr;
				window.showAddress();
			}
		}

		// Finally: Focus search field.
		$('Address').focus();
	}+')()');
}, true);
