/*==============================================================================
	MPK
    Mpk.RouteIcon class implementation
	
    Javascript library
    Require OpenLayers library
    $Id: RouteIcon.js 1233 2008-02-28 11:00:58Z mmanzato $
==============================================================================*/


/**
 * Replace HTML special characters with their respective entities
 * so that it is safe to use the string as HTML content
 */
function htmlspecialchars(t) {
    var s = t.replace(/&/,"&amp;");
    s = s.replace(/</,"&lt;");
    return s.replace(/>/,"&gt;");
}


/**
 * HTML formatter for text with simplified markup.
 * converts "[" "]" to "<b>" "</b>"
 */
function htmlFormatSimpleMarkup(t) {
    var s = htmlspecialchars(t);
    s = s.replace(/\[/,"<b>");
    return  s.replace(/\]/,"</b>");
}


/**
 * Used to create and display route place markers
 **/
Mpk.RouteIcon = OpenLayers.Class(
    OpenLayers.Feature, {
    
	saSTART:                1,		//!< 1:  Inizio del percorso
	saARRIVE:               2,		//!< 2:  Termine del percorso
	saSTOP: 				3,		//!< 3:  Tappa intermedia
	saTURNLEFT:             10,		//!< 10:  Svoltare a sinistra
	saTURNLEFTANDCONTINUE:  11,		//!< 11:  Svoltare a sinistra e continuare
	saTURNRIGHT:            12,		//!< 12:  Svoltare a destra
	saTURNRIGHTANDCONTINUE: 13,		//!< 13:  Svoltare a destra e continuare
	saCONTINUE:             14,		//!< 14:  Proseguire lungo la strada
	saMOTORWAYBEGIN:        20,		//!< 20:  Entrare in autostrada
	saMOTORWAYEND:          21,		//!< 21:  Uscire da autostrada
	saMOTORWAYENTER:        22,		//!< 22:  Entrare in autostrada ad uno svincolo
	saMOTORWAYLEAVE:        23,		//!< 23:  Uscire dall'autostrada ad uno svincolo
	saMOTORWAYCONTINUE:     24,		//!< 24:  Proseguire in autostrada (in caso in cui l'autostrada cambi nome)
	saMOTORWAYCHANGE:       25,		//!< 25:  Cambiare autostrada
	saCHANGECOUNTRY: 		26,		//!< 26:  Cambio di nazione
	saONFOOTSTRAIGHT:       30,		//!< 30:  Percorrere un tratto a piedi (non descritto)
	saONFOOTSTART: 			31,		//!< 31:  Lasciare l'auto e proseguire a piedi
	saONFOOTSTOP: 			32,		//!< 32:  Da qui in poi prendere l'auto
	saPARKING: 				33,		//!< 33:  Lasciare l'auto al parcheggio
	saFERRY: 				40,		//!< 40:  Percorrere un tratto in traghetto

    
    /**
     * @constructor
     *
     * @param {OpenLayers.Layer} layer
     * @param {OpenLayers.LonLat} icon coordinates
     * @param {XMLNode} xmlNode
     */
    initialize: function(layer, lonlat, data) {
        OpenLayers.Feature.prototype.initialize.apply(this, arguments);
        this.createMarker();
        this.marker.events.register('click', this, this.markerClick);
        layer.addMarker(this.marker);
    },

    /**
     * @destructor
     */
    destroy: function() {
        if (this.marker != null) {
            this.layer.removeMarker(this.marker);
        }
        OpenLayers.Feature.prototype.destroy.apply(this, arguments);
    },

    /**
     * Factory method
     * @param {Mpk.RouteLayer} layer for the route icon
     * @param {XMLNode} xmlNode
     *
     * @returns {bool} true if marker has been added, false otherwise
     * @type Object
     */
    build: function(layer, xmlNode) {

        var point = OpenLayers.Ajax.getElementsByTagNameNS(xmlNode,
                "http://www.opengis.net/gml", "gml", "Point");
        if (!point[0])
            return false;

        // Get coordinates
        var text =  OpenLayers.Util.getXmlNodeValue(OpenLayers.Ajax.getElementsByTagNameNS(point[0],
                "http://www.opengis.net/gml","gml", "coordinates")[0]);
        var floats = text.split(",");
        var lonlat = new OpenLayers.LonLat(parseFloat(floats[0]), parseFloat(floats[1]));

        // Get GML feature properties
        var feature = point[0].parentNode.parentNode;
        var properties = ['description', 'action', 'pointType'];
        var attrs = {};
        for(var i = 0; i < properties.length; i++) {
            attrs[properties[i]] = OpenLayers.Util.getXmlNodeValue(OpenLayers.Ajax.getElementsByTagNameNS(feature,
                "http://www.miz.it/mpk", "mpk", properties[i])[0]);
        }
        
        routeIcon = Mpk.RouteIcon.prototype.routeIcons[parseInt(attrs.action)];

        // If missing, image extension is found out by the web server
        var markerImageUrl = "http://www.verona.miz.it/libm/icons/" + routeIcon.markerImage;
        var popupImageUrl = "http://www.verona.miz.it/libm/icons/" + routeIcon.popupImage;

        // Create icon
        var offset = new OpenLayers.Pixel(-routeIcon.markerImageSize[2], -routeIcon.markerImageSize[3]);
        var icon = new OpenLayers.Icon(markerImageUrl,
                new OpenLayers.Size(routeIcon.markerImageSize[0], routeIcon.markerImageSize[1]),
                offset);
        icon.imageDiv.style.cursor = 'pointer';

        // Create popup contents
        var html =  "<div class='mpk-routePopup mpk-" + routeIcon.what + "'>" +
                    "<div class='mpk-popupHeader'>" + htmlFormatSimpleMarkup(attrs.pointType) + "</div>" +
                    "<div class='mpk-popupRouteImg'><img src='" + popupImageUrl + "' /></div>" +
                    "<p>" + htmlFormatSimpleMarkup(attrs.description) + "</p>" +
                    "</div>";

        // Create feature
        var feature = new Mpk.RouteIcon(layer, lonlat, {
                icon: icon,
                popupContentHTML: html,
                popupSize: new OpenLayers.Size(275, 100),
                id: null
            });
            
        //layer.addFeature(feature);
        return true;
    },

    /**
     * Called when the route icon marker is clicked
     */
    markerClick: function(evt) {
        sameMarkerClicked = (this == this.layer.selectedFeature);
        this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
        for (var i = 0; i < this.layer.map.popups.length; i++) {
            this.layer.map.removePopup(this.layer.map.popups[i]);
        }
        if (!sameMarkerClicked) {
            var popup = this.createPopup(true);
            popup.contentDiv.style.overflow='auto';
            this.layer.map.addPopup(popup);
        }
        OpenLayers.Event.stop(evt);
    },

    /** @final @type String */
    CLASS_NAME: "Mpk.RouteIcon"
});


Mpk.RouteIcon.prototype.routeIcons = new Array();
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saSTART] = {
    what: 'departure',
    markerImage: 'route/departure',
    markerImageSize: [28, 27, 2, 25],
    popupImage: 'route/departure.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saARRIVE] = {
    what: 'arrival',
    markerImage: 'route/arrival',
    markerImageSize: [28, 27, 2, 25],
    popupImage: 'route/arrival.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saSTOP] = {
    what: 'stop',
    markerImage: 'route/stop',
    markerImageSize: [24, 27, 2, 25],
    popupImage: 'route/stop.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saTURNLEFT] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/left.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saTURNLEFTANDCONTINUE] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/left.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saTURNRIGHT] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/right.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saTURNRIGHTANDCONTINUE] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/right.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saCONTINUE] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/ahead.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saMOTORWAYBEGIN] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/highwayStart.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saMOTORWAYEND] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/highwayEnd.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saMOTORWAYENTER] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/highwayStart.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saMOTORWAYLEAVE] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/highwayEnd.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saMOTORWAYCONTINUE] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/highwayAhead.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saMOTORWAYCHANGE] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/highwayChange.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saCHANGECOUNTRY] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/countryChange.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saONFOOTSTRAIGHT] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/foot.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saONFOOTSTART] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/foot.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saONFOOTSTOP] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/foot.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saPARKING] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/missing.gif'
};
Mpk.RouteIcon.prototype.routeIcons[Mpk.RouteIcon.prototype.saFERRY] = {
    what: 'passingPoint',
    markerImage: 'var/dot-white.gif',
    markerImageSize: [10, 10, 0, 0],
    popupImage: 'route/ferry.gif'
};



/*==============================================================================
    End of file
==============================================================================*/

