Source: temp/jsdocinputdirs/cercalia.control.meteo.js

/**
 * @classdesc
 * Weather control. Weather forecast layer: shows weather icons on the map with zoom filter,
 * and detailed weather forecast information popup.
 *
 * @constructor
 * @extends {ol.control.Control}
 */
cercalia.Control.Meteo = function(){

	/**
	 * @private
	 * @type {string}
	 */
	this.name_ = cercalia.MapControls.MeteoControl;

	/**
	 * Class name
	 * @private
	 * @type {string}
	 */
	this.CLASS_NAME_ = "cercalia.Control.Meteo";

	/**
	 * @private
	 * @type {cercalia.service.Pois}
	 */
	this.servicePoisMeteo_ = null;

	/**
	 * Button
	 * @private
	 * @type {Object}
	 */
	this.element_ = null;


	this.element_ = document.createElement('div');
	this.element_.className = "cercalia-big-icon cercalia-control cercalia-control-meteoControl";

	cercalia.jQuery(this.element_)
		.button({
			icons: {
				primary:"cercalia-big-icon cercalia-big-icon-weather",
				secondary: ""
			},
			text: false
		})
		.attr("title", cercalia.i18n._("TOOLTIP_WEATHER"));

	cercalia.jQuery(this.element_).addClass("cercalia-tool-button");

	this.element_.addEventListener('click', function(e) {
		var olMap = this.getMap();
		cercalia.jQuery(".cercalia-tool-button.cercalia-tool-button-open").click().button("refresh");
		olMap.cercalia.deactivateControls();
		this.servicePoisMeteo_ == null ?  this.enable() : this.disable();
	}.bind(this));

};
ol.inherits(cercalia.Control.Meteo, ol.control.Control);


/**
 * Enable the weather layer
 */
cercalia.Control.Meteo.prototype.enable = function() {

	var cercaliaMap = this.getMap().getCercalia();

	if(!this.servicePoisMeteo_){
		this.servicePoisMeteo_ = new cercalia.service.Pois({
			categories : [this.getCategoryByZoom_(cercaliaMap.getZoom())],
			searchByScale : true
		});

		this.getPoisMeteoOnMoveEnd_();//Primera peticion al activarlo
		this.getMap().on('moveend', this.getPoisMeteoOnMoveEnd_, this);
	}
};

/**
 * Disable weather layer. Clean all the weather layer elements
 */
cercalia.Control.Meteo.prototype.disable = function() {

	if(this.servicePoisMeteo_ != null){
		var cercaliaMap = this.getMap().getCercalia();

		if(this.servicePoisMeteo_.listMarkers){
			cercaliaMap.removeApiMarkers(this.servicePoisMeteo_.listMarkers);
			for (var i = this.servicePoisMeteo_.listMarkers.length - 1; i >= 0; i--) {
				this.servicePoisMeteo_.listMarkers[i].destroy();
			}
		}
		this.servicePoisMeteo_ = null;
		this.getMap().un('moveend', this.getPoisMeteoOnMoveEnd_);
	}
};


/**
 * @private
 * @param {number} zoom
 * @return {string} categoryname
 */
cercalia.Control.Meteo.prototype.getCategoryByZoom_ = function(zoom){
	if(zoom<5) return 'D00M13';
	else if (zoom==5) return 'D00M12';
	else if (zoom==6) return 'D00M11';
	else if (zoom==7) return 'D00M10';
	else if (zoom==8) return 'D00M09';
	else if (zoom==9) return 'D00M08';
	else if (zoom==10) return 'D00M08';
	else if (zoom==11) return 'D00M07';
	else if (zoom==12) return 'D00M06';
	else if (zoom>12) return 'D00M05';
};

/**
 * Create the button in the tools topbar
 * @private
 * @param {string} idTopBar Div element ID where the control will be embedded
 */
cercalia.Control.Meteo.prototype.setTopBar = function (idTopBar) {
	var self = this;
	ol.control.Control.call(this, {
	    element: self.element_,
	    target: document.getElementById(idTopBar)
	});
};

/**
 * @private
 * @param {number} vel
 * @param {boolean} isAEMET
 * @return {string}
 */
cercalia.Control.Meteo.prototype.getWindIcon_ = function(vel,isAEMET){
    var icon="50";
    if(!isAEMET){//conversiĆ³ m/s a Km/h
        vel=(vel*3600)/1000;
    }

    if(vel>20 && vel<41)icon="51";
    else if(vel>40 && vel<71)icon="52";
    else if(vel>70)icon="53";
    return icon;
};

/**
 * @private
 */
cercalia.Control.Meteo.prototype.getPoisMeteoOnMoveEnd_ = function(){

	if(this.servicePoisMeteo_ != null ){
		var self = this;

		var cercaliaMap = this.getMap().getCercalia();

		//Comprobamos si hace falta recalcular
		//Casos:
		//	1) response==null -> No se ha hecho ninguna peticion des de que se ha construido el objeto
		//  2) Cambio de zoom
		//  3) Los bounds despues del 'moveend' no estan dentro de los ultimos calculados que eran el doble
		if( self.servicePoisMeteo_.getResponse() == null || cercaliaMap.getZoom() != self.servicePoisMeteo_.lastZoom || !self.servicePoisMeteo_.getBounds().containsBounds(cercaliaMap.getBounds()) ){
			var size = self.getMap().getSize(); //Obtenemos los pixeles del mapa
			self.servicePoisMeteo_.lastZoom = cercaliaMap.getZoom();

			self.servicePoisMeteo_.setQueryCategories([self.getCategoryByZoom_(cercaliaMap.getZoom())]);
			self.servicePoisMeteo_.setBounds(cercaliaMap.getBounds().getDouble()); //Doble de extension (sumamos x0.5) por todos los lados
			self.servicePoisMeteo_.setWidth(size[0]*2);
			self.servicePoisMeteo_.setHeight(size[1]*2);

			self.servicePoisMeteo_.doPoisRequest(function(data){

				if(self.servicePoisMeteo_.listMarkers){
					cercaliaMap.removeApiMarkers(self.servicePoisMeteo_.listMarkers);
					for(var i = 0 ; i < self.servicePoisMeteo_.listMarkers.length; i++){
						self.servicePoisMeteo_.listMarkers[i].destroy();
					}
				}

				if(data.cercalia.map.gpoicats && data.cercalia.map.gpoicats.poilist){
					var listPoi = data.cercalia.map.gpoicats.poilist.poi;

					var listMarkers = [];

					//Puede ser que solo devuelva un elemento. Por lo tanto lo convertimos a un array
					if( !(listPoi instanceof Array) ) {
						listPoi = [listPoi];
					}

					if(listPoi.length>0 && listPoi[0]!=null){

						listPoi.forEach(function(elem,index){

							var isAEMET=(elem.id.indexOf('D00GWM')<0);
							var title=elem.name.value.split("(")[0];

							var info = elem.info.value.split("|");
							var img = cercaliaGlobals.img + '/meteo/big/' + info[6] + '.png';

							var content = "";

			                content = "<div id='localidad_" + elem.id + "' class='widTempsNexus' style='width:259px; height:270px; position:relative;'>"
			                		+ "<div class='meteoLocNexus' >"+title+"</div>";

			                //DIA 1
			                var date=info[1].split("-");
			                content +=   "<div class='meteo1' >"+date[2]+"/"+date[1]+"/"+date[0]+"</div>"
			                			+"<div class='colmeteo1' >" + cercalia.i18n._('Morning') + "</div>"
			                			+"<div class='colmeteo2' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[6]+".png' border='0' alt=''/></div>"
			                			+"<div class='colmeteo1_2' >" + cercalia.i18n._('Afternoon') + "</div>"
			                			+"<div class='colmeteo2'><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[7]+".png' border='0' alt=''/></div>"
			                			+"<div class='meteoTemprs'><div class='meteoTempMax'>"+info[10]+"&deg;</div><div class='meteoTempMin'>"+info[11]+"&deg;</div></div>"
			                			+"<div class='meteoWind' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+self.getWindIcon_(info[8],isAEMET)+".gif' border='0' alt='"+info[8]+"'/></div>";

			       			if(isAEMET && parseInt(info[6])>=23 && parseInt(info[6])<47){
			       				content += "<div class='meteoPrec' >"+info[2]+"%<br/>" + cercalia.i18n._('Rain') + "</div>";
			       			} else if(isAEMET && parseInt(info[7])>=23 && parseInt(info[7])<47){
			       				content += "<div class='meteoPrec' >"+info[3]+"%<br/>" + cercalia.i18n._('Rain') + "</div>";
			       			}

			                //DIA 2
			                date=info[12].split("-");
			                content += "<div class='meteo1' style=''>"+date[2]+"/"+date[1]+"/"+date[0]+"</div>"
			                			+"<div class='colmeteo1' >" + cercalia.i18n._('Morning') + "</div>"
			                			+"<div class='colmeteo2' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[17]+".png' border='0' alt=''/></div>"
			                			+"<div class='colmeteo1_2' >" + cercalia.i18n._('Afternoon') + "</div>"
			                			+"<div class='colmeteo2' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[18]+".png' border='0' alt=''/></div>"
			                			+"<div class='meteoTemprs'><div class='meteoTempMax'>"+info[21]+"&deg;</div><div class='meteoTempMin'>"+info[22]+"&deg;</div></div>"
			                			+"<div class='meteoWind' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+self.getWindIcon_(info[19],isAEMET)+".gif' border='0' alt='"+info[19]+"'/></div>";

			                if(isAEMET && parseInt(info[17])>=23 && parseInt(info[17])<47){
			                	content += "<div class='meteoPrec' >"+info[13]+"%<br/>" + cercalia.i18n._('Rain') + "</div>";
			                } else if(isAEMET && parseInt(info[18])>=23 && parseInt(info[18])<47){
			                	content += "<div class='meteoPrec' >"+info[14]+"%<br/>" + cercalia.i18n._('Rain') + "</div>";
			                }

			                //DIA 3
			                date=info[23].split("-");
			                content += "<div class='meteoDiaSep' >"
			                			+ "<div class='meteo12'>"+date[2]+"/"+date[1]+"/"+date[0]+"</div>"
			                			+ "<div class='meteoDiaSec' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[28]+".png' border='0' alt=''/></div>"
			                			+ "<div class='meteoTemprsB' ><span class='meteoTempMax'>"+info[30]+"&deg;</span>&nbsp;<span class='meteoTempMin'>"+info[31]+"&deg;</span>"
			                		  +"</div>";

			                if(isAEMET && parseInt(info[28])>=23 && parseInt(info[28])<47){
			                	content += "<div style='float:left;margin-top:4px;font-size:10px' >"+info[24]+"%&nbsp;" + cercalia.i18n._('Rain') + "</div>";
			                }

			                content += '</div>';
			                //DIA 4
			                date=info[32].split("-");
			                content+=
			                	"<div class='meteoDiaSep' >"
			                		+"<div class='meteo12' >"+date[2]+"/"+date[1]+"/"+date[0]+"</div>"
			                		+"<div class='meteoDiaSec' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[35]+".png' border='0' alt=''/></div>"
			                		+"<div class='meteoTemprsB' ><span class='meteoTempMax'>"+info[36]+"&deg;</span>&nbsp;<span class='meteoTempMin'>"+info[37]+"&deg;</span></div>";
			                if(isAEMET && parseInt(info[35])>=23 && parseInt(info[35])<47){
			                	content += "<div style='float:left;margin-top:4px;font-size:10px' >"+info[45]+"%&nbsp;" + cercalia.i18n._('Rain') + "</div>";
			                }
			                content += '</div>';
			                //DIA 5
			                date=info[38].split("-");

			                content += "<div class='meteoDiaSep' >"
			                			+"<div class='meteo12' >"+date[2]+"/"+date[1]+"/"+date[0]+"</div>"
			                			+"<div class='meteoDiaSec' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[41]+".png' border='0' alt=''/></div>"
			                			+"<div class='meteoTemprsB' ><span class='meteoTempMax'>"+info[42]+"&deg;</span>&nbsp;<span class='meteoTempMin'>"+info[43]+"&deg;</span></div>";
			                if(isAEMET && parseInt(info[41])>=23 && parseInt(info[41])<47){
			                	content += "<div style='float:left;margin-top:4px;font-size:10px' >"+info[39]+"%&nbsp;" + cercalia.i18n._('Rain') + "</div>";
			                }
			                content += '</div>';

			                //DIA 6
			                date=info[44].split("-");
			                content +=
			                	"<div class='meteoDiaSep' >"
			                		+"<div class='meteo12' >"+date[2]+"/"+date[1]+"/"+date[0]+"</div>"
			                		+"<div class='meteoDiaSec' ><img src='"+cercaliaGlobals.img+"/meteo/small/"+info[47]+".png' border='0' alt=''/></div>"
			                		+"<div class='meteoTemprsB' ><span class='meteoTempMax'>"+info[48]+"&deg;</span>&nbsp;<span class='meteoTempMin'>"+info[49]+"&deg;</span></div>";
			                if(isAEMET && parseInt(info[41])>=23 && parseInt(info[41])<47){
			                	content += "<div style='float:left;margin-top:4px;font-size:10px' >"+info[39]+"%&nbsp;" + cercalia.i18n._('Rain') + "</div>";
			                }
			                content += '</div>';


			                content+="<div class='meteoCopy'>"+(isAEMET?cercalia.i18n._('Information produced by AEMET'):cercalia.i18n._('OpenWeatherMap'))+"</div></div>";

			                var trafficControl = cercaliaMap.getControlsByName(cercalia.MapControls.Traffic);
							var marker = new cercalia.Marker({
								position : new cercalia.LonLat(elem.coord.x, elem.coord.y),
								icon : new cercalia.Icon({src: img}),
								popup : new cercalia.Popup({
									content: content,
									customPopup: true,
									positioning: 'bottom-center',
									offset: [0, -10],
									maxWidth: 400
								}),
								onMouseOver: function(){
									self.closePopups();
									if(trafficControl){
										trafficControl.closePopups();
									}
									self.servicePoisMeteo_.popupOpened = this.getPopup();
									self.servicePoisMeteo_.popupOpened.show();
								},
								onMouseOut: function(){
									self.closePopups();
									if(trafficControl){
										trafficControl.closePopups();
									}
								}
							});

							listMarkers.push(marker);
						});

						cercaliaMap.addApiMarkers(listMarkers);

						self.servicePoisMeteo_.listMarkers = listMarkers; //Guardamos en este objeto para poder recuperarlo
					}
				}
			});
		}
	} else {
		return;
	}
};

/**
 * Close weather popups
 */
cercalia.Control.Meteo.prototype.closePopups = function(){
	if(this.servicePoisMeteo_ && this.servicePoisMeteo_.popupOpened){
		this.servicePoisMeteo_.popupOpened.hide();
		this.servicePoisMeteo_.popupOpened = null;
	}
};