window.sitn = {};

// configuration:
sitn.config = {
    //baseUrl: '/sitn_profile',    // CONFIGURATIONS A FAIRE DANS LE CARTOCLIENT.TPL !!!
    //graphUrl: 'graph.php',
    layers: ['MNS','MNT'],
    width: 600,
    height: 400,
    nbPoints: 200,
    geom: ''
};

Map.prototype.profile_line = function(aDisplay) {

    this.resetMapEventHandlers();
    this.setCurrentLayer('profile_line');
    this.getDisplay(aDisplay).setTool('draw.line');

    this.onNewFeature = function(aFeature) {
        this.onToolUnset();
    };
 
    this.onFeatureInput = this.onFeatureChange = function(aFeature) {
        var geom = {
            type: 'LineString',
            coordinates: []
        };
        for (var i = 0, len = aFeature.vertices.length; i < len; i++) {
            var point = aFeature.vertices[i];
            geom.coordinates.push([point.x, point.y]);
        }
        // store geom so we can use it for csv export
        sitn.config.geom = geom;

        //var win = window.open(this.getChartUrl(geom),"Profile","height=620, width=820, menubar=no, status=no");
	      //xGetElementById("profile_container").style.display="block";
      	//xGetElementById("frame_profile").src=this.getChartUrl(geom);

	//alert(this.getChartUrl(geom));
	//window.open(this.getChartUrl(geom));

        var formatedGeom = this.getChartUrl(geom);
        
        var miframe = new Ext.ux.ManagedIframePanel({
            id: 'iframe_profile',
            region:'center',
            border: false,
            bodyBorder: false,
            defaultSrc: formatedGeom,
            cls: 'iframe_profile'
        });

        var popup = new Ext.Window({
            title:'My Test Window',
            title: '<span style="float: right; padding-right: 10px;" onclick="javascript:getProfileExportCsv();"><a href="#">Exporter en CSV</a></span>' +
                   'Profil altimétrique',
            closable:true,
            floating:true,
            shadow:true,
            width:sitn.config.width + 14,
            height:sitn.config.height + 32,
            layout:'border',
            items:miframe
        });
        popup.on({
            resize: function(window) {
                sitn.config.width = window.getInnerWidth();
                sitn.config.height = window.getInnerHeight();
                miframe.setSrc(this.getChartUrl(geom));
                miframe.show();
            },
            scope: this
        });

        popup.show();

        popup.resizer.on("beforeresize", function() {
            //Clear the popup content. Otherwise, the flash stays in the way...
            if (Ext.isIE) {              
              this.getComponent('iframe_profile').hide();
            }
        }, popup);
    
    };
 
    this.onToolUnset = function() {
        //clear the display layer
        this.getDisplay(aDisplay).clearLayer('profile_line');
        this.onCancel();
    };

    this.onCancel = function() {};

    this.getChartUrl = function(points) {
        var params = 'nbPoints=' + sitn.config.nbPoints + '&layers=' + sitn.config.layers.join(',') + 
                     '&geom=' + encodeURIComponent(Object.toJSON(points)) + 
                     '&baseUrl=' + sitn.config.baseUrl + 
                     '&width=' + sitn.config.width + 
                     '&height=' + sitn.config.height;
        return sitn.config.graphUrl + '?' + params;
    };
};

function getProfileExportCsv() {
  var params = 'nbPoints=' + sitn.config.nbPoints + '&layers=' + sitn.config.layers.join(',') + '&geom=' + encodeURIComponent(Object.toJSON(sitn.config.geom));

  location.href = sitn.config.baseUrl + '/profile.csv?' + params;
}