// include SPECIFIC PAGE LEVEL SCRIPTS 
document.write('<script src="assets/plugins/flot/jquery.flot.min.js"></script>');
document.write('<script src="assets/plugins/flot/jquery.flot.crosshair.min.js"></script>');
document.write('<script src="assets/plugins/flot/jquery.flot.categories.min.js" type="text/javascript"></script>');

/////////////////////////////////////
var Charts = function() {

    return {
        //main function to initiate the module

        initCaseSTAT: function(opened, resolved, closed) {

            if (!jQuery.plot) {
                return;
            }

            var data = [];

            //Case statistics
            function CaseStatistics(opened, resolved, closed) {

                var plot = $.plot($("#chart_cases"), [{
                        data: opened,
                        label: "'Opened'",
                        lines: {
                            lineWidth: 1,
                        },
                        shadowSize: 0

                    }, {
                        data: resolved,
                        label: "'Resolved'",
                        lines: {
                            lineWidth: 1,
                        },
                        shadowSize: 0
                    }, {
                        data: closed,
                        label: "'Not resolved'",
                        lines: {
                            lineWidth: 1,
                        },
                        shadowSize: 0
                    }
                ], {
                    series: {
                        lines: {
                            show: true,
                            lineWidth: 2,
                            fill: true,
                            fillColor: {
                                colors: [{
                                        opacity: 0.05
                                    }, {
                                        opacity: 0.01
                                    }
                                ]
                            }
                        },
                        points: {
                            show: true,
                            radius: 3,
                            lineWidth: 1
                        },
                        shadowSize: 2
                    },
                    grid: {
                        hoverable: true,
                        clickable: true,
                        tickColor: "#eee",
                        borderColor: "#eee",
                        borderWidth: 1
                    },
                    colors: ["#52e136", "#37b7f3", "#d12610"],
                    xaxis: {
                        ticks: 11,
                        tickDecimals: 0,
                        tickColor: "#eee",
                    },
                    yaxis: {
                        ticks: 11,
                        tickDecimals: 0,
                        tickColor: "#eee",
                    }
                });


                function showTooltip(x, y, contents) {
                    $('<div id="tooltip">' + contents + '</div>').css({
                        position: 'absolute',
                        display: 'none',
                        top: y + 5,
                        left: x + 15,
                        border: '1px solid #333',
                        padding: '4px',
                        color: '#fff',
                        'border-radius': '3px',
                        'background-color': '#333',
                        opacity: 0.80
                    }).appendTo("body").fadeIn(200);
                }

                var previousPoint = null;
                $("#chart_cases").bind("plothover", function(event, pos, item) {
                    $("#x").text(pos.x.toFixed(2));
                    $("#y").text(pos.y.toFixed(2));

                    if (item) {
                        if (previousPoint != item.dataIndex) {
                            previousPoint = item.dataIndex;

                            $("#tooltip").remove();
                            var x = item.datapoint[0].toFixed(2),
                                    y = item.datapoint[1].toFixed(2);

                            switch(x)
							{
								case "1.00" : x = " en 'Janvier' = "; 	break;
								case "2.00" : x = " en 'Février' = "; 	break;
								case "3.00" : x = " en 'Mars' = "; 		break;
								case "4.00" : x = " en 'Avril' = "; 	break;
								case "5.00" : x = " en 'Mai' = "; 		break;
								case "6.00" : x = " en 'Juin' = "; 		break;
								case "7.00" : x = " en 'Juillet' = "; 	break;
								case "8.00" : x = " en 'Août' = "; 		break;
								case "9.00" : x = " en 'Septembre' = "; break;
								case "10.00" : x = " en 'Octobre' = "; 	break;
								case "11.00" : x = " en 'Novembre' = "; break;
								case "12.00" : x = " en 'Décembre' = "; break;
									
							}
							showTooltip(item.pageX, item.pageY, "Cases " + item.series.label + x + y);
                        }
                    } else {
                        $("#tooltip").remove();
                        previousPoint = null;
                    }
                });
            }

            CaseStatistics(opened, resolved, closed);

        },
        initAvrgTime: function(avgtimes) {

            // bar chart:
            var data = avgtimes;

            var options = {
                series: {
                    bars: {show: true}
                },
                bars: {
                    barWidth: 0.5,
                    lineWidth: 0, // in pixels
                    shadowSize: 0,
                    align: 'left'
                },
                colors: ["#52e136"],
                grid: {
                    tickColor: "#eee",
                    borderColor: "#eee",
                    borderWidth: 1
                }
            };

            $.plot($("#chart_avrg_time"),
                    [{
                            data: data,
                            lines: {
                                lineWidth: 1,
                            },
                            shadowSize: 0
                        }]
                    , options);

        },
        initEarning: function(earning) {

            // bar chart:
            var data = earning;

            var options = {
                series: {
                    bars: {show: true}
                },
                bars: {
                    barWidth: 0.5,
                    lineWidth: 0, // in pixels
                    shadowSize: 0,
                    align: 'left'
                },
                colors: ["#38b7f0"],
                grid: {
                    tickColor: "#eee",
                    borderColor: "#eee",
                    borderWidth: 1
                }
            };

            $.plot($("#chart_earning"),
                    [{
                            data: data,
                            lines: {
                                lineWidth: 1,
                            },
                            shadowSize: 0
                        }]
                    , options);

        }
    };

}();
///////////////////////////////////////////
jQuery(document).ready(function() {
	function GetEarning()
	{
		earning_json = EARNING_JSON;					// var EARNING_JSON is in the bottom of statistic.php
		var earning = Array();
		
		var iCount = 0;
		for (var i in earning_json)
		{
			earning[iCount] = earning_json[i];
			iCount++;
		}
		return earning;
	}
	
	function GetCaseStatistics()
	{
		case_statistics_json = CASE_STATISTIC_JSON;		// var CASE_STATISTIC_JSON is in the bottom of statistic.php
		
		var case_statistics_opened = Array();
		var case_statistics_resolved = Array();
		var case_statistics_not_resolved = Array();
		
		var iCount = 0;
		for (var i in case_statistics_json.opened)
		{
			case_statistics_opened[iCount] = case_statistics_json.opened[i];
			case_statistics_resolved[iCount] = case_statistics_json.resolved[i];
			case_statistics_not_resolved[iCount] = case_statistics_json.not_resolved[i];
			iCount++;
		}
		
		var case_statistics = [
			case_statistics_opened,
			case_statistics_resolved,
			case_statistics_not_resolved               
		];
		
		return case_statistics;
	}
	
	function GetAvgTime()
	{
		avgtime_json = AVG_TIME_JSON;		// var AVG_TIME_JSON is in the bottom of statistic.php
		var avgtime = Array();
		
		var iCount = 0;
		for (var i in avgtime_json)
		{
			avgtime[iCount] = avgtime_json[i];
			iCount++;
		}
		return avgtime;
	}
	

	var tmp_earning = GetEarning();
	var earning = [];
	for (var i = 0; i < tmp_earning.length; i++) {
		earning.push([i+1, tmp_earning[i]]);
	}

	var case_statistics = GetCaseStatistics();

	var opened = [];
	var resolved = [];
	var closed = [];
	for (var i = 0; i < case_statistics[0].length; i++) {
		opened.push([i+1, case_statistics[0][i]]);
		resolved.push([i+1, case_statistics[1][i]]);
		closed.push([i+1, case_statistics[2][i]]);
	}
	
	var tmp_avgtimes = GetAvgTime();
	var avgtimes = [];
	for (var i = 0; i < tmp_avgtimes.length; i++) {
		avgtimes.push([i+1, tmp_avgtimes[i]]);
	}
   
	
	// initiate layout and plugins
	App.init();
	Charts.initCaseSTAT(opened, resolved, closed);
	Charts.initAvrgTime(avgtimes);
	Charts.initEarning(earning);
});

