<?
function array_average($array)
{
 return array_sum($array) / count($array);
}

function graph($aData){
    
	if(!count($aData)) return;
	
	$aData = array_slice($aData,0,6);

    echo '<table>';
    foreach($aData as $sKey => $sValue){
        echo '  <tr style="font-size:10px;color:#555555">
                    <td style="padding-right:10px"><span style="cursor:help" title="'.addslashes($sKey).'">' . character_limiter($sKey,100) . '</span></td>
                    <td style="color:#252525">' . $sValue . '</td>
                </tr>';
    }
    echo '</table>';
}

?>

<div id="linechart" style="width:918px"></div>

<div class="dashboard_box dashboard_focus">
<div style="height:17px"></div>
<p style=""><span style="font-size:18px; font-weight:bold"><?= array_sum(array_values($visitors)); ?></span> Liczba wizyt</p>
<p style=""><span style="font-size:18px; font-weight:bold"><?= array_sum(array_values($pageviews)); ?></span> Liczba odsłon</p>
<p style=""><span style="font-size:18px; font-weight:bold"><?= round(array_average(array_values($visitsperhour)),2); ?></span> Czas spędzony na stronie</p>
</div>



<div class="dashboard_box ">
<p style="font-weight:bold">Odnośniki:</p>
<?php graph($referrers); ?>
</div>

<div class="dashboard_box">
<p style="font-weight:bold">Wyszukiwane słowa:</p>
<?php graph($searchwords); ?>
</div>

<div class="dashboard_box dashboard_box_large">
<p style="font-weight:bold">Przeglądarki internetowe:</p>
<?php graph($browsers); ?>
</div>

<div class="dashboard_box dashboard_box_small">
<p style="font-weight:bold">System operacyjny:</p>
<?php graph($os); ?>
</div>

<div class="dashboard_box">
<p style="font-weight:bold">Rozdzielczość ekranu:</p>
<?php graph($screenresolutions); ?>
</div>

<?php
	$chart_data = '<chart>';
	$chart_data .= '<series>';
	
	foreach($visitors as $sKey => $sValue)
	{
		$chart_data .= '<value xid="'.$sKey.'">'.date("D j M", mktime(0, 0, 0, $month, $sKey, $year)).'</value>';
	}
	
	$chart_data .= '</series>';
	
	$chart_data .= '<graphs>';
	$chart_data .= '<graph gid="1">';
	
	foreach($visitors as $sKey => $sValue)
	{
		$chart_data .= '<value xid="'.$sKey.'">'.$sValue.'</value>';
	}
	
	$chart_data .= '</graph>';
	
	$chart_data .= '<graph gid="2">';
	
	foreach($pageviews as $sKey => $sValue)
	{
		$chart_data .= '<value xid="'.$sKey.'">'.$sValue.'</value>';
	}
	
	$chart_data .= '</graph>';
	
	$chart_data .= '</graphs>';
	
	$chart_data .= '</chart>';
?>


<script type="text/javascript">
	// <![CDATA[	
		var so = new SWFObject("<?=base_url()?>static/swf/backend/amcharts/amline.swf", "linechart", "100%", "250", "8", "#FFFFFF");
		//so.addVariable("data_file", encodeURIComponent("<?=base_url()?>../static/swf/backend/amcharts/amline_data.xml"));
		so.addVariable("settings_file", encodeURIComponent("<?=base_url()?>static/swf/backend/amcharts/amline_settings.xml"));
		so.addVariable("chart_data", encodeURIComponent('<?=$chart_data?>'));
		so.addVariable("path", '<?=base_url()?>');
		so.write("linechart");		
	// ]]>
</script>