//-----------------------------------------------------------------------#
// IdvFindSources.js - Copyright (C) 2007 IDV Solutions                  #
//-----------------------------------------------------------------------#

// make sure IDV namespace exists
if(typeof idv == "undefined") { var idv = new Object(); }


//-----------------------------------------------------------------------#
// LatLongFindSource: Find source that will go to a lat/long coordinate  #
//-----------------------------------------------------------------------#
idv.LatLongFindSource = IdvCreateClass(idv.MemoryFindSource, {

   // String constants
   ErrorBadFormat: "Invalid coordinate format",
   ErrorBadValues: "Coordinate values are not within the correct range",
   FindSourceExmp: "Example: 34.134, -118.322",
   FindSourceName: "__lat_lon",
   FindSourceLabl: "Lat/Long",

   // Called when user enteres a query
   onFindLocation: function(query, n, e, s, w)
   {
	   var space; // character index of first space
	   var comma; // character index of first comma
	   var divid; // divider character index
	   var coord; // resulting coordinate object

	   // get information about the string
	   coord = new Object();
	   comma = query.indexOf(",");
	   space = query.indexOf(" ");
	   divid = (comma < 0)? space : comma;

	   // get latitude and longiude values
	   coord.y = parseFloat(query.substr(0, divid));
	   coord.x = parseFloat(query.substr(divid + 1));

	   // report bad formatting
	   if(isNaN(coord.y) || isNaN(coord.x)) {
         this.showErrorMessage(this.$type.ErrorBadFormat);

      // report coordinate out of range
	   } else if(!this.coordinateIsValid(coord.y, coord.x)) {
         this.showErrorMessage(this.$type.ErrorBadValues);
         
      // goto the find result
	   } else {
         this.gotoSingleResult(coord);
	   }
   },

   // Determins whether a particular coordinate is valid for WGS-84
   coordinateIsValid: function(lat, lon)
   {
      return (
         -180 <= lon && lon <= 180 &&
          -90 <= lat && lat <=  90
      );
   },

   // Constructs a new lat/lon find source
   $construct: function()
   {
      this.$super.$construct.call(
         this, 
         new idv.FindSourceOptions(
            this.$type.FindSourceName,
            this.$type.FindSourceLabl,
            this.$type.FindSourceExmp
         )
      );
   }
})
