    function circlepoints(lat, lng, diameter)
    {
        var lat = parseFloat(lat);
        var lng = parseFloat(lng);
        var radius = parseInt(diameter)/2;
        var d2r = Math.PI/180;          // degrees to radians
        var r2d = 180/Math.PI;          // radians to degrees
        var Clat = (radius/6370000) * r2d;      //  using 6370 km as earth's radius
        var Clng = Clat/Math.cos(lat*d2r);

        var Cpoints = [];
        for (var i = 0 ; i < 13 ; i++) {
            var theta = Math.PI * (i/6);
            Cx = lng + (Clng * Math.cos(theta));
            Cy = lat + (Clat * Math.sin(theta));
            Cpoints.push(new google.maps.LatLng(Cy,Cx));
        }
        return Cpoints;
    }

    function addpoint(map, latitude, longitude, html, markerOptions) {
        var markerOptions = markerOptions != undefined ? markerOptions : new Array();
        var point = new google.maps.LatLng(latitude, longitude);
        markerOptions['position'] = point;
        var marker = new google.maps.Marker(markerOptions);

        google.maps.event.addListener(marker, "click", function() {
            var info = new google.maps.InfoWindow({"content": html}, marker);
            info.open(map, marker);
        });
        marker.setMap(map);
        return marker;
    }

    function circleline(map, lat, lng, diameter) {
        var points = circlepoints(lat, lng, diameter);
        var circle = new google.maps.Polyline({"map": map, "path": points});
        circle.setMap(map);
        return circle;
    }

    function circle(map, lat, lng, diameter, color) {
        color = color != undefined ? color : "#00FF00";
        var points = circlepoints(lat, lng, diameter);
        var circleobj = new google.maps.Polygon({"map": map, "paths": points, "fillColor": color, "fillOpacity": .2, "strokeOpacity": 0});
        circleobj.setMap(map);
        return circleobj;
    }

function myloc_position_update(parent, xmlHttp) {
    this.parent = parent;
    this.xmlHttp = xmlHttp;

    this.ProcessRequest = function() {
        if ( this.xmlHttp.readyState == 4 && this.xmlHttp.status == 200 ) 
        {
            var info = JSON.parse(this.xmlHttp.responseText);
            var address = info.location.address;

            if (this.parent.point !== null) {
                this.parent.point.setMap();
            }
            if (this.parent.circle !== null) {
                this.parent.circle.setMap();
            }

            this.parent.point = addpoint(this.parent.map, info.location.latitude, info.location.longitude, "You are here.<br/><br/>" + address.street_number + " " + address.street + "<br />" + address.city + ", " + address.region + ", " + address.country_code + " " + address.postal_code);
            this.parent.circle = circle(this.parent.map, info.location.latitude, info.location.longitude, this.xmlHttp.accuracy*2, "#FF0000");
            this.parent.callback(info, this.xmlHttp.accuracy);
        }
    };
}

function myloc_position(parent) {
  this.parent = parent;

  this.watch = function(position) {
    var data = '{"version": "1.1.0","host": "www.nabber.org","request_address": true,"address_language": "en_US","location": {"latitude": ' + position.coords.latitude + ',"longitude": ' + position.coords.longitude + '}}';
    var xmlHttp = new XMLHttpRequest(); 
    var updateobj = new myloc_position_update(this.parent, xmlHttp);
    xmlHttp.accuracy = position.coords.accuracy;
    xmlHttp.onreadystatechange = function (){ updateobj.ProcessRequest();};
    xmlHttp.open( "POST", '/whereami/locjson.php', true );
    xmlHttp.send(data);

  };

}

function myloc() {
  this.point = null;
  this.circle = null;
  this.map = null;
  this.xmlHttp = null;

  this.start = function(map) {
      this.map = map;
      var myvar = new myloc_position(this);
      if (navigator.geolocation) {
      /* geolocation is available */
/*          
          document.write('<div id="PosButton" style="width: 50; height: 50"><b>Your Button text</b></div>'); 

          var pos = new google.maps.ControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0,0)); 
          var ButtonDivId = document.getElementById("PosButton");
          pos.apply(ButtonDivId);
          map.getContainer().appendChild(ButtonDivId); */

          this.watchID = navigator.geolocation.watchPosition(function(position) {
              myvar.watch(position);
          }); 
      }
  };

  this.stop = function() {
    if (navigator.geolocation) {
      navigator.geolocation.clearWatch(this.watchID);
    }
    this.point.setMap();
    this.circle.setMap();
  };
  this.callback = function(info, acc) {};
}

function send_myloc_position_update(parent, xmlHttp) {
    this.parent = parent;
    this.xmlHttp = xmlHttp;

    this.ProcessRequest = function() {
        if ( this.xmlHttp.readyState == 4 && this.xmlHttp.status == 200 ) 
        {
            var info = JSON.parse(this.xmlHttp.responseText);
            this.parent.callback(info, this.xmlHttp.accuracy);
        }
    };
}

function send_myloc_position(parent) {
  this.parent = parent;

  this.watch = function(position) {
    var data = '{"version": "1.1.0","host": "www.nabber.org","request_address": true,"address_language": "en_US","location": {"latitude": ' + position.coords.latitude + ',"longitude": ' + position.coords.longitude + '}}';
    var xmlHttp = new XMLHttpRequest(); 
    var updateobj = new send_myloc_position_update(this.parent, xmlHttp);
    xmlHttp.accuracy = position.coords.accuracy;
    xmlHttp.onreadystatechange = function (){ updateobj.ProcessRequest();};
    xmlHttp.open( "POST", '/whereami/locjson.php', true );
    xmlHttp.send(data);

  };

}

function send_myloc() {
  this.point = null;
  this.circle = null;
  this.map = null;
  this.xmlHttp = null;

  this.start = function(map) {
      this.map = map;
      var myvar = new send_myloc_position(this);
      if (navigator.geolocation) {
      /* geolocation is available */
          this.watchID = navigator.geolocation.watchPosition(function(position) {
              myvar.watch(position);
          }); 
      }
  };

  this.stop = function() {
    if (navigator.geolocation) {
      navigator.geolocation.clearWatch(this.watchID);
    }
  };
  this.callback = function(info, acc) {
    var xmlHttp = new XMLHttpRequest();
    info.location.accuracy = acc;
    xmlHttp.onreadystatechange = function (){
        if ( this.readyState == 4 && this.status == 200 ) 
        {
            //alert(this.responseText);
        }
    };
    xmlHttp.open("GET", '/whereami/post.php?loc=' + JSON.stringify(info), true );
    xmlHttp.send();
  };
}


