/* Already defined variable (Before the input)
*
* var chart_1_num_day = 5;
* var chart_2_num_day = 15;
*
*/
function getSyncScriptParams() {
         var scripts = document.getElementsByTagName('script');
         var lastScript = scripts[scripts.length-1];
         var scriptName = lastScript;
         return {
             url_providersChart : scriptName.getAttribute('data-url_providersChart'),
             url_moduleCharts : scriptName.getAttribute('data-url_moduleCharts'),
         };
}
var url_providersChart = getSyncScriptParams().url_providersChart;
var url_moduleCharts = getSyncScriptParams().url_moduleCharts;
/* VARIABLES */
    var pie_threshold = 0.05
    var options = {
             series: {  pie: {  show: true,
                                radius: 3/5,
                                combine: {
                                    color: '#999',
                                    threshold: pie_threshold
                                },
                                label: {
                                    show: true,
                                    radius: 1,
                                    formatter: labelFormatter,
                                    background: {
                                        opacity: 0.5,
                                        color: '#000'
                                    }
                                }
                            }
             },
             grid: { hoverable: true, clickable: true },
             legend: { show: false  },
         };
    /* Linked graph - remember the data */
    var plot_data_old = []
    var plot_old = []
/* FUNCTIONS */
function labelFormatter(label, series) {
    return "
"
               + label + "
" + Math.round(series.percent) + "%
";
}
/* Plot, and bind chart listener */
function plot_top_graph(module_name, init){
    /**** Pie Chart ****/
    // moduleCharts is used the decide the url to request data
    var moduleCharts = "size" == module_name ? url_providersChart : ("num" == module_name ? url_providersChart : url_moduleCharts);
    console.log(moduleCharts)
    var tot_sum = 0; // used to detect elements placed in 'Other' pie's part
    var data_other = []; // used to detect elements placed in 'Other' pie's part
    var createPie = $.getJSON(moduleCharts+"?moduleName="+module_name+"&num_day="+chart_1_num_day,
        function(data) {
                  var temp_data_pie = [];
                  for(i=0; i 0 && data[0][1] < 7){ // If data is [1:6] day(s) old, put the panel in yellow
                             $("#day-"+module_name).text(data[0][1] + " Day(s) ago ");
                             $("#panel-"+module_name).removeClass("panel-green");
                             $("#panel-"+module_name).addClass("panel-yellow");
                         } else if (data[0][1] > 6) { // data old of more than 7 days, put the panel in red
                             $("#day-"+module_name).text(data[0][1] + " Day(s) ago ");
                             $("#panel-"+module_name).removeClass("panel-green");
                             $("#panel-"+module_name).addClass("panel-red");
                         }
                      } else {
                          temp_data_pie.push({label: data[i][0], data: data[i][1]});
                          tot_sum += data[i][1];
                      }
                  }
                  for(i=0; i1){ // avoid adding plot_data for previous clicked pie part
                        plot_data_old = [];
                        plot_old = [];
                    }
                    plot_data_old.push(plot.getData());
                    plot_old.push(plot);
                }
                if (can_bind){ // avoid binding two listener for provider graphs
                    binder(module_name);
                    if (module_name == "size") // bind the linked provider graph
                        binder("num");
                    else if (module_name == "num")
                        binder("size");
                }
            });
        } else { // Normal pie's part clicked
            $.getJSON(chartUrl+"?keywordName="+involved_item+"&moduleName="+module_name+"&bar=true"+"&days="+num_day,
                function(data) {
                    var temp_data_bar = []
                    for(i=1; i1){ // avoid adding plot_data for previous clicked pie part
                            plot_data_old = [];
                            plot_old = [];
                        }
                        plot_data_old.push(plot.getData());
                        plot_old.push(plot);
                    }
                    if (can_bind){ // avoid binding two listener for provider graphs
                        $("#flot-bar-chart-"+module_name).unbind( "plothover.customHandler" );
                        binder(module_name);
                        if (module_name == "size"){ // bind the linked provider graph
                            $("#flot-bar-chart-"+"num").unbind( "plothover.customHandler" );
                            binder("num");
                        }
                        else if (module_name == "num"){
                            $("#flot-bar-chart-"+"size").unbind( "plothover.customHandler" );
                            binder("size");
                        }
                    }
               });
       }
    }; // end update_bar_chart
} // end plot_top_graph
/* Bind a listener to the graph to display the value under the cursor in the approriate div */
function binder(module_name){
    $("#flot-bar-chart-"+module_name).bind("plothover.customHandler", function (event, pos, item) {
       if (item) { // a correct item is hovered
           var x = item.datapoint[0]
           var y = item.datapoint[1]
           var date = new Date(parseInt(x));
           var formated_date = date.getMonth()+'/'+date.getDate();
           var color = item.series.color;
           var color_opac = "rgba" +  color.slice(3, color.length-1)+",0.15)";
           // display the hovered value in the chart div
           $("#tooltip_graph-"+module_name).html(item.series.label + " of " + formated_date + " = " + y+"")
               .css({padding: "2px", width: 'auto', 'background': color_opac , 'border': "3px solid "+color})
               .fadeIn(200);
           /* If provider bar chart hovered, highlight and display associated value */
           if (module_name == "size" || module_name == "num"){
               new_module_name = module_name == "size" ? "num" : "size";
               /* Used to get the corresponding associated value for providers charts */
               var plot_obj = plot_data_old[0]; //contain series
               for(serie=0; serie" + curr_data_other+"")
                   .css({padding: "2px", width: 'auto', 'background': color_opac, 'border': "3px solid "+color})
                   .fadeIn(200);
               // clean up other highlighted bar
               for(i=0; i