﻿document.write('<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA-qkr8SQLQDi9Aj_qSXGeVRRjMrLliYdxz11NOxLinB_k_Z8-zBQyoiqXGc2uny9EkWUTEwWRXxuIwQ&sensor=false" type="text/javascript"></script>');

var map = null;
var geocoder = null;
var defaultZoom = 13;


function initializeGoogleMaps() 
{
    if (GBrowserIsCompatible()) 
    {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), defaultZoom);
        map.setUIToDefault();
    }
}

function setGoogleMaps(lat, lng, text)
{
    if (GBrowserIsCompatible()) 
    {
        var map = new GMap2(document.getElementById("map_canvas"));
        var center = new GLatLng(lat, lng);
        var marker = new GMarker(center);
        map.setCenter(center, defaultZoom);
        map.addOverlay(marker);
        map.openInfoWindow(map.getCenter(), document.createTextNode(text));
        map.setUIToDefault();
    }
}

function showAddressWithNoMovingMarker(address) 
{
    if (!map) {map = new GMap2(document.getElementById("map_canvas"));}
    if (!geocoder) { geocoder = new GClientGeocoder();}
    map.clearOverlays();
    map.addControl(new GSmallMapControl());
        
    geocoder.getLatLng(
        address,
        function(point) 
        {
            if (!point) 
            {
                //alert("Ne najdem naslova: " + address);
            } 
            else 
            {
              map.setCenter(point, defaultZoom);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
        }
    );
}

function btnFindLocation_ClientClick(s,e)
{
    showAddressOnMap();
}

function showAddressOnMap() 
{
    var address = _address.GetText();
    
    if (address == null || address=='')
    {
        address = "LJUBLJANA";
        document.getElementById("address").value = address;
    }
    else    
        document.getElementById("address").value = _address.GetText();
    
    if (!map) {map = new GMap2(document.getElementById("map_canvas"));}
    if (!geocoder) { geocoder = new GClientGeocoder();}
    map.clearOverlays();
    map.addControl(new GSmallMapControl());
    //map.addControl(new GMapTypeControl()); 

    geocoder.getLatLng
    (
        address, 
        function(point) 
        {
            if (!point) 
            {
                alert("Ne najdem naslova: " + address);
            } 
            else 
            {
                var lat = document.getElementById("lat").value = point.lat().toFixed(5);
	            var lng = document.getElementById("lng").value = point.lng().toFixed(5);
	            
	            showCoordinateOnMap(lat, lng, address);

                /*
                map.setCenter(point, defaultZoom);
                var marker = new GMarker(point, {draggable: true}); 
                var pt = marker.getPoint();
	            map.panTo(pt); 
	            map.setCenter(pt, defaultZoom);
		        
		        GEvent.addListener(marker, "dragstart", function() 
		        {
                    map.closeInfoWindow();
                    var pt = marker.getPoint();
	                map.panTo(pt);
                    document.getElementById("lat").value = pt.lat().toFixed(5);
	                document.getElementById("lng").value = pt.lng().toFixed(5);
                });

                GEvent.addListener(marker, "dragend", function() {
                    //marker.openInfoWindowHtml("Koordinate nove lokacije shranjene.");
                    var pt = marker.getPoint();
	                map.panTo(pt);
                    document.getElementById("lat").value = pt.lat().toFixed(5);
	                document.getElementById("lng").value = pt.lng().toFixed(5);
                });

                map.addOverlay(marker);
                
                */
		    }
        }     
    );
}

function showCoordinateOnMap(lat, lng, text)
{
    document.getElementById("address").value = text;
    document.getElementById("lat").value = lat;
    document.getElementById("lng").value = lng;
    
    if (GBrowserIsCompatible()) {
        if (!map) {map = new GMap2(document.getElementById("map_canvas"));}
        var center = new GLatLng(lat, lng);
        map.clearOverlays();
        map.addControl(new GSmallMapControl());
        map.setCenter(center, defaultZoom);
        map.openInfoWindow(map.getCenter(), document.createTextNode(text));

        var marker = new GMarker(center, {draggable: true});

        GEvent.addListener(marker, "dragstart", function() 
        {
            map.closeInfoWindow();
            var pt = marker.getPoint();
            map.panTo(pt);
            document.getElementById("lat").value = pt.lat().toFixed(5);
            document.getElementById("lng").value = pt.lng().toFixed(5);
        });

        GEvent.addListener(marker, "dragend", function() 
        {
            //marker.openInfoWindowHtml("Koordinate nove lokacije shranjene.");
            var pt = marker.getPoint();
            map.panTo(pt);
            document.getElementById("lat").value = pt.lat().toFixed(5);
            document.getElementById("lng").value = pt.lng().toFixed(5);
        });

        map.addOverlay(marker);

      }
 
} 