﻿/*Acceso a la api de Google Maps*/
// Api de cliente para todos los editores que son input simples o dropDownLists (select)

/*Carga de js externo GoogleMaps*/
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript'%3E%3C/script%3E"));

/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("Softeng.PB");
Softeng.PB.CmsGoogleMaps = function(element) {

    Softeng.PB.CmsGoogleMaps.initializeBase(this, [element]);
    this._latitud = 0;
    this._longitud = 0;
    this._zoom = 2;
    this._tipoMapa = null; // google.maps.MapTypeId.ROADMAP;
    this._map = null;
    this._geocoder = null;
    this._markManager = null;
    this._markers = new Array();

}

Softeng.PB.CmsGoogleMaps.prototype = {
    initialize: function() {

        Softeng.PB.CmsGoogleMaps.callBaseMethod(this, 'initialize');
        // Add custom initialization here

        var myLatlng = new google.maps.LatLng(39.97712, -3.625488);
        this._geocoder = new google.maps.Geocoder();
        var myOptions = {
            zoom: 6,
            center: myLatlng,
            navigationControl: true,
            /*mapTypeControl: false,*/
            scaleControl: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(this.get_element(), myOptions);
        this._map = map;

    },
    setCenter: function(aLatitud, aLongitud, aZoom) {

        this._map.setCenter(new google.maps.LatLng(aLatitud, aLongitud));
        this._map.setZoom(aZoom);

    },
    setCenterFromAddress: function(aDireccion, aZoom) {

        var self = this;
        if (self._geocoder) {
            self._geocoder.geocode({ 'address': aDireccion }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    self._map.setCenter(results[0].geometry.location);
                    self._map.setZoom(aZoom);
                }

            });
        }



    },
    getGeoLoc: function(aDireccion) {
        var self = this;
        if (self._geocoder) {
            self._geocoder.geocode({ 'address': address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    return results[0].geometry.location;
                }
            });
        }
        return null;
    },
    addMarkers: function(aArray, icon) {

        var self = this;

        $.each(aArray.d.Rows, function(i, Row) {
            var address = Row.Properties.direccion + "," + Row.Properties.ciudad;
            if (self._geocoder) {
                self._geocoder.geocode({ 'address': address }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var marker = new google.maps.Marker({
                            map: self._map,
                            position: results[0].geometry.location,
                            icon: icon

                        });
                        self._markers.push(marker);
                        google.maps.event.addListener(marker, "click", function() { self._map.setCenter(results[0].geometry.location); self._map.setZoom(10) });

                        marker.setTitle(Row.Properties.nombre)

                    }
                });
            }
        });
    },
    dispose: function() {
        //Add custom dispose actions here
        Softeng.PB.CmsGoogleMaps.callBaseMethod(this, 'dispose');
    },
    cleanMap: function() {

        for (var i = 0; i < this._markers.length; i++) {
            this._markers[i].setMap(null);
        }
        this._markers.length = 0;
        //Add custom dispose actions here
        // this._map.clearOverlays();
    },

    get_latitud: function() {
        return this._latitud;
    },
    set_latitud: function(value) {
        if (this._latitud !== value) {
            this._latitud = value;
            this.raisePropertyChanged('latitud');
        }
    }


}
Softeng.PB.CmsGoogleMaps.registerClass('Softeng.PB.CmsGoogleMaps', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();



