// ***************************************************************************
// *  markers.js
// *
// *  Copyright  2007  Larry Moore
// *  larmoor@gmail.com
// ****************************************************************************/
//
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program.  The GPL is available at http://www.gnu.org, 
//   or by writing to the Free Software Foundation, Inc., 59 Temple Place - 
//   Suite 330, Boston, MA 02111-1307, USA.
// 
//
// This module contains most of the code for setting markers and showing info window displays
// This includes constructing strings of coordinates for display in info window balloons
// USGN coordinates are computed with calls to functions in usng.js


// icons
var iconYellow
var iconCircle
var iconRectangle
var icon100k_1
var icon100k_2

// global variable...workaround to allow a marker to be deleted from within its own infowindow
var thismarker

createMarkerIcons();

// create and display a marker with usng, lat/lon, and other coord system info
function displayCoordMarker(lat,lng,zoom,info_str) {
   var point = new GLatLng(lat,lng)
   marker_point = point
   if (zoom) {  // if a zoom level supplied, apply it
      usngmap.setCenter(point,zoom)
   }
   var marker = new GMarker(point,iconYellow);
   info_str += buildCoordString(point)

   GEvent.addListener(marker, "click", function() {
      thismarker = marker
      marker.openInfoWindowHtml(info_str 
          + '<input type="button" value="Delete marker" onclick=removeOneMarker()>'
//////  comment next line out except when code resides on USGS system ///////////////////
//          + '<br><input type="button" value="Download GeoPDF USGS map" onclick=downloadUSGSmap()>'

          );
   });

   usngmap.addOverlay(marker);
}

function removeOneMarker() {
  // global variable 'this marker' is a workaround to delete a marker from within its own info window
  usngmap.removeOverlay(thismarker);
  usngmap.closeInfoWindow();
}


function buildCoordString(point)  {
        var northamerica=1
        var ngCoords = LLtoUSNG(point.y,point.x, 5);
        var mgrsCoords = LLtoMGRS(point.y,point.x, 5);
	var utmcoords=[]
	var zone
 	LLtoUTM(point.y,point.x,utmcoords,0)
	zone = utmcoords[2]

        if (point.y>13 && point.y<90 && ((point.x<-46 && point.x>-180) || (point.x>169 && point.x<180))) { 
            northamerica = 1
        }
        else {
            northamerica = 0
        }

        if (northamerica) {
           latLngStr = "USNG: " +ngCoords;
        }
        else {
           latLngStr = "MGRS: " +mgrsCoords;
        }
	
	//utm coordinates
	var utmx = utmcoords[0];
        var utmy = utmcoords[1];
	if (utmy < 0) { utmy+=10000000; }
    	//latLngStr += "<br>UTM: " + zone + UTMLetterDesignator(point.y)+", " + utmx.toFixed(0) + ", " + utmy.toFixed(0);
        // degrees and decimal minutes
        latLngStr += "<br>DD MM.mm: " + lat2dm(point.y)+", " + lon2dm(point.x)
        // deg-min-sec
        //latLngStr += "<br>DD M-S: " + lat2dms(point.y)+", " + lon2dms(point.x)
        // decimal degrees
        latLngStr += "<br>DD.dd: " + point.y.toFixed(5) + ", " + point.x.toFixed(5)

       if (northamerica) {
          latLngStr += '<br>Directions to this point from:'
          + '<br>'+'<form action="javascript:getDirections()">'
          + '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" />'
          + '<INPUT value="Go" type="SUBMIT">'
          + '<input type="hidden" id="daddr" value="'+name+"@"+point.lat() + ',' + point.lng() 
          + '"/></form>'
       }
       else {
          latLngStr += '<br>'
       }

       return(latLngStr)
}

function getDirections()
{
//alert("saddr="+document.getElementById("saddr").value+", "+"daddr="+document.getElementById("daddr").value)
     showDirections(document.getElementById("saddr").value,document.getElementById("daddr").value);
     usngmap.closeInfoWindow();
}



function createMarkerIcons() {
     iconYellow = new GIcon();
     iconYellow.image = "icons/iconYellow.png";
     iconYellow.iconSize = new GSize(12, 20);
     iconYellow.iconAnchor = new GPoint(6, 20);
     iconYellow.infoWindowAnchor = new GPoint(5, 1);

     iconCircle = new GIcon();
     iconCircle.image = "icons/iconCircle.png";
     iconCircle.iconSize = new GSize(12, 12);
     iconCircle.iconAnchor = new GPoint(6, 6);
     iconCircle.infoWindowAnchor = new GPoint(6,6);

     iconRectangle = new GIcon();
     iconRectangle.image = "icons/rectangle.png";
     iconRectangle.iconSize = new GSize(40,20);
     iconRectangle.iconAnchor = new GPoint(20,10);
//     iconRectangle.infoWindowAnchor = new GPoint(20,10);

     icon100k_1 = new GIcon();
     icon100k_1.image = "icons/label100k.png";
     icon100k_1.iconSize = new GSize(30,20);
     icon100k_1.iconAnchor = new GPoint(20,10);

     icon100k_2 = new GIcon();
     icon100k_2.image = "icons/label100k.png";
     icon100k_2.iconSize = new GSize(60,20);
     icon100k_2.iconAnchor = new GPoint(20,10);

     icon1k = new GIcon();
     icon1k.image = "icons/label1k.png";
     icon1k.iconSize = new GSize(24,20);
     icon1k.iconAnchor = new GPoint(10,10);
}



// convert a latitude to deg and decimal minutes
function lat2dm(input) {
   if (input > 0) {
      return (deg2dm(input)+"N")
   }
   else {
      return (deg2dm(input)+"S")
   }
}
 
// convert a latitude to deg and decimal minutes
function lon2dm(input) {
   if (input > 0) {
      return (deg2dm(input)+"E")
   }
   else {
      return (deg2dm(input)+"W")
   }
}
 
// converts decimal degrees to degrees and decimal minutes
// input is a float, return value a string
function deg2dm(input) {
   var cdeg
   var cmin
   var deg = Math.floor(Math.abs(input))
   var min = (Math.abs(input) - deg)*60

// more readable, but leading zeros not recommended by standards
//   if (deg < 1) { cdeg = "0"+deg }
//   else {cdeg = ""+deg }
//   if (min < 1) { cmin = "0"+min.toFixed(3) }
//   else {cmin = ""+min.toFixed(3) }


   return(deg+" "+min.toFixed(3))
}


// convert a latitude to deg-min-sec
function lat2dms(input) {
   if (input > 0) {
      return (deg2dms(input)+"N")
   }
   else {
      return (deg2dms(input)+"S")
   }
}
 
// convert a latitude to deg-min-sec
function lon2dms(input) {
   if (input > 0) {
      return (deg2dms(input)+"E")
   }
   else {
      return (deg2dms(input)+"W")
   }
}

// converts decimal degrees to deg-min-sec
// input is a float, return value a string
function deg2dms(input) {
   var cdeg
   var cmin	
   var csec

   var temp = Math.abs(input)
   var deg = Math.floor(temp)
   var min = Math.floor((temp - deg)*60)
   var sec = (((temp-deg)*60)-min)*60


   if (deg < 10) { cdeg = "0"+deg }
   else {cdeg = ""+deg }

   if (min < 10) { cmin = "0"+min }
   else {cmin = ""+min }

   if (sec < 10) { csec = "0"+sec.toFixed(1) }
   else {csec = ""+sec.toFixed(1) }

   return(cdeg+"-"+cmin+"-"+csec)
}
