// Buoy Icons - Color Coded by report scale
var baseIcon = new GIcon(); 
baseIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker.png';
baseIcon.shadow = 'http://assets.buoyalarm.com/images/gmap/shadow.png';
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

var tinyIcon = new GIcon(baseIcon);
tinyIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker_tiny.png';

var smallIcon = new GIcon(baseIcon);
smallIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker_small.png';

var mediumIcon = new GIcon(baseIcon);
mediumIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker_medium.png';

var largeIcon = new GIcon(baseIcon);
largeIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker_large.png';

var xlargeIcon = new GIcon(baseIcon);
xlargeIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker_xlarge.png';

var alarmIcon = new GIcon(baseIcon);
alarmIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker_alarm.png';

var offlineIcon = new GIcon(baseIcon);
offlineIcon.image = 'http://assets.buoyalarm.com/images/gmap/marker_offline.png';

var customIcons = [];
customIcons["tiny"] = tinyIcon;
customIcons["small"] = smallIcon;
customIcons["medium"] = mediumIcon;
customIcons["large"] = largeIcon;
customIcons["xlarge"] = xlargeIcon;
customIcons["alarm"] = alarmIcon;
customIcons["offline"] = offlineIcon;

// initialize the map
function initialize(dom_id,lat,lng,zoom) {
	if (GBrowserIsCompatible()) {
	
		var map = new GMap2(document.getElementById(dom_id));
		map.setMapType(G_NORMAL_MAP);
		map.setCenter(new GLatLng(lat, lng), zoom);
		
		// User Interface
		var customUI = map.getDefaultUI();
		customUI.controls.menumaptypecontrol = false;
		map.setUI(customUI);
		
		// Parse available buoy XML file
		// currently dynamically created, but probably should be added to cron and cached
		GDownloadUrl("/lib/xml/gmap_markers.php?<?php print time(); ?>", function(data) {
		  var xml = GXml.parse(data);
		  var markers = xml.documentElement.getElementsByTagName("marker");

		  for (var i = 0; i < markers.length; i++) {

			var id = markers[i].getAttribute("id");
			var buoyid = markers[i].getAttribute("buoyid");
			var type = markers[i].getAttribute("type");
			var status = markers[i].getAttribute("status");
			var swh = markers[i].getAttribute("swh");
			
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
			
			var marker = createMarker(point, buoyid, type, status, swh);
			map.addOverlay(marker);
		  }
		});
	}
}

function createMarker(point, buoyid, type, status, swh) {
	
	var btitle = 'Buoy ' + buoyid;
	
	// if buoy status is offline, override icon type and set title
	if (status == 0) {
		btitle = btitle + ' (Offline)';
		type = 'offline';
	} else if (status == 3) {
		btitle = btitle + ' (Adrift)';
		type = 'offline';
	} else if (status == 2) {
		btitle = btitle + ' (Delayed)';
	}

	var marker = new GMarker(point, {title:btitle,icon:customIcons[type],zIndexProcess:statusOrder});
	
	// used for z-index ordering
	marker.status = status;
	marker.zindex = swh;
	
	GEvent.addListener(marker, 'click', function() {
		window.open('/buoy.php?id='+buoyid,'_self');
	});

	return marker;
}

function statusOrder(marker,b) {
	if (marker.status != 1) {
		return GOverlay.getZIndex(marker.getPoint().lat()) - 1000000;
	} else {
		return GOverlay.getZIndex(marker.getPoint().lat()) + marker.swh*1000000;
	}
}

