﻿// Register the namespace for client control.
Type.registerNamespace('StoodThere.Web');

// Define the control class and properties.
StoodThere.Web.LocationControl = function(element) {
    StoodThere.Web.LocationControl.initializeBase(this, [element]);

    //property holders
    this._map = null;
    this._marker = null;
    this._mapElement = null;
    this._markerImageUrl = null;
    this._findImageUrl = null;
    this._latInputElement = null;
    this._lngInputElement = null;
    this._addressInputElement = null;
    this._geocodeButtonElement = null;
    this._geocoder = null;
}

StoodThere.Web.LocationControl.prototype = {

    initialize: function() {

        StoodThere.Web.LocationControl.callBaseMethod(this, 'initialize');

        var e = this.get_element();
        var center = new google.maps.LatLng(this.get_Latitude(), this.get_Longitude());
        this._map = new GMap2(this._mapElement);
        this._map.setCenter(center, 1);
        this._map.setMapType(G_PHYSICAL_MAP);

        this._map.addControl(new GSmallMapControl());

        var icon = new GIcon();
        icon.iconSize = new GSize(16, 16);
        icon.iconAnchor = new GPoint(7, 7);
        icon.infoWindowAnchor = new GPoint(7, 7);
        icon.image = this._markerImageUrl;

        this._marker = new GMarker(center, { icon: icon, draggable: true });
        this._map.addOverlay(this._marker);

        this._geocodeButtonElement = document.createElement('IMG');
        this._geocodeButtonElement.src = this._findImageUrl;
        this._addressInputElement.parentNode.insertBefore(this._geocodeButtonElement, this._addressInputElement);
        //e.appendChild(this._geocodeButtonElement);

        this._geocoder = new GClientGeocoder();

        //Create delegates
        this._dragendHandler = Function.createDelegate(this, this._onDragEnd);
        this._geocodeClickHandler = Function.createDelegate(this, this._onGeocodeClick);
        this._geocodeKeyDownHandler = Function.createDelegate(this, this._onGeocodeKeyDown);
        this._cancelEnterKeyHandler = Function.createDelegate(this, this.cancelEnterKey);
        this._latLngByAddressHandler = Function.createDelegate(this, this.latLngByAddress);
        this._addressByLatLngHandler = Function.createDelegate(this, this.addressByLatLng);
        // Attach events
        $addHandler(this._geocodeButtonElement, "click", this._geocodeClickHandler);
        this._addressInputElement.onkeydown = this._geocodeKeyDownHandler;
        this._addressInputElement.onkeypress = this._cancelEnterKeyHandler;
        this._addressInputElement.onkeyup = this._cancelEnterKeyHandler;

        GEvent.addListener(this._marker, "dragend", this._dragendHandler);
    },

    cancelEnterKey: function(e) {
        if (!e) e = window.event;
        if (e && e.keyCode == 13) {
            e.returnValue = false;
            e.cancel = true;
            return false;
        }
    },

    addressByLatLng: function(response) {
        if (response && response.Status.code == 200) {
            var place = response.Placemark[0];
            this._addressInputElement.value = place.address;
        }
    },

    latLngByAddress: function(response) {
        if (!response || response.Status.code != 200) {
            alert("Sorry, we couldn\'t find that address");
        } else {
            place = response.Placemark[0];
            point = new GLatLng(
                place.Point.coordinates[1],
                place.Point.coordinates[0]);
            this._marker.setLatLng(point);
            this._map.panTo(point);
            this._latInputElement.value = point.lat();
            this._lngInputElement.value = point.lng();
        }
    },

    dispose: function() {
        $clearHandlers(this._map);
        StoodThere.Web.LocationControl.callBaseMethod(this, 'dispose');
    },

    _onGeocodeClick: function() {
        this._geocoder.getLocations(
            this._addressInputElement.value,
            this._latLngByAddressHandler);
    },

    _onGeocodeKeyDown: function(e) {
        if (!e) e = window.event;
        if (e && e.keyCode == 13) {
            this._geocoder.getLocations(
                this._addressInputElement.value,
                this._latLngByAddressHandler);
        }
        return this.cancelEnterKey(e);
    },

    _onDragEnd: function(e) {
        var ll = this._marker.getLatLng();
        this._geocoder.getLocations(ll, this._addressByLatLngHandler);
        this._latInputElement.value = (ll.lat());
        this._lngInputElement.value = (ll.lng());
    },

    get_Latitude: function() {
        return this._latInputElement.value;
    },
    set_Latitude: function(value) {
        this._latInputElement.value
    },

    get_Longitude: function() {
        return this._lngInputElement.value;
    },
    set_Longitude: function(value) {
        this._lngInputElement.value;
    },

    get_MarkerImageUrl: function() {
        return this._markerImageUrl;
    },
    set_MarkerImageUrl: function(value) {
        this._markerImageUrl = value;
    },

    get_FindImageUrl: function() {
        return this._findImageUrl;
    },
    set_FindImageUrl: function(value) {
        this._findImageUrl = value;
    },

    get_MapElement: function() {
        return this._mapElement;
    },
    set_MapElement: function(value) {
        this._mapElement = value;
    },

    get_AddressInputElement: function() {
        return this._addressInputElement;
    },
    set_AddressInputElement: function(value) {
        this._addressInputElement = value;
    },

    get_LatInputElement: function() {
        return this._latInputElement;
    },
    set_LatInputElement: function(value) {
        this._latInputElement = value;
    },

    get_LngInputElement: function() {
        return this._lngInputElement;
    },
    set_LngInputElement: function(value) {
        this._lngInputElement = value;
    }
}

StoodThere.Web.LocationControl.registerClass('StoodThere.Web.LocationControl', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 