// For whole USA
// var centerLatitude = 47.6168;
// var centerLongitude = -122.321;
// var startZoom = 9;
// var maxIcons = 100;
// For WA state
//var centerLatitude = 47.195;
//var centerLongitude = -120.938;
//var startZoom = 7;
var map;

// icon for clusters
var iconCluster = new GIcon();
iconCluster.image = "/images/cluster.png";
iconCluster.shadow = "/images/cluster_shadow.png";
iconCluster.iconSize = new GSize(26, 25);
iconCluster.shadowSize = new GSize(37, 25);
iconCluster.iconAnchor = new GPoint(13, 25);
iconCluster.infoWindowAnchor = new GPoint(13, 1);
iconCluster.infoShadowAnchor = new GPoint(26, 13);

// icon for single points
var iconSingle = new GIcon();
iconSingle.image = "/images/standard.png";
iconSingle.shadow = "/images/single_shadow.png";
iconSingle.iconSize = new GSize(12, 20);
iconSingle.shadowSize = new GSize(22, 20);
iconSingle.iconAnchor = new GPoint(6, 20);
iconSingle.infoWindowAnchor = new GPoint(6, 1);
iconSingle.infoShadowAnchor = new GPoint(13, 13);

// icon for premium points
var iconPremium = new GIcon();
iconPremium.image = "/images/featured_orange.png";
iconPremium.shadow = "/images/single_shadow.png";
iconPremium.iconSize = new GSize(12, 20);
iconPremium.shadowSize = new GSize(22, 20);
iconPremium.iconAnchor = new GPoint(6, 20);
iconPremium.infoWindowAnchor = new GPoint(6, 1);
iconPremium.infoShadowAnchor = new GPoint(13, 13);

function init()
{
	NiftyLoad();
	if (GBrowserIsCompatible()){
		map = new GMap2(document.getElementById("map"));
		var location = new GLatLng(centerLatitude, centerLongitude);
		map.setCenter(location, startZoom);
		
		if (c == 'l') {
			map.addControl(new GLargeMapControl())
		} else {
			map.addControl(new GSmallMapControl());
		}
		map.addControl(new GMapTypeControl());

		if (c == "dz") {
			var styleOpts = {};
			var otherOpts = {};
			// Need sticky off so clicks on points don't zoom to max
			otherOpts.stickyZoomEnabled = false;
			otherOpts.buttonHTML = 'Click to zoom';
			otherOpts.buttonZoomingHTML = 'Click to exit zoom';
			zcontrol = new DragZoomControl(styleOpts, otherOpts, {});
			map.addControl(zcontrol);
		}
		
		updateMarkers();
		
		GEvent.addListener(map,'zoomend',function() {
			updateMarkers();
		})
	}
}

function updateMarkers()
{
	
	// remove the existing points
	map.clearOverlays();
	// only display markers if zoomed in to region or beyond
	var z = map.getZoom();
	if (z >= 7) {
		// get bounds
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		
		var regurl = '/funeral_homes/by_bounds?ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + '&max=' + maxIcons + '&pre=' + showPremium + "&n=" + newWindow + "&only=reg";
		// retrieve the points
		var request = GXmlHttp.create();
		request.open('GET', regurl, true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var data = request.responseText;
				var points = eval('('+data+')');
			
				// create each point from the list
				for (var i=0; i < points.length; i++) {
					var point = new GLatLng(points[i].latitude,points[i].longitude);
					var marker = createMarker(point,points[i].type,points[i].html,points[i].title);
					map.addOverlay(marker);
				}
			}
		}
		
		var premurl = '/funeral_homes/by_bounds?ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + '&max=' + maxIcons + '&pre=' + showPremium + "&n=" + newWindow + "&only=prem";
		// retrieve the points
		var request = GXmlHttp.create();
		request.open('GET', premurl, true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var data = request.responseText;
				var points = eval('('+data+')');
			
				// create each point from the list
				for (var i=0; i < points.length; i++) {
					var point = new GLatLng(points[i].latitude,points[i].longitude);
					var marker = createMarker(point,points[i].type,points[i].html,points[i].title);
					map.addOverlay(marker);
				}
			}
		}
		
		request.send(null);
	}
}

function important (marker, b) {
	return 100;
}

function createMarker(point, type, html, tooltip) {
	if (type == 'c') {
		var marker = new GMarker(point,iconCluster);
	} else if (type == 'p') {
		var marker = new GMarker(point,{icon:iconPremium, title:tooltip, zIndexProcess:important});
	} else {
		var marker = new GMarker(point,iconSingle);
	}
	GEvent.addListener(marker,'click',function() {
		marker.openInfoWindowHtml(html);
	});
	// GEvent.addListener(marker,'mouseover',function() {
	// 	marker.openInfoWindowHtml(html);
	// });
	return marker;
}

window.onload = init;
window.onunload = GUnload;