// JavaScript Document

var map;
var gmapMarkers = [];

window.addEvent('domready', function() {
	
	
	
	//Register and add Google Maps object
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmap"));
		map.setCenter(new GLatLng(37.711808, -97.245586), 3);
		map.addControl(new GLargeMapControl());
  	}
	
	//Add markers to map when moved
	GEvent.addListener(map, "dragend", getClubsByBounds);
	
	//Add markers to initial map position
	getClubsByBounds();
	
	//Ajax Functionality to update map when zip code is typed in.
	var zjax = new XHR({method: 'get'});
	
	zjax.addEvent('onSuccess', function() {
			var coords = Json.evaluate(zjax.response.text);
			$('zipcode').removeClass('ajax-loading');
			if(coords.latitude != 0 && coords.longitude != 0) {
				map.setCenter(new GLatLng(coords.latitude, coords.longitude), 9);
				getClubsByBounds();
			}
	});
	
	$('zipcode').addEvent('keyup', function() {
			z = $('zipcode').getValue();
			if(z.length == 5) {
				zjax.send('lib/ajax/getLtLngByZip.php', 'z='+z);
				$('zipcode').addClass('ajax-loading');
				$('notfound').addClass('hidden');
			}
	});

});

function getClubsByBounds() {
	var ajax = new XHR({method: 'get'});
	var bounds = map.getBounds();
	var ba = [bounds.getSouthWest().lat(), bounds.getSouthWest().lng(), bounds.getNorthEast().lat(), bounds.getNorthEast().lng()]; //Array of boundaries to join.
	
	ajax.addEvent('onSuccess', function() {
		var result = Json.evaluate(ajax.response.text);
		if(result.locations.length > 0) {
			addMarkers(result.locations);
			$('notfound').addClass('hidden');
		} else {
			$('notfound').removeClass('hidden');	
		}
	});
	
	ajax.send('lib/ajax/getClubsByBounds.php', 'b='+ba.join(','));
}

function createMarker(location) {
	var loc = location;
	//Create Custom Icon object
	var icon = new GIcon();
	icon.image = "images/btns/tfc-gIcon.png";
	icon.shadow = "images/btns/tfc-gIcon-shadow.png";
	icon.iconSize = new GSize(30, 30);
	icon.shadowSize = new GSize(38, 30);
	icon.iconAnchor = new GPoint(15, 15);
	icon.infoWindowAnchor = new GPoint(15, 15);
	icon.infoShadowAnchor = new GPoint(15, 15);
	
	//Create Marker object
	p = new GLatLng(location.latitude, location.longitude);
	var marker = new GMarker(p, {icon:icon});
	
	GEvent.addListener(marker, "click", function() {
		$('notfound').addClass('hidden');
		var htm = "<strong>"+loc.name+"</strong><br/>"
				+ loc.address1 + " " + loc.address2 + "<br/>"
				+ loc.city + ", " + loc.state + " " + loc.zip + "<br/><br/>"
				+ loc.phone + "<br/>"
				+ "<a href=\'" + loc.url + "\' target=\'_blank\'>" + loc.url + "</a>"
				+ "<p align=\'right\'><a href=\'directory.php#" + loc.id + "\'>More &raquo;</a></p>";
		marker.openInfoWindowHtml(htm);
		if(map.getZoom() < 11) { 
			map.setZoom(11);
		}
	});
	gmapMarkers[location.id] = 1;
	return marker;
}

function addMarkers(locations) {
	for(i=0; i<locations.length; i++) {
		if(!gmapMarkers[locations[i].id]) {
			map.addOverlay(createMarker(locations[i]));
		}
	}
}