/* bm-table2chart.js v1.0
   For use with the Big Medium content management system, this
   script converts simple html tables into Google Chart graphics.
   For details on this script and its operation, see:
   http://globalmoxie.com/blog/google-charts-from-tables.shtml

   Copyright 2008, Josh Clark and Global Moxie, LLC.
   
   This script adapts and expands on the excellent foundation
   provided by Christian Heilmann with his original table2chart.js:
   http://icant.co.uk/sandbox/datatable-to-chart/

   This script is licensed under the Creative Commons Attribution
   3.0 license:
   http://creativecommons.org/licenses/by/3.0/

   You may copy, distribute, and adapt this script provided that you
   provide attribution to Josh Clark, Global Moxie LLC, and
   Christian Heilmann, including the three URLs above.

   For details on the Google Chart API:
   http://code.google.com/apis/chart/

   For details about Big Medium: http://www.bigmedium.com/
   -------------------------------------------------------------- */

BM.onDOM.addEvent(function(){
  var triggerClass = 'tochart';
  var chartType = '3dpie';
  var chartClass = 'chartfromtable';
  var hideClass = 'hidden';
  var chartColor = '339933';
  var chartSize = '350x150';
  /* end variables */

  var types = {
    'line' : 'lc',
    'bar' : 'bvs',
    '3dpie' : 'p3',
    'pie' : 'p'
  };
  var labelTypes = {
    '3dpie' : 'chl=',
    'pie' : 'chl=',
    'line' : 'chxt=x,y&chxl=0:|',
    'bar' : 'chxt=x,y&chxl=0:|'
  };
  var sizeCheck = /\s?size([^\s]+)/;
  var colCheck = /\s?color([^\s]+)/;
  var typeCheck = /\s?(line|bar|3dpie|pie)chart/i;
  $$('table.'+triggerClass).each(function(t){
    var data = [];
    var labels = []
    if ( !t.hasClassName('nohide') ) { t.className += ' '+ hideClass; }
    
    var c = t.className;
    var size = sizeCheck.exec(c);
    size = size ? size[1] : chartSize;
    var col = colCheck.exec(c);
    col = col ? col[1] : chartColor;
    var typeKey = typeCheck.exec(c);
    typeKey = typeKey ? typeKey[1] : chartType;
    var type = types[typeKey] || types[chartType];
    
    var parent = t.getElementsByTagName('tbody')[0] || t;
    var tds = parent.getElementsByTagName('td');
    var maxLen = 4;
    var maxVal = 0;
    for(var j=0;tds[j];j+=2){
      var label = tds[j].innerHTML.stripTags().strip();
      labels.push(label);
      var num = new Number( tds[j+1].innerHTML.stripTags().strip() );
      data.push(num);

      if (label.length > maxLen) maxLen = label.length;
      if (num > maxVal) maxVal = num;
    };
    var topValue = Math.floor(1.05*maxVal);
    if(topValue < maxVal) topValue = maxVal;
    data = data.collect(function(v){
      return Math.floor((v/topValue)*100);
    });
      
    var charturl = 'http://chart.apis.google.com/chart?cht=' + type
      + '&chco=' + col + '&chs=' + size + '&chd=t:'
      + data.join(',') + '&' + labelTypes[typeKey] + labels.join('|')
      + '&chxr=1,0,' + topValue + '&chbh=' + (maxLen*5).toString();

    var div = document.createElement('div');
    div.className = chartClass;
    var chart = document.createElement('img');
    chart.setAttribute('src',charturl);
    chart.setAttribute('alt',t.getAttribute('summary'));
    div.insertBefore(chart,null);
    t.parentNode.insertBefore(div,t);
  });
});

