AWP.Feature = OpenLayers.Class(OpenLayers.Feature, {
	popupMouseOver: null,
    initialize: function(layer, lonlat, data) {
        OpenLayers.Feature.prototype.initialize.apply(this, [layer, lonlat, data]);
    },
	createMarker: function() {
        if (this.lonlat != null) {
            this.marker = new AWP.Marker(this.lonlat, this.data);
			
			var size = new OpenLayers.Size(20, 20);
			if (this.data.indicators) {
				for (var n in this.data.indicators) {
					var indicator = this.data.indicators[n];
					if (indicator.icon) {
						var icon = new OpenLayers.Icon(AWP.cfg.SEAWA_ICON_URL + indicator.icon, size);
						icon.events = new OpenLayers.Events(this, icon.imageDiv, null);
						//icon.events.register('click', this, this.onClick);
						icon.events.register('mouseover', this, this.onMouseOver);
						//icon.events.register('mouseout', this, this.onMouseOut);
						this.marker.icons.push(icon);
					}
				}
			}
		}
		return this.marker;	
    },
	onClick: function(ee) {
		var isSameMarker = this == this.layer.selectedFeature;
		
		if (!isSameMarker && this.layer.selectedFeature) {
			/**
			 *
			if (this.layer.selectedFeature.popup && this.layer.selectedFeature.popup.visible()) {
				this.layer.selectedFeature.popup.hide();
			}
			*/
			if (this.layer.selectedFeature.popup) {
				this.layer.selectedFeature.popup.destroy();
				this.layer.selectedFeature.popup = null;
			}
		}
		
		if (this.popupMouseOver) this.popupMouseOver.hide();
		
		if (this.popup) {
			this.popup.toggle();
		} else {
			this.popup = this.createPopupOnClick(true);
			this.layer.map.addPopup(this.popup);
		}
		
		this.layer.selectedFeature = this;
		
		OpenLayers.Event.stop(ee);
	},
	onMouseOver: function(ee) {
		if (!this.popup || !this.popup.visible()) {
			if (this.popupMouseOver) {
				if (!this.popupMouseOver.visible()) this.popupMouseOver.show();
			} else {
				this.popupMouseOver = this.createPopupMouseOver();
				this.layer.map.addPopup(this.popupMouseOver);
			}
		}
		OpenLayers.Event.stop(ee);
	},
	onMouseOut: function(ee) {
		/**
		 *
		if (this.popupMouseOver && this.popupMouseOver.visible()) {
			this.popupMouseOver.hide();
		}
		*/
		if (this.popupMouseOver) {
			this.popupMouseOver.destroy();
			this.popupMouseOver = null;
		}
		OpenLayers.Event.stop(ee);
	},
	createPopupOnClick: function(closeBox) {
		var popup;
		if (this.lonlat != null) {
			this.data.popupContentHTML = 
				'Station Name: ' + this.data.stn_name + '<br />' +
				'Station Number: ' + this.data.stn_number + '<br />' +
				'Realtime: ' + this.data.aenv_stati;
			this.data.popupContentHTML = [
				'<table border="1" cellpadding="3" cellspacing="1">',
				'  <tr>',
				'    <td class="awp-txt-little">Station Name:</td>',
				'    <td class="awp-txt-little awp-txt-bold">' + this.data.stn_name + '</td>',
				'  </tr>',
				'  <tr>',
				'    <td>Descrizione:</td>',
				'    <td class="txt_little txt_bold">Cavergno, Centrale smistamento OFIMA</td>',
				'  </tr>',
				'</table>'
			].join("\n");
			this.data.popupSize = new OpenLayers.Size(200, 100);
			popup = this.createPopup(closeBox);
		}
		return popup;
	},
	createPopupMouseOver: function(closeBox) {
		var popup;
		if (this.lonlat != null) {
			this.data.popupContentHTML = [
				'<table width="100%" border="0" cellpadding="3" cellspacing="1">',
				'  <tr>',
				'    <td class="awp-txt-little">Station Number : </td>',
				'    <td class="awp-txt-little awp-txt-bold">' + this.data.stn_number + '</td>',
				'  </tr>',
				'  <tr>',
				'    <td class="awp-txt-little">Station Name : </td>',
				'    <td class="awp-txt-little awp-txt-bold">' + this.data.stn_name + '</td>',
				'  </tr>',
				'  <tr>',
				'    <td class="awp-txt-little">Realtime Data : </td>',
				'    <td class="awp-txt-little awp-txt-bold">' + this.data.realtime + '</td>',
				'  </tr>',
				'</table>'
			].join("\n");
			this.data.popupSize = new OpenLayers.Size(300, 100);
			popup = this.createPopup(closeBox);
		}
		return popup;
	},
	createPopup: function(closeBox) {
		var popup = null;
		
        if (this.lonlat != null) {
            var id = this.id + "_popup";
            var anchor = (this.marker) ? this.marker.icon : null;
			popup = new AWP.Popup(
				id, 
				this.lonlat,
				this.data.popupSize,
				this.data.popupContentHTML,
				anchor, 
				closeBox
			);
            if (this.data.overflow != null) {
                popup.contentDiv.style.overflow = this.data.overflow;
            }    
            
            popup.feature = this;
        }        
        return popup;
    }
});
