/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * v 0.0.7
 * 0.0.7 Minor updates
 * 0.0.6 Fixed bug for find location when address empty
 * 0.0.5 Fixed bug with starting lat and lng
 * 0.0.4 Advanced map functionality (might need a bit of tidying up)
 * 0.0.3 Set width of addressMap to 100% [caution upgrading]
 * 0.0.2 add showLocationMap to show a small location map
 */
addDOMLoadEvent(modifyAddressMap);

function showLocationMap(lat, lng)
{
    var map = new GMap2($('map'));
    map.setCenter(new GLatLng(lat, lng), 10);
    var point = new GLatLng(lat, lng);
    map.addOverlay(new GMarker(point));
    map.setUIToDefault();
}

function modifyAddressMap ()
{
    if ($('addressMap') && $('lat') && $('lng') && $('locationField')) {
        $('addressMap').setStyle({width: '100%', height: '300px'});
        $('addressMap').insert({after: '<p>Adjust the location by either dragging the marker, or clicking on the map in the correct location.</p>'});
        $('locationField').insert({before: '<input class="button" type="button" id="locateAddress" value="Find on Map">'});
        $('locateAddress').insert({after: '<input class="button" type="button" id="showMap" value="Show Map"><input class="button" type="button" id="hideMap" value="Hide Map">'});
        $('locationField').insert({after: '&nbsp;<input class="button" type="button" id="resetMap" value="Zero">' });
        Event.observe($('showMap'), 'click', function(e)     {
            showMap($('addressMap'));
	}, false);
        Event.observe($('hideMap'), 'click', function(e)     {
            hideMap($('addressMap'));
	}, false);
        var map = new GMap2($('addressMap'));
        Event.observe($('resetMap'), 'click', function(e)     {
            resetMap(map);
	}, false);
        map.setCenter(new GLatLng($('lat').getValue(), $('lng').getValue()), 13);
        map.setUIToDefault();
        addDraggablePoint(map, $('lat').getValue(), $('lng').getValue());
        $('locationField').innerHTML = round_number($('lat').getValue(), 3) + ", " + round_number($('lng').getValue(), 3);
        hideMap($('addressMap'));
        Event.observe($('locateAddress'), 'click', function(e)     {
            var geocoder = new GClientGeocoder();
            address = buildAddress();
            if (address == ', UK')
            {
                showMap($('addressMap'));
                map.clearOverlays();
                addDraggablePoint(map, $('lat').getValue(), $('lng').getValue());
            } else {
                geocoder.getLatLng(
                    address,
                    function(point) {
                      if (!point) {
                        alert("Sorry but this address cannot be found.");
                      } else {
                        showMap($('addressMap'));
                        map.setCenter(point, 13);
                        lat = point.lat();
                        lng = point.lng();
                        updatePoint(map, lat, lng);
                      }
                    }
                  );
            }
            Event.stop(e);
	}, false);

        GEvent.addListener(map, "click", function(marker, point)
            {
                if (!marker)
                {
                    lat = point.lat();
                    lng = point.lng();
                    updatePoint(map, lat, lng);
                }
            }
        )
    }
}

function resetMap(map)
{
    map.clearOverlays();
    $('lat').value = 0;
    $('lng').value = 0;
    $('locationField').innerHTML = '0, 0';
}

function buildAddress()
{
    address = $('line1').getValue();
    if ($('line2').getValue()!="") {
        if (address!="") address += ', ';
        address += $('line2').getValue();
    }
    if ($('line3').getValue()!="") {
        if (address!="") address += ', ';
        address += $('line3').getValue();
    }
    if ($('townCity').getValue()!="") {
        if (address!="") address += ', ';
        address += $('townCity').getValue();
    }
    if ($('county').getValue()!="") {
        if (address!="") address += ', ';
        address += $('county').getValue();
    }
    if($('country'))
    {
        if ($('country').getValue()!="") {
        if (address!="") address += ', ';
        address += $('country').getValue();
        }
    } else address += ', UK';
    return address;
}

function hideMap(p)
{
    p.hide();
    $('hideMap').hide();
    $('showMap').show();
    p.next("p").hide();
}

function showMap(p)
{
    p.show();
    p.next("p").show();
    $('hideMap').show();
    $('showMap').hide();
}

function updatePoint (map, lat, lng)
{
    $('lat').value = lat;
    $('lng').value = lng;
    $('locationField').innerHTML = round_number(lat, 4) + ", " + round_number(lng, 4);
    map.clearOverlays();
    addDraggablePoint(map, $('lat').getValue(), $('lng').getValue());
}

function addDraggablePoint(map, lat, lng)
{
    var point = new GLatLng(lat,lng);
    var marker = new GMarker(point, {draggable: true});
    GEvent.addListener(marker, "dragend", function() {
      updatePoint(map, marker.getPoint().lat(), marker.getPoint().lng());
    });
    map.addOverlay(marker);
}
