﻿
VisKort.Utils.GetInfoWMS = OpenLayers.Class(VisKort.Utils, {

    info: null,

    infoCount: 0,

    printdiv: null,

    feautureloaded: 0,

    initialize: function() {

    },

    /*
    * Method: GetFeatureInfo
    * Gets feature info for points in list and for themelayer
    */
    GetFeutureInfo: function(map, points, themelayer, div_container) {

        this.infoCount = 0;
        this.feautureloaded = 0;
        this.info = Array();
        this.printdiv = document.createElement("div");
        for (var i = 0; i < points.length; i += 2) {
            var pixel = map.getViewPortPxFromLonLat(new OpenLayers.LonLat(points[i], points[i + 1]));
            if (themelayer.visibility && themelayer.CLASS_NAME == "OpenLayers.Layer.WMS" &&
             themelayer.options.extra != null && themelayer.options.extra.infoBoxLayout != null) {
                var url = themelayer.getFullRequestString({
                    REQUEST: "GetFeatureInfo",
                    BBOX: map.getExtent().toBBOX(),
                    X: Math.round(pixel.x),
                    Y: Math.round(pixel.y),
                    QUERY_LAYERS: themelayer.params.LAYERS,
                    INFO_FORMAT: 'text/plain',
                    FEATURE_COUNT: 1,
                    WIDTH: map.size.w,
                    HEIGHT: map.size.h
                });
                this.info[this.infoCount] = { layer: themelayer, infoUrl: url, divContainer: div_container };
                this.infoCount++;
            }
        }

        if (this.infoCount > 0) {
            for (var i = 0; i < this.infoCount; i++) {
                var request = new OpenLayers.Ajax.Request(this.info[i].infoUrl,
                { method: 'get', parameters: { InfoIdx: i },
                onComplete: this.GetFeatureInfoResponse.bind(this), onFailure: OpenLayers.nullHandler
                });
            }
        }
    },

    /*
    * Method: GetFeatureInfoResponse
    * Callback method invoked when OpenLayers.loadURL returns
    */
    GetFeatureInfoResponse: function(response) {
        var idx = response.request.options.parameters.InfoIdx;

        var infoHtml;
        if (response.responseText.indexOf('no features were found') != 0 && response.responseText != "") {
            var infoBoxLayout = this.info[idx].layer.options.extra.infoBoxLayout;
            if (infoBoxLayout["#cdata-section"] != null) {
                infoHtml = infoBoxLayout["#cdata-section"];
                var responseText = response.responseText;
                var lines = responseText.split('\n');
                for (lcv = 0; lcv < (lines.length); lcv++) {
                    var vals = lines[lcv].replace(/^\s*/, '').replace(/\s*$/, '').replace(/ = /, "=").replace(/=/, ":").replace(/'/g, '').split(':');
                    if (vals[0] != null && vals[0].length >= 1) {
                        var r = new RegExp("<!--#" + vals[0] + "#-->", 'gi');
                        var vl = vals[1];
                        if (vals.length > 2) {
                            for (var t = 2; t < vals.length; t++)
                                vl += ":" + vals[t];
                        }
                        infoHtml = infoHtml.replace(r, vl);
                    }
                }
            }
            else
                infoHtml = response.responseText;
        }
        else {
            infoHtml = "Ingen info";
        }

        this.info[idx].infoHtml = infoHtml;
        this.feautureloaded++;
        if (this.feautureloaded == this.infoCount) {

            for (var j = 0; j < this.infoCount; j++) {
                var leftmenu_div = document.createElement("div");
                leftmenu_div.innerHTML = this.info[j].infoHtml;
                var children_nodes = leftmenu_div.childNodes;
                for (var i = 0; i < children_nodes.length; i++) {
                    if (children_nodes[i].id == "InfoForbox") {
                        leftmenu_div.childNodes[i].style.display = "none";
                    }
                    if (children_nodes[i].id == "Leftmenu") {
                        leftmenu_div.childNodes[i].style.display = "inline";
                    }
                }

                var findnearestdiv = document.getElementById("Find_nearest_info");
                if (findnearestdiv) {
                    var br = document.createElement("br");
                    // Set info leftmenu
                    findnearestdiv.appendChild(br);
                    findnearestdiv.innerHTML += "Nr. " + (j + 1);
                    findnearestdiv.appendChild(br);
                    findnearestdiv.innerHTML += leftmenu_div.innerHTML;
                    findnearestdiv.appendChild(br);

                    // Set info to print
                    this.printdiv.appendChild(br);
                    this.printdiv.innerHTML += "Nr. " + (j + 1);
                    this.printdiv.appendChild(br);
                    this.printdiv.innerHTML += this.info[j].infoHtml;
                    this.printdiv.appendChild(br);
                    this.SetPrintDetailInfo(this.printdiv.innerHTML);
                }
            }

        }
    },

    CLASS_NAME: "VisKort.Utils.GetInfoWMS"
});   
