"use strict";

var GAP_TOP = 0.094492;
var SIGN_GAP = 0.077108;

var RADICAL_H0 = 1.2;
var RADICAL_H1 = 1.50732421875;
var RADICAL_H2 = 2.8;
var RADICAL_H3 = 4.08;
var RADICAL_H4 = 5.7;
var RADICAL_H5 = 7.15;


/// Gap = 0.2513

function CSignRadical()
{
    this.Parent = null;
    this.pos = null;

    this.size = null;
    this.gapArg = 0;
    this.gapSign = 0;   /// расстояние до значка радикала

    this.measure =
    {
        heightTick:     0,
        widthTick:      0,
        widthSlash:     0,
        bHigh:          false
    };

    //this.sizeTick = null;
    //this.widthSlash = null;
}
CSignRadical.prototype.new_draw = function(x, y, pGraphics)
{
    var txtPrp = this.Parent.Get_CompiledCtrPrp();
    //var txtPrp = this.Parent.getTxtPrp();
    var penW = txtPrp.FontSize*g_dKoef_pt_to_mm*0.042;

    y += this.gapTop + penW/2; // смещаем, для отрисовки верхней линии радикала

    var x1 = this.pos.x + x,
        x2 = x1 + 0.048*txtPrp.FontSize;

    var Height = this.size.height - this.gapTop;

    var y2 = this.pos.y + y + Height - this.measure.heightTick,
        y1 = y2 + 0.0242*txtPrp.FontSize;

    var tg =  0.048/0.0242;
    var tX = tg*0.85*penW,
        tY = 0.92*penW / tg;

    var x3 = x2,
        y3 = y2 - tY;

    //var plH = 9.877777777777776 * txtPrp.FontSize /36;

    var sin = 0.876,
        cos = 0.474;

    var y4 = this.pos.y + y + Height - penW;
    var y5 = y4 + penW/2*cos;

    // 0.048*txtPrp.FontSize + (- penW + this.measure.heightTick  + 0.92*penW / tg )/ tg

    var x4, x5;

    if( !this.measure.bHigh )
    {
        x4 = x3 + (y4-y3)/tg;
        x5 = x4 + penW/2*sin;
    }
    else
    {
        x4 = x1 + this.measure.widthSlash - penW/3*sin;
        x5 = x1 + this.measure.widthSlash;
    }


    var x6 = x1 + this.measure.widthSlash,
        x7 = this.pos.x + x + this.size.width;

    var y6 = this.pos.y + y,
        y7 = this.pos.y + y;


    pGraphics.p_width(penW*0.8*1000);

    pGraphics.p_color(0,0,0, 255);
    pGraphics.b_color1(0,0,0, 255);

    pGraphics._s();
    pGraphics._m(x1, y1);
    pGraphics._l(x2, y2);
    pGraphics.ds();


    pGraphics.p_width(1.7*penW*1000);
    pGraphics._s();
    pGraphics._m(x3, y3);
    pGraphics._l(x4, y4);
    pGraphics.ds();

    pGraphics.p_width(penW*1000);


    pGraphics.p_color(0,0,0, 255);
    pGraphics.b_color1(0,0,0, 255);

    pGraphics.p_width(penW*1000);
    pGraphics._s();
    pGraphics._m(x5, y5);
    pGraphics._l(x6, y6);
    pGraphics._l(x7, y7);
    pGraphics.ds();

    pGraphics.p_color(0,0,0, 255);
    pGraphics.b_color1(0,0,0, 255);

    pGraphics._s();
    pGraphics._m(x4 - penW*0.6*sin, y4 - penW/5);
    pGraphics._l(x5 + penW/3*sin, y4 - penW/5);
    pGraphics.ds();

}
CSignRadical.prototype.draw = function(x, y, pGraphics)
{
    var txtPrp = this.Parent.Get_CompiledCtrPrp();
    //var txtPrp = this.Parent.getTxtPrp();
    var penW = txtPrp.FontSize*g_dKoef_pt_to_mm*0.042;

    y += penW/2 + this.gapSign; // смещаем, для отрисовки верхней линии радикала


    //// Tick for degree ////
    var x1 = this.pos.x + x,
        x2 = x1 + 0.048*txtPrp.FontSize;

    var Height = this.size.height - this.gapSign;

    var y2 = this.pos.y + y + Height - this.measure.heightTick,
        y1 = y2 + 0.0242*txtPrp.FontSize;

    var tg =  0.048/0.0242;
    var tX = tg*0.85*penW,
        tY = 0.92*penW / tg;

    var x3 = x2,
        y3 = y2 - tY;
    //////////////////////

    //// Tick lower ////

    var sin = 0.876,
        cos = 0.474;

    var y4 = this.pos.y + y + Height - penW;
    var y7 = y4 + penW/2*cos;


    var x4, x7;

    if( !this.measure.bHigh )
    {
        x4 = x3 + (y4-y3)/tg;
        x7 = x4 + penW/2*sin;
    }
    else
    {
        x4 = x1 + this.measure.widthSlash - penW/3*sin;
        x7 = x1 + this.measure.widthSlash;
    }

    var x5 = x4 - penW*0.6*sin, y5 = y4 - penW/5,
        x6 = x7 + penW/3*sin,   y6 = y5;

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

    /// Line for argument

    var x8 = x1 + this.measure.widthSlash,
        x9 = this.pos.x + x + this.size.width;

    var y8 = this.pos.y + y,
        y9 = this.pos.y + y;

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


    pGraphics.p_width(penW*0.8*1000);

    pGraphics.p_color(0,0,0, 255);
    pGraphics.b_color1(0,0,0, 255);

    pGraphics._s();
    pGraphics._m(x1, y1);
    pGraphics._l(x2, y2);
    pGraphics.ds();


    pGraphics.p_width(1.7*penW*1000);
    pGraphics._s();
    pGraphics._m(x3, y3);
    pGraphics._l(x4, y4);
    pGraphics.ds();

    pGraphics.p_width(penW*1000);

    pGraphics._s();
    pGraphics._m(x5, y5);
    pGraphics._l(x6, y6);
    pGraphics.ds();


    pGraphics.p_width(penW*1000);
    pGraphics._s();
    pGraphics._m(x7, y7);
    pGraphics._l(x8, y8);
    pGraphics._l(x9, y9);
    pGraphics.ds();


}
CSignRadical.prototype.recalculateSize = function(oMeasure, sizeArg)
{
    var txtPrp = this.Parent.Get_CompiledCtrPrp();

    var height, width;
    var plH = 9.877777777777776 * txtPrp.FontSize/36;

    this.gapArg = txtPrp.FontSize*g_dKoef_pt_to_mm*0.077108; /// расстояние до аргумента
    this.gapSign = txtPrp.FontSize*g_dKoef_pt_to_mm*0.094492; /// расстояние до значка радикала

    var heightArg = sizeArg.height + this.gapArg,
        widthArg  = sizeArg.width;


    //////////  Height  ///////////

    /*var H0 = plH*1.2,
     H1 = plH*1.50732421875,
     H2 = plH*2.760986328125,
     H3 = plH*4.217578125,
     H4 = plH*5.52197265625,
     H5 = plH*7.029296875;*/

    var H0 = plH*1.07,
    //H1 = plH*1.50732421875,
        H1 = plH*1.6234788833214036,
        H2 = plH*2.8,
        H3 = plH*4.08,
        H4 = plH*5.7,
        H5 = plH*7.15;


    /*console.log("heightArg :" + heightArg);
     console.log("plH :" + plH);
     console.log("ShiftCenter: " + shCenter);

     var k1 = heightArg/plH,
     k2 = heightArg/shCenter;

     console.log("heightArg/plH :" + k1);
     console.log("heightArg/shCenter :" + k2);*/

    this.measure.bHigh = false;

    var bDescentArg = sizeArg.height - sizeArg.ascent > 0.4*txtPrp.FontSize/11; // т.к. у нас почему-то для строчных букв "а" и тп descent не нулевой, см метрики в mathText.js

    if(heightArg < H0 && !bDescentArg)
        height = H0*1.12;
    //height = H0*1.058;
    else if( heightArg < H1)
        height = H1*0.9284532335069441;
    //height = H1;
    else if( heightArg < H2 )
        height = H2;
    else if( heightArg < H3 )
        height = H3*1.04;
    else if( heightArg < H4 )
        height = H4;
    else if( heightArg < H5 )
        height = H5;
    else
    {
        height = heightArg;
        this.measure.bHigh = true;
    }

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


    //////////  Size of tick  //////////

    var minHgtRad = plH * 1.130493164,
        maxHgtRad = plH * 7.029296875;

    var minHgtTick = plH*0.6,
        maxHgtTick = 1.2*plH;

    var heightTick, widthSlash,
        gapLeft;

    if ( heightArg > maxHgtRad )
    {
        heightTick = maxHgtTick;
        widthSlash = plH * 0.67;

        gapLeft = 0.2*plH;
    }
    else
    {
        var H;

        if(heightArg < H1)
        {
            H = H1;
            var zetta = height < H1 ? 0.75 : 0.82;
            widthSlash = plH *zetta;
        }
        else
        {
            H = height;
            widthSlash = plH * 0.8681086138556986;
        }
        var alpha =  (H - minHgtRad)/(2*maxHgtRad);
        heightTick = minHgtTick*(1 + alpha);

        gapLeft = 0.12683105468750022* plH;
    }


    this.measure.widthSlash = widthSlash;

    this.measure.heightTick = heightTick;
    this.measure.widthTick = 0.1196002747872799*txtPrp.FontSize;

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

    //////////   WidthБ Height  //////////
    width = widthSlash + gapLeft + widthArg;
    height += this.gapSign;
    //////////////////////////////

    this.size = {height: height, width: width};
}
CSignRadical.prototype.old_recalculateSize = function()
{
    var txtPrp = this.Parent.Get_CompiledCtrPrp();
    var sizeArg = this.Parent.getBase().size;

    var height, width;
    var plH = 9.877777777777776 * txtPrp.FontSize/36;

    var SUM = 0.1216 + 0.05;
    SIGN_GAP = SUM - GAP_TOP;


    var gapSign = txtPrp.FontSize*g_dKoef_pt_to_mm*SIGN_GAP,
        heightArg = sizeArg.height + gapSign,
        widthArg  = sizeArg.width;


    this.gapTop = txtPrp.FontSize*g_dKoef_pt_to_mm*GAP_TOP;
    this.gapSign = gapSign;


    //  this.gapTop = txtPrp.FontSize*g_dKoef_pt_to_mm*SIGN_GAP;

    //console.log("Gap sign : " + gapSign);

    /////  height  //////

    /*var H0 = plH*1.2,
        H1 = plH*1.50732421875,
        H2 = plH*2.760986328125,
        H3 = plH*4.217578125,
        H4 = plH*5.52197265625,
        H5 = plH*7.029296875;*/


    var H0 = RADICAL_H0*plH,
        H1 = RADICAL_H1*plH,
        H2 = RADICAL_H2*plH,
        H3 = RADICAL_H3*plH,
        H4 = RADICAL_H4*plH,
        H5 = RADICAL_H5*plH;


    //    RADICAL_GAP =0.1216;
    //    SIGN_GAP  = 0.05;

    /*var H0 = plH*1.0992;
    var H1 = plH*1.56542421875;
    var H2 = plH*2.8275863281249998;
    var H3 = plH*4.154778125;
    var H4 = plH*5.7241726562499995;
    var H5 = plH*7.167896874999999;*/

      /*H0 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
      H1 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
      H2 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
      H3 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
      H4 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
      H5 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;*/

    /*H0 += gapSign;
    H1 += gapSign;
    H2 += gapSign;
    H3 += gapSign;
    H4 += gapSign;
    H5 += gapSign;*/


    this.measure.bHigh = false;

    if( heightArg < H0 )
        height = H0*1.058;
    else if( heightArg < H1 )
        height = H1;
    else if( heightArg < H2 )
        height = H2;
    else if( heightArg < H3 )
        height = H3*1.04;
    else if( heightArg < H4 )
        height = H4;
    else if( heightArg < H5 )
        height = H5;
    else
    {
        height = heightArg;
        this.measure.bHigh = true;
    }

    /*if(this.gapTop < g_dKoef_pt_to_mm)
    {

        this.gapTop = g_dKoef_pt_to_mm;
        height += g_dKoef_pt_to_mm;

    }*/

    //////

    /*console.log("H0: "+ H0);
    console.log("H1: "+ H1);
    console.log("H2: "+ H2);
    console.log("H3: "+ H3);
    console.log("H4: "+ H4);
    console.log("H5: "+ H5);*/

    console.log("SIGN_GAP :" + SIGN_GAP);
    console.log("GAP_TOP  : " + GAP_TOP);
    console.log("RADICAL_H0: "+ H0/plH);
    console.log("RADICAL_H1: "+ H1/plH);
    console.log("RADICAL_H2: "+ H2/plH);
    console.log("RADICAL_H3: "+ H3/plH);
    console.log("RADICAL_H4: "+ H4/plH);
    console.log("RADICAL_H5: "+ H5/plH);

    ///// Size of tick //////
    var minHgtRad = plH * 1.130493164,
        maxHgtRad = plH * 7.029296875;

    var minHgtTick = plH*0.6,
        maxHgtTick = 1.2*plH;

    var heightTick, widthSlash,
        gapLeft;

    if ( heightArg > maxHgtRad )
    {
        heightTick = maxHgtTick;
        widthSlash = plH * 0.67;

        gapLeft = 0.2*plH;
    }
    else
    {
        var H;

        if(heightArg < H1)
        {
            H = H1;
            var zetta = height < H1 ? 0.75 : 0.82;
            widthSlash = plH *zetta;
        }
        else
        {
            H = height;
            widthSlash = plH * 0.8681086138556986;
        }
        var alpha =  (H - minHgtRad)/(2*maxHgtRad);
        heightTick = minHgtTick*(1 + alpha);

        gapLeft = 0.12683105468750022* plH;
    }


    this.measure.widthSlash = widthSlash;

    this.measure.heightTick = heightTick;
    this.measure.widthTick = 0.1196002747872799*txtPrp.FontSize;

    ////// width, height //////
    width = widthSlash + gapLeft + widthArg;
    height += this.gapTop;
    //////

    //console.log("Gap top : " + this.gapTop);

    /*console.log("SIGN_GAP: " + SIGN_GAP);
    console.log("RADICAL_GAP: " + RADICAL_GAP);
    console.log("RADICAL_H0: " + RADICAL_H0);
    console.log("RADICAL_H1: " + RADICAL_H1);
    console.log("RADICAL_H2: " + RADICAL_H2);
    console.log("RADICAL_H3: " + RADICAL_H3);
    console.log("RADICAL_H4: " + RADICAL_H4);
    console.log("RADICAL_H5: " + RADICAL_H5);*/


    //console.log("Height: " + height);

    this.size = {height: height, width: width};
}
CSignRadical.prototype.old_old_recalculateSize = function()
{
    var txtPrp = this.Parent.Get_CompiledCtrPrp();
    var sizeArg = this.Parent.getBase().size;

    var height, width;
    var plH = 9.877777777777776 * txtPrp.FontSize/36;

    var SUM = 0.1216 + 0.05;
    SIGN_GAP = SUM - GAP_TOP;

    var gapSign = txtPrp.FontSize*g_dKoef_pt_to_mm*SIGN_GAP,
        heightArg = sizeArg.height + gapSign,
        widthArg  = sizeArg.width;


    this.gapTop = txtPrp.FontSize*g_dKoef_pt_to_mm*GAP_TOP;
    this.gapSign = gapSign;

    //  this.gapTop = txtPrp.FontSize*g_dKoef_pt_to_mm*SIGN_GAP;

    //console.log("Gap sign : " + gapSign);

    /////  height  //////

    var H0 = plH*1.2,
        H1 = plH*1.50732421875,
        H2 = plH*2.760986328125,
        H3 = plH*4.217578125,
        H4 = plH*5.52197265625,
        H5 = plH*7.029296875;

    //    RADICAL_GAP =0.1216;
    //    SIGN_GAP  = 0.05;

    /*var H0 = plH*1.0992;
     var H1 = plH*1.56542421875;
     var H2 = plH*2.8275863281249998;
     var H3 = plH*4.154778125;
     var H4 = plH*5.7241726562499995;
     var H5 = plH*7.167896874999999;*/

    H0 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
    H1 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
    H2 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
    H3 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
    H4 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;
    H5 += gapSign - txtPrp.FontSize*g_dKoef_pt_to_mm*0.2;

    /*H0 += gapSign;
     H1 += gapSign;
     H2 += gapSign;
     H3 += gapSign;
     H4 += gapSign;
     H5 += gapSign;*/


    this.measure.bHigh = false;

    if( heightArg < H0 )
        height = H0*1.058;
    else if( heightArg < H1 )
        height = H1;
    else if( heightArg < H2 )
        height = H2;
    else if( heightArg < H3 )
        height = H3;
    else if( heightArg < H4 )
        height = H4;
    else if( heightArg < H5 )
        height = H5;
    else
    {
        height = heightArg;
        this.measure.bHigh = true;
    }

    //////

    /*console.log("H0: "+ H0);
     console.log("H1: "+ H1);
     console.log("H2: "+ H2);
     console.log("H3: "+ H3);
     console.log("H4: "+ H4);
     console.log("H5: "+ H5);*/

    console.log("SIGN_GAP :" + SIGN_GAP);
    console.log("GAP_TOP  : " + GAP_TOP);
    console.log("RADICAL_H0: "+ H0/plH);
    console.log("RADICAL_H1: "+ H1/plH);
    console.log("RADICAL_H2: "+ H2/plH);
    console.log("RADICAL_H3: "+ H3/plH);
    console.log("RADICAL_H4: "+ H4/plH);
    console.log("RADICAL_H5: "+ H5/plH);

    ///// Size of tick //////
    var minHgtRad = plH * 1.130493164,
        maxHgtRad = plH * 7.029296875;

    var minHgtTick = plH*0.6,
        maxHgtTick = 1.2*plH;

    var heightTick, widthSlash,
        gapLeft;

    if ( heightArg > maxHgtRad )
    {
        heightTick = maxHgtTick;
        widthSlash = plH * 0.67;

        gapLeft = 0.2*plH;
    }
    else
    {
        var H;

        if(heightArg < H1)
        {
            H = H1;
            var zetta = height < H1 ? 0.75 : 0.82;
            widthSlash = plH *zetta;
        }
        else
        {
            H = height;
            widthSlash = plH * 0.8681086138556986;
        }
        var alpha =  (H - minHgtRad)/(2*maxHgtRad);
        heightTick = minHgtTick*(1 + alpha);

        gapLeft = 0.12683105468750022* plH;
    }


    this.measure.widthSlash = widthSlash;

    this.measure.heightTick = heightTick;
    this.measure.widthTick = 0.1196002747872799*txtPrp.FontSize;

    ////// width, height //////
    width = widthSlash + gapLeft + widthArg;
    height += this.gapTop;
    //////

    //console.log("Gap top : " + this.gapTop);

    /*console.log("SIGN_GAP: " + SIGN_GAP);
     console.log("RADICAL_GAP: " + RADICAL_GAP);
     console.log("RADICAL_H0: " + RADICAL_H0);
     console.log("RADICAL_H1: " + RADICAL_H1);
     console.log("RADICAL_H2: " + RADICAL_H2);
     console.log("RADICAL_H3: " + RADICAL_H3);
     console.log("RADICAL_H4: " + RADICAL_H4);
     console.log("RADICAL_H5: " + RADICAL_H5);*/


    //console.log("Height: " + height);

    this.size = {height: height, width: width};
}
CSignRadical.prototype.old_draw = function(x, y, pGraphics)
{
    var txtPrp = this.Parent.Get_CompiledCtrPrp();
    //var txtPrp = this.Parent.getTxtPrp();
    var penW = txtPrp.FontSize*g_dKoef_pt_to_mm*0.042;

    y += penW/2; // смещаем, для отрисовки верхней линии радикала

    var plH = 9.877777777777776 * txtPrp.FontSize /36;

    var x1 = this.pos.x + x,
        x2 = x1 + 0.25*this.widthSlash;

    var y2 = this.pos.y + y + this.size.height - this.sizeTick.height,
        y1 = y2 + 0.11*this.widthSlash;


    var tX = 1.7*penW * 0.5 * 25.4/96,
        tY = (-1)*tX * 11/25 *0.5; // 11/25 - тангенс угла наклона

    var x3 = x2 - tX,
        y3 = y2 - tY;

    var x4;

    /*var minHeight = plH * 1.1304931640625,
     maxHeight = plH * 7.029296875;

     var maxWidth = plH * 0.81171875;

     var k = 0.2*maxWidth/(maxHeight - minHeight),
     b = maxWidth*0.3 - k*minHeight;*/

    var k = 0.00343247*this.widthSlash,
        b = 0.3*this.widthSlash - 1.130493*plH*k;

    if(this.size.height < plH*7.029296875)
        x4 = x3 + k*this.size.height + b;
    else
        x4 = x1 + this.widthSlash;

    var y4 = this.pos.y + y + this.size.height - penW;

    var x5 = x1 + this.widthSlash,
        x6 = this.pos.x + x + this.size.width;

    var y5 = this.pos.y + y,
        y6 = this.pos.y + y;

    pGraphics.p_width(penW*0.8*1000);

    pGraphics.p_color(0,0,0, 255);
    pGraphics.b_color1(0,0,0, 255);

    pGraphics._s();
    pGraphics._m(x1, y1);
    pGraphics._l(x2, y2);
    pGraphics.ds();


    pGraphics.p_width(1.7*penW*1000);
    pGraphics._s();
    pGraphics._m(x3, y3);
    pGraphics._l(x4, y4);
    pGraphics.ds();

    pGraphics.p_width(penW*1000);
    pGraphics._s();
    pGraphics._m(x4, y4);
    pGraphics._l(x5, y5);
    pGraphics._l(x6,y6);
    pGraphics.ds();

}
CSignRadical.prototype.old_recalculateSize = function()
{
    //var txtPrp = this.Parent.getTxtPrp();
    var txtPrp = this.Parent.Get_CompiledCtrPrp();
    var sizeArg = this.Parent.getBase().size;

    var height, width;

    var top = txtPrp.FontSize*g_dKoef_pt_to_mm*0.15,
        heightArg = sizeArg.height + top,
        widthArg = sizeArg.width;

    ///// height //////
    var plH = 9.877777777777776 * txtPrp.FontSize /36;
    var H0 = plH,
        H1 = plH*1.50732421875,
        H2 = plH*2.760986328125,
        H3 = plH*4.217578125,
        H4 = plH*5.52197265625,
        H5 = plH*7.029296875;

    if(heightArg < H0)
        height = H1*0.75;
    else if( heightArg < H1 )
        height = H1;
    else if( heightArg < H2 )
        height = H2;
    else if( heightArg < H3 )
        height = H3;
    else if( heightArg < H4 )
        height = H4;
    else if(heightArg < H5)
        height = H5;
    else
        height = heightArg;

    //////

    ///// height tick //////
    var minHgtRad = plH * 1.130493164,
        maxHgtRad = plH * 7.029296875;

    var minHgtTick = plH*0.6,
    //maxHgtTick = plH * 1.50732422;
        maxHgtTick = 0.9*plH;

    var heightTick, widthTick;

    if ( heightArg > maxHgtRad )
    {
        heightTick = maxHgtTick;
        widthTick = 0.1196002747872799*txtPrp.FontSize;
    }
    else
    {
        var alpha = (heightArg - minHgtRad)/maxHgtRad;
        heightTick = minHgtTick*(1 + alpha);

        widthTick = 0.1196002747872799*txtPrp.FontSize;
    }

    ////// width //////
    //var widthSlash = plH * 0.9385498046875003;
    var widthSlash = plH * 0.81171875;
    var gap = 0.12683105468750022* plH;
    width = widthSlash + gap + widthArg;
    //////

    this.sizeTick =
    {
        width : widthTick,
        height : heightTick
    };

    this.widthSlash = widthSlash;

    this.size = {height: height, width: width};
}
CSignRadical.prototype.setPosition = function(pos)
{
    this.pos = pos;
}
CSignRadical.prototype.relate = function(parent)
{
    this.Parent = parent;
}

function CRadical(props)
{
    CRadical.superclass.constructor.call(this);

	this.Id = g_oIdCounter.Get_NewId();
    this.kind = MATH_RADICAL;

    this.Iterator = new CMathContent();
    this.Base     = new CMathContent();

    this.RealBase = null;

    this.signRadical = new CSignRadical();
    this.signRadical.relate(this);

    this.Pr =
    {
        type:       DEGREE_RADICAL,
        degHide:    false
    };

    this.gapDegree = 0;
    this.gapWidth = 0; //  в случае со степенью, если ширина степени не нулевая, добавляется расстояние для ширины

    if(props !== null && typeof(props)!== "undefined")
        this.init(props);

	g_oTableId.Add( this, this.Id );
}
Asc.extendClass(CRadical, CMathBase);
CRadical.prototype.init = function(props)
{
    this.setProperties(props);
}
CRadical.prototype.setProperties = function(props)
{
    if(props.type === SQUARE_RADICAL || props.type === DEGREE_RADICAL)
        this.Pr.type = props.type;

    if(props.degHide === true || props.degHide === 1)
    {
        this.Pr.degHide = true;
        this.Pr.type = SQUARE_RADICAL;
    }
    else if(props.degHide == false || props.degHide === 0)
    {
        this.Pr.degHide = false;
        this.Pr.type = DEGREE_RADICAL;
    }

    this.setCtrPrp(props.ctrPrp);
}
CRadical.prototype.fillContent = function()
{
    if(this.Pr.type == SQUARE_RADICAL)
    {
        this.setDimension(1, 1);
        this.setContent();
    }
    else if(this.Pr.type == DEGREE_RADICAL)
    {
        this.setDimension(1, 2);
        this.setContent();
        /*var oBase = new CMathContent();
        var oDegree = new CMathContent();
        oDegree.setArgSize(-2);

        this.addMCToContent([oDegree, oBase]);*/
    }
}
CRadical.prototype.Resize = function(oMeasure, Parent, ParaMath, RPI, ArgSize)
{
    this.Parent = Parent;
    this.ParaMath = ParaMath;

    if(this.RecalcInfo.bProps)
    {
        if(this.Pr.degHide == true)
        {
            this.setDimension(1, 1);

            if(this.Iterator !== null)
            {
                var Item = new CMathBase(true);
                Item.setDimension(1, 2);
                Item.elements[0][0] = this.Iterator;
                Item.elements[0][1] = this.Base;

                //Item.addMCToContent(this.Iterator, this.Base);

                this.elements[0][0] = Item;
            }
            else
            {
                this.elements[0][0] = this.Base;
            }

            this.RealBase = this.elements[0][0];
        }
        else
        {
            this.setDimension(1, 2);
            this.elements[0][0] = this.Iterator;
            this.elements[0][1] = this.Base;

            //this.Iterator.setArgSize(-2);

            this.RealBase       = this.Base;

        }

        this.RecalcInfo.bProps = false;
    }

    //this.Set_CompiledCtrPrp(ParaMath);

    if(this.Pr.type == SQUARE_RADICAL)
        this.RealBase.Resize(oMeasure, this, ParaMath, RPI, ArgSize);
    else
    {
        var ArgSzIter = new CMathArgSize();
        ArgSzIter.SetValue(-2);
        //ArgSzIter.Merge(ArgSize);


        this.Iterator.Resize(oMeasure, this, ParaMath, RPI, ArgSzIter);
        this.RealBase.Resize(oMeasure, this, ParaMath, RPI, ArgSize);
    }

    var shTop,
        height, width, ascent;

    this.signRadical.recalculateSize(oMeasure, this.RealBase.size);

    var txtPrp = this.Get_CompiledCtrPrp();
    var sign = this.signRadical.size,
        gSign = this.signRadical.gapSign,
    // в случае смещения baseline контента тоже смещается, и по высоте артгумент может выйти чуть за пределы (т.о. значок интеграла будет расположен чуть выше, чем следовало бы, и размер аргумента выйде за аграницы)
        gArg = this.signRadical.gapArg > 2*g_dKoef_pt_to_mm ? this.signRadical.gapArg : 2*g_dKoef_pt_to_mm; // делаем смещение, т.к. для fontSize 11, 14 и меньше высота плейсхолдера не совпадает
    // с высотой отрисовки плейсхолдера и происходит наложение черты значка радикала и плейсхолдера

    var gapBase = gSign + gArg;

    if(this.Pr.type == SQUARE_RADICAL)
    {
        //base = this.elements[0][0].size;
        shTop = (sign.height - gSign - this.RealBase.size.height)/2;
        shTop = shTop > 0 ? shTop : 0;

        height = sign.height;
        width  = sign.width;
        ascent = gapBase + shTop + this.RealBase.size.ascent;
        //ascent = height - (base.height - base.ascent);

        width += this.GapLeft + this.GapRight;

        this.size = {width: width, height: height, ascent: ascent};
    }
    else if(this.Pr.type == DEGREE_RADICAL)
    {
        //degr = this.elements[0][0].size;
        //base = this.elements[0][1].size;

        var wTick = this.signRadical.measure.widthTick,
            hTick = this.signRadical.measure.heightTick;

        var plH = 9.877777777777776 * txtPrp.FontSize /36;

        // общие gaps
        var gapHeight = 0.011*txtPrp.FontSize; // добавляем это расстояние к общей высоте радикала, также как и gapWidth
        this.gapWidth = 0.011*txtPrp.FontSize;

        var wDegree = this.Iterator.size.width > wTick ? this.Iterator.size.width - wTick : 0;
        width = wDegree + sign.width + this.gapWidth;
        width += this.GapLeft + this.GapRight;

        var gapDegree;
        if( this.RealBase.size.height < plH )
            gapDegree = 1.5*txtPrp.FontSize/36;
        else
            gapDegree = 3*txtPrp.FontSize/36;

        var h1 = gapHeight + this.Iterator.size.height + gapDegree + hTick,
            h2 = sign.height;

        shTop = (sign.height - gSign - this.RealBase.size.height)/2;

        if(h1 > h2)
        {
            height =  h1;
            ascent = height - sign.height + gapBase + shTop + this.RealBase.size.ascent;
        }
        else
        {
            height =  h2;
            ascent = gapBase + shTop + this.RealBase.size.ascent;
        }

        this.gapDegree = height - h1 + gapHeight;

        this.size = {width: width, height: height, ascent: ascent};
    }
}
CRadical.prototype.setPosition = function(pos, PosInfo)
{
    this.pos.x = pos.x;
    this.pos.y = pos.y - this.size.ascent;

    var PosBase    = new CMathPosition(),
        PosRadical = new CMathPosition();

    if(this.Pr.type == SQUARE_RADICAL)
    {
        var gapLeft = this.size.width - this.RealBase.size.width - this.GapRight;
        var gapTop = this.size.ascent - this.RealBase.size.ascent;

        PosRadical.x = this.pos.x + this.GapLeft;
        PosRadical.y = this.pos.y;

        PosBase.x = this.pos.x + gapLeft;
        PosBase.y = this.pos.y + gapTop;

        this.signRadical.setPosition(PosRadical);
        this.RealBase.setPosition(PosBase, PosInfo);
    }
    else if(this.Pr.type == DEGREE_RADICAL)
    {
        var wTick = this.signRadical.measure.widthTick;

        var PosDegree = new CMathPosition();

        PosDegree.x = this.pos.x + this.GapLeft + this.gapWidth;
        PosDegree.y = this.pos.y + this.gapDegree;

        this.Iterator.setPosition(PosDegree, PosInfo);

        var wDegree = this.Iterator.size.width > wTick ? this.Iterator.size.width - wTick : 0;

        PosRadical.x = this.pos.x + this.GapLeft + wDegree;
        PosRadical.y = this.pos.y + this.size.height - this.signRadical.size.height;

        this.signRadical.setPosition(PosRadical);

        PosBase.x = this.pos.x + this.size.width - this.RealBase.size.width - this.GapRight;
        PosBase.y = this.pos.y + this.size.ascent - this.RealBase.size.ascent;

        this.RealBase.setPosition(PosBase, PosInfo);
    }
}
CRadical.prototype.Get_ParaContentPosByXY = function(SearchPos, Depth, _CurLine, _CurRange, StepEnd)
{
    var result = false;
    if(this.Pr.type == SQUARE_RADICAL)
    {
        SearchPos.CurX += this.size.width - this.Base.size.width - this.GapRight;
        if(this.Base.Get_ParaContentPosByXY(SearchPos, Depth+2, _CurLine, _CurRange, StepEnd))
        {
            SearchPos.Pos.Update2(0, Depth);
            SearchPos.Pos.Update2(0, Depth + 1);


            SearchPos.InTextPos.Update(0, Depth);
            SearchPos.InTextPos.Update(0, Depth + 1);


            result = true;
        }
        SearchPos.CurX += this.GapRight;
    }
    else
    {
        SearchPos.CurX += this.GapLeft;
        if(this.Iterator.Get_ParaContentPosByXY(SearchPos, Depth+2, _CurLine, _CurRange, StepEnd))
        {
            SearchPos.Pos.Update2(0, Depth);
            SearchPos.Pos.Update2(0, Depth + 1);


            SearchPos.InTextPos.Update(0, Depth);
            SearchPos.InTextPos.Update(0, Depth + 1);


            result = true;
        }

        SearchPos.CurX += this.size.width - this.Iterator.size.width - this.Base.size.width - this.GapRight;

        if(this.Base.Get_ParaContentPosByXY(SearchPos, Depth+2, _CurLine, _CurRange, StepEnd))
        {
            SearchPos.Pos.Update2(0, Depth);
            SearchPos.Pos.Update2(1, Depth + 1);


            SearchPos.InTextPos.Update(0, Depth);
            SearchPos.InTextPos.Update(1, Depth + 1);

            result = true;
        }

        SearchPos.CurX += this.GapRight;
    }

    return result;
}
CRadical.prototype.draw = function(x, y, pGraphics)
{
    this.signRadical.draw(x, y, pGraphics);
    CRadical.superclass.draw.call(this, x, y, pGraphics);
}
CRadical.prototype.getBase = function()
{
    return this.Base;
}
CRadical.prototype.getDegree = function()
{
    return this.Iterator;
}
CRadical.prototype.getPropsForWrite = function()
{
    return this.Pr;
}
CRadical.prototype.Save_Changes = function(Data, Writer)
{
	Writer.WriteLong( historyitem_type_rad );
}
CRadical.prototype.Load_Changes = function(Reader)
{
}
CRadical.prototype.Refresh_RecalcData = function(Data)
{
}
CRadical.prototype.Write_ToBinary2 = function( Writer )
{
	Writer.WriteLong( historyitem_type_rad );

    Writer.WriteString2(this.Id);
	Writer.WriteString2(this.getDegree().Id);
	Writer.WriteString2(this.getBase().Id);
	
	this.CtrPrp.Write_ToBinary(Writer);
	
	var StartPos = Writer.GetCurPosition();
    Writer.Skip(4);
    var Flags = 0;
	if ( undefined != this.Pr.degHide )
    {
		Writer.WriteBool( this.Pr.degHide );
		Flags |= 1;
	}
	var EndPos = Writer.GetCurPosition();
    Writer.Seek( StartPos );
    Writer.WriteLong( Flags );
    Writer.Seek( EndPos );	
}
CRadical.prototype.Read_FromBinary2 = function( Reader )
{

    this.Id = Reader.GetString2();
    this.Iterator = g_oTableId.Get_ById( Reader.GetString2());
    this.Base     = g_oTableId.Get_ById( Reader.GetString2());

	var props = {ctrPrp: new CTextPr()};
	props.ctrPrp.Read_FromBinary(Reader);
	
	var Flags = Reader.GetLong();
	if ( Flags & 1 )
		props.degHide = Reader.GetBool();	
	
	if (props.degHide == true)
		this.Iterator = null;

    this.setProperties(props);
}
CRadical.prototype.Get_Id = function()
{
	return this.Id;
}