// Temperatur Grafik line plot 

var k="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*=";
var a1=0;
var a2=0;
var a3=0;
var a4=0;
var a5=0;
var a6=0;
var w1,w2,w3,w4,w5,w6;
var g;
var ec;
var xlg=300;

function f1(s)
{
   if (a1==0) w1=new Array(400);
   for (var i=0;i< s.length;i+=2) w1[a1++]=myParse(s.substring(i,i+2));
}
function f2(s)
{
   if (a2==0) w2=new Array(400);
   for (var i=0;i< s.length;i+=2) w2[a2++]=myParse(s.substring(i,i+2));
}
function f3(s)
{
   if (a3==0) w3=new Array(400);
   for (var i=0;i<s.length;i+=2) w3[a3++]=myParse(s.substring(i,i+2));
}
function f4(s)
{
   if (a4==0) w4=new Array(400);
   for (var i=0;i<s.length;i+=2) w4[a4++]=myParse(s.substring(i,i+2));
}
function f5(s)
{
   if (a5==0) w5=new Array(400);
   for (var i=0;i<s.length;i+=2) w5[a5++]=myParse(s.substring(i,i+2));
}

function myParse(s)
{
   var z=0;
   for(var i=0;i<s.length;i++)
   {
      z*=64;
      z+=k.indexOf(s.substring(i,i+1));
   }
   return z;
}

function eM(e)
{
   x=e.pageX;
   y=e.pageY;
   x-=ec.offsetLeft;
   y-=ec.offsetTop;
   if (y>=5 && y<=525 && x>20 && x<1172)
   {
      g.strokeStyle="#d720ee";
      g.strokeRect(x-5,y-5,10,10);
      t=395-y;
      t=Math.round((t/5.2)*10)/10;
      g.fillText(t+"°C",x-5,y+18);
      g.stroke();
   }
}

function close_tk(me,typ)
{
   var u=70;
   var m=0;
   ec=document.getElementById(me);
   g=ec.getContext("2d");
   ec.addEventListener("mousedown",eM,false);
   g.font="10px sans-serif";
   typ&=0x0F;
   g.strokeStyle="lightblue";
   g.fillText("°C",1,8);
   for (var y=31;y<525;y+=52)
   {
      mline(16.5,y+0.5,1155,0);
      g.fillText(u,1,y+2);
      u-=10;
   }
   u=0;
   if (typ==3 || typ==7)
      u=6;
   if (typ==4 || typ==8)
      u=12;
   if (typ==5 || typ==9)
      u=18;
   
   for (var x=19;x<1171;x+=48)
   {
      mline(x+0.5,5.5,0,523);
      if (typ>1)
      {
         g.fillText(u+":"+m,x-8,538);
         m+=15;
         if (m==60)
         {
            m=0;
            u++;
         }
      }
      else
      {
         g.fillText(u+":00",x-8,538);
         u++;
      }
   }
   g.strokeStyle="black";
   mline(1171.5,5.5,0,520);
   mline(19.5,5.5,0,520);
   mline(19.5,5.5,1152,0);
   mline(19.5,525.5,1152,0);
   g.font="12px sans-serif";
   g.strokeStyle="red";
   graf(a1,w1,typ);
   g.fillText("Vorlauf",xlg,553);
   xlg+=95;
   g.strokeStyle="orange";
   graf(a2,w2,typ);
   g.fillText("Rücklauf",xlg,553);
   xlg+=95;
   g.strokeStyle="black";
   graf(a3,w3,typ);
   g.fillText("Aussen",xlg,553);
   xlg+=95;
   g.strokeStyle="blue";
   graf(a4,w4,typ);
   g.fillText("WarmWasser",xlg,553);
   xlg+=95;
   g.strokeStyle="green";
   graf(a5,w5,typ);
   g.fillText("GwHaus",xlg,553);
}

function graf(ca,va,typ)
{
   var x=20;
   var w;
   var fi=1;
   if (typ<2)
   {
     if (ca>288)
       ca=288;
   }
   g.lineWidth=2;
   g.beginPath();
   for (var i=0;i<ca;i++)
   {
      w=va[i];
      if (w<4000)
      {
         w-=624;
         w*=0.325;
         var y=525-w;
         if (fi)
         {
            fi=0;
            g.moveTo(x,y);
         }
         else
           g.lineTo(x,y);
      }
      if (typ>1)
         x+=3.2;
      else
         x+=4;
   }
   g.stroke();
   g.lineWidth=3;
   mline(xlg,549.5,30,0);
   xlg+=35;
   g.lineWidth=1;
}

function mline(xs,ys,xlen,ylen)
{
   g.beginPath();
   g.moveTo(xs,ys);
   g.lineTo(xs+xlen,ys+ylen);
   g.stroke();
}
