(function() {

$(document).ready(function() {
  return window.default_plot_options = {
    plot_bgcolor: $('#plot-style').css("background-color"),
    paper_bgcolor: $('#plot-style').css("background-color"),
    font: {
      color: $('#plot-style').css("color"),
      family: $('pre').css("font-family")
    },
    titlefont: {
      color: $('#plot-style').css("color"),
      family: $('body').css("font-family")
    },
    yaxis: {
      gridcolor: $('#plot-style > .tickmarks').css("color")
    },
    xaxis: {
      gridcolor: $('#plot-style > .tickmarks').css("color")
    },
    margin: {
      t: 10,
      b: 40
    }
  };
});

$.event.special.removed = {
  remove: function(o) {
    if (o.handler) {
      return o.handler();
    }
  }
};

$.extend(WSClient.prototype.actions, {
  plot: function(data) {
    var plotDiv, target;
    target = this.__parent.check_target(data);
    this.__plots || (this.__plots = {});
    plotDiv = target.find("#plot-" + data.id);
    if (plotDiv.size() === 0) {
      plotDiv = $("<div class='plot' id='plot-" + data.id + "'></div>");
      this.__parent.add(plotDiv, target, data);
      $(window).resize(function() {
        return Plotly.relayout(plotDiv[0], {
          width: plotDiv.width(),
          height: plotDiv.height()
        });
      });
      return Plotly.newPlot(plotDiv[0], data.data, $.extend({
        width: plotDiv.width()
      }, window.default_plot_options, data));
    } else {
      plotDiv[0].data = data.data;
      Plotly.redraw(plotDiv[0]);
      if (data.layout) {
        return Plotly.relayout(plotDiv[0], data);
      }
    }
  },
  savePlot: function(data) {
    var plotDiv, target;
    target = this.__parent.check_target(data);
    this.__plots || (this.__plots = {});
    plotDiv = target.find("#plot-" + data.id)[0];
    return Plotly.toImage(plotDiv, data.format).then((function(_this) {
      return function(imgData) {
        return _this.__parent.send({
          id: data.callback_id,
          action: 'callback',
          source: 'plot',
          data: imgData,
          original_msg: data
        });
      };
    })(this));
  }
});

}).call(this);