// Generated by CoffeeScript 1.7.1
(function() {
  $(function() {
    var drawingBubbleChart, drawingChart, responsiveBubbleChart, responsiveChart;
    drawingBubbleChart = function(data, $is_homePage) {
      var $addition, $heightChart, area_x_axis, handlePrice, height, limitsY, make_x_axis, make_y_axis, margin, positionAxis_y, refactorDate, searchExtentY, startWidth, svg, text_y_anchor, unit_y_position, width, wrap_bubble_x, x, xAxis, y, yAxis;
      wrap_bubble_x = function(text, width) {
        var switchYear, year;
        switchYear = true;
        year = 0;
        text.each(function(el) {
          var $y_position_first, $y_position_second, dy, line, lineHeight, lineNumber, tspan, word, words;
          text = d3.select(this);
          if (el !== -1 && el !== data.date.length) {
            words = data.date[el].split(/\s+/).reverse();
            words[1] = words[1].slice(0, 3);
            if (el === 0) {
              year = words[0];
            }
            if (el > 0) {
              if (words[0] > year && switchYear) {
                year = words[0];
                switchYear = false;
              } else {
                words[0] = '';
              }
            }
            word = void 0;
            line = [];
            lineNumber = 0;
            lineHeight = 1.2;
            dy = parseFloat(text.attr("dy"));
            $y_position_first = 6;
            $y_position_second = 30;
            if ($is_homePage) {
              $y_position_first = -13;
              $y_position_second = -18;
            }
            tspan = text.text(null).append("tspan").attr("x", 3).attr("y", $y_position_first).attr("transform", "translate(20,0)").attr("dy", ".71em").attr("font-size", "14px");
            while (word = words.pop()) {
              line.push(word);
              tspan.text(line.join(" "));
              if (tspan.node().getComputedTextLength() > width) {
                line.pop();
                tspan.text(line.join(" "));
                line = [word];
                tspan = text.append("tspan").attr("x", 3).attr("y", $y_position_second).attr("fill", "#909aa5").attr("font-size", "14px").text(word);
              }
            }
          }
        });
      };
      handlePrice = function(price) {
        var i, text, textArray;
        i = void 0;
        text = void 0;
        textArray = void 0;
        textArray = [];
        text = price.toString();
        i = text.length - 1;
        while (i >= 0) {
          textArray.push(text[i]);
          i--;
        }
        if (textArray.length > 3) {
          textArray.splice(3, 0, " ");
          textArray.splice(7, 0, " ");
          textArray.splice(11, 0, " ");
          textArray.reverse();
          text = textArray.toString();
          text = text.replace(/\,/g, "").trim();
        }
        return text;
      };
      searchExtentY = function(data) {
        var addition_odd, extentYmas, maxValue, minValue, newMaxVal, newMinVal;
        extentYmas = [];
        data.dataObj.forEach(function(d) {
          var elemsExtent, i;
          elemsExtent = d3.extent(d.data);
          i = 0;
          while (i < elemsExtent.length) {
            extentYmas.push(elemsExtent[i]);
            i++;
          }
        });
        maxValue = d3.max(extentYmas).toFixed();
        minValue = d3.min(extentYmas).toFixed();
        addition_odd = (maxValue - minValue) / 5;
        newMaxVal = +maxValue + addition_odd;
        newMinVal = +minValue - addition_odd;

        /*
        while true
          differenceValue = (newMaxVal - newMinVal)/minStep
          console.log 'differenceValue', differenceValue
          console.log differenceValue % 5
          if  differenceValue % 5
            if (newMinVal/minStep % 2 == 0)
              newMaxVal += maxStep
            else
              newMinVal -=minStep
          else 
            break
        if (newMaxVal <= maxValue + maxStep/5)
          newMaxVal += maxStep
          if (newMaxVal/maxStep % 2 != 0)
            newMaxVal += maxStep
         */
        return [newMinVal, newMaxVal];
      };
      margin = {
        top: 0,
        right: 0,
        bottom: 0,
        left: 0
      };
      $heightChart = 344;
      if ($is_homePage) {
        $heightChart = 318;
      }
      width = 780;
      if ($is_homePage) {
        width -= 1;
      }
      height = $heightChart;
      startWidth = 65;
      if ($is_homePage) {
        startWidth = 0;
      }
      $addition = 30;
      if ($is_homePage) {
        $addition = 1;
      }
      x = d3.scale.linear().range([startWidth, width]);
      y = d3.scale.linear().range([height - $addition, 0]);
      svg = d3.select(".chart-canvas__bubble").append("svg").attr("width", width + margin.left + margin.right).attr("height", height + margin.top + margin.bottom).attr("class", "svg-chart_dynamics").attr("viewBox", "0 0 " + width + " " + height).attr("perserveAspectRatio", "xMinYMid").append("g").attr("transform", "translate(0,0)");
      x.domain([-1, data.date.length]);
      limitsY = searchExtentY(data);
      y.domain(limitsY);
      refactorDate = function(d) {
        if (d !== -1 && d !== data.date.length) {
          return data.date[d].slice(0, 3);
        }
      };
      xAxis = d3.svg.axis().scale(x).orient("bottom").tickFormat(function(d) {
        return refactorDate(d);
      }).ticks(10);
      yAxis = d3.svg.axis().scale(y).orient("left").tickFormat(function(d) {
        return handlePrice(d);
      }).ticks(5);
      make_x_axis = function() {
        return d3.svg.axis().scale(x).orient("bottom").ticks(10);
      };
      make_y_axis = function() {
        return d3.svg.axis().scale(y).orient("left").ticks(5);
      };
      svg.append("g").attr("class", "svg-grid y-grid").attr("transform", "translate(" + startWidth + ",0)").call(make_y_axis().tickSize(-width, 0, 0).tickFormat(""));
      svg.append("g").attr("class", "svg-grid x-grid").attr("transform", "translate(0," + (height - $addition) + ")").call(make_x_axis().tickSize(-height + $addition, 0, 0).tickFormat(""));
      area_x_axis = svg.append("g").attr("class", "x axis").attr("transform", "translate(0," + (height - $addition) + ")").style("font", "14px 'PlumbMedium',sans-serif").call(xAxis);
      area_x_axis.selectAll("text").style("text-anchor", "start").call(wrap_bubble_x, 50);
      positionAxis_y = 62;
      text_y_anchor = 'end';
      unit_y_position = 72;
      if ($is_homePage) {
        area_x_axis.selectAll("line").attr("y2", "-5");
        positionAxis_y = 20;
        text_y_anchor = 'start';
        unit_y_position = 67;
      }
      svg.append("g").attr("class", "y axis").attr("transform", "translate(" + positionAxis_y + ",9)").style("font", "14px 'PlumbMedium',sans-serif").call(yAxis).selectAll("text").style("text-anchor", "" + text_y_anchor);
      svg.append("svg:text").attr("transform", "translate(" + unit_y_position + ",13)").style("font", "14px 'PlumbRegular',sans-serif").attr("text-anchor", "start").text("руб/" + data.unit);
      data.dataObj.forEach(function(d) {
        var circles;
        circles = svg.append("g").attr("class", "circle").selectAll("circle").data(d.data);
        circles.enter().append("circle").attr("cx", function(d, i) {
          return x(-1);
        }).attr("cy", function(d) {
          return y(limitsY[0]);
        }).attr("r", 8.5).attr("fill", d.color).attr("display", function(d) {
          if (d === null) {
            return 'none';
          } else {
            return 'block';
          }
        });
        circles.transition().duration(400).attr("cx", function(d, i) {
          return x(i);
        }).attr("cy", function(d) {
          return y(d);
        });
        circles.on("mouseover", function(innerValue, i) {
          var coordinateTooltip, k, leftTooltip, tooltip, widthTooltip, widthWindow;
          $(this).css("cursor", "pointer").attr("fill", function(a, i) {
            return d3.rgb(i).brighter(0.4).toString();
          }).attr("r", 10);
          widthTooltip = $(".chart-type_bubble .chart-tooltip_wrap").outerWidth();
          coordinateTooltip = d3.event.pageX;
          widthWindow = $(window).width();
          tooltip = $(".chart-type_bubble .chart-tooltip_wrap");
          tooltip.find(".chart-tooltip_date").text(data.date[i]);
          tooltip.find(".chart-tooltip_name").text(d.name);
          tooltip.find(".chart-tooltip_value").text(innerValue + " руб/" + data.unit);
          if (widthWindow <= 1280) {
            if (widthWindow <= 870) {
              k = widthWindow / 870;
            } else {
              k = widthWindow / 1280;
            }
          } else {
            k = 1;
          }
          leftTooltip = $(this).offset().left - tooltip.width() / 2 + (+this.getAttribute("r") - 1) * k;
          tooltip.css("left", leftTooltip + "px");
          tooltip.css("top", d3.event.pageY - $(".chart-type_bubble .chart-tooltip_wrap").height() - 30 + "px");
          tooltip.css("display", "block");
        }).on("mouseout", function() {
          d3.select(".chart-type_bubble .chart-tooltip_wrap").style("display", "none");
          $(this).attr("fill", d.color).attr("r", 9);
        });
      });
    };
    responsiveBubbleChart = function() {
      var aspect, chart, container;
      chart = $(".svg-chart_dynamics");
      aspect = chart.width() / chart.height();
      container = $(".chart-canvas__bubble");
      $(window).on("resize", function() {
        var targetWidth;
        targetWidth = container.width();
        chart.attr("width", targetWidth);
        chart.attr("height", Math.round(targetWidth / aspect));
      }).trigger("resize");
    };
    window.initBubbleChart = function(data) {
      var $homeSection;
      $homeSection = $('.home-section');
      drawingBubbleChart(data, $homeSection.length);
      responsiveBubbleChart();
    };
    drawingChart = function(dataset) {
      var arc, arcs, height, labelr, pie, pieChart, radius, svg, width;
      width = 350;
      height = 348;
      radius = Math.min(width, height) / 2;
      labelr = radius - 35;
      pie = d3.layout.pie().value(function(d) {
        return d.end_sum;
      }).sort(null);
      arc = d3.svg.arc().innerRadius(radius - 48).outerRadius(radius - 110);
      svg = d3.select(".chart-canvas__donut").append("svg").attr("width", width).attr("height", height).attr("class", "svg-chart_statictics").attr("viewBox", "0 0 350 350").attr("perserveAspectRatio", "xMinYMid").append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
      arcs = svg.selectAll("g.slice").data(pie(dataset.dataObj)).enter().append("svg:g").attr("class", "slice");
      pieChart = arcs.append("svg:path").attr("fill", function(d, i) {
        return d.data.color.toString();
      }).attr("d", function(d, i, j) {
        d._tmp = d.endAngle;
        d.endAngle = d.startAngle;
        d.arc = d3.svg.arc().innerRadius(radius - 48).outerRadius(radius - 110);
        return d.arc(d);
      });
      pieChart.transition().delay(function(d, i, j) {
        return i * 50;
      }).duration(60).attrTween("d", function(d, x, y) {
        var i;
        i = d3.interpolate(d.startAngle, d._tmp);
        return function(t) {
          d.endAngle = i(t);
          return d.arc(d);
        };
      });
      pieChart.on("mouseover", function(d) {
        var coordinateTooltip, heightTooltip, tooltip, widthTooltip, widthWindow;
        $(this).css("cursor", "pointer").attr("opacity", 0.8);
        tooltip = d3.select(".chart-type_donut .chart-tooltip_wrap");
        tooltip.select(".chart-type_donut .chart-tooltip_name").text(d.data.name);
        widthTooltip = $(".chart-type_donut .chart-tooltip_wrap").innerWidth();
        heightTooltip = $(".chart-type_donut .chart-tooltip_wrap").height();
        coordinateTooltip = d3.event.pageX;
        widthWindow = $(window).width();
        tooltip.style("left", d3.event.pageX - $(".chart-type_donut .chart-tooltip_wrap").outerWidth() / 2 + "px");
        tooltip.style("top", d3.event.pageY - heightTooltip - 30 + "px").style("display", "block");
      }).on("mouseout", function() {
        d3.select(".chart-type_donut .chart-tooltip_wrap").style("display", "none");
        $(this).attr("opacity", 1);
      });
      arcs.append("svg:text").transition().delay(function(d, i, j) {
        return i * 50;
      }).duration(60).attr("transform", function(d, i) {
        var c, h, x, y;
        d.endAngle = d._tmp;
        c = arc.centroid(d);
        x = c[0];
        y = c[1];
        h = Math.sqrt(x * x + y * y);
        return "translate(" + (x / h * labelr) + "," + (y / h * labelr) + ")";
      }).attr("dy", ".45em").style("font", "14px 'PlumbMedium',sans-serif").attr("text-anchor", function(d) {
        if ((d.endAngle + d.startAngle) / 2 > Math.PI) {
          return "end";
        } else {
          return "start";
        }
      }).text(function(d, i) {
        return d.data.end_sum.toFixed(1) + "%";
      });
    };
    responsiveChart = function() {
      var aspect, chart, container;
      chart = $(".svg-chart_statictics");
      aspect = chart.width() / chart.height();
      container = $(".chart-canvas__donut");
      $(window).on("resize", function() {
        var targetWidth;
        targetWidth = container.width();
        chart.attr("width", targetWidth);
        chart.attr("height", Math.round(targetWidth / aspect));
      }).trigger("resize");
    };
    return window.initDonutChart = function(data) {
      drawingChart(data);
      responsiveChart();
    };
  });

}).call(this);
