<?

session_start();

header( 'Content-type: text/html; charset=windows-1251' );

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) exit();
if ( !isset( $_SESSION['iduser'] ) ) exit();

define( 'ROOT', 'http://'.$_SERVER['HTTP_HOST'] );
define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );

include_once ROOT_DIR . '/engine/config.php';

$st1 = 0;
$st2 = 0;
$st3 = 0;
$st4 = 0;
$st5 = 0;
$st6 = 0;
$st7 = 0;

$month = array( 'ÿíâ', 'ôåâ', 'ìàð', 'àïð', 'ìàé', 'èþí', 'èþë', 'àâã', 'ñåí', 'îêò', 'íîÿ', 'äåê' );

switch ( date( 'd' ) ) {
    case 1:
        $thismonth7 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth6 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth5 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth4 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth3 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth2 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth1 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        break;
    case 2:
        $thismonth7 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth6 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth5 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth4 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth3 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth2 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth1 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        break;
    case 3:
        $thismonth7 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth6 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth5 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth4 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth3 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth2 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth1 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        break;
    case 4:
        $thismonth7 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth6 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth5 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth4 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth3 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth2 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth1 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        break;
    case 5:
        $thismonth7 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth6 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth5 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth4 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth3 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth2 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth1 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        break;
    case 6:
        $thismonth7 = ' ' . $month[( int ) date( 'm', time() ) - 2] . ' ' . date( 'Y', time() );
        $thismonth6 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth5 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth4 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth3 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth2 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth1 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        break;
    default:
        $thismonth7 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth6 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth5 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth4 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth3 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth2 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        $thismonth1 = ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );
        break;
}

//$thismonth =  ' ' . $month[( int ) date( 'm', time() ) - 1] . ' ' . date( 'Y', time() );

$row_ref = mysql_query( "SELECT regdate FROM user WHERE referer = '" . $_SESSION['iduser'] . "' and regdate >= '" . time() . "' - 6 * 86400" );
if ( mysql_num_rows( $row_ref ) > 0 ) {
    while ( $row_ref_row = mysql_fetch_array( $row_ref ) ) {
        if ( date( 'd.m.Y', $row_ref_row['regdate'] ) == date( 'd.m.Y', time() - 6 * 86400 ) ) {
            $st1 ++;
        }
        if ( date( 'd.m.Y', $row_ref_row['regdate'] ) == date( 'd.m.Y', time() - 5 * 86400 ) ) {
            $st2 ++;
        }
        if ( date( 'd.m.Y', $row_ref_row['regdate'] ) == date( 'd.m.Y', time() - 4 * 86400 ) ) {
            $st3 ++;
        }
        if ( date( 'd.m.Y', $row_ref_row['regdate'] ) == date( 'd.m.Y', time() - 3 * 86400 ) ) {
            $st4 ++;
        }
        if ( date( 'd.m.Y', $row_ref_row['regdate'] ) == date( 'd.m.Y', time() - 2 * 86400 ) ) {
            $st5 ++;
        }
        if ( date( 'd.m.Y', $row_ref_row['regdate'] ) == date( 'd.m.Y', time() - 86400 ) ) {
            $st6 ++;
        }
        if ( date( 'd.m.Y', $row_ref_row['regdate'] ) == date( 'd.m.Y', time() ) ) {
            $st7 ++;
        }
    }
}

$ss = array ( $st1, $st2, $st3, $st4, $st5, $st6, $st7);
    
arsort( $ss );
$max = current( $ss );
    
$step = 0;
if ( $max >= 1 and $max <= 10 ) $step = 10;
if ( $max >= 11 and $max <= 20 ) $step = 20;
if ( $max >= 21 and $max <= 50 ) $step = 50;
if ( $max >= 51 and $max <= 100 ) $step = 100;
if ( $max >= 101 and $max <= 300 ) $step = 300;
if ( $max >= 301 and $max <= 600 ) $step = 600;
if ( $max >= 601 and $max <= 1000 ) $step = 1000;
if ( $max >= 1001 and $max <= 1500 ) $step = 1500;
if ( $max >= 1501 and $max <= 2500 ) $step = 2500;                    

$max = $max + $step;
if ( $max == 0) $max = 1;
$linestep = ( 100 / $max ) * $step;
$linestep = ( int ) $linestep + 1;

?>
<script type="text/javascript" src="<?=ROOT?>/theme/js/flot.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    var graphData = [{
		data: [ [1, <?=$st1?>], [2, <?=$st2?>], [3, <?=$st3?>], [4, <?=$st4?>], [5, <?=$st5?>], [6, <?=$st6?>], [7, <?=$st7?>] ],
		color: '#000'
    }];
    
    $.plot($('#graph-lines2'), graphData, {
		series: {
			points: {
				show: true,
				radius: 3
			},
			lines: {
				show: true
			},
			shadowSize: 0
		},
		grid: {
			color: '#000',
			borderColor: 'transparent',
			borderWidth: 20,
			hoverable: true
		},
		xaxis: {
            ticks: [ [1,'<?=( int ) date( 'd', time() - 6 * 86400 ) . $thismonth7?>'],[2,'<?=( int ) date( 'd', time() - 5 * 86400 ) . $thismonth6?>'],[3,'<?=( int ) date( 'd', time() - 4 * 86400 ) . $thismonth5?>'],[4,'<?=( int ) date( 'd', time() - 3 * 86400 ) . $thismonth4?>'],[5,'<?=( int ) date( 'd', time() - 2 * 86400 ) . $thismonth3?>'],[6,'<?=( int ) date( 'd', time() - 86400 ) . $thismonth2?>'],[7,'<?=( int ) date( 'd', time() ) . $thismonth1?>'] ]
            /*tickColor: 'transparent'*/
		},
		yaxis: {
		  min: 0, max: <?=$linestep?>
		}
	});
    
    function showTooltip(x, y, contents) {
		$('<div id="tooltip">' + contents + '</div>').css({
			top: y - 16,
			left: x + 20
		}).appendTo('body').fadeIn();
	}
    
    var previousPoint = null;

	$('#graph-lines2').bind('plothover', function (event, pos, item) {
		if (item) {
			if (previousPoint != item.dataIndex) {
				previousPoint = item.dataIndex;
				$('#tooltip').remove();
				var x = item.datapoint[0],
					y = item.datapoint[1];
					showTooltip(item.pageX, item.pageY, y);
			}
		} else {
			$('#tooltip').remove();
			previousPoint = null;
		}
	});
});
</script>
<style type="text/css">
#tooltip {
	position: absolute;
	display: none;
	padding: 2px 5px;
	border: 1px solid #809ebf;
    color: #2b587a;
    z-index: 20;
    background: #ffffff;
    font-weight: bold;
    font-size: 10px;
    opacity: 0.7;
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    -khtml-border-radius: 2px;
    border-radius: 2px;
}
</style>
<div id="graph-lines2" style="width: 450px; height: 200px; margin: 10px auto;"></div>