Index: trunk/Version.php
===================================================================
--- trunk/Version.php	(revision 61)
+++ trunk/Version.php	(revision 62)
@@ -1,4 +1,4 @@
 <?php
 
-$Revision = 56;
-$ReleaseTime = strtotime('2015-02-20');
+$Revision = 62;
+$ReleaseTime = strtotime('2016-01-01');
Index: trunk/add.php
===================================================================
--- trunk/add.php	(revision 61)
+++ trunk/add.php	(revision 62)
@@ -7,8 +7,9 @@
   $MeasureId = addslashes($_GET['MeasureId']);
   $Value = addslashes($_GET['Value']);
-  $Measure = GetMeasureById($MeasureId);
-  if(gethostbyaddr($_SERVER['REMOTE_ADDR']) == $Measure['PermissionAdd']) 
+  $Measure = new Measure($Database);
+  $Measure->Load($MeasureId);
+  if(gethostbyaddr($_SERVER['REMOTE_ADDR']) == $Measure->Data['PermissionAdd'])
   {
-    AddValue($Measure, $Value);
+    $Measure->AddValue($Value);
     echo('Hodnota '.$Value.' uložena');
   } else echo('Nemáte oprávnění k aktualizaci zadané veličiny!<br>Vaše adresa: '.gethostbyaddr($_SERVER['REMOTE_ADDR']).'('.$_SERVER['REMOTE_ADDR'].')');
Index: trunk/global.php
===================================================================
--- trunk/global.php	(revision 61)
+++ trunk/global.php	(revision 62)
@@ -31,4 +31,10 @@
 {
   return(date('j.n.Y', $Time));
+}
+
+function GetMicrotime()
+{
+  list($Usec, $Sec) = explode(" ", microtime());
+  return ((float)$Usec + (float)$Sec);
 }
 
Index: trunk/graph.php
===================================================================
--- trunk/graph.php	(revision 61)
+++ trunk/graph.php	(revision 62)
@@ -12,5 +12,5 @@
 if($EndTime < $StartTime) $EndTime = $StartTime + 60;
 $TimeDifference = $EndTime - $StartTime;
-if(!array_key_exists('Measure',$_GET)) die('Musíte zadat měřenou veličinu');
+if(!array_key_exists('Measure', $_GET)) die('Musíte zadat měřenou veličinu');
 $MeasureId = addslashes($_GET['Measure']);
 if(!array_key_exists('Width', $_GET)) $Width = $DefaultWidth;
@@ -24,11 +24,17 @@
 $StopWatchStart = GetMicrotime();
 
-$Measure = GetMeasureById($MeasureId);
+$Measure = new Measure($Database);
+$Measure->Load($MeasureId);
+$Measure->DivisionCount = $Config['DivisionCount'];
+$Measure->LevelReducing = $Config['LevelReducing'];
+$Measure->MaxLevel = $Config['MaxLevel'];
+$Measure->ReferenceTime = $Config['ReferenceTime'];
+$Measure->Differential = $Differential;
 
-$Level = floor(log(($EndTime - $StartTime) / $DivisionCount / 60) / log($LevelReducing)) - 1;
+$Level = floor(log(($EndTime - $StartTime) / $Measure->DivisionCount / 60) / log($Measure->LevelReducing)) - 1;
 if($Level < 0) $Level = 0;
-if($Level > $MaxLevel) $Level = $MaxLevel;
+if($Level > $Measure->MaxLevel) $Level = $Measure->MaxLevel;
 
-$Points = GetValues($Measure, $StartTime, $EndTime, $Level);
+$Points = $Measure->GetValues($StartTime, $EndTime, $Level);
 
 if($Debug) echo('Points count: '.count($Points).'<br>');
@@ -56,16 +62,16 @@
 
 // Generate polygon and recalculate y values to fit graph height
-$PointsMin = array(0, $Height-1);
-$PointsAvg = array(0, $Height-1);
-$PointsMax = array(0, $Height-1);
+$PointsMin = array(0, $Height - 1);
+$PointsAvg = array(0, $Height - 1);
+$PointsMax = array(0, $Height - 1);
 if(($MaxValue - $MinValue) == 0) $MaxValue = $MinValue + 1;
 {
   foreach($Points as $Index => $Item)
   {
-    $PointsMin[] = $Index*$Width/$DivisionCount;
+    $PointsMin[] = $Index * $Width / $Measure->DivisionCount;
     $PointsMin[] = $Height - 1 - ($Points[$Index]['min'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
-    $PointsAvg[] = $Index*$Width/$DivisionCount;
+    $PointsAvg[] = $Index * $Width / $Measure->DivisionCount;
     $PointsAvg[] = $Height - 1 - ($Points[$Index]['avg'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
-    $PointsMax[] = $Index*$Width/$DivisionCount;
+    $PointsMax[] = $Index * $Width / $Measure->DivisionCount;
     $PointsMax[] = $Height - 1 - ($Points[$Index]['max'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
     //echo($Index.' - '.$Item.' '.$Points[$Index].'<br>');
@@ -90,6 +96,4 @@
 //$Points[] = $Width - 1;
 //$Points[] = $Height - 1;
-
-//print_r($PointsMax);
 
 // Generate image
@@ -124,5 +128,5 @@
   $MinorTimeMarks = $TimeMarks[$TimeMarksIndex - 2];
 
-  $TimeShift = AlignTime($StartTime, $MajorTimeMarks) - $StartTime;
+  $TimeShift = $Measure->AlignTime($StartTime, $MajorTimeMarks) - $StartTime;
   //imagestring($Image, 10, 40, 50, $TimeShift, $Black);
 
@@ -136,5 +140,5 @@
   }
 
-  $TimeShift = AlignTime($StartTime, $MinorTimeMarks) - $StartTime;
+  $TimeShift = $Measure->AlignTime($StartTime, $MinorTimeMarks) - $StartTime;
 
   // Zobraz měřítko X
@@ -161,5 +165,6 @@
     $Y = $Height - 1 - ($VerticalLinesDistance * $I);
     //$Y = $Height - 1 - ($VerticalLinesDistance * $I / ($MaxValue - $MinValue) * $K * $Height);
-    $Text = $PrefixMultiplier->Add(round(($I * $VerticalLinesDistance / $Height / $K * ($MaxValue - $MinValue) + $MinValue)), $Measure['Unit'], 3);
+    $Text = $PrefixMultiplier->Add(round(($I * $VerticalLinesDistance / $Height /
+      $K * ($MaxValue - $MinValue) + $MinValue)), $Measure->Data['Unit'], 3);
     $BoundBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
     if(($Y - ($BoundBox[5] - $BoundBox[1]) / 2) > 10)
@@ -169,15 +174,15 @@
 
   $Left = $Width - 10;
-  $Text = "    Max. ".$PrefixMultiplier->Add($MaxValue, $Measure['Unit']);
+  $Text = "    Max. ".$PrefixMultiplier->Add($MaxValue, $Measure->Data['Unit']);
   $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
   $Left -= ($BoundingBox[2] - $BoundingBox[0]);
   imagettftext($Image, $FontSize, 0, $Left, 14, $Red, $FontFile, $Text);
 
-  $Text = "    Avg.  ".$PrefixMultiplier->Add($AvgValue, $Measure['Unit'], 4);
+  $Text = "    Avg.  ".$PrefixMultiplier->Add($AvgValue, $Measure->Data['Unit'], 4);
   $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
   $Left -= ($BoundingBox[2] - $BoundingBox[0]);
   imagettftext($Image, $FontSize, 0, $Left, 14, $Green, $FontFile, $Text);
 
-  $Text = "    Min.  ".$PrefixMultiplier->Add($MinValue, $Measure['Unit']);
+  $Text = "    Min.  ".$PrefixMultiplier->Add($MinValue, $Measure->Data['Unit']);
   $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
   $Left -= ($BoundingBox[2] - $BoundingBox[0]);
@@ -186,5 +191,5 @@
   //imagestring($Image, 2, 50, 30, 'Level: '.$Level, $Black);
 
-  imagettftext($Image, $FontSize, 0, 70, 14, $Black, $FontFile, $Measure['Description']);
+  imagettftext($Image, $FontSize, 0, 70, 14, $Black, $FontFile, $Measure->Data['Description']);
   imagerectangle($Image, 0, 0, $Width - 1, $Height - 1, $Black);   // Frame border
   imagepng($Image);
Index: trunk/stat_functions.php
===================================================================
--- trunk/stat_functions.php	(revision 61)
+++ trunk/stat_functions.php	(revision 62)
@@ -1,86 +1,99 @@
 <?php
 
-$ValueTypes = array('min', 'avg', 'max');
-
-function GetMicrotime()
+include_once('Point.php');
+
+class Measure
 {
-    list($Usec, $Sec) = explode(" ",microtime());
-    return ((float)$Usec + (float)$Sec);
-}
-
-function TimeSegment($Level)
-{
-  global $LevelReducing;
-  return(pow($LevelReducing, $Level) * 60);
-}
-
-function StatTableName($Level)
-{
-  if($Level == 0) return('data');
-    else return('data_cache');
-}
-
-function AlignTime($Time, $TimeSegment)
-{
-  global $ReferenceTime, $LevelReducing;
-  return(round(($Time - $ReferenceTime) / $TimeSegment) * $TimeSegment + $ReferenceTime);
-}
-
-function GetMeasureById($Id)
-{
-  global $Database;
-
-  $Result = $Database->select('measure', '*', 'Id='.$Id);
-  if($Result->num_rows > 0)
-  {
-    $Measure = $Result->fetch_array();
-    if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
-      else $Measure['ContinuityEnabled'] = 2;    // continuous graph
-  } else die('Měřená veličina nenalezena');
-  return($Measure); 
-}
-
-function AddValue($Measure, $Value)
-{
-  global $LevelReducing, $MaxLevel, $Database;
-
-  $Time = time();
-  //$Value = round($Measure['divider'] * $Value);
-
-  $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level=0 ORDER BY time DESC LIMIT 2');
-  if($Result->num_rows == 0) $Database->insert($Measure['DataTable'], 
-    array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 
-    'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 0));
-  else if($Result->num_rows == 1) $Database->insert($Measure['DataTable'], array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
-    'continuity' => 1));
-  else {
-    $LastValue = $Result->fetch_array();
-    $NextToLastValue = $Result->fetch_array();
-    if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['Period']) 
-    {
-    }
-    else
-    {
-      if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $Measure['Period']) $Continuity = 1; 
-        else $Continuity = 0;
-      if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and 
-        ($LastValue['continuity'] == 1) and ($Continuity == 1)) 
-      {
-        $Database->update($Measure['DataTable'], '(time="'.$LastValue['time'].'") AND '.
-        '(level=0) AND (measure='.$Measure['Id'].')', array('time' => TimeToMysqlDateTime($Time)));
-      } else 
-      {
-        $Database->insert($Measure['DataTable'], array('min' => $Value, 
-          'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
-          'continuity' => $Continuity));
-      }
-    }
-  }
+  var $Data;
+  var $ValueTypes;
+  var $LevelReducing;
+  var $Database;
+  var $MaxLevel;
+  var $ReferenceTime;
+  var $Differential;
+  var $DivisionCount;
+
+  function __construct($Database)
+  {
+    $this->Id = 0;
+    $this->ValueTypes = array('min', 'avg', 'max');
+    $this->Database = &$Database;
+    $this->LevelReducing = 5;
+    $this->MaxLevel = 4;
+    $this->ReferenceTime = 0;
+    $this->Differential = 0;
+    $this->DivisionCount = 500;
+  }
+
+  function TimeSegment($Level)
+  {
+    return(pow($this->LevelReducing, $Level) * 60);
+  }
+
+  function StatTableName($Level)
+  {
+    if($Level == 0) return('data');
+      else return('data_cache');
+  }
+
+  function AlignTime($Time, $TimeSegment)
+  {
+    return(round(($Time - $this->ReferenceTime) / $TimeSegment) * $TimeSegment +
+      $this->ReferenceTime);
+  }
+
+  function Load($Id)
+  {
+    $Result = $this->Database->select('measure', '*', 'Id='.$Id);
+    if($Result->num_rows > 0)
+    {
+      $this->Data = $Result->fetch_array();
+      if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
+        else $this->Data['ContinuityEnabled'] = 2;    // continuous graph
+    } else die('Měřená veličina nenalezena');
+  }
+
+  function AddValue($Value)
+  {
+    $Time = time();
+
+    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
+      '(`level`=0) ORDER BY `time` DESC LIMIT 2');
+    if($Result->num_rows == 0) $this->Database->insert($this->Data['DataTable'],
+      array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
+      'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 0));
+    else if($Result->num_rows == 1) $this->Database->insert($this->Data['DataTable'],
+      array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
+      'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
+      'continuity' => 1));
+    else {
+      $LastValue = $Result->fetch_array();
+      $NextToLastValue = $Result->fetch_array();
+      if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $this->Data['Period'])
+      {
+      }
+      else
+      {
+        if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $this->Data['Period']) $Continuity = 1;
+          else $Continuity = 0;
+        if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and
+          ($LastValue['continuity'] == 1) and ($Continuity == 1))
+        {
+          $this->Database->update($this->Data['DataTable'], '(time="'.$LastValue['time'].'") AND '.
+          '(level=0) AND (measure='.$this->Data['Id'].')', array('time' => TimeToMysqlDateTime($Time)));
+        } else
+        {
+          $this->Database->insert($this->Data['DataTable'], array('min' => $Value,
+            'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
+            'continuity' => $Continuity));
+        }
+      }
+    }
 
     // Update levels
-    for($Level = 1; $Level <= $MaxLevel; $Level++)
-    {
-      $TimeSegment = TimeSegment($Level);
-      $EndTime = AlignTime($Time, $TimeSegment);
+    for($Level = 1; $Level <= $this->MaxLevel; $Level++)
+    {
+      $TimeSegment = $this->TimeSegment($Level);
+      $EndTime = $this->AlignTime($Time, $TimeSegment);
       //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
       $StartTime = $EndTime - $TimeSegment;
@@ -88,6 +101,7 @@
       // Load values in time range
       $Values = array();
-      $Result = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.
-        TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).' ORDER BY time');
+      $Result = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.TimeToMysqlDateTime($StartTime).'") AND '.
+        '(`time` < "'.TimeToMysqlDateTime($EndTime).'") AND '.
+        '(`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`');
       while($Row = $Result->fetch_array())
       {
@@ -95,346 +109,335 @@
         $Values[] = $Row;
       }
-      //array_pop($Values);      
+      //array_pop($Values);
 
       // Load subsidary values
-      $Values = array_merge(LoadLeftSideValue($Level-1, $Measure, $StartTime), $Values, LoadRightSideValue($Level-1, $Measure, $EndTime));
-
-      $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level);
-
-      $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND 
-        (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['Id'].' AND level='.$Level);
-      $Continuity = $Values[1]['continuity'];   
-      $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'], 
-        'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime+($EndTime-$StartTime)/2)));
-
-    }
-}
-
-function Interpolation($X1, $Y1, $X2, $Y2, $X)
-{
-  $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1;
-  return($Y);
-}
-
-/* Compute one value for upper time level from multiple values */
-function ComputeOneValue($LeftTime, $RightTime, $Values, $Measure, $Level)
-{
-  global $ValueTypes, $Differential;
-
-  $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000); 
-
-  // Trim outside parts
-  foreach($ValueTypes as $ValueType)
-  {
-    $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], 
-      $Values[1]['time'], $Values[1][$ValueType], $LeftTime);
-  }
-  $Values[0]['time'] = $LeftTime;
-  foreach($ValueTypes as $ValueType)
-  {
-    $Values[count($Values) - 1][$ValueType] = Interpolation($Values[count($Values) - 2]['time'], 
-      $Values[count($Values) - 2][$ValueType], 
-      $Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType], $RightTime);
-  }
-  $Values[count($Values) - 1]['time'] = $RightTime;
-
-  // Perform computation
-  foreach($ValueTypes as $ValueType)
-  {
-    // Compute new value
-    for($I = 0; $I < (count($Values) - 1); $I++)
-    {
-      if($ValueType == 'avg') 
-      {
-        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']);
-          else if($Differential == 0)
+      $Values = array_merge(
+        $this->LoadLeftSideValue($Level - 1, $StartTime),
+        $Values,
+        $this->LoadRightSideValue($Level - 1, $EndTime));
+
+      $Point = $this->ComputeOneValue($StartTime, $EndTime, $Values, $Level);
+
+      $this->Database->delete($this->Data['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
+        (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$this->Data['Id'].' AND level='.$Level);
+      $Continuity = $Values[1]['continuity'];
+      $this->Database->insert($this->Data['DataTable'], array('level' => $Level, 'measure' => $this->Data['Id'], 'min' => $Point['min'],
+        'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity,
+        'time' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2)));
+
+    }
+  }
+
+  /* Compute one value for upper time level from multiple values */
+  function ComputeOneValue($LeftTime, $RightTime, $Values, $Level)
+  {
+    $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000);
+
+    // Trim outside parts
+    foreach($this->ValueTypes as $ValueType)
+    {
+      $Values[0][$ValueType] = Interpolation(NewPoint($Values[0]['time'], $Values[0][$ValueType]),
+        NewPoint($Values[1]['time'], $Values[1][$ValueType]), $LeftTime);
+    }
+    $Values[0]['time'] = $LeftTime;
+    foreach($this->ValueTypes as $ValueType)
+    {
+      $Values[count($Values) - 1][$ValueType] = Interpolation(NewPoint($Values[count($Values) - 2]['time'],
+        $Values[count($Values) - 2][$ValueType]),
+        NewPoint($Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType]),
+        $RightTime);
+    }
+    $Values[count($Values) - 1]['time'] = $RightTime;
+
+    // Perform computation
+    foreach($this->ValueTypes as $ValueType)
+    {
+      // Compute new value
+      for($I = 0; $I < (count($Values) - 1); $I++)
+      {
+        if($ValueType == 'avg')
+        {
+          if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled']);
+            else if($this->Differential == 0)
+            {
+              $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
+                (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
+            } else {
+              $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
+               (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2);
+            }
+        }
+        else if($ValueType == 'max')
+        {
+          if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled'])
           {
-            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
-              (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
-          } else { 
-            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
-             (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2);
+            if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
           }
-      }
-      else if($ValueType == 'max')
-      {
-        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
-        {
-          if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
-        }
-        else
-        {
-          if($Differential == 0)
+          else
           {
-            if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
+            if($this->Differential == 0)
+            {
+              if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
+            } else {
+              $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
+              if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
+            }
+          }
+        }
+        else if($ValueType == 'min')
+        {
+          if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled'])
+          {
+            if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
           } else {
-            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; 
-            if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
+            if($this->Differential == 0)
+            {
+              if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
+            } else {
+              $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
+              if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
+            }
           }
         }
       }
-      else if($ValueType == 'min') 
-      {
-        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
-        {
-          if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
-        } else {
-          if($Differential == 0)
-          {
-            if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
-          } else {
-            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; 
-            if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
-          }
-        }
-      }
-    }
-    $NewValue[$ValueType] = $NewValue[$ValueType];
-  }
-  //if(($RightTime - $LeftTime) > 0) 
-  if($Measure['Cumulative'] == 0)
-  {
-    $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
-  }
-  return($NewValue);
-}
-
-function GetTimeRange($Measure, $Level)
-{
-  global $Debug, $Database;
-
-  // Get first and last time
-  $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
-  if($Result->num_rows > 0)
-  {
-    $Row = $Result->fetch_array();
-    $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']);
-  } else $AbsoluteLeftTime = 0;
-
-  $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
-  if($Result->num_rows > 0)
-  {
-    $Row = $Result->fetch_array();
-    $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']);
-  } else $AbsoluteRightTime = 0;
-
-  if($Debug) 
-  {
-    echo('AbsoluteLeftTime: '.$AbsoluteLeftTime.'('.TimeToMysqlDateTime($AbsoluteLeftTime).')<br>');
-    echo('AbsoluteRightTime: '.$AbsoluteRightTime.'('.TimeToMysqlDateTime($AbsoluteRightTime).')<br>');
-  }
-  return(array('left' => $AbsoluteLeftTime, 'right' => $AbsoluteRightTime));
-}
-
-function LoadRightSideValue($Level, $Measure, $Time)
-{
-  global $Debug, $Database;
-  
-  $Result = array();
-  $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1');
-  if($DbResult->num_rows > 0) 
-  {
-    $Row = $DbResult->fetch_array();
-    $Row['time'] = MysqlDateTimeToTime($Row['time']);
-    return(array($Row));
-  } 
-  else 
-  {
-    //$Time = $Values[count($Values)-1]['time'] + 60;
-    //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
-    $Result[] = array('time' => ($Time + TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
-    $DbResult = $Database->select($Measure['DataTable'], '*', '(time < "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time DESC LIMIT 1');
+      $NewValue[$ValueType] = $NewValue[$ValueType];
+    }
+    //if(($RightTime - $LeftTime) > 0)
+    if($this->Data['Cumulative'] == 0)
+    {
+      $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
+    }
+    return($NewValue);
+  }
+
+  function GetTimeRange($Level)
+  {
+    // Get first and last time
+    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
+      '(`level`='.$Level.') ORDER BY `time` LIMIT 1');
+    if($Result->num_rows > 0)
+    {
+      $Row = $Result->fetch_array();
+      $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']);
+    } else $AbsoluteLeftTime = 0;
+
+    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
+      '(`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
+    if($Result->num_rows > 0)
+    {
+      $Row = $Result->fetch_array();
+      $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']);
+    } else $AbsoluteRightTime = 0;
+
+    return(array('left' => $AbsoluteLeftTime, 'right' => $AbsoluteRightTime));
+  }
+
+  function LoadRightSideValue($Level, $Time)
+  {
+    $Result = array();
+    $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$this->Data['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1');
     if($DbResult->num_rows > 0)
     {
       $Row = $DbResult->fetch_array();
-      array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time'])+10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 
-    } 
-   // if($Debug) print_r($Result);
-    return($Result);
-  }
-}
-
-function LoadLeftSideValue($Level, $Measure, $Time)
-{
-  global $Debug, $Database;
-
-  $Result = array();
-  $DbResult = $Database->select($Measure['DataTable'], '*', '(time < "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time DESC LIMIT 1');
-  if($DbResult->num_rows > 0) 
-  {
-    $Row = $DbResult->fetch_array();
-    $Row['time'] = MysqlDateTimeToTime($Row['time']);
-    return(array($Row));
-  } else {
-    //$Time = $Values[0]['time'] - 60;
-    //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
-    $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
-
-    $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1');
+      $Row['time'] = MysqlDateTimeToTime($Row['time']);
+      return(array($Row));
+    }
+    else
+    {
+      //$Time = $Values[count($Values)-1]['time'] + 60;
+      //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
+      $Result[] = array('time' => ($Time + $this->TimeSegment($Level)), 'min' => 0,
+        'avg' => 0, 'max' => 0, 'continuity' => 0);
+      $DbResult = $this->Database->select($this->Data['DataTable'], '*',
+        '(`time` < "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
+        'AND (`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
+      if($DbResult->num_rows > 0)
+      {
+        $Row = $DbResult->fetch_array();
+        array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time']) + 10),
+          'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
+      }
+      return($Result);
+    }
+  }
+
+  function LoadLeftSideValue($Level, $Time)
+  {
+    $Result = array();
+    $DbResult = $this->Database->select($this->Data['DataTable'], '*',
+      '(`time` < "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
+      'AND (`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
     if($DbResult->num_rows > 0)
     {
       $Row = $DbResult->fetch_array();
-      array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time']) - 10), 
-        'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
-    }
-    return($Result);
-  }
-}
-
-function GetValues($Measure, $TimeFrom, $TimeTo, $Level)
-{
-  global $DivisionCount, $Debug, $Database;
-
-  if($Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')<br>');
-  if($Debug) echo('TimeTo: '.$TimeTo.'('.TimeToMysqlDateTime($TimeTo).')<br>');
-
-  //$AbsoluteTime = GetTimeRange($MeasureId);
-
-//  if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and 
+      $Row['time'] = MysqlDateTimeToTime($Row['time']);
+      return(array($Row));
+    } else {
+      //$Time = $Values[0]['time'] - 60;
+      //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
+      $Result[] = array('time' => ($Time - $this->TimeSegment($Level)), 'min' => 0,
+        'avg' => 0, 'max' => 0, 'continuity' => 0);
+
+      $DbResult = $this->Database->select($this->Data['DataTable'], '*',
+        '(`time` > "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
+        'AND (`level`='.$Level.') ORDER BY `time` ASC LIMIT 1');
+      if($DbResult->num_rows > 0)
+      {
+        $Row = $DbResult->fetch_array();
+        array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time']) - 10),
+          'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
+      }
+      return($Result);
+    }
+  }
+
+  function GetValues($TimeFrom, $TimeTo, $Level)
+  {
+  //$AbsoluteTime = GetTimeRange($this->DataId);
+
+//  if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
 //    ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime))
 //  {
 
-  // Load values in time range
-  $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.
-    TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time');
-  $Values = array();
-  while($Row = $Result->fetch_array())
-  {
-    $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']);
-  }
- // array_pop($Values);
-  if($Debug) echo('Item count: '.count($Values));
-
-  $Points = array();
-  if(count($Values) > 0)
-  {
-    $Values = array_merge(LoadLeftSideValue($Level, $Measure, $TimeFrom), $Values, LoadRightSideValue($Level, $Measure, $TimeTo));
-
-    $StartIndex = 0;
+    // Load values in time range
+    $Result = $this->Database->select($this->Data['DataTable'], '`time`, `min`, `avg`, `max`, `continuity`',
+      '(`time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '.
+      '(`time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '.
+      '(`measure`='.$this->Data['Id'].') AND (`level`='.$Level.') ORDER BY `time`');
+    $Values = array();
+    while($Row = $Result->fetch_array())
+    {
+      $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']);
+    }
+   // array_pop($Values);
+
     $Points = array();
-    for($I = 0; $I < $DivisionCount; $I++)
-    {
-      $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * $I;
-      //if($Debug) echo('TimeStart '.$I.': '.$TimeStart.'('.TimeToMysqlDateTime($TimeStart).')<br>');
-      $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * ($I+1);
-      //if($Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>');
-
-      $EndIndex = $StartIndex;
-      while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
-      $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
-      $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level);
-      $StartIndex = $EndIndex - 1;  
-    }
-  } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0);
-  return($Points);
-}
-
-function RebuildMeasureCache($Measure)
-{
-  global $MaxLevel, $LevelReducing, $Database;
-
-  echo('Velicina '.$Measure['Name']."<br>\n");
-  if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
-    else $Measure['ContinuityEnabled'] = 2;    // continuous graph
-
-  // Clear previous items
-  $DbResult = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level>0 AND measure='.$Measure['Id']);
-  $Row = $DbResult->fetch_array();
-  echo("Mazu starou cache (".$Row[0]." polozek)...");
-  $Database->delete($Measure['DataTable'], 'level>0 AND measure='.$Measure['Id']);
-  echo("<br>\n");
-
-  for($Level=1; $Level <= $MaxLevel; $Level++)
-  {
-    echo('Uroven '.$Level."<br>\n");
-    $TimeRange = GetTimeRange($Measure, $Level-1);
-    $TimeSegment = TimeSegment($Level);
-    $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
-    $EndTime = AlignTime($TimeRange['right'], $TimeSegment);
-    $BurstCount = 500; 
-    echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
-    for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
-    {
-      echo($I.' ');
-      $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
-      $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
-      $Values = array();
-      $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($StartTime2).'") AND (time < "'.
-        TimeToMysqlDateTime($EndTime2).'") AND (measure='.$Measure['Id'].') AND (level='.($Level - 1).') ORDER BY time');
-      while($Row = $DbResult->fetch_array())
-      {
-        $Row['time'] = MysqlDateTimeToTime($Row['time']);
-        $Values[] = $Row;
-      }
-
-      if(count($Values) > 0)
-      {
-        $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime2));
-
-        $StartIndex = 0;
-        for($B = 0; $B < $BurstCount; $B++)
-        {
-          echo('.');
-          $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
-          $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
-
-          $EndIndex = $StartIndex;
-          while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
-          $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
-         if(count($SubValues) > 2)
+    if(count($Values) > 0)
+    {
+      $Values = array_merge(
+        $this->LoadLeftSideValue($Level, $TimeFrom),
+        $Values,
+        $this->LoadRightSideValue($Level, $TimeTo));
+
+      $StartIndex = 0;
+      $Points = array();
+      for($I = 0; $I < $this->DivisionCount; $I++)
+      {
+        $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * $I;
+        $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I+1);
+
+        $EndIndex = $StartIndex;
+        while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
+        $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
+        $Points[] = $this->ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Level);
+        $StartIndex = $EndIndex - 1;
+      }
+    } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0);
+    return($Points);
+  }
+
+  function RebuildMeasureCache()
+  {
+    echo('Velicina '.$this->Data['Name']."<br>\n");
+    if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
+      else $this->Data['ContinuityEnabled'] = 2;    // continuous graph
+
+    // Clear previous items
+    $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', '(`level`>0) AND (`measure`='.$this->Data['Id'].')');
+    $Row = $DbResult->fetch_array();
+    echo("Mazu starou cache (".$Row[0]." polozek)...");
+    $this->Database->delete($this->Data['DataTable'], '(`level`>0) AND (`measure`='.$this->Data['Id'].')');
+    echo("<br>\n");
+
+    for($Level = 1; $Level <= $this->MaxLevel; $Level++)
+    {
+      echo('Uroven '.$Level."<br>\n");
+      $TimeRange = $this->GetTimeRange($Level - 1);
+      $TimeSegment = $this->TimeSegment($Level);
+      $StartTime = $this->AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
+      $EndTime = $this->AlignTime($TimeRange['right'], $TimeSegment);
+      $BurstCount = 500;
+      echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
+      for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
+      {
+        echo($I.' ');
+        $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
+        $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
+        $Values = array();
+        $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.TimeToMysqlDateTime($StartTime2).'") AND (`time` < "'.
+          TimeToMysqlDateTime($EndTime2).'") AND (`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`');
+        while($Row = $DbResult->fetch_array())
+        {
+          $Row['time'] = MysqlDateTimeToTime($Row['time']);
+          $Values[] = $Row;
+        }
+
+        if(count($Values) > 0)
+        {
+          $Values = array_merge(
+            $this->LoadLeftSideValue($Level - 1, $StartTime2),
+            $Values,
+            $this->LoadRightSideValue($Level - 1, $EndTime2));
+
+          $StartIndex = 0;
+          for($B = 0; $B < $BurstCount; $B++)
           {
-            $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level);
-            $Continuity = $SubValues[1]['continuity'];
-            $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 
-              'min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max'], 
-              'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
+            echo('.');
+            $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
+            $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
+
+            $EndIndex = $StartIndex;
+            while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
+            $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
+            if(count($SubValues) > 2)
+            {
+              $Point = $this->ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Level);
+              $Continuity = $SubValues[1]['continuity'];
+              $this->Database->insert($this->Data['DataTable'], array('level' => $Level, 'measure' => $this->Data['Id'],
+                'min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max'],
+                'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
+            }
+            $StartIndex = $EndIndex - 1;
           }
-          $StartIndex = $EndIndex - 1;
-        }
-      }
-      // Load values in time range
-      //array_pop($NextValues);
-    }
-    echo("Uroven dokoncena<br>\n");
-    $DbResult = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level='.$Level.' AND measure='.$Measure['Id']);
-    $Row = $DbResult->fetch_array();
-    echo("Vlozeno ".$Row[0]." polozek.<br>\n");
-  }
-}
-
-function RebuildAllMeasuresCache()
-{
-  global $Database;
-
-  // Load measures
-  $Measures = array();
-  $Result = $Database->select('measure', '*');
-  while($Measures[] = $Result->fetch_array());
-    array_pop($Measures);
-
-  foreach($Measures as $Measure)
-  {
-    RebuildMeasureCache($Measure);
-    echo('Velicina dokoncena<br/>');
-  }
-}
-
-function InitMeasureDataTable($Measure)
-{
-  global $Database;
-
-  $Database->query('CREATE TABLE `data_'.$Measure['Name'].'` (
+        }
+        // Load values in time range
+        //array_pop($NextValues);
+      }
+      echo("Uroven dokoncena<br>\n");
+      $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)',
+        '(`level`='.$Level.') AND (`measure`='.$this->Data['Id'].')');
+      $Row = $DbResult->fetch_array();
+      echo("Vlozeno ".$Row[0]." polozek.<br>\n");
+    }
+  }
+
+  function RebuildAllMeasuresCache()
+  {
+    $Result = $this->Database->select('measure', 'Id');
+    while($Row = $Result->fetch_array())
+    {
+      $Measure = new Measure();
+      $Measure->Load($Row['Id']);
+      $Measure->RebuildMeasureCache();
+      echo('Velicina id '.$Row['Id'].' dokoncena<br/>');
+    }
+  }
+
+  function InitMeasureDataTable()
+  {
+    $this->Database->query('CREATE TABLE `data_'.$this->Data['Name'].'` (
+      `Time` TIMESTAMP NOT NULL ,
+      `Avg` '.$this->Data['DataType'].' NOT NULL ,
+      `Continuity` BOOL NOT NULL
+      ) ENGINE = MYISAM ;');
+
+    $this->Database->query('CREATE TABLE `data_'.$this->Data['Name'].'_cache` (
 `Time` TIMESTAMP NOT NULL ,
-`Avg` '.$Measure['DataType'].' NOT NULL ,
+`Level` TINYINT NOT NULL ,
+`Min` '.$this->Data['DataType'].' NOT NULL ,
+`Avg` '.$this->Data['DataType'].' NOT NULL ,
+`Max` '.$this->Data['DataType'].' NOT NULL ,
 `Continuity` BOOL NOT NULL
 ) ENGINE = MYISAM ;');
-
-  $Database->query('CREATE TABLE `data_'.$Measure['Name'].'_cache` (
-`Time` TIMESTAMP NOT NULL ,
-`Level` TINYINT NOT NULL ,
-`Min` '.$Measure['DataType'].' NOT NULL ,
-`Avg` '.$Measure['DataType'].' NOT NULL ,
-`Max` '.$Measure['DataType'].' NOT NULL ,
-`Continuity` BOOL NOT NULL
-) ENGINE = MYISAM ;');
+  }
 }
