'use:strict';
var _ = require('underscore')
  , d3 = require('d3')
;

module.exports = HorisontalBarGraph;

/**
 * The entrypoint
 * @return {[type]} [description]
 */
function HorisontalBarGraph () {

  var options = {};
  function chart (selection) {
    // set the dafaults
    options.padding = 2;


      selection.each(function () {

        var data = _.map(options.data, function (d) {
              return d.values;
            });

        var barSpacing  = options.height / options.data.length;
        var barHeight   = barSpacing - options.padding;
        var maxValue    = d3.max(data);
        var widthScale  = options.width / maxValue;


        var dom = d3.select(this);
        var svg = dom.append('svg')
            .attr('class', 'chart barchart')
            .attr('height', options.height)
            .attr('width', options.width);

        var bars = svg.selectAll('rect.chart__bar')
            .data(data)
            .enter()
            .append('rect')
            .attr('class', 'chart__bar')
            .attr('y', function (d, i) { return i * barSpacing;  })
            .attr('height', barHeight)
            .attr('x', 0)
            .attr('width', function (d) { return d * widthScale; })
            .attr('fill', function (d, i) {
              var data = options.data[i];
              if (data && data.color) {
                return data.color;
              }
              return options.fillColor;
            });

      });
    }


    chart.fillColor = function (value) {
      if (!arguments.length) return options.fillColor;
      options.fillColor = value;
      return chart;
    };

    /**
     * Sets the chart-padding
     * @param  {Number} value - the padding of the chart
     * @return {Mixed}        - the value or chart
     */
    chart.padding = function (value) {
      if (!arguments.length) return options.padding;
      options.padding = value;
      return chart;
    };

    /**
     * Sets the width of a chart
     * @param  {Number} value - the width of the chart
     * @return {Mixed}        - the value or this
     */
    chart.width = function (value) {
      if (!arguments.length) return options.width;
      options.width = value;
      return chart;
    };
    /**
     * Sets the height of a chart
     * @param  {Number} value - the height of the chart
     * @return {Mixed}        - the value or chart
     */
    chart.height = function (value) {
      if (!arguments.length) return options.height;
      options.height = value;
      return chart;
    };
    /**
     * Sets the data on a chart
     * @param  {Number} value - the data used to draw the chart
     * @return {Mixed}        - the value or chart
     */
    chart.data = function  (value) {
      if (!arguments.length) return options.data;
      options.data = value;
      return chart;
    };

    /**
     * Sets a listener on the clices of the chart
     * @param  {String} action    - the type of action to listen to ( ie. 'click', 'mouseover')
     * @param  {Function} method  -  A bound method to be called when the action is invoked, passes the datum for this specific slice
     * @return {Mixed}            - the value or chart
     */
    chart.on = function (action, method) {
      if (!arguments.length) return options.on;
      options.on = {action: action, method: method};
      return chart;
    };

  return chart;
}
@@ -60,16 +60,45 @@
 e('d3')%0A
+  , base = require('./base')%0A
 ;%0A%0Amodul
@@ -234,102 +234,244 @@
 var 
-options = %7B%7D;%0A  function chart (selection) %7B%0A    // set the dafaults%0A    options.padding = 2;%0A
+chart = %7B%0A    options: %7B%0A        padding: 2%0A    %7D,%0A%0A    init: function (selection) %7B%0A%0A      if (arguments.length) %7B%0A        this.draw(selection);%0A      %7D%0A      return this;%0A    %7D,%0A%0A    draw: function (selection) %7B%0A      var that = this;
 %0A%0A  
@@ -529,16 +529,21 @@
 = _.map(
+that.
 options.
@@ -633,24 +633,29 @@
 rSpacing  = 
+that.
 options.heig
@@ -659,16 +659,21 @@
 eight / 
+that.
 options.
@@ -724,16 +724,21 @@
 acing - 
+that.
 options.
@@ -812,16 +812,21 @@
 cale  = 
+that.
 options.
@@ -989,16 +989,21 @@
 eight', 
+that.
 options.
@@ -1037,16 +1037,21 @@
 width', 
+that.
 options.
@@ -1495,16 +1495,21 @@
  data = 
+that.
 options.
@@ -1633,1968 +1633,133 @@
 urn 
-options.fillColor;%0A            %7D);%0A%0A      %7D);%0A    %7D%0A%0A%0A    c
+t
 ha
-r
 t.
-fillColor = function (value) %7B%0A      if (!arguments.length) return options.fillColor;%0A      options.fillColor = value;%0A      return chart;%0A
+options.fillColor;%0A        
     %7D
+)
 ;
-%0A
 %0A    
-/**%0A     * Sets the chart-padding%0A     * @param  %7BNumber%7D value - the padding of the chart%0A     * @return %7BMixed%7D        - the value or chart%0A     */%0A    chart.padding = function (value) %7B%0A      if (!arguments.length) return options.padding;%0A      options.padding = value;%0A      return chart;
+      %7D);%0A
 %0A    %7D
-;
+,
 %0A%0A  
-  /**%0A     * Sets the width of a chart%0A     * @param  %7BNumber%7D value - the width of the chart%0A     * @return %7BMixed%7D        - the value or this%0A     */%0A    chart.width = function (value) %7B%0A      if (!arguments.length) return options.width;%0A      options.width = value;%0A      return chart;%0A    %7D;%0A    /**%0A     * Sets the height of a chart%0A     * @param  %7BNumber%7D value - the height of the chart%0A     * @return %7BMixed%7D        - the value or chart%0A     */%0A    chart.height = function (value) %7B%0A      if (!arguments.length) return options.height;%0A      options.height = value;%0A      return chart;%0A    %7D;%0A    /**%0A     * Sets the data on a chart%0A     * @param  %7BNumber%7D value - the data used to draw the chart%0A     * @return %7BMixed%7D        - the value or chart%0A     */%0A    chart.data = function  (value) %7B%0A      if (!arguments.length) return options.data;%0A      options.data = value;%0A      return chart;%0A    %7D;%0A%0A    /**%0A     * Sets a listener on the clices of the chart%0A     * @param  %7BString%7D action    - the type of action to listen to ( ie. 'click', 'mouseover')%0A     * @param  %7BFunction%7D method  -  A bound method to be called when the action is invoked, passes the datum for this specific slice%0A     * @return %7BMixed%7D            - the value or chart%0A     */%0A    chart.on = function (action, method) %7B%0A      if (!arguments.length) return options.on;%0A      options.on = %7Baction: action, method: method%7D;%0A      return chart;%0A    %7D;%0A%0A  return chart;
+%7D;%0A%0A  chart = _.extend(chart, base);%0A  return (chart.init());%0A
 %0A%7D

refactor charts to use a base for common methods