/**
 * Neuron Frame Work
 * for Golem OEE MES
 * www.neuron.com.pl
 */

'use strict';

function _SetImg(link){
    var img= new Image();
    img.src=link;
    return img
}

var imgzc0= _SetImg('../mes_lib/zc0.png');
var imgzc1= _SetImg('../mes_lib/zc1.png');
var imgzc2= _SetImg('../mes_lib/zc2.png');
var imgzc3= _SetImg('../mes_lib/zc3.png');
var imgzc4= _SetImg('../mes_lib/zc4.png');
var imgzc5= _SetImg('../mes_lib/zc5.png');
var imgzc=[imgzc0,imgzc1,imgzc2,imgzc3,imgzc4,imgzc5  ];


/***********************************************************

 /**********************************************************/



/***********************************************************
 Zestaw prototypów kontrolek wyświetlających na głównym CANVAS widoku
 dane o obiektach z kolekcji mes_data.
 Większość funkcji posiada argument nrg który określa numer pozycji w kolekcji ( tabeli )
 mes_data.

 /**********************************************************/

MES_View_constructor.prototype.drawClock=function (x,y,styl){
    this.cx.frame(x,y,100,35,1, this.css.bl(), this.css.bg() );
    this.cx.dtext(x+10,y+4 ,34, this.css.fcoff(),ktime.TimeStrNow().substring(0,5) );
};

//// STATUS

// ikona statusu isize-rozmiar; back-rysule tło ( frame); mono-kolor wg czcionki, nie statusu
MES_View_constructor.prototype.status_ico=function(nrg,x,y,isize,back,mono) {
    if (back) {   this.cx.frame(x, y, isize, isize, 1, this.css.bl())   }
    var sc=mes_data.stat_color_chart(nrg);
    if (mono){sc=this.css.fc()}
    if (this.css.hud) {
        var lc=3;
        if (mes_data.stat_nr(nrg) == 5){lc=canvas_flash.loop4s }
        this.cx.drawIcoEx(x, y, mes_data.stat_ico_code(nrg), isize, sc, lc)
    } else {

        var bf = true;
        if (mes_data.stat_nr(nrg) == 5) {   bf = canvas_flash.f1s  }
        if (bf) {   this.cx.drawIco(x, y, mes_data.stat_ico_code(nrg), isize, sc)   }
    }
};

// rysuje prostokąt z kolorem statusu
MES_View_constructor.prototype.status_rect=function(nrg,x,y,w,h,dark){
    var cx=this.cx;
    var sc= mes_data.stat_color(nrg);
    if (dark){ sc= mes_data.stat_color_chart(nrg); }
    cx.frame(x, y, w, h, 1, sc, sc);
};

MES_View_constructor.prototype.status_chart=function(nr,nrc, x,y,bh,bw,bsp,sep,fs ,anim, animnr){
    var cx=this.cx;
    var j=0;
    var xx=x;
    var sdata=  mes_data.stat_chart_data(nr, nrc);

    var b=false;
    var bwc='red';
    var bwb=(11*(bw+bsp))+sep-bsp ;
    for (j = 0; j < 10; ++j) {
        if (b){bwc= this.css.bgc1() } else {bwc= this.css.bgc2() }
        cx.box(x,y+(j*(bh/10)),bwb ,bh/10,bwc);
        b=!b;
    }

    for (j = 0; j < 11; ++j) {
        cx.cv.beginPath();
        var pp=sdata[j];
        if (this.css.hud && anim ) { pp= canvas_flash.aVal(animnr,pp); }
        cx.barVB(  xx,   y, bw  , bh , pp , 'white', state_colors_chart[j] );
        cx.dtext(xx,y+bh+2 ,fs,this.css.fc(),lang[90+j]);
        if(sdata[j]!=0)  {cx.dtext(xx,y+bh+2+fs ,fs*0.7,this.css.fc(),sdata[j])}
        xx=xx+bw+bsp;
        if (j==5){ xx=xx+sep }
    }
};


MES_View_constructor.prototype.status_chart_fx=function(nr,nrc, x,y,step,fs, legend ,anim, animnr){
    var cx=this.cx;
    var css= this.css;
    //var step=bh/3;
    var bh=step*3;
    var rx=0;
    var yp=bh/2+y;
    var xx=x;
    var j=0;
    var sdata=  mes_data.stat_chart_data(nr, nrc);

    cx.lineH(x-(step/2),x+(11*step),yp,1,css.fc());

    for (j = 0; j < 11; ++j) {
        cx.cv.beginPath();
        var pp=sdata[j];
        if (this.css.hud && anim ) { pp= canvas_flash.aVal(animnr,pp); }
        if (pp>100){pp=100}
        rx= (step/100)*pp;
        if (rx>0 && rx<4){rx=4}

        if (j==5&& pp>30 && canvas_flash.f1s  ){ cx.circle_l(xx,yp,rx+5,this.css.fc(),2) }
        if (j==8&& pp>20 && canvas_flash.f1s  ){ cx.circle_l(xx,yp,rx+5,this.css.fc(),2) }
        if (pp!=0){ cx.circle(xx,yp,rx,state_colors_chart[j],'#4C4C4C',1)} else {cx.lineV(xx,yp+5,yp-5,1,css.fc())}

        if (legend){
            cx.lineV(xx,y+bh-20,y+bh-5,1,css.bl() );
            cx.lineH(xx,xx+(fs*1.5),y+bh-5,1,css.bl());
            cx.dtext(xx,y+bh ,fs,this.css.fc(),lang[90+j]);
        }
        xx=xx+step;
    }

};


MES_View_constructor.prototype.status_ind1=function(nrg,x,y,isize){
    var cx=this.cx;
    var dx=(isize/100)*10;
    this.status_ico(nrg,x,y,isize,true);
    cx.dtext(x+dx+isize, y, isize/2, this.css.fc() , mes_data.stat_name(nrg)  );
    cx.dtext(x+dx+isize, y+isize/2, isize/2, this.css.fc() , mes_data.stat_r_name(nrg)  );


    if ( mes_data.stat_pt(nrg)!=0 ){
        cx.alpha(0.8);
        cx.frame(x+isize+5,y,4.5*isize,isize,1,this.css.bl() ,'#FFFF4A');
        if( canvas_flash.f1s ) {cx.drawIco(x+isize+5,y,'\uf5a8',isize,'black')}
        cx.dtext(x+(2*isize), y+isize/2,isize/2.2,'black', mes_data.cfg_pta_name(nrg)  );
        cx.alpha(1)
    }

};
MES_View_constructor.prototype.status_box=function(nrg, x, y, w, h){
    var cx=this.cx;
    var css=this.css;
    var dxy=(h/100)*10;
    var isize=h-(2*dxy);

    if(canvas_flash.ffast &&  css.hud &&  mes_data.stat_nr(nrg)==5 ){  cx.setBlur('red',20,1,1); }
    this.panel(x,y,w,h);
    cx.resetBlur();
    this.status_ind1(nrg,x+dxy,y+dxy,h-(2*dxy));
    this.status_rect(nrg,x+w-20-dxy ,y+dxy,20,isize,true);
    cx.textIco(x+w/2,y+dxy, mes_data.stat_time(nrg)+'   [ '+ mes_data.stat_time_s(nrg)+' ]'   ,'\uf1d2',h/4,this.css.fc(),this.css.fc() );

};

MES_View_constructor.prototype.status_time=function(nrg,x,y,fs,fc){
    this.cx.textIco(x,y, mes_data.stat_time(nrg)+'   [ '+ mes_data.stat_time_s(nrg)+' ]'   ,'\uf1d2',fs,fc,fc );
};




MES_View_constructor.prototype.status_count_ind=function(nr,x,y,isize){
    var fc=this.css.fc();
    var fs=(isize/100)*70;
    var cc=mes_data.grupa_stc[nr];
    var sc=state_colors[nr];
    var isc=isize*0.2;
    var isp=isc/2;

    this.cx.frame(x, y, isize, isize, 1, this.css.bl(),sc);
    this.cx.frame(x, y+isize, isize, fs, 1, this.css.bl() );
    this.cx.drawIco(x+isp, y+isp, state_ico_code[nr], isize-isc,"black" );

    if (cc!=0) {this.cx.dtext(x+(isize/2),y+isize+2,fs,fc, cc,'center')}

    if(nr==6){
        this.cx.frame(x+isize+4, y, isize, isize, 1, this.css.bl(),'red');
        this.cx.frame(x+isize+4, y+isize, isize, fs, 1, this.css.bl());
        this.cx.drawIco(x+isize+4, y, state_ico_code[nr], isize, 'white' );

        var na=mes_data.grupa_nwork;
        if (na!=0) {
            this.cx.dtext(x + isize + (isize / 2) + 4, y + isize + 2, fs, fc, na, 'center')
        }
    }

};



// wyświetla wykres trendu wydajności
MES_View_constructor.prototype.performance_chart=function(nrg,x,y,bheight,xstep,xstep2,fs,anim,animnr) {
    var cx = this.cx;
    var css=this.css;
    var dmax = 0;
    var xx = x;
    var b=false;
    var bwc='red';
    var bwb=(61*xstep) ;
    for (j = 0; j < 10; ++j) {
        if (b){bwc= css.bgc1() } else {bwc= css.bgc2() }
        cx.box(x,y+(j*(bheight/10)),bwb ,bheight/10,bwc);
        b=!b;
    }

    for (var i = 0; i < 61; ++i) {
        if (mes_data.pbar_data(nrg, i) > dmax) {
            dmax = mes_data.pbar_data(nrg, i)
        }
    }
    //cx.alpha(0.6);
    for (var j = 0; j < 61; ++j) {
        var dp = myprc(dmax, mes_data.pbar_data(nrg, j));
        if (css.hud && anim){
            dp=canvas_flash.aVal(animnr,dp);
        }
        cx.barV(xx, y, xstep2, bheight, dp, css.green() );
        xx = xx + xstep;
    }
    cx.alpha(1);
    cx.lineH(x, x + (61 * xstep), y + bheight + 2, 3, 'silver' );
    cx.lineH(x, x + (15 * xstep), y + bheight + 2, 3, 'gray' );
    cx.lineH(x + (30 * xstep), x + (45 * xstep), y + bheight + 2, 3, 'gray');

    cx.dtext(x,y+bheight+6,fs,css.fc(),'max:'+dmax+'/h');
    cx.dtext(x+(60*xstep) ,y+bheight+6,fs,css.fc(),'ost. 4h','right')
};


MES_View_constructor.prototype.bar24chart=function(nrg,xx,yy,styl,anim,animnr){
    var that=this;
    var cx=this.cx;
    var css=this.css;
    var wd=34;
    var wds=46;
    var hx=150;
    var pd=1.5;
    var lfs=14;

    if (styl==1){ //big board
        wds=62;   wd=50;   lfs=20;
    }
    if (styl==2){ //pinfo
        wds=28;   wd=20;   lfs=9;
    }
    if (styl==3){ //
        wds=49;   wd=37;   lfs=14;
    }
    if (styl==4){ //
        wds=37;   wd=28;   lfs=12;
    }
    if (styl==5){ //
        wds=11;   wd=6;   lfs=0; hx=95; pd=0.95;
    }
    if (styl==6){ //
        wds=14;   wd=8;   lfs=0; hx=60; pd=0.60;
    }
    if (styl==7){ //
        wds=13;   wd=8;   lfs=0; hx=95; pd=0.95;
    }
    function b24(x,y, np ){

        var ppp=    mes_data.b24_pp(nrg,np)*pd ;
        var pstrat= mes_data.b24_strat(nrg,np)*pd;
        var pcp=    mes_data.b24_ecp(nrg,np)*pd;
        var psetup= mes_data.b24_setup(nrg,np)*pd;

        if (css.hud && anim){
        ppp=canvas_flash.aVal(animnr,ppp);
        pstrat=canvas_flash.aVal(animnr,pstrat);
        pcp=canvas_flash.aVal(animnr,pcp);
        psetup=canvas_flash.aVal(animnr,psetup);
        }

        cx.frame(x,y,wd,hx,0,'white','white');
        var bl=that.css.bl();
        if (css.hud){bl='white'}
        cx.frame(x-1,y-1,wd+2,hx+2,1,bl);
        if(ppp!=0)   {cx.frame(x,y+(hx-ppp),wd,ppp,0,'#808080','#808080')}
        if(psetup!=0){cx.frame(x,y+(hx-ppp-psetup),wd,psetup,0,'#00A2E8','#00A2E8')}
        if(pstrat!=0){cx.frame(x,y+(hx-ppp-psetup-pstrat),wd,pstrat,0,'#FF0000','#FF0000')}
        if(pcp!=0)   {cx.frame(x,y+(hx-ppp-psetup-pstrat-pcp),wd,pcp,0,'#00B900','#00B900')}

        var bh=mes_data.b24_h(nrg,np);
        var eh=bh+1;
        if(eh==24){ bh=23; eh=0 }
        cx.dtext(x,y+hx+4,lfs,that.css.fc(),bh+'-'+eh)

    }
    for (var j = 0; j < 24 ; ++j) {    b24(xx+(j*wds),yy,j)    }

};

// LAYOUT & Panels

MES_View_constructor.prototype.matrix_left_panel_panels=function(nrg,x,y,crr){
    var cx=this.cx;
    var css=this.css;
    var aname=mes_data.cfg_nameA(nrg);
    var fname=mes_data.cfg_name(nrg);
    var bl=css.bl();
    var nrc = mes_data.get_nr_count(0,nrg) ;

    if (crr) {
        cx.setBlur(css.shadow(),30,0,10);
        bl=css.shadow();
    }
    if (mes_data.matrix.ps==0){// duże

        cx.frame(x+6, y+6, 280, 110, 1,bl, css.bg());
        cx.resetBlur();
        this.status_rect(nrg,x+7,y+55,278,60,false);
        cx.dtextMw(x+9 , y + 8, 33, css.fc(), aname,99 );
        cx.dtext(x+9,y+38,15,css.fc(),fname);
        this.lamp(nrg,x+10,y+60,30,15);
        this.task_label_name_i(nrg,x+50,y+60,14,'black');
        this.task_micro_line_bar(nrg,x+50,y+75,200,7);
        this.operator_label_i(nrg,x+50,y+86,14,'black');
        this.oee_micro_bar(nrg,nrc,x+50,y+105,200,10);
        this.andon_ico(nrg,x+220,y+60,50,true);
    }
    if (mes_data.matrix.ps==1){ // srednie

        cx.frame(x+6, y+6, 170, 110, 1,bl, css.bg());
        cx.resetBlur();
        this.status_rect(nrg,x+7,y+55,168,60,false);
        cx.dtextMw(x+9 , y + 8, 33, css.fc(), aname,99 );
        cx.dtext(x+9,y+38,15,css.fc(),fname);
        this.lamp(nrg,x+10,y+54,30,14);
        this.task_micro_line_bar(nrg,x+6,y+100,170,7);
        this.oee_micro_bar(nrg,nrc,x+6,y+107,170,10);
        this.andon_ico(nrg,x+100,y+60,50,true);
    }

    if (mes_data.matrix.ps==2){// mini

        cx.frame(x+6, y+6, 80, 80, 1, bl, css.bg());
        cx.resetBlur();
        this.status_rect(nrg,x+7,y+45,78,40,false);
        cx.dtextMw(x+9 , y + 10, 33, css.fc(), aname,75 );
        this.lamp(nrg,x+10,y+40,20,9);
        this.task_micro_line_bar(nrg,x+6,y+75,80,5);
        this.oee_micro_bar(nrg,nrc,x+6,y+79,80,7);
        this.andon_ico(nrg,x+48,y+48,38,true);

    }
};

MES_View_constructor.prototype.matrix__panels=function(nrg,x,y,crr){
    var cx=this.cx;
    var css=this.css;
    var aname=mes_data.cfg_nameA(nrg);
    var fname=mes_data.cfg_name(nrg);

    if (mes_data.matrix.ps==0){// duże
        cx.frame(x+6, y+6, 440, 230, 1, css.fc(), css.bg());
        this.status_rect(nrg,x+7,y+7,438,28,true);
        cx.dtext(x+15,y+10,16,'black', mes_data.cfg_name(nrg) );
        cx.dtextMw(x+10 , y + 40, 55, css.fc(), aname,140 );


        this.work_indicator(nrg,x+270,y+40,18);
        this.lamp(nrg,x+395,y+20,44,40);

        this.status_ind1(nrg,x+10,y+185,48);
        this.oee_box_mini(nrg,-1,x+10,y+90,0,true);
        this.task_box_mini(nrg,x+170,y+90,0,true);
        this.operator_label_i(nrg,x+10,y+165,15,css.fc());
        this.andon_ico(nrg,x+340,y+140,90,true);
        if (css.hud){
          if(mes_data.work(nrg) )  {cx.bar_fx_progress(x+360,y+230,80,10,'white','gray')}
        }
    }
    if (mes_data.matrix.ps==1){ // srednie
        cx.frame(x+6, y+6, 290, 180, 1, css.fc(), css.bg());
        this.status_rect(nrg,x+7,y+7,288,25,true);
        cx.dtext(x+15,y+10,16,'black', mes_data.cfg_name(nrg) );
        cx.dtextMw(x+10 , y + 35, 60, css.fc(), aname,140 );
        this.lamp(nrg,x+250,y+14,40,35);

        this.work_indicator(nrg,x+10,y+120,16);

        this.status_ind1(nrg,x+10,y+146,35);
        this.andon_ico(nrg,x+220,y+110,75,true);
    }

    if (mes_data.matrix.ps==2){// mini
        cx.frame(x+6, y+6, 215, 110, 1, css.fc(), css.bg());
        this.status_rect(nrg,x+7,y+7,213,15,true);
        cx.dtextMw(x+8 , y + 25, 60, css.fc(), aname,140 );
        this.lamp(nrg,x+185,y+12,30,25);
        this.status_ind1(nrg,x+8,y+84,30);
        this.andon_ico(nrg,x+149,y+60,55,true);
    }
};

/*           v.panel(xx+860,220,265,150,1,mes_data.lamp_state(nr)==4);
 v.work_indicator(nr,xx+870,230,30,30,250);
 v.performance_indicator(nr,xx+870,270,28 );
 v.ctime_indicator(nr,xx+870,310,28,true );*/


MES_View_constructor.prototype.matrix_andon_panels=function(nrg,x,y){
    var cx=this.cx;
    var css=this.css;
    var aname=mes_data.cfg_nameA(nrg);
    var sc= mes_data.stat_color_chart(nrg);
    var snr=mes_data.stat_nr(nrg);
    var fc='black';
    if ( snr==2 || snr==5  ) { fc='white' }
    var vb=true;
    if (snr==5){ vb= !canvas_flash.f1s  }
    if (mes_data.matrix.ps==0){// duże
        if(vb){ cx.frame(x+6, y+6, 440, 230, 1, sc, sc)}
        cx.dtextMw(x+12 , y + 20, 100, fc, aname,280 );
        this.lamp_simple(nrg,x+30,y+120,100,100,'black');
        this.andon_ico(nrg,x+270,y+70,150,true);
    }
    if (mes_data.matrix.ps==1){ // srednie
        if(vb){ cx.frame(x+6, y+6, 290, 180, 1, sc, sc)}
        cx.dtextMw(x+12 , y + 20, 100, fc, aname,280 );
        this.lamp_simple(nrg,x+30,y+115,100,80,'black');
        this.andon_ico(nrg,x+170,y+70,110,true);
    }
    if (mes_data.matrix.ps==2){// mini

      if(vb){ cx.frame(x+6, y+6, 215, 110, 1, sc, sc)}
        cx.dtextMw(x+12 , y + 20, 80, fc, aname,180 );
        this.lamp_simple(nrg,x+160,y+85,50,40,'black');
        this.andon_ico(nrg,x+140,y+55,70,true);
    }
};


MES_View_constructor.prototype.matrix_left_panel=function(styl){
    var cx=this.cx;
    var css=this.css;
    var phe=950;
    if(styl==1){phe=790}
    if(styl==2){phe=830}
    cx.frame(10,10,620,phe,1, css.bl(), css.bg()   );
    cx.textIco(30,20, mes_data.grupa_name,'\uf134',35, css.fc(), css.fcoff() );
    cx.dtext(600,20,11, css.fc(), mes_data.crr+'/'+mes_data.grupa_count ,'right' );
    for (var j = 0; j < 7 ; ++j) { this.status_count_ind(j,30+(j*55),80,40)    }
    this.andon_count_ind(470,80,40);
    this.op_count_ind(525,80,40);


    cx.frame(660,10,1180,phe,1, css.bl()   );
    cx.setBlur(css.shadow(),20,0,5);
    cx.drawIco(620,mes_data.matrix.y,'\uf14a',60, css.shadow() );
    cx.resetBlur();

    var nr= 0,x= 0,y=0;
    mes_data.matrix._pc=0;
    for (var r = 0; r < mes_data.matrix.r_count ; ++r) {
        for (var c = 0; c < mes_data.matrix.c_count ; ++c) {
            x=mes_data.matrix.ppx [ c ];
            y=mes_data.matrix.ppy [ r ];
            mes_data.matrix._pc++;
            if( mes_data.matrix._pc <= mes_data.matrix.base_count  ) {
                nr=mes_data.matrix._pc-1;
                this.matrix_left_panel_panels(nr,x+20,y+180, mes_data.crr == nr   );
                if(mes_data.crr == nr) {mes_data.matrix.y=y+210}
            }
        }
    }// end loop
};


MES_View_constructor.prototype.minipanels=function(nrg,x,y,styl,crr){
    var cx=this.cx;
    var css=this.css;
    var aname=mes_data.cfg_nameA(nrg);
    var bl= css.bl();

    if (crr) {
        cx.setBlur(css.shadow(),30,0,10);
        bl=css.shadow();
    }
    if (styl==0){
        cx.frame(x, y, 40, 55, 1,bl, css.bg());
        cx.resetBlur();
        this.status_rect(nrg,x+1,y+21,38,35,false);
        cx.dtextMw(x+3 , y + 3, 15, css.fc(), aname,36 );
        this.lamp(nrg,x+4,y+24,15,7);
        this.andon_ico(nrg,x+20,y+22,20,false);
    }
    if(styl==1){
        cx.frame(x, y, 80, 55, 1, bl, css.bg());
        cx.resetBlur();
        this.status_rect(nrg,x+1,y+21,78,35,false);
        cx.dtextMw(x+3 , y + 3, 15, css.fc(), aname,66 );
        this.lamp(nrg,x+4,y+24,15,7);
        this.andon_ico(nrg,x+24,y+22,30,false);
    }


};

MES_View_constructor.prototype.group_group_horizont=function(x, y,use_name, name_bottom){
    var cx=this.cx;     var css=this.css;
    var lp = mes_data.grupa_count;
    var xx=x; var yy=y;
    var aname='';

    if (use_name && !name_bottom){ yy=y+20; cx.dtext(x,y,16,css.fc(), mes_data.grupa_name) }
    if (use_name && name_bottom){  cx.dtext(x,y+60,20,css.fc(), mes_data.grupa_name) }
    for (var j = 0; j < lp ; ++j) {

        aname=mes_data.cfg_nameA(j);
        if(lp>20) {
            this.minipanels(j,xx,yy,0,j==mes_data.crr);
            xx = xx + 46;
        } else {
            this.minipanels(j,xx,yy,1,j==mes_data.crr);
            xx = xx + 86;
        }
    }
};

MES_View_constructor.prototype.group_group_horizont2=function(x, y){
    var cx=this.cx;     var css=this.css;
    var lp = mes_data.grupa_count;
    var xx=x; var yy=y;
    cx.dtext(x,y+60,20,css.fc(), mes_data.grupa_name);
    for (var j = 0; j < lp ; ++j) {

        if(lp>20) {
            this.minipanels(j,xx,yy,0,false);
            xx = xx + 46;
        } else {
            this.minipanels(j,xx,yy,1,false);
            xx = xx + 86;
        }
    }
};



//// ut tool
MES_View_constructor.prototype.panel=function(x,y,w,h,hm,hf,hc){

    this.cx.frame(x,y,w,h,1, this.css.bl(), this.css.bg() );
    if(this.css.hud) {
        this.cx.frame(x-1,y-1,w+2,h+2,1, 'gray'  );
        this.cx.hud_corners_rect(3,200/10,1,'white',0,0);
    if (hm==1 && hf){  this.cx.bar_fx_progress( x+20,y+h+2,w*0.8,2,'silver','gray' )  }
    }
};




// rysuje ikonę zakresu 0- b. miesiąc. 1-b.zmiana 2-b. zlecenie itd
MES_View_constructor.prototype.time_range_ico=function(x,y,nr){
    this.cx.cv.drawImage(imgzc[nr],x,y   )
};





//// OPERATORS

// panel z informacjami o operatorze, pracowniku UR oraz ustawiaczu
MES_View_constructor.prototype.operators_ind1=function(nrg,x,y,isize,mode){
    var cx=this.cx;
    var dx=(isize/100)*10;
    var cico='\uf103';
    var fc=this.css.fc();
    var is2=isize/2;
    var oname='', otime='';

    this.cx.frame(x, y, isize, isize, 1, this.css.bl());

    if (mode==0){ //operator
        if (mes_data.cfg_op_use(nrg)) {
            if (mes_data.op_op_id(nrg) != 0) {
                oname = mes_data.op_op_name(nrg);
                otime = mes_data.op_op_time(nrg)+'   [ '+ mes_data.op_op_time_s2(nrg)+' ]'  ;
            } else {   fc = this.css.fcoff()   }
            this.cx.drawIco(x, y, cico, isize, fc);
        }
    }
    if (mode==1){ //UR
       if (mes_data.cfg_op_ur_use(nrg)){
        if (mes_data.op_ur_id(nrg)!=0){
            oname=mes_data.op_ur_name(nrg);
            otime=mes_data.op_ur_time(nrg);
        } else {fc= this.css.fcoff() }
        this.cx.drawIco(x, y, cico, is2, fc  );
        this.cx.drawIco(x+is2, y+is2, '\uf633', is2, fc  )
       }

    }
    if (mode==2){ //ustawiacz
        if (mes_data.cfg_op_setup_use(nrg)) {
            if (mes_data.op_set_id(nrg) != 0) {
                oname = mes_data.op_set_name(nrg);
                otime = mes_data.op_set_time(nrg);
            } else {   fc = this.css.fcoff()     }
            this.cx.drawIco(x, y, cico, is2, fc);
            this.cx.drawIco(x + is2, y + is2, '\uf2b9', is2, fc)
        }
    }
    cx.dtext(x+dx+isize, y, isize/2, fc , oname  );
    cx.dtext(x+dx+isize, y+(isize/2)+5, isize/3, this.css.fc() , otime  );
};

// nazwa operatora z ikoną
MES_View_constructor.prototype.operator_label_i=function(nrg,x,y,fs,fc){
    if(mes_data.op_op_id(nrg)!=0){ this.cx.textIco(x,y,mes_data.op_op_name(nrg), '\uf103',  fs,fc,fc  )}
};




//// ANDON, SIGNAL, performance, cc
MES_View_constructor.prototype.andon_icoxxxxxxxxxxx=function(nrg,x,y,isize,back,nico) {
    var anr= mes_data.andon_nr(nrg);
    var ac= andon_colors[anr];

    if (anr!=0){
        if (back ) {   this.cx.frame(x, y, isize, isize, 1, '#4B4B4B', '#4B4B4B')   }

        if (this.css.hud) {
            this.cx.drawIcoEx(x, y, andon_ico_code , isize, ac, canvas_flash.loop4s)
        } else {   if (canvas_flash.f1s) {   this.cx.drawIco(x+2, y+2, andon_ico_code, isize, ac)   }
        }
    } else {
        if (nico){ this.cx.drawIco(x,y,andon_ico_code_n,isize,this.css.slight() ); }
    }

};
MES_View_constructor.prototype.andon_ico=function(nrg,x,y,isize,back,nico) {
    var anr= mes_data.andon_nr(nrg);
    var insp_c=mes_data.inspection_countAr(nrg) ;
    var ac= andon_colors[anr];
    var fl=canvas_flash.f1s;

    if (anr!=0 || insp_c >0 ){
        if (back) {
            this.cx.frame(x, y, isize, isize, 1, '#4B4B4B', '#4B4B4B')
        }

        if(!fl){
            // inspekcja
            if (insp_c>0 ){
            //this.cx.dtext(x,y,44,'blue','INS');
                this.cx.drawIco(x + 2, y + 2, insp_ico_code, isize,'#FFB56A')

            }
        }else{
            //andon

            if (anr != 0) {
                if (back) {
                    this.cx.frame(x, y, isize, isize, 1, '#4B4B4B', '#4B4B4B')
                }

                if (this.css.hud) {
                    this.cx.drawIcoEx(x, y, andon_ico_code, isize, ac, canvas_flash.loop4s)
                } else {
                    if (canvas_flash.f1s) {
                        this.cx.drawIco(x + 2, y + 2, andon_ico_code, isize, ac)
                    }
                }
            } else {
                if (nico) {
                    this.cx.drawIco(x, y, andon_ico_code_n, isize, this.css.slight());
                }
            }
        }

    }




};

MES_View_constructor.prototype.andon_box=function(nrg,x,y,styl){
    var cx=this.cx;
    var css=this.css;
    var isize=120;
    var fs=20;
    var fs2=20;
    var fs3=20;
    var rmb=17;
    var anr= mes_data.andon_nr(nrg);

    if (styl==1){   isize=200;  rmb=24;  fs2=30;  }
    if (styl==2){   isize=160;  rmb=20;  fs2=25; fs3=45 }

    if (anr!=0){
    if(canvas_flash.ffast &&  css.hud){  cx.setBlur('yellow',20,1,1); }
    cx.romb_line(x+10,y,20,10,-3,rmb,css.ry() );
    cx.resetBlur();
        cx.dtext(x+isize,y+40 ,fs3, css.fc()  , mes_data.andon_time_s(nrg));
        cx.dtext(x+isize,y+80+fs ,fs, css.fc()  , mes_data.andon_user(nrg))
    }

    this.andon_ico(nrg,x,y+10,isize,false,true);
    this.andon_label_name(nrg,x+10,y+isize+10,fs2);

};




MES_View_constructor.prototype.andon_count_ind=function(x,y,isize){
    var fs=(isize/100)*70;
    this.cx.frame(x, y, isize, isize, 1, this.css.bl(), andon_colors[1]);
    this.cx.frame(x, y+isize, isize, fs, 1, this.css.bl() );
    this.cx.drawIco(x, y, andon_ico_code, isize, "black");
    if ( mes_data.grupa_ac !=0) {this.cx.dtext(x+(isize/2),y+isize+2,fs,this.css.fc(), mes_data.grupa_ac,'center')}

};

MES_View_constructor.prototype.op_count_ind=function(x,y,isize){
    var fs=(isize/100)*70;
    this.cx.frame(x, y, isize*2, isize, 1, this.css.bl(), '#F5F5F5');
    this.cx.frame(x, y+isize, isize*2, fs, 1, this.css.bl() );
    this.cx.drawIco(x, y, '\uf103', isize, 'black' );

   if ( mes_data.grupa_op !=0) {
       this.cx.dtext(x + (isize), y + isize + 2, fs, this.css.fc(), mes_data.grupa_op + '/' + mes_data.grupa_opm, 'center')
       var obc= mes_data.grupa_opm / mes_data.grupa_op;

       this.cx.dtext(x + (isize*2)-4, y + 6  , fs*0.7, 'black', obc.toFixed(1),'right')


   }

};


MES_View_constructor.prototype.andon_label_name=function(nrg,x,y,fs){
   var fc = this.css.fc();
    if( mes_data.andon_nr(nrg)==0 ){  fc=this.css.fcoff()  }
    this.cx.dtext(x,y,fs,fc, mes_data.cfg_a_name(nrg, mes_data.andon_nr(nrg)  )   )
};
MES_View_constructor.prototype.andon_label_cmt=function(nrg,x,y,fs){
    var fc = this.css.fc();
    if( mes_data.andon_nr(nrg)==0 ){  fc=this.css.fcoff()  }
    this.cx.dtext(x,y,fs,fc, mes_data.andon_aw_cmt(nrg)   )
};

MES_View_constructor.prototype.work_indicator=function(nr,x,y,fs){
    var cx=this.cx;
    var stime=mes_data.signal_time_s(nr);
    if (stime==0){stime=1}
    if ( mes_data.lamp_state(nr)==4  ) { //mes_data.lamp_state(nr)==4 ||
        var ic='\uf271';
        if ( canvas_flash.f1s){ic='\uf272'}
        cx.textIco(x, y, ktime.SecToDhms( stime), ic, fs, this.css.fc(), this.css.green()) //mes_data.signal_time_cp(nr)
    } else {
        cx.textIco(x, y, ktime.SecToDhms(stime), '\uf14f', fs, this.css.fc(), this.css.fc())
    }
};

MES_View_constructor.prototype.time_to_end_indicator=function(nr,x,y,fs,h,w){
    var cx=this.cx;
    if (mes_data.lamp_state(nr)==4 && mes_data.time_tt_occ(nr)>20 ) {
        var ic='\uf2a6';
        var tt=Math.abs((mes_data.signal_time_s(nr)-mes_data.time_tt_occ(nr)));
        cx.textIco(x, y, ktime.TimeMS( tt ), ic, fs, this.css.fc(), this.css.green())
    }
};

MES_View_constructor.prototype.performance_indicator=function(nr,x,y,fs){

    if (!mes_data.cfg_use_p_cc(nr)){return }
    var s=mes_data.performance(nr) +'/h' ;
    var s2='['+mes_data.performance_opt(nr)+'/h]';
    this.cx.textIco(x, y, s , '\uf361', fs, this.css.fc(), this.css.fc());
    if(mes_data.performance_opt(nr)!=0) {
        this.cx.dtext(x + this.cx.len(s) + (2 * fs), y + (fs * 0.3), fs * 0.7, this.css.fc(), s2)
    }
};

MES_View_constructor.prototype.ctime_indicator=function(nr,x,y,fs,ctr){
    if (!mes_data.cfg_use_p_cc(nr)){return }
    var s=mes_data.ctime(nr) +'s.' ;
    var s2='['+mes_data.ctime_opt(nr)+'s.]';
    this.cx.textIco(x, y, s , '\uf5a4', fs, this.css.fc(), this.css.fc());
    if(mes_data.ctime_opt(nr)!=0) {
        this.cx.dtext(x + this.cx.len(s) + (2 * fs), y + (fs * 0.3), fs * 0.7, this.css.fc(), s2)
    }

    if(mes_data.ctime_ctr(nr)!='' && ctr ) {
        this.cx.dtext(this.cx.xx, this.cx.yy + (fs ), fs * 0.8, this.css.fc(), 'CTR  ' + mes_data.ctime_ctr(nr))
    }
};


MES_View_constructor.prototype.lamp_simple=function(nrg,x,y,w,h,bcolor){
    var lc=bcolor;
    var bl=false;
    switch(mes_data.lamp_state(nrg)){
        case 1:    break;
        case 2: lc='#FF2A2A';   break;
        case 3: lc='#FF2A2A'; bl=canvas_flash.f1s;   break;
        case 4: lc='#3FDA6D';   break;
        case 5: lc='#FDF000';   break;
        case 6: lc='#FDF000'; bl=canvas_flash.f1s;   break;
    }
    if (bl){ lc=bcolor }
    this.cx.frame(x,y,w,h,1,'black',lc)
};

MES_View_constructor.prototype.lamp=function(nrg,x,y,sw,sh){
    var cx=this.cx;
    var ls= mes_data.lamp_state(nrg)  ;
    var blink= canvas_flash.f1s;
    var sr=false, sy=false, sg=false;
    var sph=sh/8;

    switch(ls){
        case 1: sr=false; sy=false; sg=false;   break;
        case 2: sr=true; sy=false; sg=false;   break;
        case 3: sr=blink; sy=false; sg=false;   break;
        case 4: sr=false; sy=false; sg=true;   break;
        case 5: sr=false; sy=true; sg=false;   break;
        case 6: sr=false; sy=blink; sg=false;   break;

    }

    var cr='#603333', cy='#605E37', cg='#2D662D', csep='#616161';
    if(sr){cr='#FF2A2A'}
    if(sy){cy='#FDF000'}
    if(sg){cg='#3FDA6D'}

    cx.box(x,y,sw,sh,cr );  if(sr){  cx.box(x+(sw/4),y,sw/2,sh-2,'#FF8888' ); cx.box(x+(sw/3),y,sw/5,sh-2,'#FFC1C1' )  }
    cx.box(x,y+sh,sw,sh,cy ); if(sy){  cx.box(x+(sw/4),y+sh,sw/2,sh-2,'#FFF759' ); cx.box(x+(sw/3),y+sh,sw/5,sh-2,'#FFFDD5' )  }
    cx.box(x,y+(2*sh),sw,sh,cg );  if(sg){  cx.box(x+(sw/4),y+(2*sh),sw/2,sh-2,'#00FF00' ); cx.box(x+(sw/3),y+(2*sh),sw/5,sh-2,'#B9FFB9' )  }

    cx.box(x,y,sw,sph,csep );
    cx.box(x,y+sh,sw,sph,csep );
    cx.box(x,y+sh+sh,sw,sph,csep );
    cx.box(x,y+sh+sh+sh,sw,sph,csep );

};

//// OEE
MES_View_constructor.prototype.oee_box=function(nrg,nrc,x,y,styl){
    var cx=this.cx;
    var oee=mes_data.oee(nrg,nrc,0  );
    var teep=mes_data.oee(nrg,nrc,5  );
    var pwidth=460;
    var pheight=270;
    var basew=380;
    var baseh=200;
    var basebw=30;
    var ix = x+50;
    var fs=55;
    var fst=20;
    var fstd=50;

    if (styl==1|| styl==2){
        pwidth=590;
        pheight=330;
        basew=510;
        baseh=270;
        basebw=40;
        fs=79;
        fstd=50;
    }

    this.panel(x,y,pwidth,pheight);
    this.time_range_ico(x+10,y+30, nrc );

    if ( nrc==2 && mes_data.task_id(nrg)==0  ){
        cx.dtext(x+40,y+30,14,this.css.fcoff(),'Brak aktywnego zlecenia')
    }else {
        cx.barVMark(ix,y+30,basew,baseh, basebw,oee  ,this.css.barc(),'gray', this.css.fc()  ,2,0,false);
        cx.number_float(ix,y+baseh-fs+20,oee,fs,fs/2,this.css.fc(),'OEE: ','%');
        cx.number_float(ix,y+baseh-fst+fstd,teep,fst,fst/2,this.css.fc(),'TEEP: ','%');

        if (styl==0){  this.oee_chart(nrg,nrc,ix,y+30,240,22,8,false,true,true) }
        if (styl==1){  this.oee_chart(nrg,nrc,ix,y+30,290,27,10,false,true,true) }
        if (styl==2){  this.oee_chart(nrg,nrc,ix,y+30,290,27,10,false,true,false) }
    }
};


MES_View_constructor.prototype.oee_box_mini=function(nrg,nrc,x,y,styl,box){
    var cx=this.cx;
    if(nrc==-1){  nrc= mes_data.get_nr_count(0,nrg ) }
    var oee=mes_data.oee(nrg,nrc,0  );
    var basew=140;
    var baseh=60;
    var basebw=20;
    var fs=30;
    var dxy=0;

    if(box){cx.hud_corners(x,y,basew+8,baseh+8,20,1,this.css.fcoff(),0,0 );  x=x+4; y=y+4 }

    this.time_range_ico(x,y, nrc );
    cx.dtext(x+30,y,fs,this.css.fc(),'OEE');
        cx.barVMark(x,y,basew,baseh, basebw,oee  ,this.css.barc(),'gray', this.css.fc()  ,1,0,false);
        cx.number_float(x,y+baseh-fs-2,oee,fs,fs/2,this.css.fc(),'','%')
};

MES_View_constructor.prototype.oee_label=function(nrg,nrc,x,y,fs1,fs2,cp_mode){
    var cx=this.cx;
    var oee=mes_data.oee(nrg,nrc,0  );
    var fc=this.css.fc();
    if(cp_mode==0){
        cx.number_float(x,y,oee,fs1,fs2,fc,'OEE: ','%')
    }
    if(cp_mode==1){
        cx.number_float(x,y,oee,fs1,fs2,fc,'','%')
    }
    if(cp_mode==2){
        cx.dtext(x,y,fs1,fc,'OEE');
        cx.number_float(x,y+(fs1*0.7),oee,fs1,fs2,fc,'','%')
    }
};

MES_View_constructor.prototype.teep_label=function(nrg,nrc,x,y,fs1,fs2,cp_mode){
    var cx=this.cx;
    var teep=mes_data.oee(nrg,nrc,5  );
    var fc=this.css.fc();
    if(cp_mode==0){
        cx.number_float(x,y,teep,fs1,fs2,fc,'TEEP: ','%')
    }
    if(cp_mode==1){
        cx.number_float(x,y,teep,fs1,fs2,fc,'','%')
    }
    if(cp_mode==2){
        cx.dtext(x,y,fs1,fc,'TEEP');
        cx.number_float(x,y+(fs1*0.7),teep,fs1,fs2,fc,'','%')
    }
};


MES_View_constructor.prototype.oee_label_box=function(nrg,nrc,x,y){
    var cx=this.cx;
    var oee=mes_data.oee(nrg,nrc,0  );
    var teep=mes_data.oee(nrg,nrc,5  );
    var fc=this.css.fc();

    this.panel(x,y,160,70);
    this.time_range_ico(x+5,y+2,nrc);

    if( nrc==2 &&  mes_data.task_id(nrg)==0 ) { return 0}

        cx.barH(x + 40, y + 12, 110, 15, oee, '#FF6666', this.css.barc());
        cx.number_float(x + 5, y + 26, oee, 23, 16, fc, 'OEE: ', '%')

        cx.number_float(x + 5, y + 51, teep, 14, 12, fc, 'TEEP: ', '%')
};

MES_View_constructor.prototype.oee_chart=function(
    nr,nrc, x,y, bar_width,bar_height,bspace,
    use_z,use_cop, anim
)
{
    var cx=this.cx;
    var bar_step=y;
    var d_step=0;

    var cop=mes_data.oee(nr,nrc,4  );
    var vd= mes_data.oee(nr,nrc,1  );
    var vw= mes_data.oee(nr,nrc,2  );
    var vj= mes_data.oee(nr,nrc,3  );
    var fcolor= this.css.fc();

    if (cop>100){cop=100}
    if (vd>100){vd=100}
    if (vw>100){vw=100}
    if (vj>100){vj=100}
    //cop = cop.toFixed(1);

    if (this.css.hud && anim){  cop=canvas_flash.aVal(3,cop);   }

    if (use_z){
        cx.barHPL(x,bar_step,bar_width  , bar_height , 100 ,'green','green', bar_height,fcolor);
        bar_step=bar_step+bar_height+bspace;
    }

    if(use_cop){
        cx.barHPL(x,bar_step,bar_width,bar_height,cop,'#C0C0C0','#808080', bar_height,fcolor );
        bar_step=bar_step+bar_height+bspace;
        d_step=cop* ( bar_width/100 );
    }
    else{ d_step= 100* ( bar_width/100 ) }

    if (d_step > bar_width){d_step= bar_width }

    cx.box(x,bar_step,2,bar_height,fcolor); cx.barHPL(x,bar_step,d_step,bar_height,  vd    ,'#FF6666','#808080', bar_height ,fcolor);

    bar_step=bar_step+bar_height+bspace;
    d_step=vd* ( d_step/100 );
    if (d_step > bar_width){d_step= bar_width }
    cx.box(x,bar_step,2,bar_height,fcolor); cx.barHPL(x,bar_step,d_step,bar_height,  vw    ,'#FF6666','#808080', bar_height,fcolor );

    bar_step=bar_step+bar_height+bspace;
    d_step=vw* ( d_step/100 );
    if (d_step > bar_width){d_step= bar_width }
    cx.box(x,bar_step,2,bar_height,fcolor); cx.barHPL(x,bar_step,d_step,bar_height,  vj    ,'#FF6666','#808080', bar_height,fcolor );
};


MES_View_constructor.prototype.oee_chart_mini=function(     nr,nrc, x,y, bar_width,bar_height,bspace ){
    var cx=this.cx;
    var bar_step=y;
    var d_step=0;

    var cop=mes_data.oee(nr,nrc,4  );
    var vd= mes_data.oee(nr,nrc,1  );
    var vw= mes_data.oee(nr,nrc,2  );
    var vj= mes_data.oee(nr,nrc,3  );
    var fcolor= this.css.fc();

    if (cop>100){cop=100}
    if (vd>100){vd=100}
    if (vw>100){vw=100}
    if (vj>100){vj=100}
    d_step= 100* ( bar_width/100 );
    if (d_step > bar_width){d_step= bar_width }
    cx.box(x,bar_step,2,bar_height,fcolor); cx.barH(x,bar_step,d_step,bar_height,  vd    ,'#FF6666','#808080');
    bar_step=bar_step+bar_height+bspace;
    d_step=vd* ( d_step/100 );
    if (d_step > bar_width){d_step= bar_width }
    cx.box(x,bar_step,2,bar_height,fcolor); cx.barH(x,bar_step,d_step,bar_height,  vw    ,'#FF6666','#808080');

    bar_step=bar_step+bar_height+bspace;
    d_step=vw* ( d_step/100 );
    if (d_step > bar_width){d_step= bar_width }
    cx.box(x,bar_step,2,bar_height,fcolor); cx.barH(x,bar_step,d_step,bar_height,  vj    ,'#FF6666','#808080');
};

MES_View_constructor.prototype.oee_micro_bar=function(  nr,nrc, x,y, bar_width,bar_height ){
    this.cx.frame(x,y, bar_width, bar_height,1, 'black' );
    this.cx.barH(x+1,y+1,bar_width-1,bar_height-1,  mes_data.oee(nr,nrc,0  )    ,'#FF6666', this.css.barc());

}

///// TASK

MES_View_constructor.prototype.p_b_kwh=function(nrg,nrc,x,y){
    this.cx.frame(x,y,120,24,1,this.css.bl(),this.css.bg() );
    this.cx.drawIco(x+3,y+3,'\uf299',18,this.css.fc() );
    if(mes_data.count(nrg,nrc,1 )!=0) { this.cx.dtext(x+113,y+5,16,this.css.fc(),numFC(mes_data.count(nrg,nrc,1 )),'right') }

    this.cx.frame(x+125,y,70,24,1,this.css.bl(),this.css.bg() );
    this.cx.drawIco(x+128,y+3,'\uf5d1',18,this.css.ry() );
    if(mes_data.count(nrg,nrc,1 )!=0) {  this.cx.dtext(x+190,y+5,16,this.css.fc(),numFC(mes_data.count(nrg,nrc,2 )),'right')}

    if (mes_data.cfg_kwh_use(nrg)) {
        this.cx.frame(x + 200, y, 140, 24, 1, this.css.bl(), this.css.bg());
        this.cx.drawIco(x + 203, y + 3, '\uf4cd', 18, this.css.fc());
        if (mes_data.count(nrg, nrc, 1) != 0) {
            this.cx.dtext(x + 335, y + 5, 16, this.css.fc(), numFC(mes_data.count(nrg, nrc, 4)), 'right')
        }
    }
};

MES_View_constructor.prototype.task_label_name=function(nrg,x,y,fs){
    this.cx.dtext(x,y,fs,this.css.fc(), mes_data.task_name(nrg)  )
};
MES_View_constructor.prototype.task_label_name_i=function(nrg,x,y,fs,fc){
    if(mes_data.task_id(nrg)!=0){     this.cx.textIco(x,y,mes_data.task_name(nrg), '\uf5ef',  fs,fc,fc  ) }
};

MES_View_constructor.prototype.task_label_start_i=function(nrg,x,y,fs,fc){
       this.cx.textIco(x,y,'start: '+mes_data.task_time(nrg), '\uf1d2',  fs,fc,fc  );
};

MES_View_constructor.prototype.task_label_product=function(nrg,x,y,fs){
    this.cx.dtext(x,y,fs,this.css.fc(), mes_data.task_product(nrg)  )
};

MES_View_constructor.prototype.task_label_target=function(nrg,x,y,fs){
    this.cx.dtext(x,y,fs,this.css.fc(), numFC(mes_data.task_target(nrg))  )
};

MES_View_constructor.prototype.task_label_count=function(nrg,x,y,fs){
    this.cx.dtext(x,y,fs,this.css.fc(), numFC(mes_data.count(nrg,2,1))  )
};
MES_View_constructor.prototype.task_label_todo=function(nrg,x,y,fs,fc){
    this.cx.dtext(x,y,fs,fc, numFC( mes_data.task_target(nrg) -  mes_data.count(nrg,2,1))  )
};

MES_View_constructor.prototype.task_label_prc=function(nrg,x,y,fs){
    this.cx.dtext(x,y,fs,this.css.fc(), mes_data.task_prc(nrg)+'%'  )
};
MES_View_constructor.prototype.task_label_braki=function(nrg,x,y,fs){
    this.cx.dtext(x,y,fs,this.css.fc(), numFC(mes_data.count(nrg,2,2))  )
};
MES_View_constructor.prototype.task_label_brakip=function(nrg,x,y,fs){
    this.cx.dtext(x,y,fs,this.css.fc(), mes_data.count(nrg,2,3)+'%'  )
};

MES_View_constructor.prototype.task_count=function(x,y,isize){
    this.cx.frame(x, y, isize, isize, 1, this.css.bl());
    this.cx.drawIco(x, y, '\uf15e', isize, this.css.fc()  );
    this.cx.dtext(x+isize+10,y,isize,this.css.fc(), mes_data.grupa_task )
};

MES_View_constructor.prototype.task_braki_ind1=function(nrg,x,y,isize){
    var b=mes_data.count(nrg,2,2);
    if (b==0) {return 0}
    this.cx.frame(x,y,isize,isize,1,'red');
    this.cx.drawIco(x,y,'\uf5d1',isize,'red');
    this.task_label_braki(nrg,x+(isize)+10,y,isize/2);
    this.task_label_brakip(nrg,x+(isize)+10,y+isize/2,isize/2);
};

MES_View_constructor.prototype.task_box=function(nrg,x,y,styl){
    var cx=this.cx;
    var css=this.css;
    var bwidth=420;
    var bheight=200;
    var tfs=22;
    var dfs=35;
    var fs=55;
    var dtfs=14;

    if(styl==1){
        bheight=270;
        bwidth=480;
        fs=75;
        dfs=45;
        tfs=28;
    }
    if(styl==2){
        bheight=180;
        bwidth=400;
        fs=45;
        dfs=25;
        tfs=18;
        fs=28;
    }
    if(styl==3){
        bheight=180;
        bwidth=390;
        fs=55;
        dfs=25;
        tfs=22;
        fs=28;
        dtfs=18;
    }
    if(styl==4){
        bheight=190;
        bwidth=340;
        fs=55;
        dfs=25;
        tfs=20;
        fs=28;
        dtfs=18;
    }

    if (mes_data.cfg_tryb(nrg)!=0){ cx.dtext(x,y,18,this.css.fcoff(), 'Not use'  );  return false}

    if(  !mes_data.cfg_task_use(nrg) ){

        if (mes_data.cfg_task_view_count(nrg) ){
            //this.task_label_count(nrg, x + 10, y+10 , 30);
            this.time_range_ico(x+10,y+10, 1 );
            cx.dtext(x+50,y+8,36,this.css.fc(), numFC(mes_data.count(nrg,1,1))  )
        }

        return false}

    if (  mes_data.task_id(nrg)==0  ){
        cx.dtext(x,y,tfs,css.fcoff(),lang[163])
    }else {

        this.task_label_name(nrg,x,y,tfs);
        this.task_label_product(nrg,x,y+(1.5*tfs),tfs);
        this.task_label_start_i(nrg,x,y+(2.8*tfs),dtfs,css.fc());

        if(mes_data.cfg_task_target(nrg)){
            //var t = mes_data.task_target(nrg) - mes_data.count(nrg, 2, 1);
            var fb = false;
            var bcc=css.barc();
            if ( (mes_data.task_target(nrg) - mes_data.count(nrg, 2, 1)) <0) {   fb = canvas_flash.f1s   }
            if (fb){bcc='gray'};
            this.task_braki_ind1(nrg, x + bwidth / 2, y + 100, 50);
            cx.barVMark(x, y, bwidth, bheight, 35, mes_data.task_prc(nrg), bcc, 'gray', css.fc(), 2, 20, false);
            //cx.barVMark(x, y, bwidth, bheight, 35, mes_data.task_prc(nrg), css.barc(), 'gray', css.fc(), 2, 20, false);

            cx.number_float(x, y + bheight - fs - 10, mes_data.task_prc(nrg), fs, fs / 2, css.fc(), '', '%');
            cx.lineV(x + bwidth - 10, y - 10, y, 2, css.fc());
            cx.lineH(x + bwidth - 10, x + bwidth + 20, y - 10, 2, css.fc());
            this.task_label_target(nrg, x + bwidth + 30, y - tfs, tfs);
            var dy = 0;
            if (mes_data.task_prc(nrg) > 80) {   dy = dfs   }
            this.task_label_count(nrg, cx.xx + 10, cx.yy - dfs + dy, dfs);
            this.task_label_todo(nrg, cx.xx + 10, cx.yy + dy, tfs, 'red')
            cx.dtext(x, y + bheight + 5, tfs - 4, css.fc(), lang[164]+': ' + mes_data.task_prognoza(nrg))
        }else{
            if (mes_data.cfg_task_view_count(nrg)){ this.task_label_count(nrg, x, y+(4*tfs), dfs);   }
        }
    }
};

MES_View_constructor.prototype.task_box_mini=function(nrg,x,y,styl,box){
    if(  !mes_data.cfg_task_use(nrg) ){ return false}
    var cx=this.cx;
    var css=this.css;
    var bwidth=200;
    var bheight=60;
    var tfs=14;
    var fs=30;
    if(  !mes_data.cfg_task_use(nrg) ){ return false}
    if (styl==1){ bwidth=300  }
    if (styl==2){ bwidth=300; bheight=45  }
    if (styl==3){ bwidth=300; bheight=90  }
    if (styl==4){ bwidth=400; bheight=100; tfs=18  }
    if (styl==5){ bwidth=300; bheight=130; tfs=18  }
    if (styl==6){ bwidth=400; bheight=140; tfs=18  }
    if (styl==7){ bwidth=250; bheight=75; tfs=15  }
    if (styl==8){ bwidth=300; bheight=75; tfs=15  }

    if (mes_data.cfg_tryb(nrg)!=0){ cx.dtext(x,y,18,this.css.fcoff(), 'Not use'  );  return false}

    if(box){cx.hud_corners(x,y,bwidth+8,bheight+8,20,1,this.css.fcoff(),0,0 );  x=x+4; y=y+4 }

    if (  mes_data.task_id(nrg)==0  ){
        cx.dtext(x,y,tfs,css.fcoff(),lang[163])
    }else {
        this.task_label_name(nrg,x,y,tfs);
        if (styl==3 || styl==4 || styl==5|| styl==6 || styl==7){ this.task_label_product(nrg,x,y+25,tfs);  }
        if (styl==5|| styl==6){
            var scc=numFC(mes_data.count(nrg,2,1))+' / ';
            cx.dtext(x,y+60,tfs,css.fc() ,scc );
            cx.dtext(x + cx.len(scc)  ,y+62,tfs-3,css.ry() ,numFC( mes_data.task_target(nrg) -  mes_data.count(nrg,2,1)) );
        }
        cx.barVMark(x,y,bwidth,bheight, 20, mes_data.task_prc(nrg)  ,this.css.barc(),'gray', css.fc()  ,1,0,false);
        cx.number_float(x,y+bheight-fs-2,mes_data.task_prc(nrg) ,fs,fs/2,css.fc(),'','%')

        if (styl==8){  cx.dtext(x, y + bheight + 5, tfs - 2, css.fc(), lang[164]+': ' + mes_data.task_prognoza(nrg)) }

    }
};

MES_View_constructor.prototype.task_micro_line_bar=function(  nr, x,y, bar_width,bar_height ){
    if (mes_data.cfg_tryb(nr)!=0  || mes_data.task_id(nr)==0){  return false}

    this.cx.frame(x,y, bar_width, bar_height,1, 'black','gray' );
    var b=true;
    if (mes_data.task_prc(nr)>100){ b= canvas_flash.f1s }
    if (b){  this.cx.barH(x+1,y+1,bar_width-1,bar_height-1,  mes_data.task_prc(nr), 'gray'    ,'#B5E61D');  }


};


///////// Panele operatorskie
MES_View_constructor.prototype.po_nabel=function(x,y,aname){
    y=y-6;
    this.cx.frame(x,y,140,20,1,this.css.bl(), this.css.bg());
    this.cx.hud_corners_rect(3,5,1,this.css.bl(),0 );
    this.cx.dtext(x+8,y+3,15,this.css.fc(),aname)
};

MES_View_constructor.prototype.po_cmt=function(x,y,styl){
    var w=430;
    var h=110;
    if(styl==3){
        w=350;
    }
    this.panel(x,y,w,h);
    this.btny=y+20;
    this.btnx=x+10;
    this.po_nabel(x+10,y,lang[165]);
};

MES_View_constructor.prototype.po_status=function(nrg,x,y,styl,nrc){
    var cx=this.cx;
    var css=this.css;
    var w=750;
    var h=230;
    var isize=90;
    var fs=20;
    var fs2=20;

    if(styl==0){ //m-pionowo
        w=550;
        h=130;
        this.panel(x,y,w,h);
        this.status_rect(nrg,x+1,y+1,30,128,false);
        this.status_rect(nrg,x+530,y+1,20,128,false);
        this.status_ind1(nrg,x+40,y+20,40);
        this.cx.textIco(x+40,y+70, mes_data.stat_time(nrg)+'   [ '+ mes_data.stat_time_s(nrg)+' ]'   ,'\uf1d2',14,this.css.fc(),this.css.fc() );
        this.lamp(nrg,x+40,y+90,30,12);
        this.work_indicator(nrg,x+90,y+90,14,18,250);
    }
    if(styl==1){ //m-poziomo
        w=460;
        h=140;
        this.panel(x,y,w,h);
        this.status_rect(nrg,x+1,y+1,30,138,false);
        this.status_rect(nrg,x+440,y+1,20,138,false);
        this.status_ind1(nrg,x+40,y+20,40);
        this.cx.textIco(x+40,y+70, mes_data.stat_time(nrg)+'   [ '+ mes_data.stat_time_s(nrg)+' ]'   ,'\uf1d2',14,this.css.fc(),this.css.fc() );
        this.lamp(nrg,x+40,y+90,30,15);
        this.work_indicator(nrg,x+90,y+90,14,18,250);
    }
    if(styl==2){ //-d poziomo

        this.panel(x,y,w,h);
        this.status_rect(nrg,x+1,y+1,40,228,true);
        this.status_rect(nrg,x+730,y+1,20,228,true);
        this.status_ind1(nrg,x+50,y+30,60);
        //this.cx.textIco(x+370,y+10, mes_data.stat_time(nrg)+'   [ '+ mes_data.stat_time_s(nrg)+' ]'   ,'\uf1d2',14,this.css.fc(),this.css.fc() );
        this.status_time(nrg,x+370,y+10,14,this.css.fc());

        this.status_chart_fx(nrg,nrc,x+280, y+80, 42, 14,true, true,2 );
        this.time_range_ico(x+700,y+90, nrc );

        this.panel(x+50,y+110,200,110 , 1, mes_data.lamp_state(nrg)==4 ,0);
        this.lamp(nrg,x+60,y+120,40,30);
        this.work_indicator(nrg,x+109,y+120,20,18,250);
        this.time_to_end_indicator(nrg,x+130,y+140,18,0,0);
        this.performance_indicator(nrg,x+110,y+170,18);
        this.ctime_indicator(nrg,x+110,y+195,18,false );

    }
    if(styl==3){ //pos
        w=590;
        h=240;
        this.panel(x,y,w,h);
        this.status_rect(nrg,x+1,y+1,20,228,true);
        this.status_rect(nrg,x+570,y+1,20,228,true);
        this.status_ind1(nrg,x+30,y+30,60);
        this.status_time(nrg,x+200,y+10,14,this.css.fc());

        this.status_chart_fx(nrg,nrc,x+250, y+120, 30, 12,true, true,2 );

        this.panel(x+30,y+110,200,110 , 1, mes_data.lamp_state(nrg)==4 ,0);
        this.lamp(nrg,x+35,y+120,40,30);
        this.work_indicator(nrg,x+80,y+120,20,18,250);
        this.time_to_end_indicator(nrg,x+110,y+140,18,0,0);
        this.performance_indicator(nrg,x+85,y+170,18);
        this.ctime_indicator(nrg,x+85,y+195,18,false );


         }

    this.btny=y+10;
    this.btnx=x+w-100;
    this.po_nabel(x+10,y,'Status')
};

MES_View_constructor.prototype.po_task=function(nrg,x,y,styl){
    var that=this;
    function _set(wt,ht){
        w=wt;  h=ht;
        that.panel(x,y,w,h);
        that.po_nabel(x+10,y,lang[162]);
        that.btny=y+10;
        that.btnx=x+w-100;
    }

    var cx=this.cx;
    var css=this.css;
    var w=750;
    var h=280;
    var isize=90;
    var fs=20;
    var fs2=20;

    if(styl==0){ //m-pionowo
        _set(550,200);
        this.task_box_mini(nrg,x+10,y+30,6,false)
    }
    if(styl==1){ //m-poziomo
        //w=460;
        //h=180;
        _set(460,180);
        this.task_box_mini(nrg,x+10,y+30,5,false)

    }
    if(styl==2) {
        _set(750,280);
        this.task_box(nrg, x + 30, y + 40, 0);
    }
    if(styl==3) {
        _set(590,280);
        this.task_box(nrg, x + 30, y + 40, 4);
    }

};

MES_View_constructor.prototype.po_andon=function(nrg, x, y, styl){
    var that=this;
    function _set(wt,ht){
        w=wt;  h=ht;
        that.panel(x,y,w,h);
        that.po_nabel(x+10,y,'Andon');
        that.btny=y+10;
        that.btnx=x+w-100;
    }

    var cx=this.cx;
    var css=this.css;
    var w=430;
    var h=160;
    var anr= mes_data.andon_nr(nrg);

    if(styl==0){ //m-pionowo

        _set(550,110);
        this.andon_ico(nrg,x+10,y+10,100,false,true);
        this.andon_label_name(nrg,x+110,y+80,20);
        if (anr!=0){
            cx.dtext(x+110,y+40 ,16, css.fc()  , mes_data.andon_time_s(nrg));
            cx.dtext(x+110,y+60 ,16, css.fc()  , mes_data.andon_user(nrg))
        }
    }
    if(styl==1){ //m-poziomo
        _set(400,140);
        this.andon_ico(nrg,x+10,y+10,100,false,true);
        this.andon_label_name(nrg,x+10,y+110,20);
        if (anr!=0){
            cx.dtext(x+110,y+40 ,16, css.fc()  , mes_data.andon_time_s(nrg));
            cx.dtext(x+110,y+60 ,16, css.fc()  , mes_data.andon_user(nrg))
        }
    }
    if(styl==2){ //d-poziomo
         _set(430,160);
        this.andon_ico(nrg,x+10,y+10,100,false,true);
        this.andon_label_name(nrg,x+10,y+110,20);
        this.andon_label_cmt(nrg,x+10,y+132,14);
        if (anr!=0){
            if(canvas_flash.ffast &&  css.hud){  cx.setBlur('yellow',20,1,1); }
            cx.romb_line(x+10,y+150,20,10,-3,24,css.ry() );
            cx.resetBlur();
            cx.dtext(x+110,y+40 ,16, css.fc()  , mes_data.andon_time_s(nrg));
            cx.dtext(x+110,y+60 ,16, css.fc()  , mes_data.andon_user(nrg))
        }
    }

    if(styl==3){ //fhd
        //w=350;
        //h=140;
        _set(350,140);
        this.andon_ico(nrg,x+10,y+10,90,false,true);
        this.andon_label_name(nrg,x+10,y+90,20);
        this.andon_label_cmt(nrg,x+10,y+112,14);
        if (anr!=0){
            if(canvas_flash.ffast &&  css.hud){  cx.setBlur('yellow',20,1,1); }
            cx.romb_line(x+10,y+130,20,10,-3,19,css.ry() );
            cx.resetBlur();
            cx.dtext(x+110,y+40 ,16, css.fc()  , mes_data.andon_time_s(nrg));
            cx.dtext(x+110,y+60 ,16, css.fc()  , mes_data.andon_user(nrg))
        }
    }

};


MES_View_constructor.prototype.po_operator=function(nrg,x,y,styl){
    var that=this;
    function _set(wt,ht,bx,by){
        w=wt;  h=ht;
        that.panel(x,y,w,h);
        that.po_nabel(x+10,y,lang[171]);
        that.btny=y+by;
        that.btnx=x+w+bx;
    }

    var w=430;
    var h=140;

    if(styl==0){ //m-pionowo
        _set(550,110,-100, 10);
        this.operators_ind1(nrg,x+10,y+30,36,0);
        this.operators_ind1(nrg,x+10,y+75,20,1);
        this.operators_ind1(nrg,x+220,y+75,20,2);
    }
    if(styl==1){ //m-poziomo
        _set(400,110,-100, 10);
        this.operators_ind1(nrg,x+10,y+20,30,0);
        this.operators_ind1(nrg,x+10,y+55,20,1);
        this.operators_ind1(nrg,x+10,y+80,20,2);
    }
    if(styl==2){ //d-poziomo
       _set(430,140,-100, 10);
        this.operators_ind1(nrg,x+10,y+25,40,0);
        this.operators_ind1(nrg,x+10,y+90,30,1);
        this.operators_ind1(nrg,x+200,y+90,30,2);


    }
    if(styl==3){ //fhd
       _set(350,160,-100,80);
        this.operators_ind1(nrg,x+10,y+25,40,0);
        this.operators_ind1(nrg,x+10,y+80,30,1);
        this.operators_ind1(nrg,x+10,y+120,30,2);

    }
};


////////////////////////////////////////////

function MES_View_constructor(parent,aw,ah) {

    var that = this;
    this.paused=false;
    this.btnx=0;
    this.btny=0;
    this.cx ='';
    this.css = new Canvas_css_constructor( nApp.cfgi.theme_style );
    this.init=function(call_draw   ){
        this.cx = new Canvas_tool_constructor( canvas_in_div(parent,$(parent).innerWidth(),$(parent).innerHeight() ) );
        var flag=false;
        nApp.BindEvent(function(ev,a2,a3){
            if(a2==1) {
            if(!that.paused){
                if (!flag){
                    flag=true;
                    is_callback(call_draw(that));
                    flag=false;
                }
            }
            }
        });
    };
}





/***********************************************************
 message box - klasa i obiekt wyskakujacego okienka z komunikatami
 o zmianie Andon i statusu
 /***********************************************************/
function MES_MessageBox_constructor(){

    var that=this;
    this.audio_mode=0;
    this.mb= '';
    this._audio_loop=0;

    this.init=function(){
        this.audio_mode=mes_set.audio_alarm;

        $("#kit_right_slide_box").css({'width':1000,'height':600,'background-color':'red' });
        this.mb = new MES_View_constructor( "#kit_right_slide_box" ,1000,600 );
        that.mb.paused=true;
        this.mb.init(  function(){
            that.mb.cx.clear(false,false);
            var cx= that.mb.cx;
            cx.frame(40,40,920,120,4,'black','black');
            cx.dtext(60,60,90,'white',mes_data.msg_box.svname );
            cx.frame(40,160,920,400,4,'black', mes_data.msg_box.bgc );
            cx.dtext(60,180,60,'black',mes_data.msg_box.name1 );
            cx.dtext(60,250,60,'black',mes_data.msg_box.name2 );

            if(mes_data.msg_box.mode==0){
                cx.drawIco(60, 300, state_ico_code[ mes_data.msg_box.st_nr  ] , 260 , 'black'  );
            }else{
                cx.drawIco(60, 300, andon_ico_code , 260 , 'black'  );
            }

        });
        //audio
        if(this.audio_mode!=0){
            setInterval( function(){
                if (that._audio_loop>0 ){
                    --that._audio_loop;
                    audioElement.setAttribute('src', '../mes_lib/alarm1.mp3'  );
                    audioElement.play();
                }
            } , 2500);
        }
    };
    this.start=function(){
        nApp.view.right_box_slide(true);
        that.mb.paused=false;
        setTimeout( function(){ nApp.view.right_box_slide(false);  that.mb.paused=true;  } , 6000);

        if(mes_data.msg_box.mode==0){
            if(this.audio_mode==2 && mes_data.msg_box.st_nr==5  ){  that._audio_loop=6 }
        } else {
            if(this.audio_mode>0  ){  that._audio_loop=6 }
        }

    }
}

var Mes_Messagebox = new MES_MessageBox_constructor();