var towns = new Array ( 34.157,-118.755, 34.095,-118.125, 34.188,-118.130, 33.822,-117.910, 33.555,-116.672, 34.140,-118.030, 35.207,-118.828, 35.487,-120.667, 33.343,-118.322, 34.133,-117.917, 35.265,-116.072, 35.373,-119.002, 32.730,-117.142, 34.003,-118.372, 34.085,-117.962, 33.927,-116.877, 34.888,-117.023, 33.928,-116.977, 33.888,-118.125, 34.070,-118.402, 34.265,-116.853, 34.243,-116.910, 37.170,-118.292, 33.610,-114.595, 33.688,-118.040, 33.350,-115.733, 35.000,-117.650, 33.255,-116.373, 32.962,-115.535, 33.917,-117.900, 34.613,-120.193, 34.185,-118.315, 34.195,-118.352, 33.922,-116.785, 32.668,-115.495, 35.125,-117.958, 33.127,-115.515, 34.135,-118.125, 34.215,-119.038, 35.553,-121.083, 34.200,-118.605, 34.397,-119.518, 33.830,-118.278, 33.780,-116.467, 34.525,-118.618, 34.058,-118.417, 33.860,-118.063, 34.257,-118.603, 34.018,-117.688, 35.722,-120.297, 32.618,-117.008, 34.105,-117.723, 36.138,-120.360, 33.888,-118.218, 33.875,-117.565, 36.045,-117.948, 34.090,-117.888, 34.000,-118.400, 33.817,-118.045, 33.462,-117.717, 33.715,-115.400, 33.962,-116.502, 34.222,-117.402, 34.000,-117.810, 33.935,-118.122, 33.852,-115.478, 34.033,-118.172, 34.072,-118.258, 32.795,-116.962, 32.793,-115.552, 34.068,-118.027, 33.917,-118.393, 34.662,-118.387, 34.160,-118.500, 33.117,-117.085, 33.385,-117.250, 34.397,-118.918, 34.107,-117.435, 33.717,-117.953, 34.820,-118.945, 36.738,-119.790, 33.870,-117.922, 36.450,-116.852, 33.773,-117.955, 34.143,-118.255, 34.137,-117.865, 35.353,-116.887, 34.438,-119.830, 34.797,-118.853, 34.272,-118.502, 34.943,-118.930, 34.120,-118.298, 35.115,-120.625, 33.917,-118.350, 33.747,-116.970, 34.422,-117.302, 34.120,-118.198, 36.852,-121.402, 34.097,-118.325, 33.745,-116.713, 36.803,-118.198, 33.962,-118.323, 33.715,-116.217, 33.668,-117.822, 34.412,-119.853, 34.137,-116.315, 33.078,-116.600, 34.205,-118.200, 34.225,-118.238, 32.850,-117.255, 33.932,-117.953, 32.768,-117.048, 33.542,-117.782, 33.527,-117.712, 33.860,-118.125, 34.248,-117.188, 33.667,-117.330, 34.472,-114.348, 33.238,-116.762, 35.637,-118.467, 34.273,-118.363, 34.690,-118.135, 34.043,-118.937, 34.048,-117.253, 34.638,-120.455, 36.603,-118.063, 33.775,-118.188, 33.947,-118.437, 34.055,-118.247, 34.443,-116.945, 34.057,-118.277, 34.035,-118.685, 33.885,-118.408, 33.977,-118.447, 33.570,-116.077, 33.180,-116.767, 32.778,-117.253, 33.600,-117.675, 35.052,-118.170, 34.147,-118.000, 34.077,-117.688, 36.600,-121.900, 34.062,-118.133, 34.285,-118.882, 34.045,-116.583, 35.363,-120.843, 34.288,-117.648, 34.100,-116.827, 36.580,-118.292, 34.225,-118.058, 34.848,-114.613, 34.378,-118.527, 33.615,-117.932, 33.238,-115.518, 34.172,-118.377, 33.527,-115.938, 34.235,-118.533, 33.902,-118.082, 33.170,-115.637, 33.195,-117.382, 32.735,-115.993, 33.143,-116.132, 34.447,-119.248, 36.282,-118.005, 34.065,-117.650, 33.787,-117.852, 34.197,-119.178, 34.283,-118.438, 33.722,-116.390, 33.815,-116.547, 34.578,-118.112, 33.355,-116.863, 33.775,-118.425, 34.222,-118.448, 35.898,-120.432, 35.633,-120.687, 33.782,-117.147, 35.137,-120.638, 34.577,-120.648, 34.465,-120.470, 34.000,-118.805, 33.705,-118.295, 34.902,-120.670, 34.055,-117.750, 34.145,-119.205, 36.082,-119.018, 33.040,-116.867, 34.107,-117.593, 34.055,-117.177, 33.838,-118.383, 34.202,-118.533, 34.107,-117.370, 35.622,-117.667, 33.985,-117.375, 34.062,-118.082, 34.205,-117.103, 32.820,-118.345, 33.278,-115.965, 34.107,-117.295, 33.418,-117.605, 33.032,-118.605, 32.715,-117.162, 34.285,-118.437, 34.093,-118.098, 33.503,-117.663, 35.280,-120.657, 34.122,-118.105, 34.030,-120.367, 33.240,-119.505, 32.552,-117.043, 34.422,-119.702, 33.727,-117.883, 33.472,-119.062, 34.032,-119.518, 34.072,-119.930, 35.387,-120.605, 34.952,-120.433, 34.020,-118.490, 33.945,-120.105, 34.413,-118.538, 33.742,-118.102, 34.150,-118.448, 35.977,-116.272, 34.162,-118.050, 33.798,-118.167, 34.092,-118.265, 34.278,-118.708, 35.352,-120.000, 34.595,-120.137, 33.955,-118.205, 34.112,-118.150, 36.603,-117.148, 34.145,-118.392, 34.258,-118.302, 35.142,-119.450, 34.171,-118.552, 35.130,-118.447, 36.170,-117.087, 34.170,-118.838, 33.833,-118.365, 35.767,-117.370, 33.733,-117.815, 34.137,-116.053, 34.105,-117.650, 34.395,-118.563, 34.187,-118.448, 34.280,-119.300, 34.535,-117.292, 36.327,-119.293, 33.200,-117.243, 34.072,-117.925, 34.085,-118.398, 33.037,-115.622, 34.060,-118.447, 35.005,-118.943, 33.963,-118.032, 34.032,-118.052, 34.168,-118.603, 34.358,-117.623, 33.888,-117.812, 34.033,-117.033, 34.118,-116.445, 32.698,-114.625 ); 
var townnames= new Array ( "Agoura Hills, CA", "Alhambra, CA", "Altadena, CA", "Anaheim, CA", "Anza, CA", "Arcadia, CA", "Arvin, CA", "Atascadero, CA", "Avalon, Catalina Island, CA", "Azusa, CA", "Baker, CA", "Bakersfield, CA", "Balboa Park, San Diego, CA", "Baldwin Hills, CA", "Baldwin Park, CA", "Banning, CA", "Barstow, CA", "Beaumont, CA", "Bellflower, CA", "Beverly Hills, CA", "Big Bear City, CA", "town of Big Bear Lake, CA", "Big Pine, CA", "Blythe, CA", "Bolsa Chica Beach, CA", "Bombay Beach, CA", "Boron, CA", "Borrego Springs, CA", "Brawley, CA", "Brea, CA", "Buellton, CA", "Burbank, CA", "Burbank Airport, CA", "Cabazon, CA", "Calexico, CA", "California City, CA", "Calipatria, CA", "Caltech, Pasadena, CA", "Camarillo, CA", "Cambria, CA", "Canoga Park, CA", "Carpinteria, CA", "Carson, CA", "Cathedral City, CA", "Castaic Lake dam, CA", "Century City, CA", "Cerritos, CA", "Chatsworth, CA", "Chino, CA", "Cholame, CA", "Chula Vista, CA", "Claremont Colleges, CA", "Coalinga, CA", "Compton, CA", "Corona, CA", "Coso Junction, CA", "Covina, CA", "Culver City, CA", "Cypress, CA", "Dana Point, CA", "Desert Center, CA", "Desert Hot Springs, CA", "Devore, CA", "Diamond Bar, CA", "Downey, CA", "Eagle Mtn., CA", "East Los Angeles, CA", "Echo Park, CA", "El Cajon, CA", "El Centro, CA", "El Monte, CA", "El Segundo, CA", "Elizabeth Lake, CA", "Encino, CA", "Escondido, CA", "Fallbrook, CA", "Fillmore, CA", "Fontana, CA", "Fountain Valley, CA", "Frazier Park, CA", "Fresno, CA", "Fullerton, CA", "Furnace Creek, CA", "Garden Grove, CA", "Glendale, CA", "Glendora, CA", "Goldstone Lake, CA", "Goleta, CA", "Gorman, CA", "Granada Hills, CA", "Grapevine, CA", "Griffith Observatory, CA", "Grover City, CA", "Hawthorne, CA", "Hemet, CA", "Hesperia, CA", "Highland Park, CA", "Hollister, CA", "Hollywood, CA", "Idyllwild, CA", "Independence, CA", "Inglewood, CA", "Indio, CA", "Irvine, CA", "Isla Vista, CA", "Joshua Tree, CA", "Julian, CA", "La Canada/Flintridge, CA", "La Crescenta, CA", "La Jolla, CA", "La Habra, CA", "La Mesa, CA", "Laguna Beach, CA", "Laguna Niguel, CA", "Lakewood, CA", "Lake Arrowhead, CA", "Lake Elsinore, CA", "Lake Havasu City, AZ", "Lake Henshaw, CA", "town of Lake Isabella, CA", "Lake View Terrace, CA", "Lancaster, CA", "Leo Carrillo State Beach, CA", "Loma Linda, CA", "Lompoc, CA", "Lone Pine, CA", "Long Beach, CA", "Los Angeles Airport, CA", "Los Angeles Civic Center, CA", "Lucerne Valley, CA", "Macarthur Park, CA", "Malibu, CA", "Manhattan Beach, CA", "Marina del Rey, CA", "Mecca, CA", "Mesa Grande, CA", "Mission Beach, CA", "Mission Viejo, CA", "Mojave, CA", "Monrovia, CA", "Montclair, CA", "Monterey, CA", "Monterey Park, CA", "Moorpark, CA", "Morongo Valley, CA", "Morro Bay, CA", "Mt. Baldy, CA", "Mt. San Gorgonio, CA", "Mt. Whitney, CA", "Mt. Wilson Observatory, CA", "Needles, CA", "Newhall, CA", "Newport Beach, CA", "Niland, CA", "North Hollywood, CA", "North Shore, CA", "Northridge, CA", "Norwalk, CA", "Obsidian Butte, CA", "Oceanside, CA", "Ocotillo, CA", "Ocotillo Wells, CA", "Ojai, CA", "Olancha, CA", "Ontario, CA", "Orange, CA", "Oxnard, CA", "Pacoima, CA", "Palm Desert, CA", "Palm Springs, CA", "Palmdale, CA", "Palomar Observatory, CA", "Palos Verdes Point, CA", "Panorama City, CA", "Parkfield, CA", "Paso Robles, CA", "Perris, CA", "Pismo Beach, CA", "Pt. Arguello, CA", "Pt. Conception, CA", "Pt. Dume, CA", "Pt. Fermin, San Pedro, CA", "Pt. Sal, CA", "Pomona, CA", "Port Hueneme, CA", "Porterville, CA", "Ramona, CA", "Rancho Cucamonga, CA", "Redlands, CA", "Redondo Beach, CA", "Reseda, CA", "Rialto, CA", "Ridgecrest, CA", "Riverside, CA", "Rosemead, CA", "Running Springs, CA", "San Clemente Is., CA (SE tip)", "Salton City, CA", "San Bernardino, CA", "San Clemente, CA", "San Clemente Is., CA (NW tip)", "San Diego, CA", "San Fernando, CA", "San Gabriel, CA", "San Juan Capistrano, CA", "San Luis Obispo, CA", "San Marino, CA", "San Miguel Is. , CA", "San Nicolas Is., CA", "San Ysidro, CA", "downtown Santa Barbara, CA", "Santa Ana, CA", "Santa Barbara Is. , CA", "Santa Cruz Is., CA (E end)", "Santa Cruz Is., CA (NW end)", "Santa Margarita, CA", "Santa Maria, CA", "Santa Monica, CA", "Santa Rosa Is., CA", "Saugus, CA", "Seal Beach, CA", "Sherman Oaks, CA", "Shoshone, CA", "Sierra Madre, CA", "Signal Hill, CA", "Silver Lake, CA", "Simi Valley, CA", "Simmler, CA", "Solvang, CA", "South Gate, CA", "South Pasadena, CA", "Stovepipe Wells, CA", "Studio City, CA", "Sunland, CA", "Taft, CA", "Tarzana, CA", "Tehachapi, CA", "Telescope Peak, CA", "Thousand Oaks, CA", "Torrance, CA", "Trona, CA", "Tustin, CA", "Twentynine Palms, CA", "Upland, CA", "Valencia, CA", "Van Nuys, CA", "Ventura, CA", "Victorville, CA", "Visalia, CA", "Vista, CA", "West Covina, CA", "West Hollywood, CA", "Westmorland, CA", "Westwood Village, CA", "Wheeler Ridge, CA", "Whittier, CA", "Whittier Narrows Rec. Area, CA", "Woodland Hills, CA", "Wrightwood, CA", "Yorba Linda, CA", "Yucaipa, CA", "Yucca Valley, CA", "Yuma, AZ");

var PI=           3.14159265358979;
var TWO_PI=       6.28318530718;
var EARTH_RADIUS_EQUATOR= 3963.189;
var EARTH_RADIUS_POLE=    3949.901;
var compassPoints = new Array ( "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");

var dirstring;

function dircompassstr(x,y) {
  var closest=0;
  var d=-1;
  var dx,dy;
  var i=0;
  for (i=0; i<263; i++) {
    dx=x-towns[i*2];
    dy=y-towns[i*2+1];
    dx=(dx*dx)+(dy*dy);
    if (d<0 || dx<d) {
       closest=i;
       d=dx;
    }
  }
  dx=x-towns[closest*2];
  dy=y-towns[closest*2+1];
  var a=0;

    if (dx!=0.0) {
      a=Math.atan(dy/dx);
      if (dx<0) a+=PI;
      a=a+(PI/32.0);
      while (a<0.0) a+=TWO_PI;
      while (a>TWO_PI) a-=TWO_PI;
      a=a*8.0/PI;
    } else {
      if (dy>0) a=4;
      else a=12;
    }
  dx=dx*dx;
  dy=dy*dy;
  var radius=EARTH_RADIUS_EQUATOR*EARTH_RADIUS_POLE;
  var coslon=Math.cos(x)/180.0*PI;
  radius=radius/Math.sqrt(EARTH_RADIUS_EQUATOR*EARTH_RADIUS_EQUATOR-(EARTH_RADIUS_EQUATOR*EARTH_RADIUS_EQUATOR-EARTH_RADIUS_POLE*EARTH_RADIUS_POLE)*coslon*coslon);
  d=(Math.sqrt(d)/180.0*PI*radius);
  var a_int = Math.floor(a);
  var d_int = Math.floor(d+0.5);
  dirstring="";
  if (d>0) {
    if (d==1) {
      dirstring+="1 mile " +compassPoints[a_int]+" of ";
    } else {
      dirstring+=d_int+" miles "+compassPoints[a_int]+" of ";
    }
  }
  dirstring+=townnames[closest];

  return dirstring;
}

function dircompass(x,y) {
  document.write (dircompassstr(x,y));
}
