google.load('visualization', '1', {'packages':['corechart']});

VotingCharts = {
  /**
   * The colors being used for drawing the charts.
   */
  COLORS:["5c5c5c","5f2569","00e4ce","0090a7","349e39","006984", "00a8cb","95d637","d31114","fae500","ffa900",
    "00b5b7","3e365f"],

  /**
   * Maximum string length for a chart entry.
   */
  MAX_STRING_LENGTH: 50,

  /**
   * Searches the document for all divs having the "voting" class and draws the charts for each of them.
   */
  initializeCharts : function() {
    $('div.voting').each(function(i, voting) {
      VotingCharts.initializeChart(voting);
    })
  },

  /**
   * Draws the chart for a single voting. The values are being extracted from the divs marked with the
   * "voting-option" class.
   * @param voting the voting to be charted.
   */
  initializeChart : function(voting) {
    var pieNode = $(voting).find('div.voting-chart-pie');
    var barNode = $(voting).find('div.voting-chart-bar');

    if (!barNode.length && !pieNode.length) {
      // nohing to draw.
      return;
    }

    var magnitude = [];
    var text = [];
    var color = [];

    $(voting).find('div.voting-option').each(function(idx, option) {
      var num = parseInt(option.className.slice(option.className.search(/\d+/)));

      var txt = $(option).find('p > strong').text().slice(0, VotingCharts.MAX_STRING_LENGTH);
      var mag = parseInt($(option).find('span.vote-size').text());

      for (var i = 0, len = magnitude.length + 1; i < len; i++) {
        if (magnitude[i] && magnitude[i] >= mag) {
          continue;
        }
        magnitude.splice(i, 0, mag);
        text.splice(i, 0, txt);
        color.splice(i, 0, VotingCharts.COLORS[num - 1]);
        break;
      }
    });

    if (pieNode.length) {
      VotingCharts.drawPieChart(pieNode[0], magnitude, text, color);
    }

    if (barNode.length) {
      VotingCharts.drawBarChart(barNode[0], magnitude, text, color);
    }
  },

  /**
   * Draws a pie chart at the given node's location.
   * @param node the dom node, where to draw the pie chart.
   * @param magnitude an array containing the number of votes for each option.
   * @param text an array containing the descriptions of each option
   * @param color an array containg the color for each option
   */
  drawPieChart : function(node, magnitude, text, color) {
    var data = new google.visualization.DataTable();

    data.addColumn('string', 'answer');
    data.addColumn('number', 'amount');

    data.addRows(magnitude.length);

    for (i = 0,len = magnitude.length; i < len; i++) {
      data.setValue(i, 0, text[i]);
      data.setValue(i, 1, magnitude[i]);
    }

    var area = {left : 0, top : 0, width : '100%', height : '100%'};

    var chart = new google.visualization.PieChart(node);
    chart.draw(data, {
      width : 542,
      height : 300,
      colors : color,
      legend : 'none',
      chartArea : area
    });
  },

  /**
   * Draws a bar chart at the given node's location.
   * @param node the dom node, where to draw the bar chart.
   * @param magnitude an array containing the number of votes for each option.
   * @param text an array containing the descriptions of each option
   * @param color an array containg the color for each option
   */
  drawBarChart : function (node, magnitude, text, color) {
    var data = new google.visualization.DataTable();

    data.addColumn('string', 'answer');
    //data.addColumn('number', 'Anzahl');

    data.addRows(1);
    //data.addRows(magnitude.length);

    for (i = 0,len = magnitude.length; i < len; i++) {
      data.addColumn('number', text[i]);
      data.setValue(0, i + 1, magnitude[i]);
      //data.setValue(i, 0, text[i]);
      //data.setValue(i, 1, magnitude[i]);
    }

    var area = {left : 0, top : 50, width : '100%', height : '100%'};
    var hAxis = {textPosition : 'none'};
    var vAxis = {textPosition : 'none'};

    var chart = new google.visualization.BarChart(node);
    chart.draw(data, {
      width : 542,
      height: 300,
      colors : color,
      legend : 'none',
      //axisTitlesPosition : 'none',
      chartArea : area,
      hAxis : hAxis,
      vAxis : vAxis,
      gridlineColor : '#fff'
    });
  }

};


$(function() {
  google.setOnLoadCallback(VotingCharts.initializeCharts);
});

