// Generated by CoffeeScript 1.3.3
var __hasProp = {}.hasOwnProperty,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

define(['highcharts', 'underscore', 'backbone', 'charts/options'], function(Highcharts, _, Backbone, chartOptions) {
  var AreaChart, AreaSplineChart, BarChart, Chart, ColumnChart, LineChart, OPTIONS_MAP, PieChart, ScatterChart, Sparkline, SplineChart;
  OPTIONS_MAP = {
    el: 'chart.renderTo',
    type: 'chart.type',
    height: 'chart.height',
    width: 'chart.width',
    labelFormatter: 'plotOptions.series.dataLabels.formatter',
    tooltipFormatter: 'tooltip.formatter',
    animate: 'plotOptions.series.animation',
    categories: 'xAxis.categories',
    title: 'title.text',
    subtitle: 'subtitle.text',
    xAxis: 'xAxis.title.text',
    yAxis: 'yAxis.title.text',
    stacking: 'plotOptions.series.stacking',
    legend: 'legend.enabled',
    suffix: 'tooltip.valueSuffix',
    prefix: 'tooltip.valuePrefix',
    series: 'series'
  };
  Chart = (function(_super) {

    __extends(Chart, _super);

    function Chart() {
      return Chart.__super__.constructor.apply(this, arguments);
    }

    Chart.prototype.className = 'chart';

    Chart.prototype.chartOptions = chartOptions.defaults;

    Chart.prototype.initialize = function(options) {
      var key, value;
      if (!(options.el != null)) {
        options.el = this.el;
      }
      this.chartOptions = $.extend(true, {}, this.chartOptions, options.options);
      for (key in options) {
        value = options[key];
        if (OPTIONS_MAP[key]) {
          this.setOption(OPTIONS_MAP[key], value);
        }
      }
    };

    Chart.prototype.setOption = function(key, value) {
      var last, options, tok, toks, _i, _len;
      options = this.chartOptions;
      toks = key.split('.');
      last = toks.pop();
      for (_i = 0, _len = toks.length; _i < _len; _i++) {
        tok = toks[_i];
        if (!(options[tok] != null)) {
          options[tok] = {};
        }
        options = options[tok];
      }
      return options[last] = value;
    };

    Chart.prototype.render = function() {
      if (this.chart) {
        this.chart.destroy();
      }
      if (this.model) {
        this.chartOptions.series = [$.extend(true, {}, this.model.toJSON())];
        if (this.model.categories) {
          this.setOption('xAxis.categories', this.model.categories);
        }
      } else if (this.collection) {
        if (this.collection instanceof Series) {
          this.chartOptions.series = [$.extend(true, {}, this.collection.toJSON())];
        } else {
          this.chartOptions.series = $.extend(true, [], this.collection.toJSON());
        }
        if (this.collection.categories) {
          this.setOption('xAxis.categories', this.collection.categories);
        }
      }
      this.chart = new Highcharts.Chart(this.chartOptions);
      return this.$el;
    };

    return Chart;

  })(Backbone.View);
  Chart.setDefaultOption = function(key, value) {
    var last, options, prev, prevTok, tok, toks, _i, _len;
    options = this.prototype.chartOptions = _.clone(this.prototype.chartOptions);
    toks = key.split('.');
    last = toks.pop();
    prev = null;
    prevTok = null;
    for (_i = 0, _len = toks.length; _i < _len; _i++) {
      tok = toks[_i];
      prev = options;
      if (prev[tok] != null) {
        options = _.clone(prev[tok]);
      } else {
        options = {};
      }
      prev[tok] = options;
    }
    return options[last] = value;
  };
  AreaChart = (function(_super) {

    __extends(AreaChart, _super);

    function AreaChart() {
      return AreaChart.__super__.constructor.apply(this, arguments);
    }

    return AreaChart;

  })(Chart);
  AreaChart.setDefaultOption('chart.type', 'area');
  AreaSplineChart = (function(_super) {

    __extends(AreaSplineChart, _super);

    function AreaSplineChart() {
      return AreaSplineChart.__super__.constructor.apply(this, arguments);
    }

    return AreaSplineChart;

  })(Chart);
  AreaSplineChart.setDefaultOption('chart.type', 'areaspline');
  BarChart = (function(_super) {

    __extends(BarChart, _super);

    function BarChart() {
      return BarChart.__super__.constructor.apply(this, arguments);
    }

    return BarChart;

  })(Chart);
  BarChart.setDefaultOption('chart.type', 'bar');
  ColumnChart = (function(_super) {

    __extends(ColumnChart, _super);

    function ColumnChart() {
      return ColumnChart.__super__.constructor.apply(this, arguments);
    }

    return ColumnChart;

  })(Chart);
  ColumnChart.setDefaultOption('chart.type', 'column');
  LineChart = (function(_super) {

    __extends(LineChart, _super);

    function LineChart() {
      return LineChart.__super__.constructor.apply(this, arguments);
    }

    return LineChart;

  })(Chart);
  LineChart.setDefaultOption('chart.type', 'line');
  PieChart = (function(_super) {

    __extends(PieChart, _super);

    function PieChart() {
      return PieChart.__super__.constructor.apply(this, arguments);
    }

    return PieChart;

  })(Chart);
  PieChart.setDefaultOption('chart.type', 'pie');
  PieChart.setDefaultOption('legend.enabled', true);
  ScatterChart = (function(_super) {

    __extends(ScatterChart, _super);

    function ScatterChart() {
      return ScatterChart.__super__.constructor.apply(this, arguments);
    }

    return ScatterChart;

  })(Chart);
  ScatterChart.setDefaultOption('chart.type', 'scatter');
  SplineChart = (function(_super) {

    __extends(SplineChart, _super);

    function SplineChart() {
      return SplineChart.__super__.constructor.apply(this, arguments);
    }

    return SplineChart;

  })(Chart);
  SplineChart.setDefaultOption('chart.type', 'spline');
  Sparkline = (function(_super) {

    __extends(Sparkline, _super);

    function Sparkline() {
      return Sparkline.__super__.constructor.apply(this, arguments);
    }

    Sparkline.prototype.className = 'sparkline';

    Sparkline.prototype.chartOptions = chartOptions.sparkline;

    return Sparkline;

  })(Chart);
  Backbone.Chart = Chart;
  Backbone.AreaChart = AreaChart;
  Backbone.AreaSplineChart = AreaSplineChart;
  Backbone.BarChart = BarChart;
  Backbone.ColumnChart = ColumnChart;
  Backbone.LineChart = LineChart;
  Backbone.PieChart = PieChart;
  Backbone.ScatterChart = ScatterChart;
  Backbone.SplineChart = SplineChart;
  Backbone.Sparkline = Sparkline;
});
