/**
 * Displays the graph from data points residing in the global variable product_graph_data
 */

// add extra data points just before each data point to make the graph square
function addExtraPoints(data) {
  var data_out = [];
  var last_price = 0;
  $.each(data, function(index, value) {
    if (index == 0) {
      last_price = value[1];
    }
    $.merge(data_out, [[value[0] - 0.00000000001, last_price], [value[0], value[1]]]);
    last_price = value[1];
  });
  return data_out;
}
  
$(document).ready(function() {

  $("div.product-item").each(function(i) {
    var prod_id = parseInt($(this).attr('data-product_id'));
    if (product_price_data[prod_id].length > 0) {
      var d_square = addExtraPoints(product_price_data[prod_id]);
      
      var plot = $.plot(
        $(this).find(".graph-container-small"),
        [{ data: d_square, color: "white"}],
        { 
          lines: { 
            show: true, 
            fill: false, 
            fillColor: "rgb(187,30,49)", 
            lineWidth: 2
          },
          points: { 
            show: false 
          },
          selection: { 
            mode: "xy" 
          },
          grid: { 
            hoverable: false, 
            clickable: false, 
            backgroundColor: "#829a2e" 
          },
          xaxis: { 
            show: false,
            minTickSize: 1, 
            tickDecimals: 0,
            ticks: 2,
            min: -8, 
            max: .02, 
            tickFormatter: function (v, axis) {
               if ((-1 * v.toFixed(axis.tickDecimals)) % 2 != 0) {
                 return '';
               }
              if ((v*-1) > 1){
                var labelOut = xlabel_weeks.replace('%%', (-1 * v.toFixed(axis.tickDecimals)));
                timePassed = '<div class="days-passed">' + labelOut + '</div>';
              }else if ((v*-1) == 1){
                var labelOut = xlabel_week.replace('%%', (-1 * v.toFixed(axis.tickDecimals)));
                timePassed = '<div class="days-passed">' + labelOut + '</div>';
              }else{
                timePassed = '<div class="days-passed today">' + xlabel_today + '</div>';
              }
              return timePassed;
            } 
          },
          yaxis: {
            ticks: function(axis) {
              // custom formatter to ensure that there are always only 3 ticks
              var res = [];
              res.push(axis.min);
              res.push(((axis.max - axis.min) / 2) + axis.min);
              res.push(axis.max);
              return res;
            },
            autoscaleMargin: 0.05,
            tickFormatter: function (v, axis) {
              var the_num = v.toFixed(axis.tickDecimals);
              var decimal = '.';
              var thousands = ' ';
              // override the defaults if we're on germany
              if ('de' == country) {
                var decimal = ',';
                var thousands = ' ';
              }
              
              return '<div class="price-format">' + symbol + parseFloat(the_num).formatMoney(2, decimal, thousands) + '</div>';
            }
          }
        }
      ); // end flot
    } // end if product_price_data available
  });
  
  
  
  
});

