<?
function initstat($user,$block){
$tmpstr=date("w"); // получаем текущий день в виде 1 2 3 ...
$u1=day($tmpstr-6);
$u2=day($tmpstr-5);
$u3=day($tmpstr-4);
$u4=day($tmpstr-3);
$u5=day($tmpstr-2);
$u6=day($tmpstr-1);
$u7=day($tmpstr); // Текущий день недели

$col_us = mysql_num_rows(mysql_query("SELECT id FROM `b_statistic` WHERE `user_id`='$user'"));
if($col_us == 0){
mysql_query("INSERT INTO `b_statistic` (`user_id`, `typ`, `total`, `date`) VALUES ('$user', '0', '0', '".date('w')."')") or die(mysql_error());
mysql_query("INSERT INTO `b_statistic` (`user_id`, `typ`, `total`, `date`) VALUES ('$user', '1', '0', '".date('w')."')") or die(mysql_error());
mysql_query("INSERT INTO `b_statistic` (`user_id`, `typ`, `total`, `date`) VALUES ('$user', '2', '0', '".date('w')."')") or die(mysql_error());
}

// вытаскиваем все данные о текущем пользователе
$row0 = mysql_fetch_array(mysql_query("SELECT * FROM `b_statistic` WHERE `typ`='0' AND `user_id`='$user'"));
$row1 = mysql_fetch_array(mysql_query("SELECT * FROM `b_statistic` WHERE `typ`='1' AND `user_id`='$user'"));
$row2 = mysql_fetch_array(mysql_query("SELECT * FROM `b_statistic` WHERE `typ`='2' AND `user_id`='$user'"));
// клики
$s0=$row0[dayin($u1)];
$s1=$row0[dayin($u2)];
$s2=$row0[dayin($u3)];
$s3=$row0[dayin($u4)];
$s4=$row0[dayin($u5)];
$s5=$row0[dayin($u6)];
$s6=$row0[dayin($u7)];
// заданияя
$z0=$row1[dayin($u1)];
$z1=$row1[dayin($u2)];
$z2=$row1[dayin($u3)];
$z3=$row1[dayin($u4)];
$z4=$row1[dayin($u5)];
$z5=$row1[dayin($u6)];
$z6=$row1[dayin($u7)];
// письма
$m0=$row2[dayin($u1)];
$m1=$row2[dayin($u2)];
$m2=$row2[dayin($u3)];
$m3=$row2[dayin($u4)];
$m4=$row2[dayin($u5)];
$m5=$row2[dayin($u6)];
$m6=$row2[dayin($u7)];

$color0='3dc536';
$color1='ED5A62';
$color2='368ac5';

if($block == 1){
echo "<tr class='remove-me' style='text-align:center;' id='infous_$user' onclick=\"inf_user('$user');\"><td class='ext' colspan='8' style='padding: 2px;' align='center'>";
}
?>
<div id="container" style="min-width: 310px; margin: 0 auto"></div>
<table style="width: 100%;" class="ref-stat"><thead>
  <th width="18%"></th>
    <th width="12%">Всего</th>
    <th width="10%"><?=$u1?></th>
    <th width="10%"><?=$u2?></th>
    <th width="10%"><?=$u3?></th>
    <th width="10%"><?=$u4?></th>
    <th width="10%"><?=$u5?></th>
    <th width="10%"><?=$u6?></th>
    <th width="10%" class="current"><?=$u7?></th>
    </tr></thead><tbody>
    <tr>
    <td><span style="color: #<?=$color0?>;">Серфинг</span></td><td class="high2"><?=$row0["total"] ?></td><td><?=$s0?></td><td class="high2"><?=$s1?></td><td><?=$s2?></td><td class="high2"><?=$s3?></td><td><?=$s4?></td><td class="high2"><?=$s5?></td><td><?=$s6?></td>
    </tr>
    <tr>
    <td><span style="color: #<?=$color1?>;">Задания</span></td><td class="high2"><?=$row1["total"] ?></td><td><?=$z0?></td><td class="high2"><?=$z1?></td><td><?=$z2?></td><td class="high2"><?=$z3?></td><td><?=$z4?></td><td class="high2"><?=$z5?></td><td><?=$z6?></td>
    </tr>
    <tr>
    <td><span style="color:#<?=$color2?>;">Письма</span></td><td class="high2"><?=$row2["total"] ?></td><td><?=$m0?></td><td class="high2"><?=$m1?></td><td><?=$m2?></td><td class="high2"><?=$m3?></td><td><?=$m4?></td><td class="high2"><?=$m5?></td><td><?=$m6?></td>
</tbody></table>
<script src="/js/highcharts.js" type="text/javascript">
<script src="http://code.highcharts.com/modules/exporting.js"></script>											
<script>
$(function () {
$('#container').highcharts({
chart: { backgroundColor: false, type: 'line', height: 200 },
exporting: { enabled: false },
title: { text: '', },
subtitle: { text: '', },
xAxis: { categories: ['<?=$u1?>', '<?=$u2?>', '<?=$u3?>', '<?=$u4?>', '<?=$u5?>', '<?=$u6?>', '<?=$u7?>'] },
yAxis: { min: 0, title: { text: '' } },
legend: { enabled: false },
tooltip: {
/*
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' + '<td style="padding:0"><b>{point.y}</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
*/
enabled: false
},
series: [{
name: 'Сёрфинг', data: [<?=$s0?>, <?=$s1?>, <?=$s2?>, <?=$s3?>, <?=$s4?>, <?=$s5?>, <?=$s6?>], color: '#ffa500' }, {
name: 'Задания', data: [<?=$z0?>, <?=$z1?>, <?=$z2?>, <?=$z3?>, <?=$z4?>, <?=$z5?>, <?=$z6?>], color: '#53cc19' }, {
name: 'Письма', data: [<?=$m0?>, <?=$m1?>, <?=$m2?>, <?=$m3?>, <?=$m4?>, <?=$m5?>, <?=$m6?>], color: '#318ce7', }]
});
}); 
</script>
<?
if($block == 1){
echo "</td></tr>";
}

}

function dayin($varstr){
 if($varstr=='Пн') $day='mon';
 if($varstr=='Вт') $day='tue';
 if($varstr=='Ср') $day='wed';
 if($varstr=='Чт') $day='thu';
 if($varstr=='Пт') $day='fri';
 if($varstr=='Сб') $day='sat';
 if($varstr=='Вс') $day='sun';
 return $day;
}
function day($varstr){
 if($varstr<0)$varstr=$varstr+7;
 $day="";
 if($varstr==1) $day='Пн';
 if($varstr==2) $day='Вт';
 if($varstr==3) $day='Ср';
 if($varstr==4) $day='Чт';
 if($varstr==5) $day='Пт';
 if($varstr==6) $day='Сб';
 if($varstr==0) $day='Вс';
 return $day;
} 
?>