'use strict';

var Node = require('../node');
var dot = require('dot');

var TYPE = 'weighted-centroid';

var PARAMS = {
    source : Node.PARAM.NODE(Node.GEOMETRY.POINT),
    weight_column : Node.PARAM.STRING,
    category_column : Node.PARAM.STRING
};

var WeightedCentroid  = Node.create(TYPE, PARAMS, {cache: true});

module.exports = WeightedCentroid;
module.exports.TYPE = TYPE;
module.exports.PARAMS = PARAMS;

var weightedCentroidTemplate = dot.template([
    'SELECT the_geom , class',
    'FROM cdb_crankshaft.CDB_WeightedMean(\'{{=it.query}}\',\'{{=it.weight_column}}\',\'{{=it.category_column}}\')'
].join('\n'));

function query(it) {
    return weightedCentroidTemplate(it);
}

WeightedCentroid.prototype.sql = function(){
    return query({
        query : this.source.getQuery(),
        weight_column : this.weight_column,
        category_column : this.category_column
    });
};
        

@@ -534,25 +534,61 @@
 ean(
-%5C'%7B%7B=it.query%7D%7D%5C'
+$weightedmean_query$%7B%7B=it.query%7D%7D$weightedmean_query$
 ,%5C'%7B

Use $$ for escaping