// Portions ©2006 Topaz Maps Inc.
// www.topazmaps.com
// Custom cartography and mapping applications

// data field definitions

var GLOSS_idField = 0;
var GLOSS_xField = 1;
var GLOSS_yField = 2;
var GLOSS_nameField = 3;
var GLOSS_cntryField = 4;
var GLOSS_statusField = 5;

// set up symbol array

var GLOSS_syms = new Array();
GLOSS_syms[0] = new Array('images/cat0.gif',9,9,4.5,4.5,'Category 0: Stations with real-time data.');
GLOSS_syms[1] = new Array('images/cat1.gif',9,9,4.5,4.5,'Category 1: "Operational" stations for which the latest data is 2005 or later.');
GLOSS_syms[2] = new Array('images/cat2.gif',9,9,4.5,4.5,'Category 2: "Probably operational" stations for which the latest data is within the period 1995-2004.');
GLOSS_syms[3] = new Array('images/cat3.gif',9,9,4.5,4.5,'Category 3: "Historical" stations for which the latest data is earlier than 1995.');
GLOSS_syms[4] = new Array('images/cat4.gif',9,9,4.5,4.5,'Category 4: Stations for which no PSMSL data exist.');
GLOSS_syms[5] = new Array('images/cat0s.gif',9,9,4.5,4.5,'Category 0: Stations with real-time data.');
GLOSS_syms[6] = new Array('images/cat1s.gif',13,13,6.5,6.5,'Category 1: "Operational" stations for which the latest data is 2005 or later.');
GLOSS_syms[7] = new Array('images/cat2s.gif',13,13,6.5,6.5,'Category 2: "Probably operational" stations for which the latest data is within the period 1995-2004.');
GLOSS_syms[8] = new Array('images/cat3s.gif',13,13,6.5,6.5,'Category 3: "Historical" stations for which the latest data is earlier than 1995.');
GLOSS_syms[9] = new Array('images/cat4s.gif',13,13,6.5,6.5,'Category 4: Stations for which no PSMSL data exist.');
	
// display preferences (true or false)

topaz_displayLocTitles = true;  // display location name in floating box on mouse-over
topaz_autoZoom = true;  // zoom out to largest map when selection is made from menu for a location that is not visible
topaz_showBox = false;  // show sub-map frames as dotted areas

function GLOSS_LocationMap(mapDisplayArea)
{
	// initialize low-level data field variables that the TopazMaps routines will access
	topaz_dataFld_x = GLOSS_xField;
	topaz_dataFld_y = GLOSS_yField;
	topaz_dataFld_name = GLOSS_nameField;
	topaz_dataFld_group = GLOSS_cntryField;
	
	var theMapDispID = 'tpz1';
	
	var theHTML = "";
	theHTML += '<h3>GLOSS status within the PSMSL data set - October 2009</h3>';
	theHTML += '<div id="nav" class="tpzmapsNav">';
	theHTML += '<select id="mapMenu" onChange="TOPAZMAPS_selectMap(\''+theMapDispID+'\',this.options[selectedIndex].value);return false;">';
	theHTML += GLOSS_buildMapMenu(gloss_mapMenu);
	theHTML += '</select>';
	theHTML += ' &nbsp;&nbsp;<select id="locMenu" onChange="TOPAZMAPS_selectLoc(\''+theMapDispID+'\',this.options[selectedIndex].value);return false;">';
	theHTML += GLOSS_buildLocationMenu(gloss_stations);
	theHTML += '</select>';
	theHTML += ' &nbsp;&nbsp;&nbsp;&nbsp;<a href="index.html#legend" target="_parent">Legend</a> | <a href="javascript:GLOSS_print();">Print</a> | <a href="index.html#help" target="_parent">Help</a>';
	theHTML += '</div>';
	theHTML += TOPAZMAPS_newMap(theMapDispID,'World',gloss_maps,gloss_stations,gloss_mapMenu);
	theHTML += '<div id="GLOSS_Info" class="tpzmapsInfo"></div>';	
	document.getElementById(mapDisplayArea).innerHTML = theHTML;  // write the HTML code to the map display DIV
	TOPAZMAPS_selectMap(theMapDispID,'World'); // display the world map at the outset
};

function GLOSS_buildMapMenu(theMenuArray)
{
	var lineNo = 0;
	var theStr = "";
	
    for (var i=0; i<theMenuArray.length; i++)
    {
    	theStr += '<option value="'+theMenuArray[i][1]+'">'+theMenuArray[i][0]+'</option>';
	}
	return(theStr);
};

function GLOSS_buildLocationMenu(theLocs)
{
	var lineNo = 0;
	var theStr = "";
	theStr += '<option value=\"\">GLOSS Core Stations</option>';
	lineNo++;
	var currCtry = "";
	
    for (var i=0; i<theLocs.length; i++)
    {
    	if (currCtry != theLocs[i][GLOSS_cntryField])
    	{
    		currCtry = theLocs[i][GLOSS_cntryField];
    		theStr += '<option value="'+currCtry+'">'+currCtry+'</option>';
			lineNo++;
    	}
    	theStr += '<option value="'+i+'">- '+theLocs[i][GLOSS_nameField]+'</option>';
    	topaz_menu[topaz_menu.length] = lineNo;
		lineNo++;
	}
	return(theStr);
};

function GLOSS_showLegend()  // called when the Legend button is clicked
{
	var theLegend = open("GLOSSlegend.html","Legend","width=620,height=250,left=100,top=100,toolbar=no,scrollbars=no,location=no,resizable=yes");
	theLegend.focus();
};

function GLOSS_print()  // called when the Legend button is clicked
{
	var thePrintWindow = open("print.html","Print");
	thePrintWindow.focus();
};

function GLOSS_help()  // called when the help button is clicked
{
	var theLegendWindow = open("GLOSShelp.html","Help","width=620,height=350,left=100,top=100,toolbar=no,scrollbars=yes,location=no,resizable=yes");
	theLegendWindow.focus();
};

function TOPAZMAPS_info(theMatch) // hook to Topaz Maps code to display location information when a location is clicked
{
	var	infoDIV = document.getElementById("GLOSS_Info");
	var theData = gloss_stations[theMatch];
	if (theMatch.toString().length<4)
	{	
		var theStr = '';
		theStr += '<a href="/station_handbook/stations/'+theData[GLOSS_idField]+'/" target="_blank">'+'GLOSS station '+theData[GLOSS_idField]+'</a>'; // added closing forward slash
		theStr += ' '+theData[GLOSS_nameField]+' ('+theData[GLOSS_cntryField]+')<br />';
		
		var theStatus = GLOSS_status(theData[GLOSS_statusField]);
		if (theStatus > -1)
		{
			theStr += ' <img src="'+GLOSS_syms[theStatus][0]+'" width='+GLOSS_syms[theStatus][1]+' height='+GLOSS_syms[theStatus][2]+'"></img> Category '+theStatus;
			if (theStatus == 1)
			{
				theStr += ': Most recent data from '+theData[GLOSS_statusField];
			}
			else if (theStatus == 2)
			{
				theStr += ': Most recent data from '+theData[GLOSS_statusField];
			}
			else if (theStatus == 3)
			{
				theStr += ': Most recent data from '+theData[GLOSS_statusField];
			}
			else if (theStatus == 4)
			{
				theStr += ': No PSMSL data available for this station';
			}
		}
    	infoDIV.innerHTML = theStr;
    }
    else
    {
        infoDIV.innerHTML = "";
    }
};

function TOPAZMAPS_symbol(theLoc,isSelected) // hook to Topaz Maps code to select which symbol to display
{
	if (isSelected)
	{
		return(GLOSS_syms[GLOSS_status(topaz_locs[theLoc][GLOSS_statusField])+5]);
	}
	else
	{
		return(GLOSS_syms[GLOSS_status(topaz_locs[theLoc][GLOSS_statusField])]);
	}
};

function GLOSS_status(yearLastData)  // to determine the station status from the year of last available data
{
	if (yearLastData == '0')
	{
		return(4);
	}
	else if (yearLastData > 2003)
	{
		return(1);
	}
	else if (yearLastData > 1993)
	{
		return(2);
	}
	else
	{
		return(3);
	}
	return(-1);
};