/* 
	This version parses an xml file, displays custom
	triangular icons and an info window with image. 
	It does not highlight or use Jeremy's functions
	with 'this'.
	
	26 November 2007 dyv
*/
var centerLatitude = 42.509;
var centerLongitude = -150.664;
var startZoom = 3;
var map;
var markers_loaded = false;

var colors = new Array();
var colorsIdx = 0;
var markers = new Array();
var markersIdx = 0;

var coord_pacific = new GLatLng(centerLatitude, centerLongitude);
var zoom_pacific = 3;
var coord_alaska = new GLatLng(57.751, -156.368);
var zoom_alaska = 4;
var coord_hawaii = new GLatLng(20.6, -157.4);
var zoom_hawaii = 7;
var coord_cnmi = new GLatLng(17.6, 145.6);
var zoom_cnmi = 6;
var coord_utnm = new GLatLng(36.8, -109.5);
var zoom_utnm = 6;
var coord_canv = new GLatLng(37.4, -120.5);
var zoom_canv = 6;
var coord_waor = new GLatLng(45.3, -118.0);
var zoom_waor = 6;
var coord_idwy = new GLatLng(44.3, -111.2);
var zoom_idwy = 6;


//global, icons for volcano statuses
var icons = {
	'fields': 						{'width': 12, 'height': 12},
	'observatory': 				{'width': 21, 'height': 16},
	'unassigned_unassigned': 	{'width': 18, 'height': 14},
	'green_normal': 				{'width': 21, 'height': 16},
	'yellow_advisory': 			{'width': 27, 'height': 20},
	'yellow_watch': 				{'width': 27, 'height': 20},
	'orange_watch':				{'width': 33, 'height': 24},
	'orange_warning': 			{'width': 33, 'height': 24},
	'red_warning': 				{'width': 40, 'height': 31},
	'red_watch': 					{'width': 40, 'height': 31}
};

/* 
	Called every time a map region is drawn, on initial load region == World
*/
function vhp_reposition(region) {

	if ( region != "World" ){
		$("#map-logo-box").fadeOut("slow");
	}

	if (!markers_loaded) {
		
		setChecks();		
		
		colors = new Array();
		colorsIdx = 0;		
		markers = new Array();
		markersIdx = 0;
		
		map = new GMap2(document.getElementById("map"));

		map.addControl(new GSmallZoomControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		
		map.setCenter(coord_pacific, zoom_pacific, G_HYBRID_MAP);
		//map.setCenter(coord_pacific, zoom_pacific, G_SATELLITE_MAP);
		
		retrieveMarkers();

		markers_loaded = true;
		
		return;
	}

	if (region == "World") {
		map.setCenter(coord_pacific, zoom_pacific);
	} else if (region == "AK") {
		map.setCenter(coord_alaska, zoom_alaska);
	} else if (region == "HI") {
		map.setCenter(coord_hawaii, zoom_hawaii);
	} else if (region == "CNMI") {
		map.setCenter(coord_cnmi, zoom_cnmi);
	} else if (region == "UT-NM") {
		map.setCenter(coord_utnm, zoom_utnm);
	} else if (region == "CA-NV") {
		map.setCenter(coord_canv, zoom_canv);
	} else if (region == "WA-OR") {
		map.setCenter(coord_waor, zoom_waor);
	} else if (region == "ID-WY") {
		map.setCenter(coord_idwy, zoom_idwy);
	} else {
		map.setCenter(coord_default, zoom_default);
	}

}

var zElse = 100000;
var zUnassigned = 200000;
var zGreen = 300000;
var zYellow = 400000;
var zOrange = 500000;
var zRed = 600000;
var zMarkerIndex = 0;

// Google's API needs to call a function, so here it is.
function getZindexVal(){
	return zMarkerIndex;
}

function retrieveMarkers() {

	markers_loaded = true;
	
	/* 
	* The path needs to be the same as where the file is kept. I
	* have been moving the vhp_status.xml file from production over
	* for testing.
	*/

	GDownloadUrl("/rss/vhp_status.xml", function(data, status) {

		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		var len = markers.length;

		for (var i = 0; i < len; i++) {
			var m = markers[i];
			var point = new GLatLng(parseFloat(m.getAttribute("latitude")),parseFloat(m.getAttribute("longitude")));
			map.addOverlay(createMarker(point, markers[i]));
		}
	});

}

function getIconURL(name) {
	return '/images/icons/map/'+name+'.png';
}

function getHighlightIconURL(name) {
	return '/images/icons/map/'+name+'_highlight.png';
}

//memoized icons
function getIcon(name) {
	
	if (!icons[name]) {
		return null;
	}
	
	if (!icons[name].icon) {
		var icon = new GIcon();
		icon.image = getIconURL(name);
		icon.iconSize = new GSize(icons[name].width, icons[name].height);
		var anchor = new GPoint(10, 12);
		icon.iconAnchor = anchor;
		icon.infoWindowAnchor = anchor;
		//save it for later
		icons[name].icon = icon;
	}
	
	return icons[name].icon;
}

/*
	Create each marker (or icon or symbol for the map
*/
function createMarker(point, marker){

	var lat = marker.getAttribute("latitude"); 
	var lng = marker.getAttribute("longitude");
	var volcanoName = marker.getAttribute("volcano_name"); 
	var alertLevel = marker.getAttribute("alert_level"); 
	var colorCode = marker.getAttribute("color_code"); 
	var region = marker.getAttribute("region"); 
	var volcanoLocation = marker.getAttribute("location"); 
	var volcanoNameID = volcanoName.replace(" ", "_");
	var elevation = marker.getAttribute("elevation");
	var view = marker.getAttribute("view");  
	var image = marker.getAttribute("image"); 
	var status = marker.getAttribute("status"); 
	var obsLink = marker.getAttribute("obslink"); 	
	var vhpLink = marker.getAttribute("vhplink");
	var moreInfo = marker.getAttribute("more_info"); 	
	var alertDate = marker.getAttribute("alertdate"); 	
	var alertTime = marker.getAttribute("alerttime"); 	
	var timeZone = marker.getAttribute("timezone"); 
	var nvewsthreat = marker.getAttribute("nvewsthreat");	

	colorCodes = colorCode.toLowerCase();
	alertLevels = alertLevel.toLowerCase();
	
/*	if (nvewsthreat == "Very Low Threat") {
		
		var iconName = "fields";
		
	} else {

		var iconName = colorCodes + '_' + alertLevels;
	}*/
	
  var iconName = colorCodes + '_' + alertLevels;
	// Set zIndex global variable zMarkerIndex
	if ( colorCode == "UNASSIGNED" ){
	
		zUnassigned++;
		zMarkerIndex = zUnassigned;
	
	} else if ( colorCode == "GREEN" ){
		
		zGreen++;
		zMarkerIndex = zGreen;
			
	 } else if ( colorCode == "YELLOW" ){
		
		zYellow++;
		zMarkerIndex = zYellow;		
			
	 } else if ( colorCode == "ORANGE" ){
		
		zOrange++;
		zMarkerIndex = zOrange;		
			
	 } else if ( colorCode == "RED" ){
		
		zRed++;
		zMarkerIndex = zRed;	
			
	 } else {
		
		zElse++;
		zMarkerIndex = zElse;		
	 }

	//create marker
	marker = new GMarker(new GLatLng(lat, lng), {icon: getIcon(iconName), clickable:true, title: volcanoName, zIndexProcess: getZindexVal}); 

	marker.normalIcon = getIconURL(iconName);

	var hasObsLink = false;
	try
	{
		if ( ( obsLink != null && obsLink != "null" && obsLink != "NULL" ) ) hasObsLink = true;
 		
	} catch(err) {
		
			//alert(err.description);
	}

	if ( !hasObsLink ){
		
		var noObsLink = "";	
	
		if ( volcanoLocation == "HI" ){

			noObsLink = "http://hvo.wr.usgs.gov/";

		} else if ( volcanoLocation == "MARIANA ISLANDS" ){

			noObsLink = "http://volcanoes.usgs.gov/nmi/activity/";

		} else if ( volcanoLocation == "CA" || volcanoLocation == "NV" ){

			noObsLink = "http://volcanoes.usgs.gov/lvo/";

		} else if ( volcanoLocation == "OR" || volcanoLocation == "WA" ){

			noObsLink = "http://vulcan.wr.usgs.gov/";

		} else if ( volcanoLocation == "ID" || volcanoLocation == "UT" || 
						volcanoLocation == "WY" ){

			noObsLink = "http://volcanoes.usgs.gov/yvo/";

		} else if ( volcanoLocation == "AZ" || volcanoLocation == "CO"  || 
						volcanoLocation == "NM" ){

			if ( volcanoName == "Dotsero" ){
				
				noObsLink = "http://www.volcano.si.edu/world/volcano.cfm?vnum=1208-01-";
				
			} else if ( volcanoName == "Carrizozo" ){
				
				noObsLink = "http://www.volcano.si.edu/world/volcano.cfm?vnum=1210-01-";
				
			} else if ( volcanoName == "San Francisco Peaks volcanic field" ){
				
				noObsLink = "http://geopubs.wr.usgs.gov/fact-sheet/fs017-01/";
				
			} else if ( volcanoName == "Uinkaret volcanic field" ){

				noObsLink = "http://www.volcano.si.edu/world/volcano.cfm?vnum=1209-01-";
				
			} else if ( volcanoName == "Valles Caldera" ){

				noObsLink = "http://www.volcano.si.edu/world/volcano.cfm?vnum=121002-D";
			
			} else if ( volcanoName == "Zuni-Bandera volcanic field" ){

				noObsLink = "http://www.volcano.si.edu/world/volcano.cfm?vnum=1210-02-";
				
			} else {
				
				//alert(volcanoName + " Location: >"+volcanoLocation+"<");				
			}
			
		} else {

			//alert(volcanoName + " Location: >"+volcanoLocation+"<");	

		}

		GEvent.addListener(marker, "click", function(){
			// alert("No obsLink: " + noObsLink);	
			window.location = noObsLink;
			});
			
	} else {
		
		GEvent.addListener(marker, "click", function(){
			// alert(obsLink);
			window.location = obsLink;
			});

	}
	
/*
	//the unassigned ones have windows that are too big. Works fine in startmapworking.js.
	if ((region == 'Alaska') && (alertLevel == "NORMAL") && (vhpLink = "")){
		
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml("<div class=\"infoWindow\"><center><b>" + 
			volcanoName + "</b><br />" +
			lat + ", " + lng + " (" + elevation + " m) </center><br />" +
			"<img class=\"mapimage\" src=\"" + image + "\" width=\"200\" height=\"150\" />" + 
		    "Volcano Alert Level: <b>" + alertLevel + "</b><br />" +
		    "Aviation Color Code: <span class= \"" + colorCode + "\" ><b>" + colorCode + "</b></span>" +
		    "<br /><br />" + 
		    status + " Please see the <b><a target=\"new\" href=\"" + 
		    vhpLink +  "\"> " + volcanoName + " page</a></b> for more information.</div>");
		});
		
	} else if ((colorCode == "UNASSIGNED") || (status == "")) {
		
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml("<center><b>" + volcanoName + "</b><br />" +
			lat + ", " + lng + " (" + elevation + " m) </center><br /><br />" +
		    "<img src=\"" + image + "\" width=\"200\" height=\"150\" /><br /></center>");
		});	
		
	} else if (!vhpLink) {
		
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml("<div class=\"infoWindow\"><center><b>" + 
			volcanoName + "</b><br />" +
			lat + ", " + lng + " (" + elevation + " m) </center><br />" +
			"<img class=\"mapimage\" src=\"" + image + "\" width=\"200\" height=\"150\" />" + 
		    "Volcano Alert Level: <b>" + alertLevel + "</b><br />" +
		    "Aviation Color Code: <span class= \"" + colorCode + "\" ><b>" + colorCode + "</b></span>" +
		    "<br /><br />" + status + "</div>");
		});

	
	} else if (alertLevel == 'NORMAL') {
	
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml("<div class=\"infoWindow\"><center><b>" + 
			volcanoName + "</b><br />" +
			lat + ", " + lng + " (" + elevation + " m) </center><br />" +
			"<img class=\"mapimage\" src=\"" + image + "\" width=\"200\" height=\"150\" />" + 
		    "Volcano Alert Level: <b>" + alertLevel + "</b><br />" +
		    "Aviation Color Code: <span class= \"" + colorCode + "\" ><b>" + colorCode + "</b></span>" +
		    "<br /><br />"  +   status + " Please see the <b><a target=\"new\" href=\"" + 
		    vhpLink +
		    "\">" + volcanoName + " page</a></b> for more information.</div>");
		});
	
	} else {
		
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml("<div class=\"infoWindow\"><center><b>" + 
			volcanoName + "</b><br />" +
			lat + ", " + lng + " (" + elevation + " m) </center><br />" +
			"<img class=\"mapimage\" src=\"" + image + "\" width=\"200\" height=\"150\" />" + 
		    "Volcano Alert Level: <b>" + alertLevel + "</b><br />" +
		    "Aviation Color Code: <span class= \"" + colorCode + "\" ><b>" + colorCode + "</b></span>" +
		    "<br /><br />As of " + alertDate + " " + alertTime + " " + timeZone + ": " + 
		    status + " Please see the <b><a target=\"new\" href=\"" + 
		    vhpLink +
		    "\">" + volcanoName + " page</a></b> for more information.</div>");
		});

	}
	*/
	markers[markersIdx] = marker;
	markersIdx++;
	
	colors[colorsIdx] = colorCode;
	colorsIdx++;	
	
	return marker;
}


function vhp_onload() {
	vhp_reposition("World");
}

/*
  
*/
function filter_vsmap(checkbox) {
  	
	//alert("filter checkbox " + checkbox.value + ", ckd? " + checkbox.checked);  	
  	
	for (var i = 0; i < colors.length; i++) {
		
		if (
			colors[i]==checkbox.name
			||
			(
				checkbox.value=='Elevated' &&
				(
					colors[i]=='YELLOW' ||
					colors[i]=='ORANGE' ||
					colors[i]=='RED' ||
					colors[i]=='Elevated'
				)
			)
		) {
			
			if ( checkbox.checked ){
			
				markers[i].show();				
			
			} else {
			
				markers[i].hide();	
			}
		}
	}

}

// Firefox remembers checked status on page refreshes, but all icons are
// displayed on a refresh so check the checkboxes here.
function setChecks(){
	
	document.getElementById("vsck_e").checked = true;
	document.getElementById("vsck_g").checked = true;
	document.getElementById("vsck_u").checked = true;	
}

window.onload = vhp_onload;



