<?php
function nastavbarvu($maxhodnota,$hodnota)
{
   if (($hodnota/$maxhodnota)<0.01) {return 1;}
   else if (($hodnota/$maxhodnota)<0.3) {return 2;}
   else if (($hodnota/$maxhodnota)<0.6) {return 3;}
   else if (($hodnota/$maxhodnota)<0.9) {return 4;}
   return 5;
   break;
}
$obsah="";
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

include("config.php");

     //$maximalni=mysql_query("SELECT max(data.data),devices.id FROM devices,buildings,data,sensors WHERE data.device=devices.id AND buildings.mapname='".$_GET['b']."' AND buildings.id=devices.building AND devices.floor=".$_GET['f']." AND devices.sensor=sensors.id AND sensors.active=1 GROUP BY data.device ORDER BY max(data.data) DESC LIMIT 1");
     $maximalni=mysql_query("SELECT max(data.time),data.data,devices.id FROM devices,buildings,data,sensors WHERE data.device=devices.id AND buildings.mapname='".$_GET['b']."' AND buildings.id=devices.building AND devices.floor=".$_GET['f']." AND devices.sensor=sensors.id AND sensors.active=1 GROUP BY devices.id DESC");
     $max=-1;
     for($i=0;$i<mysql_num_rows($maximalni);$i++)
     {    
     $data=mysql_fetch_row($maximalni);
     if ($data[1]>$max) {$max=$data[1];}
     }
     
     
     
     $aktualni=mysql_query("SELECT devices.id,devices.name,sensors.active,icons.picture,devices.type,devices.building,devices.floor,devices.loc_x,devices.loc_y FROM devices,sensors,icons,buildings WHERE buildings.mapname='".$_GET['b']."' AND buildings.id=devices.building AND devices.floor=".$_GET['f']." AND icons.id=devices.icon AND devices.sensor=sensors.id AND sensors.active=1 ORDER BY devices.id");
     $poceta=mysql_num_rows($aktualni);
     for($i=0;$i<$poceta;$i++)
     {
     $aktdata=mysql_fetch_row($aktualni);
     $x=$aktdata[7]+$_GET['x']-20;
     $y=$aktdata[8]+$_GET['y']-20;
     
     $dotaz=mysql_query("SELECT data.data FROM devices,data WHERE devices.id=data.device AND devices.id=".$aktdata[0]." ORDER BY data.time DESC LIMIT 1");
     $posledni=mysql_fetch_row($dotaz);

     $obsah.='<div onclick="location.href=\'detail.php?id='.$aktdata[0].'\'" class="device" id="dev'.$aktdata[0].'" style="position:absolute; top:'.$y.'px; left:'.$x.'px; width:40px; height:40px; background-image:url(\'images/mapy/'.nastavbarvu($max,$posledni[0]).'.png\')"></div>'; 
     if (strlen($aktdata[4])>1)
     {$tt='<br/>('.$aktdata[4].')';} else {$tt='';}
     $obsah.='<div class="popupinfo" id="idev'.$aktdata[0].'"><strong>'.$aktdata[1].'</strong>'.$tt.'</div>';
     }
echo $obsah;     
?>