/**
 * Visualize an HTML table using Highcharts. The top (horizontal) header
 * is used for series names, and the left (vertical) header is used
 * for category names. This function is based on jQuery.
 * @param {Object} table The reference to the HTML table to visualize
 * @param {Object} options Highcharts options
 */
Highcharts.visualize = function(table, options) {
    // the categories
    options.xAxis.categories = [];
    // the data series
    options.series = [];

    $('tr', table).each( function(i) {
        var tr = this;
        if(i == 0) {
            $('th', tr).each( function(j) {
                if (j > 0) {
                    options.xAxis.categories.push(this.innerHTML);
                }
            });
        } else {
            $('td', tr).each( function(c) {
                if (c == 0) { // get the name and init the series
                    options.series[i-1] = {
                        name: this.innerHTML,
                        data: []
                    };
                } else { // add values
				
                    options.series[i-1].data.push(parseFloat(stripNonNumeric(this.innerHTML)));
                }
            });
        }
    });

    var chart = new Highcharts.Chart(options);
}

// On document ready, call visualize on the datatable.
$(document).ready(function() {

    $('.pageThemeTable').each(function(i) {
        var obj = this;	
            $('table',obj).each(function(j) {
				var table = this;
                $(table).addClass('calc');
				$('tr',table).each(function(c) {
					$('td:first', this).addClass("title");
				});
            });
    });

    $('.diagramClass').each(function(i) {
        var obj = this;

        if(i >= 0) {		
            $('table' , obj).each(function(j) {
                var divID = 'chart-container-'+i+'-'+j;
                var table = this;
                if($('.tableDiagram',obj).size() == 0) {
                    $(obj).prepend('<div id="' + divID + '" class="tableDiagram"/>');
                }
                else{
                    $('.tableDiagram', obj).last().after('<div id="' + divID + '" class="tableDiagram"/>');
                }
                options = {
                    chart: {
                        renderTo: divID,
                        defaultSeriesType: 'column'
                    },
                    title: {
                        text: ''
                    },
                    xAxis: {
                    },
                    yAxis: {
                        title: {
                            text: ''
                        }
                    },
                    tooltip: {
                        formatter: function() {
                            return '<b>'+ this.series.name +'</b><br/>'+  parseFloat(stripNonNumeric(this.y));
                        }
                    },
					 
					plotOptions : {
						column :  {
							borderWidth:'0px',
							shadow:false
						}
						
					}
                };
                if($(obj).hasClass('green')) {
                    options.colors = ['#83A225', '#C2CF84', '#E0E7C1'];
                }
                 else if($(obj).hasClass('blue')) {
                    options.colors = ['#4482A0', '#8FB0C6', '#CFDDE7'];
                }
                else {
                    options.colors = ['#4482A0', '#8FB0C6', '#CFDDE7','#B3A58C', '#684C1A', '#9C9D9F','#646567', '#CFD0D2'];
                }
                Highcharts.visualize(table, options);
            });
        }
    });

});

function stripNonNumeric( str )
{
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for( var i = 0; i < str.length; i++ )
  {
    if( rgx.test( str.charAt(i) ) ){
      if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
             ( str.charAt(i) == '-' && out.length != 0 ) ) ){
        out += str.charAt(i);
      }
    }
  }
  return out;
}

   
