var map = null;
var geocoder = null;
var latsgn = 1;
var lgsgn = 1;
var zm = 0; 
var marker = null;
var markermanager = null;
function placemarkeronpoint(point) {
//  alert('marker='+marker);
  if (marker != null) {
    marker.setPoint(point);
  } else {
    marker = new GMarker(new GLatLng(20.0, -10.0), {draggable: true});
    map.addOverlay(marker);
    marker.setPoint(point);
    GEvent.addListener(marker, "dragend", function() {
       var point = marker.getLatLng();
       if (zm == 0)
       {map.setCenter(point); zm = 1;}
       //{map.setCenter(point,7); zm = 1;}
       else
       {map.setCenter(point);}
//       alert('dragend3');
       computepos(point);
     }
    );
    GEvent.addListener(marker, "click", function() {
      var point = marker.getLatLng();
      marker.openInfoWindowHtml(marker.getLatLng().toUrlValue(6));
      computepos (point);
    });


  }
//  map.setCenter(point, 8);
  map.setCenter(point);
  computepos(point);
}

function center_on_latlonbox() {
//alert(document.getElementById("latbox").value);
//alert(document.getElementById("lonbox").value);
  point = new GLatLng(document.getElementById("latbox").value,document.getElementById("lonbox").value);
  placemarkeronpoint(point);
  map.setCenter(point, 12);
}


function setUserGoogleCoords() {
  lat = prompt ("Lattitude ?","");
  lon = prompt ("Longitud ?","");
  
  if (lat != "" && lon != "") {
    document.getElementById("latbox").value = lat;
    document.getElementById("lonbox").value = lon;
    center_on_latlonbox();
  }
}

function center_on_latlondegree(latitude, latitudem,latitudes, longitude,  longitudem,  longitudes) {
  var point = convertToPoint(latitude, latitudem,latitudes, longitude,  longitudem,  longitudes);
  placemarkeronpoint(point)
}


function xz() {
xz(null,null,null);
}

function xz(x, y, z) {
  xz(x,y,z,null);
}

function xz(x, y, z, markerid) {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
//map.setCenter(new GLatLng(20.0, -10.0), 2);
if (x == null) {
  map.setCenter(new GLatLng(55.74412004299435,37.6171875), 9);
} else {
  map.setCenter(new GLatLng(x, y), z);

}

map.setMapType(G_NORMAL_MAP);
map.addControl(new GLargeMapControl());
map.addControl(new MapTypeControl());
map.addControl(new GScaleControl());
map.enableScrollWheelZoom();
map.disableDoubleClickZoom();
geocoder = new GClientGeocoder();

markermanager = new GMarkerManager(map);

if (markerid != null) downloadmymarker(markerid);
else
downloadmymarkers();

}
else alert('Ваш браузер не совместим с интерактивной картой Google. Попробуйте Firefox 2.0 ');
}

function computepos (point)
{
var latA = Math.abs(Math.round(value=point.y * 1000000.));
var lonA = Math.abs(Math.round(value=point.x * 1000000.));

if(value=point.y < 0)
{
	var ls = '-' + Math.floor((latA / 1000000));
}
else
{
	var ls = Math.floor((latA / 1000000));
}

var lm = Math.floor(((latA/1000000) - Math.floor(latA/1000000)) * 60);
var ld = ( Math.floor(((((latA/1000000) - Math.floor(latA/1000000)) * 60) - Math.floor(((latA/1000000) - Math.floor(latA/1000000)) * 60)) * 100000) *60/100000 );

if(value=point.x < 0)
{
  var lgs = '-' + Math.floor((lonA / 1000000));
}
else
{
	var lgs = Math.floor((lonA / 1000000));
}

var lgm = Math.floor(((lonA/1000000) - Math.floor(lonA/1000000)) * 60);
var lgd = ( Math.floor(((((lonA/1000000) - Math.floor(lonA/1000000)) * 60) - Math.floor(((lonA/1000000) - Math.floor(lonA/1000000)) * 60)) * 100000) *60/100000 );

document.getElementById("latbox").value=point.y;
document.getElementById("latboxm").value=ls;
document.getElementById("latboxmd").value=lm;
document.getElementById("latboxms").value=ld;

document.getElementById("lonbox").value=point.x;
document.getElementById("lonboxm").value=lgs;
document.getElementById("lonboxmd").value=lgm;
document.getElementById("lonboxms").value=lgd;
}

function showLatLong(latitude, longitude) {
latitude1 = Math.abs( Math.round(latitude * 1000000.));
if(latitude1 > (90 * 1000000)) { alert(' Latitude must be between -90 to 90. ');  document.getElementById("latbox1").value=''; return;}
longitude1 = Math.abs( Math.round(longitude * 1000000.));
if(longitude1 > (180 * 1000000)) { alert(' Longitude must be between -180 to 180. ');  document.getElementById("lonbox1").value='';  return;}

var point = new GLatLng(latitude,longitude);

if (zm == 0)
{
	map.setMapType(G_HYBRID_MAP);
	map.setCenter(point,16);
	zm = 1;
}
else
{
	map.setCenter(point);
}

 var html = "";
 html += html + "Latitude, Longitude<br>" + point.toUrlValue(6);

 var baseIcon = new GIcon();
 baseIcon.iconSize=new GSize(32,32);
 baseIcon.shadowSize=new GSize(56,32);
 baseIcon.iconAnchor=new GPoint(16,32);
 baseIcon.infoWindowAnchor=new GPoint(16,0);
 var thisicon = new GIcon(baseIcon, "http://itouchmap.com/i/blue-dot.png", null, "http://itouchmap.com/i/msmarker.shadow.png");

 var marker = new GMarker(point,thisicon);
 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
 map.addOverlay(marker);

 GEvent.trigger(marker, "click");
}

function convertToPoint(latitude, latitudem,latitudes, longitude,  longitudem,  longitudes) {
if(latitude < 0)  { latsgn = -1; }
alat = Math.abs( Math.round(latitude * 1000000.));
if(alat > (90 * 1000000)) { alert(' Degrees Latitude must be between -90 to 90. ');  document.getElementById("latbox1m").value=''; document.getElementById("latbox1md").value=''; document.getElementById("latbox1ms").value=''; return; }
latitudem = Math.abs(Math.round(latitudem * 1000000.)/1000000);  //integer
absmlat = Math.abs(Math.round(latitudem * 1000000.));  //integer
if(absmlat >= (60 * 1000000)) {  alert(' Minutes Latitude must be between 0 to 59. ');  document.getElementById("latbox1md").value=''; document.getElementById("latbox1ms").value=''; return;}
latitudes = Math.abs(Math.round(latitudes * 1000000.)/1000000);
absslat = Math.abs(Math.round(latitudes * 1000000.));
if(absslat > (59.99999999 * 1000000)) {  alert(' Seconds Latitude must be between 0 and 59.99. '); document.getElementById("latbox1ms").value=''; return; }

if(longitude < 0)  { lgsgn = -1; }
alon = Math.abs( Math.round(longitude * 1000000.));
if(alon > (180 * 1000000)) {  alert(' Degrees Longitude must be between -180 to 180. '); document.getElementById("lonbox1m").value=''; document.getElementById("lonbox1md").value=''; document.getElementById("lonbox1ms").value=''; return;}
longitudem = Math.abs(Math.round(longitudem * 1000000.)/1000000);
absmlon = Math.abs(Math.round(longitudem * 1000000));
if(absmlon >= (60 * 1000000))   {  alert(' Minutes Longitude must be between 0 to 59. '); document.getElementById("lonbox1md").value=''; document.getElementById("lonbox1ms").value='';   return;}
longitudes = Math.abs(Math.round(longitudes * 1000000.)/1000000);
absslon = Math.abs(Math.round(longitudes * 1000000.));
if(absslon > (59.99999999 * 1000000)) {  alert(' Seconds Longitude must be between 0 and 59.99. '); document.getElementById("lonbox1ms").value=''; return;}

latitude = Math.round(alat + (absmlat/60.) + (absslat/3600.) ) * latsgn/1000000;
longitude = Math.round(alon + (absmlon/60) + (absslon/3600) ) * lgsgn/1000000;

var point = new GLatLng(latitude,longitude);
 return point;
}

function setOnLatLonDegree() {
  latitude = document.getElementById("latboxm").value;
  latitudem = document.getElementById("latboxmd").value;
  latitudes = document.getElementById("latboxms").value;

  longitude = document.getElementById("lonboxm").value;
  longitudem = document.getElementById("lonboxmd").value;
  longitudes = document.getElementById("lonboxms").value;

  point = convertToPoint(latitude, latitudem,latitudes, longitude,  longitudem,  longitudes);
//  alert(point);

  placemarkeronpoint(point);
  map.setCenter(point, 12);
}


function showLatLong1(latitude, latitudem,latitudes, longitude,  longitudem,  longitudes) {

var point = convertToPoint(latitude, latitudem,latitudes, longitude,  longitudem,  longitudes);
if (zm == 0)
{
	map.setMapType(G_HYBRID_MAP);
	map.setCenter(point,16);
	zm = 1;
}
else
{
	map.setCenter(point);
}
 var html = "";
 html += html + "Latitude, Longitude<br>" + point.toUrlValue(6);

 var baseIcon = new GIcon();
 baseIcon.iconSize=new GSize(32,32);
 baseIcon.shadowSize=new GSize(56,32);
 baseIcon.iconAnchor=new GPoint(16,32);
 baseIcon.infoWindowAnchor=new GPoint(16,0);
 var thisicon = new GIcon(baseIcon, "http://itouchmap.com/i/blue-dot.png", null, "http://itouchmap.com/i/msmarker.shadow.png");

 var marker = new GMarker(point,thisicon);
 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
 map.addOverlay(marker);

 GEvent.trigger(marker, "click");
}

function fc( point )
{
 var html = "";
 html += html + "Latitude, Longitude<br>" + point.toUrlValue(6);

 var baseIcon = new GIcon();
 baseIcon.iconSize=new GSize(32,32);
 baseIcon.shadowSize=new GSize(56,32);
 baseIcon.iconAnchor=new GPoint(16,32);
 baseIcon.infoWindowAnchor=new GPoint(16,0);
 var thisicon = new GIcon(baseIcon, "http://itouchmap.com/i/blue-dot.png", null, "http://itouchmap.com/i/msmarker.shadow.png");

 var marker = new GMarker(point,thisicon);
 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
 map.addOverlay(marker);
}


function createMarker(point, html) 
{
 var marker = new GMarker(point);
 GEvent.addListener(marker, "click", function()
 {
 marker.openInfoWindowHtml(html);
 });
 return marker;
}

function reset() {
map.clearOverlays();
document.getElementById("latbox").value='';
document.getElementById("latboxm").value='';
document.getElementById("latboxmd").value='';
document.getElementById("latboxms").value='';
document.getElementById("lonbox").value='';
document.getElementById("lonboxm").value='';
document.getElementById("lonboxmd").value='';
document.getElementById("lonboxms").value='';
marker = new GMarker(new GLatLng(20.0, -10.0), {draggable: true});
map.addOverlay(marker);
marker.setPoint(map.getCenter());

GEvent.addListener(marker, "dragend", function() {
var point = marker.getLatLng();
if (zm == 0)
{map.setCenter(point,7); zm = 1;}
else
{map.setCenter(point);}
//alert('dragend');
computepos(point);
}
);

GEvent.addListener(marker, "click", function() {
var point = marker.getLatLng();
marker.openInfoWindowHtml(marker.getLatLng().toUrlValue(6));
computepos (point);
});
}

function reset1() {
//marker.setPoint(map.getCenter());
  placemarkeronpoint(map.getCenter());
}

// ---------------------------------------------------------------------------
// Load map
// xml
var gmarkers = [];
var i = 0;

function createMarker(point,name,mtype, message,id,micon,replies,mark) {
   var marker = new GMarker(point, {icon:micon, title:name});
//   alert('cm:'+name);
   GEvent.addListener(marker, "click", 
     function() {
       marker.openInfoWindowHtml("<img src=\"http://www.radarinfo.ru/"+getIconUrlForType(mtype)+".gif\" align=\"left\" class=\"popupimage\">"+
"<b><a href=\"/gibdd/camera/"+id+"\">"+name+"</a></b><br>"+ message+"<br>Достоверность:"+mark+"&nbsp;&nbsp;Отзывов:"+replies);
     }
   );

 gmarkers[i] = marker;
 i++;
 return marker;
};

function getIconUrlForType(mtype) {
   if ("G" == mtype) {
     return "G";
   }
   else
   if ("M" == mtype) {
     return "M";
   }
   else
   if ("Z" == mtype) {
     return "Z";
   }
    else
   if ("S" == mtype) { 
     return "S";
   }
   else       
   if ("‘" == mtype) {
     return "C";
   }
   else       
   if ("C" == mtype) {
     return "C";
   }
   else  { 
     System.err.println('unknown type='+mtype);
     return "M";
   }
}

function getMyMarkers(markersurl,vlevel) {
  var baseIcon = new GIcon();
  baseIcon.iconSize=new GSize(20,20);
  baseIcon.iconAnchor=new GPoint(10,20);
  baseIcon.infoWindowAnchor=new GPoint(10,0);
  baseIcon.shadowSize=new GSize(36,20);


GDownloadUrl(markersurl, function(data, responseCode) {
 var batch = new Array(0);
 if(responseCode == 200) {

 var xml = GXml.parse(data);
 if (xml != null) try {
 var markers = xml.documentElement.getElementsByTagName("marker");
 var pntr = 0;
 for (var k = 0; k < markers.length; k++) {
   var point = new GLatLng(parseFloat(markers[k].getAttribute("lat")), 
                  parseFloat (markers[k].getAttribute("lon")));
   var name = markers[k].getAttribute("name");
   var message = markers[k].getAttribute("message");
   var micon = new GIcon(baseIcon);
   var mid = markers[k].getAttribute("id");
   var mtype = markers[k].getAttribute("type");
   var mmark = markers[k].getAttribute("mark");
   var mreplies = markers[k].getAttribute("replies");
   micon.image = "http://radarinfo.ru/"+getIconUrlForType(mtype)+"-20.gif";
   var marker = createMarker(point,name,mtype,message,mid, micon,mreplies,mmark);
   markermanager.addMarker(marker, vlevel);
 }
 }catch (err) { }
 
  } else if(responseCode == -1) {
      alert("Data request timed out. Please try later.");
        } else { 
            alert("Request resulted in error. Check XML file is retrievable.");
              }
               
 }
 
);

//alert(batch.size);
//  return batch;
}

function downloadmymarkers() {
getMyMarkers("/radar/xml?cameratype=G", 6);
getMyMarkers("/radar/xml?cameratype=S", 8);
getMyMarkers("/radar/xml?cameratype=C", 8);
getMyMarkers("/radar/xml?cameratype=Z", 10);
//markermanager.addMarkers(getMyMarkers("/radar/xml?cameratype=S"), 8);
//markermanager.addMarkers(getMyMarkers("/radar/xml?cameratype=G"), 3);
//markermanager.addMarkers(getMyMarkers("/radar/xml?cameratype=S"), 8);
markermanager.refresh();

}

function downloadmymarker(id) {
getMyMarkers("/radar/xml?id="+id, 10);
markermanager.refresh();

}


function goToGiveCenterLinkPage() {
  goToGiveLinkPage(map.getCenter(), map.getZoom());
}
function goToGiveLinkPage(vpnt, vzoom) {
 s = "http://www.radarinfo.ru/radar/radar?m=GIVELINK&lon="+vpnt.x+"&lat="+vpnt.y+"&zoom="+vzoom;
 location.href = s;
}

function goToAddPoint() {
  goToGiveLinkPage(map.getCenter(), map.getZoom());
}
function goToAddPoint(vpnt, vzoom) {
 s = "http://www.radarinfo.ru/radar/radar?m=SHOWADDFORM&lon="+vpnt.x+"&lat="+vpnt.y+"&zoom="+vzoom;
 location.href = s;
}