/**
 * Displays the refunds graph.
 *
 * @access  public
 * @since   2.5
*/
function rcp_refunds_graph()
{
    global $rcp_options, $wpdb;
    // Retrieve the queried dates
    $dates = rcp_get_report_dates();
    // Determine graph options
    switch ($dates['range']) {
        case 'today':
            $time_format = '%d/%b';
            $tick_size = 'hour';
            $day_by_day = true;
            break;
        case 'last_year':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'this_year':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'last_quarter':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'this_quarter':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'other':
            if ($dates['m_end'] - $dates['m_start'] >= 2) {
                $time_format = '%b';
                $tick_size = 'month';
                $day_by_day = false;
            } else {
                $time_format = '%d/%b';
                $tick_size = 'day';
                $day_by_day = true;
            }
            break;
        default:
            $time_format = '%d/%b';
            // Show days by default
            $tick_size = 'day';
            // Default graph interval
            $day_by_day = true;
            break;
    }
    $time_format = apply_filters('rcp_graph_timeformat', $time_format);
    $tick_size = apply_filters('rcp_graph_ticksize', $tick_size);
    $refunds = (double) 0.0;
    // Total refunds for time period shown
    $subscription = isset($_GET['subscription']) ? absint($_GET['subscription']) : false;
    $payments_db = new RCP_Payments();
    $args = array('subscription' => rcp_get_subscription_name($subscription), 'date' => array());
    ob_start();
    ?>
	<script type="text/javascript">
	   jQuery( document ).ready( function($) {
	   		$.plot(
	   			$("#rcp_refunds_graph"),
	   			[{
   					data: [
	   					<?php 
    if ($dates['range'] == 'this_week' || $dates['range'] == 'last_week') {
        //Day by day
        $day = $dates['day'];
        $day_end = $dates['day_end'];
        $month = $dates['m_start'];
        while ($day <= $day_end) {
            $args = array('date' => array('day' => $day, 'month' => $month, 'year' => $dates['year']), 'fields' => 'amount');
            $args['date'] = array('day' => $day, 'month' => $month, 'year' => $dates['year']);
            $payments = $payments_db->get_refunds($args);
            $refunds += $payments;
            $date = mktime(0, 0, 0, $month, $day, $dates['year']);
            ?>
								[<?php 
            echo $date * 1000;
            ?>
, <?php 
            echo $payments;
            ?>
],
								<?php 
            $day++;
        }
    } else {
        $y = $dates['year'];
        while ($y <= $dates['year_end']) {
            if ($dates['year'] == $dates['year_end']) {
                $month_start = $dates['m_start'];
                $month_end = $dates['m_end'];
            } elseif ($y == $dates['year']) {
                $month_start = $dates['m_start'];
                $month_end = 12;
            } else {
                $month_start = 1;
                $month_end = 12;
            }
            $i = $month_start;
            while ($i <= $month_end) {
                if ($day_by_day) {
                    $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                    $d = 1;
                    while ($d <= $num_of_days) {
                        $args['date'] = array('day' => $d, 'month' => $i, 'year' => $y);
                        $payments = $payments_db->get_refunds($args);
                        $refunds += $payments;
                        $date = mktime(0, 0, 0, $i, $d, $y);
                        ?>
											[<?php 
                        echo $date * 1000;
                        ?>
, <?php 
                        echo $payments;
                        ?>
],
										<?php 
                        $d++;
                    }
                } else {
                    $args['date'] = array('day' => null, 'month' => $i, 'year' => $y);
                    $payments = $payments_db->get_refunds($args);
                    $refunds += $payments;
                    $date = mktime(0, 0, 0, $i, 1, $y);
                    ?>
										[<?php 
                    echo $date * 1000;
                    ?>
, <?php 
                    echo $payments;
                    ?>
],
									<?php 
                }
                $i++;
            }
            $y++;
        }
    }
    ?>
,
	   				],
	   				yaxis: 2,
   					label: "<?php 
    _e('Refunds', 'rcp');
    ?>
",
   					id: 'sales'
   				}],
	   		{
               	series: {
                   lines: { show: true },
                   points: { show: true }
            	},
            	grid: {
           			show: true,
					aboveData: false,
					color: '#ccc',
					backgroundColor: '#fff',
					borderWidth: 2,
					borderColor: '#ccc',
					clickable: false,
					hoverable: true
           		},
            	xaxis: {
	   				mode: "time",
	   				timeFormat: "<?php 
    echo $time_format;
    ?>
",
	   				minTickSize: [1, "<?php 
    echo $tick_size;
    ?>
"]
   				},
   				yaxis: {
   					min: 0,
   					minTickSize: 1,
   					tickDecimals: 0
   				}

            });

			function rcp_flot_tooltip(x, y, contents) {
		        $('<div id="rcp-flot-tooltip">' + contents + '</div>').css( {
		            position: 'absolute',
		            display: 'none',
		            top: y + 5,
		            left: x + 5,
		            border: '1px solid #fdd',
		            padding: '2px',
		            'background-color': '#fee',
		            opacity: 0.80
		        }).appendTo("body").fadeIn(200);
		    }

		    var previousPoint = null;
		    $("#rcp_refunds_graph").bind("plothover", function (event, pos, item) {
	            if (item) {
	                if (previousPoint != item.dataIndex) {
	                    previousPoint = item.dataIndex;
	                    $("#rcp-flot-tooltip").remove();
	                    var x = item.datapoint[0].toFixed(2),
                        y = item.datapoint[1].toFixed(2);
                    	if( rcp_vars.currency_pos == 'before' ) {
							rcp_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + rcp_vars.currency_sign + y );
                    	} else {
							rcp_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y + rcp_vars.currency_sign );
                    	}
	                }
	            } else {
	                $("#rcp-flot-tooltip").remove();
	                previousPoint = null;
	            }
		    });
	   });
    </script>
	<h2><?php 
    _e('Refunds Report', 'rcp');
    ?>
</h2>
	<div class="metabox-holder" style="padding-top: 0;">
		<div class="postbox">
			<div class="inside">
				<?php 
    rcp_reports_graph_controls();
    ?>
				<div id="rcp_refunds_graph" style="height: 300px;"></div>
				<p class="rcp_graph_totals"><strong><?php 
    _e('Total refunds for period shown: ', 'rcp');
    echo rcp_currency_filter(number_format_i18n($refunds, 2));
    ?>
</strong></p>
			</div>
		</div>
	</div>
	<?php 
    echo ob_get_clean();
}