// 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(lat,lng,zoom) {
	if (GBrowserIsCompatible()) {
	
		var map = new GMap2(document.getElementById("gmap"));
		map.setMapType(G_NORMAL_MAP);
		map.setCenter(new GLatLng(lat,lng),zoom);
		
		// User Interface
		var customUI = map.getDefaultUI();
		customUI.maptypes.physical = false;
		customUI.maptypes.satellite = false;
		//customUI.controls.maptypecontrol = 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 location = markers[i].getAttribute("location");
			var type = markers[i].getAttribute("type");
			var ts = markers[i].getAttribute("ts");
			var swd = markers[i].getAttribute("swd");
			var swh = markers[i].getAttribute("swh");
			var swp = markers[i].getAttribute("swp");
			var status = markers[i].getAttribute("status");
			
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
			
			var reportdate = friendlyDate(ts);
			
			var marker = createMarker(point, buoyid, location, reportdate, swd, swh, swp, type, status);
			map.addOverlay(marker);
		  }
		});
	}
}

function createMarker(point, buoyid, location, reportdate, swd, swh, swp, type, status) {
	
//	var btitle = 'Buoy ' + buoyid;
	var btitle = location;
	var body = '<table cellpadding="0" cellspacing="0" class="report-data"><tr><th>Last Report</th><th>Dir</th><th>Height</th><th>Period</th></tr><tr><td>' + reportdate + '</td><td>' + swd + '</td><td>' + swh + ' ft</td><td>' + swp + ' sec</td></tr></table>';
	
	// if buoy status is offline, override icon type and set title
	if (status == 0) {
		btitle = btitle + ' (Offline)';
		type = 'offline';
		body = '<div class="error">This buoy is not currently reporting data. The last report was received on <strong>' + reportdate + '</strong></div>';
	} else if (status == 3) {
		btitle = btitle + ' (Adrift)';
		type = 'offline';
		body = '<div class="error">This buoy is adrift and no longer reports spectral wave summaries. The last report was received on <strong>' + reportdate + '</strong></div>';
	} 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;
	
	// buoy infoWindow HTML
	var html = '<h3><a href="/buoy.php?id=' + buoyid + '">' + btitle + '</a></h3><h4>Buoy ' + buoyid + '</h4>' + body + '<p><a href="/buoy.php?id=' + buoyid + '">View buoy chart and summary</a></p>';
	html = '<div class="buoy-info' + ' ' + type + '" style="height:95px;width:250px;">' + html + '</div>';

	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});

	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;
	}
}
