<?
include('../connect/connect.php');
include('../lib/lib.php');

$site_1=$_SERVER['HTTP_REFERER'];
$site_2=doman_name($site_1);
$sites_1=$_SERVER['HTTP_HOST'];
$sites_2=doman_name($sites_1);
if($site_2[1] != $sites_2[1])
exit();

?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; utf-8"/>
<title>Выставление маркера на карте по адресу места</title>
<link rel="stylesheet" href="../style/template_adm.css" type="text/css"/>

    <style>
      html, body, #map_canvas {
        margin: 0;
        padding: 0;
        height: 100%;
      }
 
	  #coord_form{
		position: absolute;
		z-index: 1000;
		background: none repeat scroll 0% 0% rgb(255, 255, 255);
		list-style: none outside none;
		border: solid 1px #999999; 

		padding: 10px;
		margin: 0px;
		right: 5px;
		top: 70px;
	  }
	  
	  #search_form{
		position: absolute;
		z-index: 1000;
		background: none repeat scroll 0% 0% rgb(255, 255, 255);
		list-style: none outside none;

		padding: 0px;
		margin: 0px;
		left: 100px;
		top: 2px;
		height:0px;
	  }
	  
	  .input-medium {
	  width:120px;
	  margin-top: 5px;
	  margin-bottom: 5px;
	  }
	  
	  .sp {
	  color:#990000;
	  }

    </style>


<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">

var geocoder;
var map;
var marker

<?
if($_GET['map_x'])
	{
	echo "\n var x=".$_GET['map_x'].";";
	echo "\n var y=".$_GET['map_y'].";";
	}
else
	{
	echo "\n var x=55.755786;";
	echo "\n var y=37.617633;";
	}
?>


// Карта + передвижной маркер с обновлением координат
function initialize() {
geocoder = new google.maps.Geocoder();
   var myLatlng = new google.maps.LatLng(x,y);
   var myOptions = {
     zoom: 12,
     center: myLatlng,
     mapTypeId: google.maps.MapTypeId.ROADMAP
   }
   document.getElementById('spot-lat').value = x.toFixed(4);
   document.getElementById('spot-longi').value = y.toFixed(4);

   map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   
   
   //Добавляем маркер
   marker = new google.maps.Marker({
       position: myLatlng, 
       map: map,
       draggable:true
   });
   google.maps.event.addListener(
       marker,
       'drag',
       function() {
           document.getElementById('spot-lat').value = marker.position.lat().toFixed(4);
           document.getElementById('spot-longi').value = marker.position.lng().toFixed(4);
       }
   );
}

 
// Определяем местоположение по адресу
function showAddress(address) {
   geocoder.geocode( { 'address': address}, function(results, status) {
	if (status == google.maps.GeocoderStatus.OK) {
		map.setCenter(results[0].geometry.location);
		marker.position=results[0].geometry.location;
           document.getElementById('spot-lat').value = marker.position.lat().toFixed(4);
           document.getElementById('spot-longi').value = marker.position.lng().toFixed(4);
		} else {
		alert("Ошибка геокодера! Причина: " + status);
		}
   });
}

function save_address() {
	var x = document.getElementById('spot-lat').value; 
	var y = document.getElementById('spot-longi').value; 
	window.opener.document.getElementById('map_x').value = x; 
	window.opener.document.getElementById('map_y').value = y; 
	self.close ();
	}


</script>
</head>

<body onload="initialize()" >
<div id="map_canvas" style="width: 620px; height: 520px"></div>
<div id="search_form">
		<form method="post" action="#" onsubmit="showAddress(this.address.value); return false">
			<input type="text" size="30" name="address" value="<?=$address?>" />
			<input class="submit" type="submit" name="submit1" value="Найти" />
		</form>
</div>


<div id="coord_form">
		<form name="form2" action="#" onsubmit="save_address(); return false">
			Широта <br />
			<input readonly="1" class="input-medium"  name="lat" id="spot-lat" value="<?=$x?>" /><br />
			
			Долгота <br />
			<input readonly="1" class="input-medium"  name="longi" id="spot-longi" value="<?=$y?>" /><br />
			
			<span class=sp>
			После перемещения<br />маркера не забудьте<br />сохранить изменения
			</span>
			<br />
			<input class="submit" type="submit" name="submit2" value="Сохранить" />
		</form>
</div>

</body>
</html>