
var GStreetMap = function (args){
    if (args){
        this.containerId = args.containerId ? args.containerId : null;
        this.latlng = args.latlng ? args.latlng : null;
        this.zoom = args.zoom ? args.zoom : null;
    }else{
        this.containerId = null;
        this.latlng = null;
        this.zoom = 10;
    }
}

GStreetMap.prototype = {
    render : function (){
        var container = document.getElementById(this.containerId);
        if (container==null) throw 'No se ha indicado ninguna capa contenedora';
        this.map = new GMap2(container);
        this.geocoder = new GClientGeocoder(); 
GStreetMap.prototype.map = this.map;

        if (this.latlng==null) throw 'No se ha indicado las coordenadas iniciales del mapa';
        this.map.setCenter(this.latlng, this.zoom);

        this.map.setMapType(G_NORMAL_MAP);
        this.map.addControl(new GMapTypeControl());
        this.map.addControl(new GLargeMapControl());
        this.renderHLPoints();
    },
    attachHLPManager : function (hlpManager){
        this.hlpManager = hlpManager;
    },
    renderHLPoints : function (){
        if (!this.map || !this.hlpManager) return;
        this.hlpManager.addHLPointsToMap(this.map);
    },
    refreshHLPointsViewState : function (){
        if (!this.map || !this.hlpManager) return;
        this.hlpManager.refreshHLPointsViewState();
    },
    moveToHLPoint : function (hlPoint){
        if (!hlPoint) return;
        this.map.setCenter(hlPoint.latlng, hlPoint.zoom);
        hlPoint.show();
    },
    goToHLPoint : function (id){
        if (!this.map || !this.hlpManager) return null;
        var hlPoint = null;
        if (hlPoint = this.hlpManager.getHLPoint(id)){
            this.moveToHLPoint(hlPoint);
        }else{
            var ajaxRequest = new Ajax.Request(
                GStreetMap.url,
                {
                        method: 'get',
                        parameters: "id="+id,
                        asynchronous: false,
                        onSuccess: function (transport){
                            var hlpMap = transport.responseJSON;
                            var icon = hlpMap.icon ? hlpMap.icon : null;
                            if (icon){
                                icon = 'upload/laciudad/callejero/categorias/iconos/max/'+icon
                            }
                            var hlPoint = new GStreetMapHighLightPoint (
                                    hlpMap.id ? hlpMap.id : null,
                                    hlpMap.categoryid ? hlpMap.categoryid : null,
                                    new GLatLng(hlpMap.lat,hlpMap.lng),
                                    icon,
                                    {ajax:true}
                                    );
                            this.hlpManager.add(hlPoint);
                            this.map.addOverlay(hlPoint.marker)
                            this.moveToHLPoint(hlPoint);
                        }.bind(this)
                });
        }
        //Se centra la pagina sobre el mapa
        this.goToMap();
        if (hlPoint != null){
            hlPoint.openInfo();
        }
    },
    goToMap : function (){
        var url = document.location.href;
        var url_base = url.split('#');
        url_base = url_base[0];
        document.location.href = url_base + "#link" + this.containerId;
    },
    goToAddress : function (address){
      if (this.geocoder) {
        this.geocoder.getLatLng( address, this.goToPoint );
      }
    },
    goToPoint : function (point){
      //Problema null, verrr
      if (point) {
         GStreetMap.prototype.map.setCenter(point, 17);
         var marker = new GMarker(point);
         GStreetMap.prototype.map.addOverlay(marker);
      }
    }
}

