﻿if (Poseidon.Controls.PointInfo === undefined) {
    Poseidon.Controls.PointInfo = {};
}

Poseidon.Controls.PointInfo.Classic = function () {
    var PointLayer = null;
    var ClickControl = null;
    var Window = null;

    this.initialize = function (layerHandle, clickcontrol) {
        if (layerHandle && clickcontrol) {
            this.PointLayer = layerHandle;
            this.ClickControl = clickcontrol;
        } else {
            alert('Point Info is not ready!');
        }
    }

    /* 
    Method which is executed by OpenLayers WFSGetFeatureInfo upon returning with a result from the GeoServer.
    The 'this' points a WFSGetFeatureInfo, thus 'this' is not available in this method.
    */
    this.getFeatureInfo = function (event) {
        this.PointLayer.destroyFeatures();

        // If nothing was selected, do nothing.
        if (event.features.length > 0) {
            this.show();
            this.Window.body.update(''); // HTML ON

            var HTML = this.handleCustomizedResponse(event.features);

            this.Window.body.update(HTML); // HTML ON
            this.Window.doLayout();

            // Go through the list of features collected by WMSGetFeatureInfo. They should be concatenated in event.features.
            for (var currentFeatureID = 0; currentFeatureID < event.features.length; currentFeatureID++) {
                var selectedFeature;

                // If any geometry was returned.
                if (event.features[currentFeatureID].geometry) {
                    // Determine the type of geometry.
                    switch (event.features[currentFeatureID].geometry.CLASS_NAME) {
                        case 'OpenLayers.Bounds':
                            selectedFeature = new OpenLayers.Feature.Vector(event.features[currentFeatureID].geometry.toGeometry());
                            break;

                        case 'OpenLayers.Geometry.MultiPolygon':
                            selectedFeature = new OpenLayers.Feature.Vector(event.features[currentFeatureID].geometry);
                            break;

                        case 'OpenLayers.Geometry.Polygon':
                            selectedFeature = new OpenLayers.Feature.Vector(event.features[currentFeatureID].geometry);
                            break;

                        case 'OpenLayers.Geometry.MultiLineString':
                            selectedFeature = new OpenLayers.Feature.Vector(event.features[currentFeatureID].geometry);
                            break;

                        case 'OpenLayers.Geometry.LineString':
                            selectedFeature = new OpenLayers.Feature.Vector(event.features[currentFeatureID].geometry);
                            break;

                        case 'OpenLayers.Geometry.Point':
                            selectedFeature = new OpenLayers.Feature.Vector(event.features[currentFeatureID].geometry);
                            break;

                        default:
                            alert('Unhandled geometry intercepted!');
                    }

                    // Add the geometry to the layer.
                    this.PointLayer.addFeatures(selectedFeature);
                }
            }
        } else {
            // Clear window.
            if (this.Window) {
                this.Window.body.update(''); // HTML ON
            }
        }
    }

    this.createTextNode = function (caption,
				   		            captionCSS,
                                    seperator_symbol,
						            value,
						            valueCSS) {
        var HTML = '';

        HTML += '<tr>';
        if (value && value.length > 0) {
            HTML += '    <td class="pointinfo-classic-caption-default ' + captionCSS + '">';
            HTML += '        ' + caption + seperator_symbol + '</td>';
            HTML += '    <td class="pointinfo-classic-value-default ' + valueCSS + '">';
            HTML += '        ' + value + '</td>';
        } else {
            HTML += '    <td class="pointinfo-classic-caption-value-missing">';
            HTML += '        ' + caption + '</td>';
            HTML += '    <td class="pointinfo-classic-value-missing ' + valueCSS + '">&nbsp;</td>';
        }
        HTML += '</tr>';

        return HTML;
    }

    this.createLinkNode = function (caption,
                                    captionCSS,
                                    seperator_symbol,
                                    url,
                                    url_target,
                                    value,
                                    valueCSS) {
        var HTML = '<tr>';
        if (value && value.length > 0) {
            HTML += '    <td class="pointinfo-classic-caption-default ' + captionCSS + '">';
            HTML += '        ' + caption + seperator_symbol + '</td>';
            HTML += '    <td class="pointinfo-classic-value-default ' + valueCSS + '">';
            HTML += '        <a href="' + url + '" target="' + url_target + '">' + value + '</a></td>';
        } else {
            HTML += '    <td class="pointinfo-classic-caption-value-missing">';
            HTML += '        ' + caption + seperator_symbol + '</td>';
            HTML += '    <td class="pointinfo-classic-value-missing ' + valueCSS + '">&nbsp;</td>';
        }
        HTML += '</tr>';

        return HTML;
    }

    this.createImageNode = function (caption,
                                     captionCSS,
                                     seperator_symbol,
                                     image,
                                     value) {
        var HTML = '<tr>';
        HTML += '    <td class="pointinfo-classic-caption-default ' + captionCSS + '">';
        HTML += '        ' + caption + seperator_symbol + '</td>';
        HTML += '    <td><img src="' + image_url + '" alt="' + value + '" style="width: 48px"></td>';
        HTML += '</tr>';
        return HTML;
    }

    this.handleCustomizedResponse = function (features) {
        alert('The method "handleCustomizedResponse()" has not been assigned!');
    }

    this.enable = function () {
        this.ClickControl.activate();
    }

    this.show = function () {
        if (!this.Window) {
            this.CreateWindow();
            this.Window.addListener('hide', this.hide.bind(this));
        }
        this.Window.show();
    }

    this.hide = function () {
        this.Window.body.update(''); // HTML ON
        this.PointLayer.destroyFeatures();
        this.ClickControl.deactivate();
    }

    this.CreateWindow = function () {
        // The point info window.
        this.Window = new Ext.Window({
            title: 'Punkt info',
            width: 384,
            height: 300, // HTML ON
            //autoHeight: true,
            layout: 'fit',
            resizable: true,
            closable: true,
            closeAction: 'hide',
            autoScroll: true,
            collapsible: true,
            x: 139,
            y: 50
        });
    }
}
