dojo.require("dojox.charting.Chart2D");
//dojo.require("dojox.charting.widget.Legend");

  makeCharts = function(){
    dojo.query('h4.chart_title"').forEach(function(x){
      dojo.style(x, "marginBottom", "0px");
      dojo.style(x, "marginTop", "25px");
    });
    
//    dojo.query('.legend').forEach(function(x){
//      dojo.style(x, "width", "657px");
//      dojo.style(x, "height", "35px");
//      dojo.style(x, "background", "url(/images/legend.png) no-repeat top right");
//    });
    
    
    dojo.query('.chartData').forEach(function(item){dojo.style(item, "display", "none")});
    dojo.query(".simplechart").forEach(function(chart){
    
    var chartRow = dojo.query('.result', chart);    
    var labels = dojo.map(chartRow, function(item){return dojo.query('h4', item)[0].innerHTML}).reverse();  
    var finalLabels = new Array();  
    for (b=0;b<=labels.length-1;b=b+1) 
    {  
        finalLabels[b] = { value: b+1, text:labels[b] };  
    }
    
    dojo.style(chart, "width", "680px");
    dojo.style(chart, "height", (45 * labels.length) + 70 + "px");
    
    var chart1 = new dojox.charting.Chart2D(chart);
    chart1.addPlot("default", {
      type: "StackedBars",
      gap: 4,
      hMajorLines: false,
      hMinorLines: false,
      vMajorLines: true,
      vMinorLines: false,
    });
    chart1.addPlot("default2", {
      hAxis: "other x", 
      vAxis: "other y",
      type: "StackedBars",
      gap: 4,
      hMajorLines: false,
      hMinorLines: false,
      vMajorLines: true,
      vMinorLines: false
    });
    chart1.addAxis("x", {
      min: 1,
      max: 5,
      majorLabels: true,
      minorTicks: false,
      minorLabels: false,
      font: "bold 11pt", 
      microTicks: false
    });
    chart1.addAxis("other x", {
      leftBottom: false,
      majorLabels: false,
      minorTicks: false,
      minorLabels: false,
      microTicks: false
    });
    chart1.addAxis("y", {
      vertical: true,
      majorLabels: true,
      minorTicks: (labels.length > 10),
      minorLabels: true,
      microTicks: false,
      font: "bold 11pt", 
      labels: finalLabels
    });
    
    chart1.addAxis("other y", {
      vertical: true,
      leftBottom: false,
      majorLabels: false,
      minorTicks: false,
      minorLabels: false
    });

      var leftBufferTDs = dojo.query('table tr:nth-child(2) td:nth-child(1)', chart);   
      var leftBuffers = dojo.map(leftBufferTDs, function(item){ return parseFloat(item.innerHTML)}).reverse();
      var negTwoTDs = dojo.query('table tr:nth-child(2) td:nth-child(2)', chart);   
      var negTwo = dojo.map(negTwoTDs, function(item){ return parseFloat(item.innerHTML)}).reverse();
      var negOneTDs = dojo.query('table tr:nth-child(2) td:nth-child(3)', chart);   
      var negOne = dojo.map(negOneTDs, function(item){ return parseFloat(item.innerHTML)}).reverse();
      var posOneTDs = dojo.query('table tr:nth-child(2) td:nth-child(4)', chart);   
      var posOne = dojo.map(posOneTDs, function(item){ return parseFloat(item.innerHTML)}).reverse();
      var posTwoTDs = dojo.query('table tr:nth-child(2) td:nth-child(5)', chart);   
      var posTwo = dojo.map(posTwoTDs, function(item){ return parseFloat(item.innerHTML)}).reverse();
      var means = labels.map(function(){return 0.05});
        
      chart1.addSeries("Left Buffer", leftBuffers, {stroke: 'none', fill: "white"});
      chart1.addSeries("-2 Std",      negTwo, {stroke: 'none', fill: "#f90"});
      chart1.addSeries("-1 Std",      negOne, {stroke: 'none', fill: "yellow"});
      chart1.addSeries("Mean",        means, {stroke: 'none', fill: "black"});
      chart1.addSeries("+1 Std",      posOne, {stroke: 'none', fill: "lightblue"});
      chart1.addSeries("+2 Std",      posTwo, {stroke: 'none', fill: "blue"});
        
      chart1.render();
   });
  };


$(document).ready(function(){
   if($(".simplechart").length > 0)
  {
    dojo.addOnLoad(makeCharts);
  }
});

  
//Unused stuff at this point

//  /* Show "Help" tooltips when rolling over the help icon on the survey */
//  $('body').append('<div id="tooltip" class="help_tip"><div id="tooltip-inner"></div></div>');
//  $('#tooltip').hide();
//
//  $('#tooltip').bind('mouseover',function() { $(this).show(); } );
//  $('#tooltip').bind('mouseleave',function() { setTimeout("$('#tooltip').fadeOut(250);",250) } );
//
//  $('.inline_help_icon').hoverIntent(
//  function() {
//    var pos = $(this).position();
//    var txt = $(this).attr('title');
//    $('#tooltip-inner').empty().append('<p>' + txt + '</p>');
//    $('#tooltip').css('top', pos.top-4)
//    .css('left', pos.left-4)
//    .show();
//  },
//  function() { }
//);


  /* Show/Hide the Filters on Survey Results */
//
//  $('.filters h4 a').click(function() {
//    $(this).parents('.filters').toggleClass('open').toggleClass('closed');
//    $(this).parents('.filters').find('.filters_inner').slideToggle('250');
//    return false;
//  }).hover(
//  function() { $(this).parents('.filters').addClass('over'); },
//  function() { $(this).parents('.filters').removeClass('over'); }
//);