﻿// http://google-maps-utility-library-v3.googlecode.com/svn/tags/markermanager/1.0/src/markermanager_packed.js
eval(function (p, a, c, k, e, r) { e = function (c) { return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36)) }; if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function (e) { return r[e] } ]; e = function () { return '\\w+' }; c = 1 }; while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p } ('9 m(c,b){4 a=3;a.17=c;a.Q=c.1S();a.1f=u K(c);r.p.J.16(a.1f,\'15\',9(){a.2l=3.1I();a.1D(c,b)})}m.l.1D=9(d,b){4 f=3;b=b||{};f.11=m.1u;4 h=d.10;4 i=1;t(4 c 2H h){n(1l d.10.1n(c)===\'2r\'&&1l d.10.1n(c).1h===\'1N\'){4 g=d.10.1n(c).1h;n(g>i){i=g}}}f.F=b.1h||19;f.1E=b.29;f.v=b.1C||A;4 e;n(1l b.1y===\'1N\'){e=b.1y}12{e=m.1w}f.1v=u r.p.E(-e,e);f.1s=u r.p.E(e,-e);f.27=e;f.L={};f.V={};f.V[f.F]={};f.H={};f.H[f.F]=0;r.p.J.16(d,\'1Z\',9(){f.18()});r.p.J.16(d,\'1Y\',9(){f.18()});f.I=9(a){a.1a(2G);f.W--};f.O=9(a){n(f.v){a.1a(f.17);f.W++}};f.1m();f.W=0;f.q=f.1k();r.p.J.1g(f,\'2u\')};m.1u=2q;m.1w=2p;m.1M=2m;m.l.1m=9(){4 a=m.1M;t(4 b=0;b<=3.F;++b){3.V[b]={};3.H[b]=0;3.L[b]=s.2i(a/3.11);a<<=1}};m.l.2h=9(){3.P(3.q,3.I);3.1m()};m.l.w=9(a,c,b){4 d=3.1f.1G(a,c);4 e=u r.p.2e(s.1F((d.x+b.2a)/3.11),s.1F((d.y+b.28)/3.11));o e};m.l.1e=9(i,d,j){4 f=3;4 e=i.1d();i.1B=d;n(3.1E){r.p.J.16(i,\'1A\',9(a,b,c){f.1z(a,b,c)})}4 h=3.w(e,j,u r.p.E(0,0,0,0));t(4 g=j;g>=d;g--){4 k=3.1c(h.x,h.y,g);k.1x(i);h.x=h.x>>1;h.y=h.y>>1}};m.l.N=9(d){4 b=3.q.B<=d.y&&d.y<=3.q.D;4 e=3.q.C;4 c=e<=d.x&&d.x<=3.q.G;n(!c&&e<0){4 a=3.L[3.q.z];c=e+a<=d.x&&d.x<=a-1}o b&&c};m.l.1z=9(f,b,g){4 c=3.F;4 a=U;4 d=3.w(b,c,u r.p.E(0,0,0,0));4 e=3.w(g,c,u r.p.E(0,0,0,0));1t(c>=0&&(d.x!==e.x||d.y!==e.y)){4 h=3.X(d.x,d.y,c);n(h){n(3.1j(h,f)){3.1c(e.x,e.y,c).1x(f)}}n(c===3.Q){n(3.N(d)){n(!3.N(e)){3.I(f);a=A}}12{n(3.N(e)){3.O(f);a=A}}}d.x=d.x>>1;d.y=d.y>>1;e.x=e.x>>1;e.y=e.y>>1;--c}n(a){3.M()}};m.l.26=9(d){4 b=3.F;4 a=U;4 e=d.1d();4 c=3.w(e,b,u r.p.E(0,0,0,0));1t(b>=0){4 f=3.X(c.x,c.y,b);n(f){3.1j(f,d)}n(b===3.Q){n(3.N(c)){3.I(d);a=A}}c.x=c.x>>1;c.y=c.y>>1;--b}n(a){3.M()}3.H[d.1B]--};m.l.25=9(b,a,c){4 d=3.1b(c);t(4 i=b.T-1;i>=0;i--){3.1e(b[i],a,d)}3.H[a]+=b.T};m.l.1b=9(a){o a||3.F};m.l.24=9(a){4 b=0;t(4 z=0;z<=a;z++){b+=3.H[z]}o b};m.l.23=9(c,e,d){4 b=u r.p.22(c,e);4 f=3.w(b,d,u r.p.E(0,0,0,0));4 g=u r.p.21({20:b});4 a=3.X(f.x,f.y,d);n(a!==1r){t(4 i=0;i<a.T;i++){n(c===a[i].1q().1p()&&e===a[i].1q().Z()){g=a[i]}}}o g};m.l.1X=9(d,a,b){4 e=3.1b(b);3.1e(d,a,e);4 c=3.w(d.1d(),3.Q,u r.p.E(0,0,0,0));n(3.N(c)&&a<=3.q.z&&3.q.z<=e){3.O(d);3.M()}3.H[a]++};9 Y(a){3.C=s.R(a[0].x,a[1].x);3.G=s.S(a[0].x,a[1].x);3.B=s.R(a[0].y,a[1].y);3.D=s.S(a[0].y,a[1].y)}Y.l.1o=9(a){n(3.G===a.G&&3.D===a.D&&3.C===a.C&&3.B===a.B){o A}12{o U}};Y.l.1W=9(a){4 b=3;o(b.C<=a.x&&b.G>=a.x&&b.B<=a.y&&b.D>=a.y)};m.l.1c=9(x,y,z){4 b=3.V[z];n(x<0){x+=3.L[z]}4 c=b[x];n(!c){c=b[x]=[];o(c[y]=[])}4 a=c[y];n(!a){o(c[y]=[])}o a};m.l.X=9(x,y,z){4 a=3.V[z];n(x<0){x+=3.L[z]}4 b=a[x];o b?b[y]:1r};m.l.1U=9(j,b,c,e){b=s.R(b,3.F);4 i=j.2F();4 f=j.2E();4 d=3.w(i,b,c);4 g=3.w(f,b,e);4 a=3.L[b];n(f.Z()<i.Z()||g.x<d.x){d.x-=a}n(g.x-d.x+1>=a){d.x=0;g.x=a-1}4 h=u Y([d,g]);h.z=b;o h};m.l.1k=9(){o 3.1U(3.17.2D(),3.Q,3.1v,3.1s)};m.l.18=9(){3.1T(3,3.1J,0)};m.l.1T=9(b,a,c){o 2C.2B(9(){a.2A(b)},c)};m.l.2z=9(){o 3.v?A:U};m.l.2y=9(){o!3.v};m.l.1C=9(){3.v=A;3.13()};m.l.2x=9(){3.v=U;3.13()};m.l.2w=9(){3.v=!3.v;3.13()};m.l.13=9(){n(3.W>0){3.P(3.q,3.I)}n(3.v){3.P(3.q,3.O)}3.M()};m.l.1J=9(){3.Q=3.17.1S();4 a=3.1k();n(a.1o(3.q)&&a.z===3.q.z){o}n(a.z!==3.q.z){3.P(3.q,3.I);n(3.v){3.P(a,3.O)}}12{3.1i(3.q,a,3.1Q);n(3.v){3.1i(a,3.q,3.1P)}}3.q=a;3.M()};m.l.M=9(){r.p.J.1g(3,\'1A\',3.q,3.W)};m.l.P=9(b,a){t(4 x=b.C;x<=b.G;x++){t(4 y=b.B;y<=b.D;y++){3.14(x,y,b.z,a)}}};m.l.14=9(x,y,z,a){4 b=3.X(x,y,z);n(b){t(4 i=b.T-1;i>=0;i--){a(b[i])}}};m.l.1Q=9(x,y,z){3.14(x,y,z,3.I)};m.l.1P=9(x,y,z){3.14(x,y,z,3.O)};m.l.1i=9(c,d,a){4 b=3;b.1O(c,d,9(x,y){a.2o(b,[x,y,c.z])})};m.l.1O=9(j,k,b){4 f=j.C;4 a=j.B;4 d=j.G;4 h=j.D;4 g=k.C;4 c=k.B;4 e=k.G;4 i=k.D;4 x,y;t(x=f;x<=d;x++){t(y=a;y<=h&&y<c;y++){b(x,y)}t(y=s.S(i+1,a);y<=h;y++){b(x,y)}}t(y=s.S(a,c);y<=s.R(h,i);y++){t(x=s.R(d+1,g)-1;x>=f;x--){b(x,y)}t(x=s.S(f,e+1);x<=d;x++){b(x,y)}}};m.l.1j=9(a,c,b){4 d=0;t(4 i=0;i<a.T;++i){n(a[i]===c||(b&&a[i]===c)){a.2n(i--,1);d++}}o d};9 K(b){3.1a(b);4 d=8;4 c=1<<d;4 a=7;3.1L=b;3.2k=-1;3.2j=3.2s=3.2t=3.2g=-1}K.l=u r.p.2v();K.l.1K=9(a){o(1+a/1V)};K.l.1H=9(b){4 a=s.2f(b*s.1R/1V);o(1-0.5/s.1R*s.2d((1+a)/(1-a)))};K.l.1G=9(a,d){4 c=3.1L;4 b=3.1I().2c(a);4 e={x:~~(0.5+3.1K(a.Z())*(2<<(d+6))),y:~~(0.5+3.1H(a.1p())*(2<<(d+6)))};o e};K.l.2b=9(){n(!3.15){3.15=A;r.p.J.1g(3,\'15\')}};', 62, 168, '|||this|var|||||function||||||||||||prototype|MarkerManager|if|return|maps|shownBounds_|google|Math|for|new|show_|getTilePoint_||||true|minY|minX|maxY|Size|maxZoom_|maxX|numMarkers_|removeOverlay_|event|ProjectionHelperOverlay|gridWidth_|notifyListeners_|isGridPointVisible_|addOverlay_|processAll_|mapZoom_|min|max|length|false|grid_|shownMarkers_|getGridCellNoCreate_|GridBounds|lng|mapTypes|tileSize_|else|refresh|processCellMarkers_|ready|addListener|map_|onMapMoveEnd_||setMap|getOptMaxZoom_|getGridCellCreate_|getPosition|addMarkerBatch_|projectionHelper_|trigger|maxZoom|rectangleDiff_|removeFromArray_|getMapGridBounds_|typeof|resetManager_|get|equals|lat|getLatLng|undefined|nePadding_|while|DEFAULT_TILE_SIZE_|swPadding_|DEFAULT_BORDER_PADDING_|push|borderPadding|onMarkerMoved_|changed|MarkerManager_minZoom|show|initialize|trackMarkers_|floor|LatLngToPixel|LatToY_|getProjection|updateMarkers_|LngToX_|_map|MERCATOR_ZOOM_LEVEL_ZERO_RANGE|number|rectangleDiffCoords_|addCellMarkers_|removeCellMarkers_|PI|getZoom|objectSetTimeout_|getGridBounds_|180|containsPoint|addMarker|zoom_changed|dragend|position|Marker|LatLng|getMarker|getMarkerCount|addMarkers|removeMarker|borderPadding_|height|trackMarkers|width|draw|fromLatLngToDivPixel|log|Point|sin|_Y1|clearMarkers|ceil|_X0|_zoom|projection_|256|splice|apply|100|1024|object|_Y0|_X1|loaded|OverlayView|toggle|hide|isHidden|visible|call|setTimeout|window|getBounds|getNorthEast|getSouthWest|null|in'.split('|'), 0, {}));

var Tiempo_GMaps = {
    URLS: { SIGNOS: "http://img.europapress.net/imagenes/estaticos/tiempo/",
        SOMBRAS: "http://img.europapress.net/imagenes/estaticos/tiempo/sombra/s",
        FONDO_CAJA: "http://img.europapress.net/imagenes/estaticos/tiempo/fondo.png"
    },

    map_t: null,
    mgr: new Array(3),

    load: function () {
        var map_t_center = new google.maps.LatLng(CENTER_INI_LAT, CENTER_INI_LONG);
        var map_t_opts = { "zoom": ZOOM_INI, "center": map_t_center,
            "mapTypeId": google.maps.MapTypeId.TERRAIN, // También posible usar HYBRID
            "mapTypeControl": false, "navigationControl": true, "streetViewControl": false, "disableDoubleClickZoom": true
        };
        // Para mapas que no queremos que muevan: "draggable" = false

        map_t = new google.maps.Map(document.getElementById("map_tiempo"), map_t_opts);

        // Límites de Zoom
        google.maps.event.addListener(map_t, "zoom_changed", function () {
            if (map_t.getZoom() > ZOOM_MAX) map_t.setZoom(ZOOM_MAX);
            if (map_t.getZoom() < ZOOM_MIN) map_t.setZoom(ZOOM_MIN);
        }
	    );

        // Cargo los signos del mapa para hoy
        Tiempo_GMaps.cargaTiempo(0);

    },

    cargaTiempo: function (dia) {
        if (!Tiempo_GMaps.mgr[dia]) {
            // MarkerManager para gestionar los signos de cada día
            var mgrOptions = { borderPadding: 50, maxZoom: 9, trackMarkers: false };
            Tiempo_GMaps.mgr[dia] = new MarkerManager(map_t, mgrOptions);
            google.maps.event.addListener(Tiempo_GMaps.mgr[dia], 'loaded', function () {
                // Cargo los signos correspondientes
                var s;
                for (i = 0; i < Predicciones.length; i += 1) {
                    if (map_t.fullInfoBox) map_t.fullInfoBox.setMap(null);
                    s = Tiempo_GMaps.creaSigno(Predicciones[i], dia);
                    Tiempo_GMaps.mgr[dia].addMarker(s, Predicciones[i].provincia.zoom[0], Predicciones[i].provincia.zoom[1]);
                }
                // Oculto los otros
                for (i = 0; i < Tiempo_GMaps.mgr.length; i++)
                    if (Tiempo_GMaps.mgr[i] && i != dia && !Tiempo_GMaps.mgr[i].isHidden()) Tiempo_GMaps.mgr[i].hide();
                Tiempo_GMaps.mgr[dia].refresh();
            });
        } else {
            // Oculto los otros
            for (i = 0; i < Tiempo_GMaps.mgr.length; i++)
                if (Tiempo_GMaps.mgr[i] && i != dia && !Tiempo_GMaps.mgr[i].isHidden()) Tiempo_GMaps.mgr[i].hide();
            // Muestro el que corresponde
            if (Tiempo_GMaps.mgr[dia].isHidden()) {Tiempo_GMaps.mgr[dia].show();}
        }
    },

    creaSigno: function (prediccion, dia) {
        var ciudad = new google.maps.LatLng(prediccion.provincia.coordenadas[0], prediccion.provincia.coordenadas[1]);
        var marker;

        // Si tenemos signo, lo ponemos (10 = "Sin Signo")
        if (prediccion.fechas[dia].signo.codigo != '0000000010') {
            // Imagen, tamaño, origen, anchor
            var signoIcon = new google.maps.MarkerImage(Tiempo_GMaps.URLS.SIGNOS + prediccion.fechas[dia].signo.codigo + ".png", new google.maps.Size(33, 33), new google.maps.Point(0, 0), new google.maps.Point(16, 33));
            var signoShadow = new google.maps.MarkerImage(Tiempo_GMaps.URLS.SOMBRAS + prediccion.fechas[dia].signo.codigo + ".png", new google.maps.Size(33, 50), new google.maps.Point(0, 0), new google.maps.Point(16, 33));
            var shape = { "coord": [0, 0, 33, 0, 33, 50, 0, 50], "type": "poly" };
            //signoIcon.infoWindowAnchor = new GPoint(0, 0);

            // Configuramos el marcador:
            marker = new google.maps.Marker({ "position": ciudad, "icon": signoIcon, "shadow": signoShadow, "shape": shape });
        } else {
            // Si no tenemos signo, que el GoogleMaps ponga la marquita que tenga por defecto.
            marker = new google.maps.Marker({ "position": ciudad });
        }

        var ToolTipOptions = {
            "content": "<b>" + prediccion.provincia.descripcion + "</b><br />" + prediccion.fechas[dia].signo.descripcion + "<br />Temperaturas: <span class=\"TiempoMin\">" + prediccion.fechas[dia].temp[0] + "ºC</span> - <span class=\"TiempoMax\">" + prediccion.fechas[dia].temp[1] + "ºC</span><br />",
            "offsetHorizontal": -75,
            "offsetVertical": 0,
            "height": -1,
            "width": 150,
            "closeButton": false,
            "className": ""
        };

        // OnMouseOver ponemos el tooltip sólo con las temperaturas
        google.maps.event.addListener(marker, "mouseover", function () {
            if (!map_t.fullInfoBox) {
                if (!marker.tooltip) marker.tooltip = new InfoBox(ciudad, map_t, ToolTipOptions);
                marker.tooltip.setMap(map_t);
                map_t.tooltip = marker.tooltip;
            }
        });

        // Y OnMouseOut lo quitamos
        google.maps.event.addListener(marker, "mouseout", function () {
            if (map_t.tooltip) {
                map_t.tooltip.setMap(null);
                map_t.tooltip = null;
            }
        });

        // Ventana la información completa
        var html = ""; //"<img src=\"" + signoIcon.image + "\" align=\"left\" />"
        html += "<b>" + prediccion.provincia.descripcion + "</b>"
        if (dia > 0) html += " (día " + prediccion.fechas[dia].fecha.substring(6, 8) + '/' + prediccion.fechas[dia].fecha.substring(4, 6) + ')';
        html += "<br />Temperaturas: <span class=\"TiempoMin\">" + prediccion.fechas[dia].temp[0] + "ºC</span> - <span class=\"TiempoMax\">" + prediccion.fechas[dia].temp[1] + "ºC</span><br /><br />"
        html += prediccion.fechas[dia].comentario;

        var fullInfoBoxOptions = {
            "content": html,
            "offsetHorizontal": 30,
            "offsetVertical": -100,
            "height": -1,
            "width": 200,
            "closeButton": true,
            "className": "infoBox"
        };

        // OnClick mostramos la ventana completa
        google.maps.event.addListener(marker, "click", function () {
            if (!map_t.fullInfoBox) {
                map_t.fullInfoBox = "PlaceHolder";

                // Quitamos el tooltip
                if (map_t.tooltip) {
                    map_t.tooltip.setMap(null);
                    map_t.tooltip = null;
                }

                // Mostramos el InfoBox
                fullInfoBox = new InfoBox(ciudad, map_t, fullInfoBoxOptions);
                fullInfoBox.setMap(map_t);
                map_t.infoBox = fullInfoBox;

                // Centramos en la caja de texto
                var ciudad_xy = fullInfoBox.getProjection().fromLatLngToContainerPixel(ciudad);
                var nuevoCentro = new google.maps.Point(ciudad_xy.x + fullInfoBoxOptions.offsetHorizontal + fullInfoBoxOptions.width / 2, ciudad_xy.y);
                map_t.panTo(fullInfoBox.getProjection().fromContainerPixelToLatLng(nuevoCentro));
            }
        });

        return marker;
    },

    // Cambio del día mostrado en el mapa
    setDiaTiempo: function (dia, caja, enlace) {
        // Cargamos el tiempo del día correspondiente
        Tiempo_GMaps.cargaTiempo(dia);
        // Desactivamos todos los enlaces de cambio de día menos el activo
        var todosEnlaces = document.getElementById(caja).getElementsByTagName("a");
        for (i = 0; i < todosEnlaces.length; i++) {
            if (todosEnlaces[i].id == enlace)
                todosEnlaces[i].className = "tabActivo";
            else
                todosEnlaces[i].className = "";
        }
    }
}


/*
* http://gmaps-samples-v3.googlecode.com/svn/trunk/infowindow_custom/infobox.js
*
* An InfoBox is like an info window, but it displays
* under the marker, opens quicker, and has flexible styling.
* @param {GLatLng} latlng Point to place bar at
* @param {Object} opts Passes configuration options - content, 
*   offsetVertical, offsetHorizontal, className, height, width
*/
function InfoBox(latlng, map, opts) {
    google.maps.OverlayView.call(this, map);
    this.latlng_ = latlng;
    this.content_ = opts.content || "";
    this.offsetVertical_ = opts.offsetVertical || -5;
    this.offsetHorizontal_ = opts.offsetHorizontal || -5;

    this.closeButton_ = opts.closeButton;

    this.height_ = opts.height || 175;
    this.width_ = opts.width || 300;
    this.map_ = map;
}

/* InfoBox extends GOverlay class from the Google Maps API
*/
InfoBox.prototype = new google.maps.OverlayView();

/* Creates the DIV representing this InfoBox
* @param {GMap2} map The map to add infobox to
*/
InfoBox.prototype.panes_changed = function () {
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }

    var panes = this.get('panes');

    if (panes) {
        // Create the DIV representing our Bar
        var div = this.div_ = document.createElement("div");
        div.style.border = "2px solid #2f4f88";
        div.style.position = "absolute";
        div.style.background = "#ffffff";
        div.style.padding = "3px";
        div.style.width = this.width_ + "px";
        if (this.height_ > 0) div.style.height = this.height_ + "px";
        var contentDiv = document.createElement("div");
        contentDiv.innerHTML = this.content_;

        if (this.closeButton_) {
            var topDiv = document.createElement("div");
            topDiv.style.textAlign = "right";
            topDiv.style.paddingRight = "10px";
            var closeImg = document.createElement("img");
            closeImg.style.cursor = "pointer";
            closeImg.src = "http://www.google.com/intl/en_us/mapfiles/close.gif";
            topDiv.appendChild(closeImg);

            function removeInfoBox(ib) {
                return function () {
                    ib.getMap().fullInfoBox = null;
                    ib.setMap(null);
                };
            }

            google.maps.event.addDomListener(closeImg, 'click', removeInfoBox(this));

            div.appendChild(topDiv);
        }

        div.appendChild(contentDiv);

        this.draw();

        // Then add this overlay to the DOM
        panes.floatPane.appendChild(div);
    }
}

/* Redraw the Bar based on the current projection and zoom level
*/
InfoBox.prototype.draw = function () {
    if (!this.div_) return;

    // Calculate the DIV coordinates of two opposite corners of our bounds to
    // get the size and position of our Bar
    var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
    if (!pixPosition) return;

    // Now position our DIV based on the DIV coordinates of our bounds
    this.div_.style.width = this.width_ + "px";
    this.div_.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
    if (this.height_ > 0) this.div_.style.height = this.height_ + "px";
    this.div_.style.top = (pixPosition.y + this.offsetVertical_) + "px";
    this.div_.style.display = 'block';
}

InfoBox.prototype.onRemove = function () {
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }
}

google.maps.event.addDomListener(window, 'load', Tiempo_GMaps.load);
