﻿function CDocumentReaderMode()
{
    this.DefaultFontSize = 12;

    this.CorrectDefaultFontSize = function(size)
    {
        if (size < 6)
            return;

        this.DefaultFontSize = size;
    }

    this.CorrectFontSize = function(size)
    {
        var dRes = size / this.DefaultFontSize;
        dRes = (1 + dRes) / 2;
        dRes = (100 * dRes) >> 0;
        dRes /= 100;

        return "" + dRes + "em";
    }
}

window.USER_AGENT_MACOS = (navigator.userAgent.toLowerCase().indexOf('mac') > -1) ? true : false;
window.USER_AGENT_SAFARI_MACOS = (navigator.userAgent.toLowerCase().indexOf('safari') > -1 && window.USER_AGENT_MACOS) ? true : false;
if (window.USER_AGENT_SAFARI_MACOS)
{
    // браузеры под мак все определяются как сафари
    // проверим на дополнительные параметры
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
        window.USER_AGENT_SAFARI_MACOS = false;
}
window.USER_AGENT_IE = ((/MSIE/g.test(navigator.userAgent)) || window.opera) ? true : false;
window.USER_AGENT_WEBKIT = (navigator.userAgent.toLowerCase().indexOf('webkit') > -1) ? true : false;

window.GlobalPasteFlagCounter = 0;
window.GlobalPasteFlag = false;

var COPY_ELEMENT_ID = "SelectId";
var PASTE_ELEMENT_ID = "wrd_pastebin";
var ELEMENT_DISPAY_STYLE = "none";
var copyPasteUseBinery = true;

if (window.USER_AGENT_SAFARI_MACOS)
{
    var PASTE_ELEMENT_ID = "SelectId";
    var ELEMENT_DISPAY_STYLE = "block";
}

var g_bIsDocumentCopyPaste = true;
var isOnlyLocalBufferSafariWord = false;
function Editor_Copy_GetElem(api)
{
    var ElemToSelect = document.getElementById( COPY_ELEMENT_ID );
    if ( !ElemToSelect )
    {
        ElemToSelect = document.createElement("div");
        ElemToSelect.id = COPY_ELEMENT_ID;
        ElemToSelect.style.position = "absolute";
        //���� ������� width ���������, �� �������� ����� ��������� ������������ �� span
        //� �������� � ����� ������ ��������� ������ <span>1</span><span> </span><span>2</span>
        //style.left = 0px, ���� ������� ���������� ������ ������� ������� ����, �� ����� ������� ������ �������� �����, �� � ����������� ����(� iframe) ������� ������������� � ����� ������ ��� ����� ������� ������ ����������.
        ElemToSelect.style.left = '0px';
        ElemToSelect.style.top = '-100px';
        ElemToSelect.style.width = '10000px';
        ElemToSelect.style.height = '100px';
        ElemToSelect.style.overflow = 'hidden';
        ElemToSelect.style.zIndex = -1000;
        ElemToSelect.style.MozUserSelect = "text";
        ElemToSelect.style["-khtml-user-select"] = "text";
        ElemToSelect.style["-o-user-select"] = "text";
        ElemToSelect.style["user-select"] = "text";
        ElemToSelect.style["-webkit-user-select"] = "text";
        ElemToSelect.setAttribute("contentEditable", true);

        if (!api || !api.GetCopyPasteDivId)
            document.body.appendChild( ElemToSelect );
        else
        {
            var _div_id = api.GetCopyPasteDivId();
            if ("" == _div_id)
                document.body.appendChild( ElemToSelect );
            else
            {
                var _div = document.getElementById(_div_id);
                _div.appendChild( ElemToSelect );
            }
        }
    }
    return ElemToSelect;
}
function Editor_Copy_Button(api, bCut)
{
    /*if(false == g_bIsDocumentCopyPaste)
    {
        switch(api.WordControl.Thumbnails.FocusObjType)
        {
            case FOCUS_OBJECT_MAIN:
            {
                var _logic_document = api.WordControl.m_oLogicDocument;
                if(_logic_document && _logic_document.CurPos.Type != docpostype_FlowObjects )
                {
                    _logic_document.slidesBuffer.length = 0;
                    _logic_document.glyphsBuffer = _logic_document.Slides[_logic_document.CurPage].elementsManipulator.glyphsCopy();
                    return true;
                }
                break;
            }
            case FOCUS_OBJECT_THUMBNAILS :
            {
                var _selected_array = api.WordControl.m_oLogicDocument.DrawingDocument.m_oWordControl.Thumbnails.GetSelectedArray();
                api.WordControl.m_oLogicDocument.glyphsBuffer.length = 0;
                api.WordControl.m_oLogicDocument.slidesCopy(_selected_array);
                return true;
            }
        }
    }*/
    if(/MSIE/g.test(navigator.userAgent))
    {
        var ElemToSelect = Editor_Copy_GetElem(api);
        ElemToSelect.style.display  = "block";

        while ( ElemToSelect.hasChildNodes() )
            ElemToSelect.removeChild( ElemToSelect.lastChild );

        //������ ��������� ���������
        document.body.style.MozUserSelect = "text";
        delete document.body.style["-khtml-user-select"];
        delete document.body.style["-o-user-select"];
        delete document.body.style["user-select"];
        document.body.style["-webkit-user-select"] = "text";

        if (null != api.WordControl.m_oLogicDocument)
        {
            var oCopyProcessor = new CopyProcessor(api, ElemToSelect);
            oCopyProcessor.Start();
        }
        else
        {
            ElemToSelect.innerHTML = api.WordControl.m_oDrawingDocument.m_oDocumentRenderer.Copy();
        }

        var selection = window.getSelection();
        var rangeToSelect = document.createRange();
        rangeToSelect.selectNodeContents (ElemToSelect);
        selection.removeAllRanges ();
        selection.addRange(rangeToSelect);

        document.execCommand("copy");
        //rangeToSelect.execCommand("copy");

        //�������� ����������� ���������
        ElemToSelect.style.display  = "none";
        document.body.style.MozUserSelect = "none";
        document.body.style["-khtml-user-select"] = "none";
        document.body.style["-o-user-select"] = "none";
        document.body.style["user-select"] = "none";
        document.body.style["-webkit-user-select"] = "none";

        if(true == bCut)
        {
            //������� ���������� ��������
            api.WordControl.m_oLogicDocument.Remove(1, true, true);
            api.WordControl.m_oLogicDocument.Document_UpdateSelectionState();
        }
        return true;
    }
	else
	{
		var ElemToSelect = Editor_Copy_GetElem(api);
		while ( ElemToSelect.hasChildNodes() )
			ElemToSelect.removeChild( ElemToSelect.lastChild );
		if (null != api.WordControl.m_oLogicDocument)
		{
			var oCopyProcessor = new CopyProcessor(api, ElemToSelect);
			oCopyProcessor.Start();
		}
		else
		{
			ElemToSelect.innerHTML = api.WordControl.m_oDrawingDocument.m_oDocumentRenderer.Copy();
		}
		if(true == bCut)
        {
            //������� ���������� ��������
            api.WordControl.m_oLogicDocument.Remove(1, true, true);
            api.WordControl.m_oLogicDocument.Document_UpdateSelectionState();
        }
		return true;
	}
    return false;
}
function Editor_Copy(api, bCut)
{
    //todo ��������� �����
    //������� ������� ����� ����������� ����������
    var ElemToSelect = Editor_Copy_GetElem(api);
    ElemToSelect.style.display  = "block";

    while ( ElemToSelect.hasChildNodes() )
        ElemToSelect.removeChild( ElemToSelect.lastChild );

    //������ ��������� ���������
    document.body.style.MozUserSelect = "text";
    delete document.body.style["-khtml-user-select"];
    delete document.body.style["-o-user-select"];
    delete document.body.style["user-select"];
    document.body.style["-webkit-user-select"] = "text";

    if (window.USER_AGENT_IE)
    {
        // не убирать!!! это для ие. чтобы не селектились элементы
        document.onselectstart= function() {
        }
    }

    if (null != api.WordControl.m_oLogicDocument)
    {
        var oCopyProcessor = new CopyProcessor(api, ElemToSelect);
        oCopyProcessor.Start();
    }
    else
    {
        ElemToSelect.innerHTML = api.WordControl.m_oDrawingDocument.m_oDocumentRenderer.Copy();
    }
    //��������
    if (window.getSelection) // all browsers, except IE before version 9
    {
        var selection = window.getSelection ();
        var rangeToSelect = document.createRange ();

        //� FF 8 ����� ��������� selectNodeContents ���������� � ������������ div, ����� ����� �������� � FF8 � ������ ��������� ����� ��� ����� ������ ��������� �������������� node
        //�������� ���� ��� ��� ��������� ��������� �� ����������. Chrome ������ ��������� ������ ����� ����� ������ ����������
        var is_gecko = window.navigator.userAgent.indexOf('Gecko/') >= 0;
        if(is_gecko)
        {
            ElemToSelect.appendChild( document.createTextNode( '\xa0' ) );
            ElemToSelect.insertBefore( document.createTextNode( '\xa0' ), ElemToSelect.firstChild );
            rangeToSelect.setStartAfter (ElemToSelect.firstChild);
            rangeToSelect.setEndBefore (ElemToSelect.lastChild);
        }
        else
        {
            //�������� � webkit: ���� ���������� ����� ���� ��������, �� ���������� ������ ��� ������������
            //������ ��� docs.google ����������� ��� � ��� <b>
            if(window.USER_AGENT_WEBKIT && (true !== window.USER_AGENT_SAFARI_MACOS))
            {
                var aChildNodes = ElemToSelect.childNodes;
                if(aChildNodes.length == 1)
                {
                    var elem = aChildNodes[0];
                    var wrap = document.createElement("b");
                    wrap.setAttribute("style", "font-weight:normal");
                    elem = ElemToSelect.removeChild(elem);
                    wrap.appendChild(elem);
                    ElemToSelect.appendChild(wrap);
                }
            }
            rangeToSelect.selectNodeContents (ElemToSelect);
        }

        selection.removeAllRanges ();
        selection.addRange (rangeToSelect);
    }
    else
    {
        if (document.body.createTextRange) // Internet Explorer
        {
            var rangeToSelect = document.body.createTextRange ();
            rangeToSelect.moveToElementText (ElemToSelect);
            rangeToSelect.select ();
        }
    }

    //���� ���������� copy
    var time_interval = 200;
    if (window.USER_AGENT_SAFARI_MACOS)
        time_interval = 200;

    window.setTimeout( function()
    {
        //�������� ����������� ���������
        ElemToSelect.style.display  = ELEMENT_DISPAY_STYLE;
        document.body.style.MozUserSelect = "none";
        document.body.style["-khtml-user-select"] = "none";
        document.body.style["-o-user-select"] = "none";
        document.body.style["user-select"] = "none";
        document.body.style["-webkit-user-select"] = "none";

        if (window.USER_AGENT_IE)
        {
            // не убирать!!! это для ие. чтобы не селектились элементы
            document.onselectstart= function() {
                return false;
            }
        }

        if(true == bCut)
        {
            //������� ���������� ��������
            api.WordControl.m_oLogicDocument.Remove(-1, true, true);
            api.WordControl.m_oLogicDocument.Document_UpdateSelectionState();
        }
    }, time_interval);
    /*
     if (e.dataTransfer)
     {
     e.dataTransfer.setData('text', '2|16|190');
     }

     if (e.clipboardData)
     {
     e.preventDefault();
     e.clipboardData.setData('text/xcustom', 'Added this custom data at copy');
     e.clipboardData.setData('text/plain', 'This is not the text you copied, it was changed by the copy event handler');

     var TempXCustom = e.clipboardData.getData('text/xcustom');
     var TempPlain = e.clipboardData.getData('text/plain');
     var Temp = "sdf";
     }
     if (window.clipboardData)
     {
     e.returnValue = false;
     var setStatus = window.clipboardData.setData('Text', 'This is not the text you copied, it was changed by the copy event handler');
     }
     */
};
function CopyProcessor(api, ElemToSelect)
{
	this.api = api;
    this.oDocument = api.WordControl.m_oLogicDocument;
	this.oBinaryFileWriter = new BinaryFileWriter(this.oDocument);
    this.fontsArray = api.FontLoader.fontInfos;
    this.ElemToSelect = ElemToSelect;
    this.Ul = document.createElement( "ul" );
    this.Ol = document.createElement( "ul" );
    this.oTagPr;
    this.orPr;
    this.aInnerHtml;
    this.Para;
    this.bOccurEndPar;
    this.oCurHyperlink = null;
    this.oCurHyperlinkElem = null;
    this.oPresentationWriter = new CBinaryFileWriter();
};
CopyProcessor.prototype =
{
    getSrc : function(src)
    {
        //������� ����������� �� �� ��� editor.DocumentUrl ������������� ����.
        //���������� ����� ����� ��� ����������� � word � docs.google
        var start = src.substring(0, 6);
        if(0 != src.indexOf("http:") && 0 != src.indexOf("data:") && 0 != src.indexOf("https:") && 0 != src.indexOf("ftp:") && 0 != src.indexOf("file:"))
            return documentOrigin + src;
        else
            return src;
    },
    RGBToCSS : function(rgb)
    {
        var sResult = "#";
        var sR = rgb.r.toString(16);
        if(sR.length == 1)
            sR = "0" + sR;
        var sG = rgb.g.toString(16);
        if(sG.length == 1)
            sG = "0" + sG;
        var sB = rgb.b.toString(16);
        if(sB.length == 1)
            sB = "0" + sB;
        return "#" + sR + sG + sB;
    },
    CommitList : function(oDomTarget)
    {
        if(this.Ul.childNodes.length > 0)
        {
            oDomTarget.appendChild( this.Ul );
            this.Ul = document.createElement( "ul" );
        }
        if(this.Ol.childNodes.length > 0)
        {
            oDomTarget.appendChild( this.Ol );
            this.Ol = document.createElement( "ol" );
        }
    },
    Commit_pPr : function(Item)
    {
        //pPr
        var apPr = new Array();
        var Def_pPr = this.oDocument.Styles.Default.ParaPr;
        var Item_pPr = Item.CompiledPr.Pr.ParaPr;
        if(Item_pPr)
        {
            //Ind
            if(Def_pPr.Ind.Left != Item_pPr.Ind.Left)
                apPr.push("margin-left:" + (Item_pPr.Ind.Left * g_dKoef_mm_to_pt) + "pt");
            if(Def_pPr.Ind.Right != Item_pPr.Ind.Right)
                apPr.push("margin-right:" + ( Item_pPr.Ind.Right * g_dKoef_mm_to_pt) + "pt");
            if(Def_pPr.Ind.FirstLine != Item_pPr.Ind.FirstLine)
                apPr.push("text-indent:" + (Item_pPr.Ind.FirstLine * g_dKoef_mm_to_pt) + "pt");
            //Jc
            if(Def_pPr.Jc != Item_pPr.Jc){
                switch(Item_pPr.Jc)
                {
                    case align_Left: apPr.push("text-align:left");break;
                    case align_Center: apPr.push("text-align:center");break;
                    case align_Right: apPr.push("text-align:right");break;
                    case align_Justify: apPr.push("text-align:justify");break;
                }
            }
            //KeepLines , WidowControl
            if(Def_pPr.KeepLines != Item_pPr.KeepLines || Def_pPr.WidowControl != Item_pPr.WidowControl)
            {
                if(Def_pPr.KeepLines != Item_pPr.KeepLines && Def_pPr.WidowControl != Item_pPr.WidowControl)
                    apPr.push("mso-pagination:none lines-together");
                else if(Def_pPr.KeepLines != Item_pPr.KeepLines)
                    apPr.push("mso-pagination:widow-orphan lines-together");
                else if(Def_pPr.WidowControl != Item_pPr.WidowControl)
                    apPr.push("mso-pagination:none");
            }
            //KeepNext
            if(Def_pPr.KeepNext != Item_pPr.KeepNext)
                apPr.push("page-break-after:avoid");
            //PageBreakBefore
            if(Def_pPr.PageBreakBefore != Item_pPr.PageBreakBefore)
                apPr.push("page-break-before:always");
            //Spacing
            if(Def_pPr.Spacing.Line != Item_pPr.Spacing.Line)
            {
                if(linerule_AtLeast == Item_pPr.Spacing.LineRule)
                    apPr.push("line-height:"+(Item_pPr.Spacing.Line * g_dKoef_mm_to_pt)+"pt");
                else if( linerule_Auto == Item_pPr.Spacing.LineRule)
                {
                    if(1 == Item_pPr.Spacing.Line)
                        apPr.push("line-height:normal");
                    else
                        apPr.push("line-height:"+parseInt(Item_pPr.Spacing.Line * 100)+"%");
                }
            }
            if(Def_pPr.Spacing.LineRule != Item_pPr.Spacing.LineRule)
            {
                if(linerule_Exact == Item_pPr.Spacing.LineRule)
                    apPr.push("mso-line-height-rule:exactly");
            }
            //��� ������� � word ����� ����� ��� �������� ������������ ������
            //if(Def_pPr.Spacing.Before != Item_pPr.Spacing.Before)
            apPr.push("margin-top:" + (Item_pPr.Spacing.Before * g_dKoef_mm_to_pt) + "pt");
            //if(Def_pPr.Spacing.After != Item_pPr.Spacing.After)
            apPr.push("margin-bottom:" + (Item_pPr.Spacing.After * g_dKoef_mm_to_pt) + "pt");
            //Shd
            if(Def_pPr.Shd.Value != Item_pPr.Shd.Value)
                apPr.push("background-color:" + this.RGBToCSS(Item_pPr.Shd.Color));
            //Tabs
            if(Item_pPr.Tabs.Get_Count() > 0)
            {
                var sRes = "";
                //tab-stops:1.0cm 3.0cm 5.0cm
                for(var i = 0, length = Item_pPr.Tabs.Get_Count(); i < length; i++)
                {
                    if(0 != i)
                        sRes += " ";
                    sRes += Item_pPr.Tabs.Get(i).Pos / 10 + "cm";
                }
                apPr.push("tab-stops:" + sRes);
            }
            //Border
            if(null != Item_pPr.Brd)
            {
                apPr.push("border:none");
                var borderStyle = this._BordersToStyle(Item_pPr.Brd, "mso-", "-alt");
                if(null != borderStyle)
                {
                    var nborderStyleLength = borderStyle.length;
                    if(nborderStyleLength> 0)
                        borderStyle = borderStyle.substring(0, nborderStyleLength - 1);
                    apPr.push(borderStyle);
                }
            }
        }
        if(apPr.length > 0)
            this.Para.setAttribute("style", apPr.join(';'));
    },
    InitRun : function(bInitPr)
    {
        if(bInitPr)
        {
            this.oTagPr = new Object();
            this.orPr = new Object();
        }
        this.aInnerHtml = new Array();
    },
    CommitSpan : function(bInitPr)
    {
        if(this.aInnerHtml.length > 0)
        {
            var Run = document.createElement( "span" );
            //Run.setAttribute("xml:space", "preserve");
            var sStyle = "";
            for(prop in this.orPr)
                sStyle += prop + ":" + this.orPr[prop] + ";";
            if("" != sStyle)
                Run.setAttribute("style", sStyle);
            if(this.aInnerHtml.length > 0)
            {
                var sText = this.aInnerHtml.join('');
                //���� � ������ ��������� ���������, �� ��� ����� ��������
                //�������� ��� ����� ������ ������� �� &nbsp;
                sText = sText.replace(/\s+/g, function(str)
                {
                    var sResult = "";
                    for(var i = 0, length = str.length; i < length - 1; i++)
                    {
                        sResult += "&nbsp;";
                    }
                    sResult += " ";
                    return sResult;
                });
                //����������� ����� � ���� ������
                var sStart = "";
                var aEnd = new Array();
                for(prop in this.oTagPr)
                {
                    sStart += "<" + prop + ">";
                    aEnd.push("</" + prop + ">");
                }
                if("" != sStart && aEnd.length > 0)
                {
                    aEnd.reverse();
                    sText = sStart + sText + aEnd.join('');
                }

                Run.innerHTML = sText;
            }
            if(null != this.oCurHyperlinkElem)
                this.oCurHyperlinkElem.appendChild(Run);
            else
                this.Para.appendChild(Run);
        }
        this.InitRun(bInitPr);
    },
    parse_para_TextPr : function(Value)
    {
        //���������� ������� span
        this.CommitSpan( false);
        //���������� ����� ���������
        for( var prop in Value )
        {
            this.SetProp(prop, Value[prop]);
        }
    },
    SetProp : function(prop, val)
    {
        var oPropMap =
        {
            RFonts: function( oThis, val )
            {
                var sFontName = null;
				if(null != val.Ascii)
					sFontName = val.Ascii.Name;
				else if(null != val.HAnsi)
					sFontName = val.HAnsi.Name;
				else if(null != val.EastAsia)
					sFontName = val.EastAsia.Name;
				else if(null != val.CS)
					sFontName = val.CS.Name;
				if(null != sFontName)
					oThis.orPr["font-family"] = "'" + sFontName + "'";
            },

            FontSize: function( oThis, val )
            {
                if (!oThis.api.DocumentReaderMode)
                    oThis.orPr["font-size"] = val + "pt";//font-size � pt ��� ��������� ������� � mm
                else
                {
                    oThis.orPr["font-size"] = oThis.api.DocumentReaderMode.CorrectFontSize(val);
                }
            },

            Bold: function( oThis, val )
            {
                if(val)
                    oThis.oTagPr["b"] = 1;//arPr.push("font-weight:bold");
                else
                    delete oThis.oTagPr["b"];
            },

            Italic: function( oThis, val )
            {
                if(val)
                    oThis.oTagPr["i"] = 1;//arPr.push("font-style:italic");
                else
                    delete oThis.oTagPr["i"];
            },

            Underline:  function( oThis, val )
            {
                if(val)
                    oThis.oTagPr["u"] = 1;
                else
                    delete oThis.oTagPr["u"];
            },

            Strikeout:  function( oThis, val )
            {
                if(val)
                    oThis.oTagPr["s"] = 1;
                else
                    delete oThis.oTagPr["s"];
            },

            HighLight:  function( oThis, val )
            {
                if(val != highlight_None){
                    oThis.orPr["background-color"] = oThis.RGBToCSS(val);
                }
                else
                    delete oThis.orPr["background-color"];
            },

            Color:  function( oThis, val )
            {
                var color = oThis.RGBToCSS(val);
                oThis.orPr["color"] = color;
                oThis.orPr["mso-style-textfill-fill-color"] = color;
            },

            VertAlign:  function( oThis, val )
            {
                if(vertalign_SuperScript == val)
                    oThis.orPr["vertical-align"] = "super";
                else if(vertalign_SubScript == val)
                    oThis.orPr["vertical-align"] = "sub";
                else
                    delete oThis.orPr["vertical-align"];
            }
        };

        if( "undefined" != typeof( oPropMap[prop] ) )
        {
            oPropMap[prop]( this, val );
        }
    },
    ParseItem : function(ParaItem, Par, nParIndex, aDocumentContent, nDocumentContentIndex)
    {
        switch ( ParaItem.Type )
        {
            case para_Text:
                //���������� �����������
                var sValue = ParaItem.Value;
                if(sValue)
                {
                    sValue = CopyPasteCorrectString(sValue);
                    this.aInnerHtml.push(sValue);
                }
                break;
            case para_Space:    this.aInnerHtml.push(" "); break;
            case para_Tab:        this.aInnerHtml.push("<span style='mso-tab-count:1'>    </span>"); break;
            case para_NewLine:
                if( break_Page == ParaItem.BreakType)
                {
                    //todo ��������� ���� �������� � ������ �����
                    this.aInnerHtml.push("<br clear=\"all\" style=\"mso-special-character:line-break;page-break-before:always;\" />");
                }
                else
                    this.aInnerHtml.push("<br style=\"mso-special-character:line-break;\" />");
                break;
            //������� ������� ����� ��������� ������ �� ���������� ��������
            case para_End:        this.bOccurEndPar = true; break;
            case para_TextPr:
                if(null != Par)
                {
                    var oCalculateTextPr = Par.Internal_CalculateTextPr(nParIndex);
                    this.parse_para_TextPr(oCalculateTextPr);
                }
                break;
            case para_Drawing:
                var oGraphicObj = ParaItem.GraphicObj;
                var sSrc = oGraphicObj.getBase64Img();
                if(sSrc.length > 0)
                {
                    sSrc = this.getSrc(sSrc);
                    this.aInnerHtml.push("<img width=\""+Math.round(ParaItem.W * g_dKoef_mm_to_pix)+"\" height=\""+Math.round(ParaItem.H * g_dKoef_mm_to_pix)+"\" src=\""+sSrc+"\"  alt=\""+ParaItem.writeToBinaryForCopyPaste()+"\"/>"); break;
                }
                // var _canvas     = document.createElement('canvas');
                // var w = img.width;
                // var h = img.height;

                // _canvas.width   = w;
                // _canvas.height  = h;

                // var _ctx        = _canvas.getContext('2d');
                // _ctx.globalCompositeOperation = "copy";
                // _ctx.drawImage(img, 0, 0);

                // var _data = _ctx.getImageData(0, 0, w, h);
                // _ctx = null;
                // delete _canvas;
                break;
            case para_FlowObjectAnchor:
                var oFlowObj = ParaItem.FlowObject;
                if(flowobject_Image == oFlowObj.Get_Type())
                {
                    var sSrc = oFlowObj.Img;
                    if(sSrc.length > 0)
                    {
                        sSrc = this.getSrc(sSrc);
						var sStyle = "";
						var nLeft = oFlowObj.X;
						var nRight = nLeft + oFlowObj.W;
						if(Math.abs(nLeft - X_Left_Margin) < Math.abs(Page_Width - nRight - X_Right_Margin))
							sStyle = "float:left;";
						else
							sStyle = "float:right;";
						if(!this.api.DocumentReaderMode)
						{
							if(null != oFlowObj.Paddings)
								sStyle += "margin:" + (oFlowObj.Paddings.Top * g_dKoef_mm_to_pt) + "pt " + (oFlowObj.Paddings.Right * g_dKoef_mm_to_pt) + "pt " +  + (oFlowObj.Paddings.Bottom * g_dKoef_mm_to_pt) + "pt " + + (oFlowObj.Paddings.Left * g_dKoef_mm_to_pt) + "pt;";
						}
						else
							sStyle += "margin:0pt 10pt 0pt 10pt;";
						this.aInnerHtml.push("<img style=\""+sStyle+"\" width=\""+Math.round(oFlowObj.W * g_dKoef_mm_to_pix)+"\" height=\""+Math.round(oFlowObj.H * g_dKoef_mm_to_pix)+"\" src=\""+sSrc+"\" />"); break;
                    }
                }
                break;
            case para_HyperlinkStart:
				if(null == this.oCurHyperlinkElem)
				{
					this.CommitSpan(false);
					this.oCurHyperlink = ParaItem;
					this.oCurHyperlinkElem = document.createElement( "a" );
					if(null != this.oCurHyperlink.Value)
						this.oCurHyperlinkElem.href = this.oCurHyperlink.Value;
					if(null != this.oCurHyperlink.Tooltip)
						this.oCurHyperlinkElem.setAttribute("title", this.oCurHyperlink.Tooltip);
				}
                break;
            case para_HyperlinkEnd:
                this.CommitSpan(false);
                if(null != this.oCurHyperlinkElem)
                {
                    //������ hyperlink ������ � ���������
                    this.Para.appendChild(this.oCurHyperlinkElem);
                }
                else
                {
                    //������ hyperlink �� ������ � ���������, ����� ��� �����
                    var oHyperlink = null;
                    //������� ���� � ������� ���������
                    for(var i = nParIndex - 1; i >= 0; --i)
                    {
                        var item = Par.Content[i];
                        if(para_HyperlinkStart == item.Type)
                        {
                            oHyperlink = item;
                            break;
                        }
                    }
                    //���� � ���������� ����������
                    if(null == oHyperlink)
                    {
                        for(var i = nDocumentContentIndex - 1; i >= 0; --i)
                        {
                            var item = aDocumentContent[i];
                            if(type_Paragraph == item.Type)
                            {
                                for(var j = item.Content.length -1 ; j >= 0; --j)
                                {
                                    var Paritem = item.Content[ji];
                                    if(para_HyperlinkStart == Paritem.Type)
                                    {
                                        oHyperlink = Paritem;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if(null != oHyperlink)
                    {
                        bReset = false;
                        this.CommitSpan(false);
                        this.oCurHyperlink = oHyperlink;
                        this.oCurHyperlinkElem = document.createElement( "a" );
                        if(null != this.oCurHyperlink.Value)
                            this.oCurHyperlinkElem.href = this.oCurHyperlink.Value;
                        if(null != this.oCurHyperlink.Tooltip)
                            this.oCurHyperlinkElem.setAttribute("title", this.oCurHyperlink.Tooltip);
                        //���������� ��� �������������� �������� � ������
                        for(var i = 0; i < this.Para.childNodes.length; i++)
                        {
                            var child = this.Para.childNodes[i];
                            child = this.Para.removeChild(child);
                            this.oCurHyperlinkElem.appendChild( child );
                        }
                        this.Para.appendChild(this.oCurHyperlinkElem);
                    }
                }
                this.oCurHyperlink = null;
                this.oCurHyperlinkElem = null;
                break;
        }
    },
    CopyParagraph : function(oDomTarget, Item, bLast, bUseSelection, aDocumentContent, nDocumentContentIndex)
    {
        var oDocument = this.oDocument;
        this.Para = null;
        //��� heading ����� � h1
        var styleId = Item.Style_Get();
        if(styleId)
        {
            var styleName = oDocument.Styles.Get_Name( styleId ).toLowerCase();
            //������ "heading n" (n=1:6)
            if(0 == styleName.indexOf("heading"))
            {
                var nLevel = parseInt(styleName.substring("heading".length));
                if(1 <= nLevel && nLevel <= 6)
                    this.Para = document.createElement( "h" + nLevel );
            }
        }
        if(null == this.Para)
            this.Para = document.createElement( "p" );

        this.bOccurEndPar = false;
        var oNumPr;
        var bIsNullNumPr = false;
        if(g_bIsDocumentCopyPaste)
        {
            oNumPr = Item.Numbering_Get();
            bIsNullNumPr = (null == oNumPr || 0 == oNumPr.NumId);
        }
        else
        {
            oNumPr = Item.PresentationPr.Bullet;
            bIsNullNumPr = (0 == oNumPr.m_nType);
        }
        if(bIsNullNumPr)
            this.CommitList(oDomTarget);
        else
        {
            var bBullet = false;
            var sListStyle = "";
            if(g_bIsDocumentCopyPaste)
            {
				var aNum = this.oDocument.Numbering.Get_AbstractNum( oNumPr.NumId );
				if(null != aNum)
				{
					var LvlPr = aNum.Lvl[oNumPr.Lvl];
					if(null != LvlPr)
					{
						switch(LvlPr.Format)
						{
							case numbering_numfmt_Decimal: sListStyle = "decimal";break;
							case numbering_numfmt_LowerRoman: sListStyle = "lower-roman";break;
							case numbering_numfmt_UpperRoman: sListStyle = "upper-roman";break;
							case numbering_numfmt_LowerLetter: sListStyle = "lower-alpha";break;
							case numbering_numfmt_UpperLetter: sListStyle = "upper-alpha";break;
							default:
								sListStyle = "disc";
								bBullet = true;
								break;
						}
					}
				}
            }
            else
            {
                var _presentation_bullet = Item.PresentationPr.Bullet;
                switch(_presentation_bullet.m_nType)
                {
                    case numbering_presentationnumfrmt_ArabicPeriod:
                    case numbering_presentationnumfrmt_ArabicParenR:
                    {
                        sListStyle = "decimal";
                        break;
                    }
                    case numbering_presentationnumfrmt_RomanLcPeriod: sListStyle = "lower-roman";break;
                    case numbering_presentationnumfrmt_RomanUcPeriod: sListStyle = "upper-roman";break;

                    case numbering_presentationnumfrmt_AlphaLcParenR:
                    case numbering_presentationnumfrmt_AlphaLcPeriod:
                    {
                        sListStyle = "lower-alpha";
                        break;
                    }
                    case numbering_presentationnumfrmt_AlphaUcParenR:
                    case numbering_presentationnumfrmt_AlphaUcPeriod:
                    {
                        sListStyle = "upper-alpha";
                        break;
                    }

                    default:
                        sListStyle = "disc";
                        bBullet = true;
                        break;
                }
            }
            var Li = document.createElement( "li" );
            Li.setAttribute("style", "list-style-type: " + sListStyle);
            Li.appendChild( this.Para );
            if(bBullet)
                this.Ul.appendChild( Li );
            else
                this.Ol.appendChild( Li );
        }
        //pPr
        this.Commit_pPr(Item);
        //run
        this.InitRun(true);

        var ParaStart = 0;
        var ParaEnd   = Item.Content.length - 1;
        if(true == bUseSelection)
        {
            ParaStart = Item.Selection.StartPos;
            ParaEnd   = Item.Selection.EndPos;
            if ( ParaStart > ParaEnd )
            {
                var Temp2 = ParaEnd;
                ParaEnd = ParaStart;
                ParaStart = Temp2;
            }
        }

        //���� ������ ��������� ����� �� ParaStart
        var oCalculateTextPr = Item.Internal_CalculateTextPr(ParaStart);
        this.parse_para_TextPr(oCalculateTextPr);

        //��� hyperlink: ���� ������ ���� �� ��������� ����������, �� � ����� ��������� ���� �������� �� ������
        if(null != this.oCurHyperlink)
        {
            this.oCurHyperlinkElem = document.createElement( "a" );
            if(null != this.oCurHyperlink.Value)
                this.oCurHyperlinkElem.href = this.oCurHyperlink.Value;
            if(null != this.oCurHyperlink.Tooltip)
                this.oCurHyperlinkElem.setAttribute("title", this.oCurHyperlink.Tooltip);
        }
        // TODO : ����������� ��������, ���  ������ ������� Hyperlink. ������� �� ����������.
        if ( ParaStart > 0 )
        {
            while ( Item.Content[ParaStart - 1].Type === para_TextPr || Item.Content[ParaStart - 1].Type === para_HyperlinkStart )
            {
                ParaStart--;
                if(0 == ParaStart)
                    break;
            }
        }
        //����������� �� �������� ���������
        for ( var Index2 = ParaStart; Index2 < ParaEnd; Index2++ )
        {
            var ParaItem = Item.Content[Index2];
            this.ParseItem(ParaItem, Item, Index2, aDocumentContent, nDocumentContentIndex);
        }
        this.CommitSpan(true);

        //��� hyperlink: ���� ������ ���� � ���������� ����������, �� ���� ��������� ������ � ������� ���������
        if(null != this.oCurHyperlinkElem)
            this.Para.appendChild(this.oCurHyperlinkElem);
        this.oCurHyperlink = null;
        this.oCurHyperlinkElem = null;


        if(bLast && false == this.bOccurEndPar)
        {
            if(false == bIsNullNumPr)
            {
                //������ �������� � ������. ������� ������� �� ������. ���������� ����� ��� span
                var li = this.Para.parentNode;
                var ul = li.parentNode;
                ul.removeChild(li);
                this.CommitList(oDomTarget);
            }
            for(var i = 0; i < this.Para.childNodes.length; i++)
                oDomTarget.appendChild( this.Para.childNodes[i].cloneNode(true) );
        }
        else
        {
            //����� ��������� ������ ���������
            if(this.Para.childNodes.length == 0)
                this.Para.appendChild( document.createTextNode( '\xa0' ) );
            if(bIsNullNumPr)
                oDomTarget.appendChild( this.Para );
        }
    },
    _BorderToStyle : function(border, name)
    {
        var res = "";
        if(border_None == border.Value)
            res += name + ":none;";
        else
        {
            bBorder = true;
            var size = 0.5;
            var color = { r : 0, g : 0, b : 0 };
            if(null != border.Size)
                size = border.Size * g_dKoef_mm_to_pt;
            if(null != border.Color)
                color = border.Color;
            res += name + ":"+size+"pt solid "+this.RGBToCSS(color)+";";
        }
        return res;
    },
    _MarginToStyle : function(margins, styleName)
    {
        var res = "";
        var nMarginLeft = 1.9;
        var nMarginTop = 0;
        var nMarginRight = 1.9;
        var nMarginBottom = 0;
        if(null != margins.Left && tblwidth_Mm == margins.Left.Type && null != margins.Left.W)
            nMarginLeft = margins.Left.W;
        if(null != margins.Top && tblwidth_Mm == margins.Top.Type && null != margins.Top.W)
            nMarginTop = margins.Top.W;
        if(null != margins.Right && tblwidth_Mm == margins.Right.Type && null != margins.Right.W)
            nMarginRight = margins.Right.W;
        if(null != margins.Bottom && tblwidth_Mm == margins.Bottom.Type && null != margins.Bottom.W)
            nMarginBottom = margins.Bottom.W;
        res = styleName + ":"+(nMarginTop * g_dKoef_mm_to_pt)+"pt "+(nMarginRight * g_dKoef_mm_to_pt)+"pt "+(nMarginBottom * g_dKoef_mm_to_pt)+"pt "+(nMarginLeft * g_dKoef_mm_to_pt)+"pt;";
        return res;
    },
    _BordersToStyle : function(borders, mso, alt)
    {
        var res = "";
        if(null == mso)
            mso = "";
        if(null == alt)
            alt = "";
        if(null != borders.Left)
            res += this._BorderToStyle(borders.Left, mso + "border-left" + alt);
        if(null != borders.Top)
            res += this._BorderToStyle(borders.Top, mso + "border-top" + alt);
        if(null != borders.Right)
            res += this._BorderToStyle(borders.Right, mso + "border-right" + alt);
        if(null != borders.Bottom)
            res += this._BorderToStyle(borders.Bottom, mso + "border-bottom" + alt);
        if(null != borders.InsideV)
            res += this._BorderToStyle(borders.InsideV, "mso-border-insidev");
        if(null != borders.InsideH)
            res += this._BorderToStyle(borders.InsideH, "mso-border-insideh");
        if(null != borders.Between)
            res += this._BorderToStyle(borders.Between, "mso-border-between");
        return res;
    },
    _MergeProp : function(elem1, elem2)
    {
        if( !elem1 || !elem2 )
        {
            return;
        }

        var p, v;
        for(p in elem2)
        {
            if(elem2.hasOwnProperty(p) && false == elem1.hasOwnProperty(p))
            {
                v = elem2[p];
                if(null != v)
                    elem1[p] = v;
            }
        }
    },
    CopyCell : function(tr, cell, tablePr, width, rowspan)
    {
        var tc = document.createElement( "td" );
        //Pr
        var tcStyle = "";
        if(width > 0)
        {
            tc.setAttribute("width", Math.round(width * g_dKoef_mm_to_pix));
            tcStyle += "width:"+(width * g_dKoef_mm_to_pt)+"pt;";
        }
        if(rowspan > 1)
            tc.setAttribute("rowspan", rowspan);
        var cellPr = null;
        if(null != cell.CompiledPr && null != cell.CompiledPr.Pr)
        {
            cellPr = cell.CompiledPr.Pr;
            //��� ������ � �������� ����� ������������ margin � �� colspan
            if(null != cellPr.GridSpan && cellPr.GridSpan > 1)
                tc.setAttribute("colspan", cellPr.GridSpan);
        }
        if(null != cellPr && null != cellPr.Shd)
        {
            if(shd_Nil != cellPr.Shd.Value)
                tcStyle += "background-color:"+this.RGBToCSS(cellPr.Shd.Color)+";";
        }
        else if(null != tablePr && null != tablePr.Shd)
        {
            if(shd_Nil != tablePr.Shd.Value)
                tcStyle += "background-color:"+this.RGBToCSS(tablePr.Shd.Color)+";";
        }
        var oCellMar = new Object();
        if(null != cellPr && null != cellPr.TableCellMar)
            this._MergeProp(oCellMar, cellPr.TableCellMar);
        if(null != tablePr && null != tablePr.TableCellMar)
            this._MergeProp(oCellMar, tablePr.TableCellMar);
        tcStyle += this._MarginToStyle(oCellMar, "padding");

        var oCellBorder = new Object();
        if(null != cellPr && null != cellPr.TableCellBorders)
            this._MergeProp(oCellBorder, cellPr.TableCellBorders);
        if(null != tablePr && null != tablePr.TableBorders)
            this._MergeProp(oCellBorder, tablePr.TableBorders);
        tcStyle += this._BordersToStyle(oCellBorder);

        if("" != tcStyle)
            tc.setAttribute("style", tcStyle);
        //Content
        this.CopyDocument(tc, cell.Content, false);

        tr.appendChild(tc);
    },
    CopyRow : function(oDomTarget, table, nCurRow, elems, nMaxRow)
    {
        var row = table.Content[nCurRow];
		if(null == elems)
			elems = {gridStart: 0, gridEnd: table.TableGrid.length - 1, indexStart: null, indexEnd: null, after: null, before: null, cells: row.Content};
        var tr = document.createElement( "tr" );
        //Pr
		var gridSum = table.TableSumGrid;
        var trStyle = "";
        var nGridBefore = 0;
		var rowPr = null;
		if(null != row.CompiledPr && null != row.CompiledPr.Pr)
			rowPr = row.CompiledPr.Pr;
        if(null != rowPr)
        {
			if(null == elems.before && null != rowPr.GridBefore && rowPr.GridBefore > 0)
			{
                elems.before = rowPr.GridBefore;
				elems.gridStart += rowPr.GridBefore;
			}
			if(null == elems.after && null != rowPr.GridAfter && rowPr.GridAfter > 0)
			{
                elems.after = rowPr.GridAfter;
				elems.gridEnd -= rowPr.GridAfter;
			}
            //height
            if(null != rowPr.Height && heightrule_Auto != rowPr.Height.HRule && null != rowPr.Height.Value)
            {
                trStyle += "height:"+(rowPr.Height.Value * g_dKoef_mm_to_pt)+"pt;";
            }
        }
		//WBefore
        if(null != elems.before)
        {
            if(elems.before > 0)
            {
                nGridBefore = elems.before;
                var nWBefore = gridSum[elems.gridStart - 1] - gridSum[elems.gridStart - nGridBefore - 1];
                //���������� margin
                trStyle += "mso-row-margin-left:"+(nWBefore * g_dKoef_mm_to_pt)+"pt;";
                //��������� td ��� ��� ��� �� �������� mso-row-margin-left
                var oNewTd = document.createElement( "td" );
                oNewTd.setAttribute("style", "mso-cell-special:placeholder;border:none;padding:0cm 0cm 0cm 0cm");
                oNewTd.setAttribute("width", Math.round(nWBefore * g_dKoef_mm_to_pix));
                if(nGridBefore > 1)
                    oNewTd.setAttribute("colspan", nGridBefore);
                var oNewP = document.createElement( "p" );
				oNewP.setAttribute("style", "margin:0cm");
                oNewP.appendChild(document.createTextNode( '\xa0' ));
                oNewTd.appendChild(oNewP);
                tr.appendChild(oNewTd);
            }
        }
        //tc
        for(var i in elems.cells)
        {
            var cell = row.Content[i];
			if(vmerge_Continue != cell.Get_VMerge())
			{
				var StartGridCol = cell.Metrics.StartGridCol;
				var GridSpan = cell.Get_GridSpan();
				var width = gridSum[StartGridCol + GridSpan - 1] - gridSum[StartGridCol - 1];
				//��������� rowspan
				var nRowSpan = table.Internal_GetVertMergeCount(nCurRow, StartGridCol, GridSpan);
				if(nCurRow + nRowSpan - 1 > nMaxRow)
				{
					nRowSpan = nMaxRow - nCurRow + 1;
					if(nRowSpan <= 0)
						nRowSpan = 1;
				}
				var tablePr = null;
				if(null != table.CompiledPr && null != table.CompiledPr.Pr && null != table.CompiledPr.Pr.TablePr)
					tablePr = table.CompiledPr.Pr.TablePr;
				this.CopyCell(tr, cell, tablePr, width, nRowSpan);
			}
        }
        //WAfter
        if(null != elems.after)
        {
            if(elems.after > 0)
            {
                var nGridAfter = elems.after;
                var nWAfter = gridSum[elems.gridEnd + nGridAfter] - gridSum[elems.gridEnd];
                //���������� margin
                trStyle += "mso-row-margin-right:"+(nWAfter * g_dKoef_mm_to_pt)+"pt;";
                //��������� td ��� ��� ��� �� �������� mso-row-margin-left
                var oNewTd = document.createElement( "td" );
                oNewTd.setAttribute("style", "mso-cell-special:placeholder;border:none;padding:0cm 0cm 0cm 0cm");
                oNewTd.setAttribute("width", Math.round(nWAfter * g_dKoef_mm_to_pix));
                if(nGridAfter > 1)
                    oNewTd.setAttribute("colspan", nGridAfter);
                var oNewP = document.createElement( "p" );
				oNewP.setAttribute("style", "margin:0cm");
                oNewP.appendChild(document.createTextNode( '\xa0' ));
                oNewTd.appendChild(oNewP);
                tr.appendChild(oNewTd);
            }
        }
        if("" != trStyle)
            tr.setAttribute("style", trStyle);
			
        oDomTarget.appendChild(tr);
    },
    CopyTable : function(oDomTarget, table, aRowElems)
    {
        this.CommitList(oDomTarget);
        var DomTable = document.createElement( "table" );
        var Pr = null;
        if(null != table.CompiledPr && null != table.CompiledPr.Pr && null != table.CompiledPr.Pr.TablePr)
            Pr = table.CompiledPr.Pr.TablePr;
        var tblStyle = "";
        var bBorder = false;
        if(null != Pr)
        {
			var align = "";
            if(true != table.Inline && null != table.PositionH)
			{
				var PositionH = table.PositionH;
				if(true == PositionH.Align)
				{
					switch(PositionH.Value)
					{
						case c_oAscXAlign.Outside:
						case c_oAscXAlign.Right: align = "right";break;
						case c_oAscXAlign.Center: align = "center";break;
					}
				}
				else if(table.TableSumGrid)
				{
					var TableWidth = table.TableSumGrid[ table.TableSumGrid.length - 1 ];
					var nLeft = PositionH.Value;
					var nRight = nLeft + TableWidth;
					var nFromLeft = Math.abs(nLeft - X_Left_Margin);
					var nFromCenter = Math.abs((Page_Width - X_Right_Margin + X_Left_Margin) / 2 - (nLeft + nRight) / 2);
					var nFromRight = Math.abs(Page_Width - nRight - X_Right_Margin);
					if(nFromRight < nFromLeft || nFromCenter < nFromLeft)
					{
						if(nFromRight < nFromCenter)
							align = "right";
						else
							align = "center";
					}
				}
			}
			else if(null != Pr.Jc)
            {
                switch(Pr.Jc)
                {
                    case align_Center:align = "center";break;
                    case align_Right:align = "right";break;
                }
            }
			if("" != align)
				DomTable.setAttribute("align", align);
            if(null != Pr.TableInd)
                tblStyle += "margin-left:"+(Pr.TableInd * g_dKoef_mm_to_pt)+"pt;";
            if(null != Pr.Shd && shd_Nil != Pr.Shd.Value)
                tblStyle += "background:"+this.RGBToCSS(Pr.Shd.Color)+";";
            if(null != Pr.TableCellMar)
                tblStyle += this._MarginToStyle(Pr.TableCellMar, "mso-padding-alt");
            if(null != Pr.TableBorders)
                tblStyle += this._BordersToStyle(Pr.TableBorders);
        }
        //���� cellSpacing
        var bAddSpacing = false;
        if(table.Content.length > 0)
        {
            var firstRow = table.Content[0];
            if(null != firstRow.Pr && null != firstRow.Pr.TableCellSpacing)
            {
                bAddSpacing = true;
                var cellSpacingMM = firstRow.Pr.TableCellSpacing;
                tblStyle += "mso-cellspacing:"+(cellSpacingMM * g_dKoef_mm_to_pt)+"pt;";
                DomTable.setAttribute("cellspacing", Math.round(cellSpacingMM * g_dKoef_mm_to_pix));
            }
        }
        if(!bAddSpacing)
            DomTable.setAttribute("cellspacing", 0);
        DomTable.setAttribute("border", false == bBorder ? 0 : 1);
        DomTable.setAttribute("cellpadding", 0);
        if("" != tblStyle)
            DomTable.setAttribute("style", tblStyle);

        //rows
		if(null == aRowElems)
		{
			for(var i = 0, length = table.Content.length; i < length; i++)
				this.CopyRow(DomTable, table, i , null, table.Content.length - 1);
		}
		else
		{
			var nMaxRow = 0;
			for(var i = 0, length = aRowElems.length; i < length; ++i)
			{
				var elem = aRowElems[i];
				if(elem.row > nMaxRow)
					nMaxRow = elem.row;
			}
			for(var i = 0, length = aRowElems.length; i < length; ++i)
			{
				var elem = aRowElems[i];
				this.CopyRow(DomTable, table, elem.row, elem, nMaxRow);
			}
		}

        oDomTarget.appendChild(DomTable);
    },
    CopyDocument : function(oDomTarget, oDocument, bUseSelection)
    {
        var Start = 0;
        var End = 0;
        if(bUseSelection)
        {
            if ( true === oDocument.Selection.Use)
            {
                if ( selectionflag_DrawingObject === oDocument.Selection.Flag )
                {
                    this.Para = document.createElement( "p" );
                    //������ ���� �������� ������ ���� ��������
                    this.InitRun();
                    this.ParseItem(oDocument.Selection.Data.DrawingObject);
                    this.CommitSpan(false);

                    for(var i = 0; i < this.Para.childNodes.length; i++)
                        this.ElemToSelect.appendChild( this.Para.childNodes[i].cloneNode(true) );
                }
                else
                {
                    Start = oDocument.Selection.StartPos;
                    End = oDocument.Selection.EndPos;
                    if ( Start > End )
                    {
                        var Temp = End;
                        End = Start;
                        Start = Temp;
                    }
                }
            }
        }
        else
        {
            Start = 0;
            End = oDocument.Content.length - 1;
        }
        // HtmlText
        for ( var Index = Start; Index <= End; Index++ )
        {
            var Item = oDocument.Content[Index];
            if(type_Table === Item.GetType() )
            {
                if(bUseSelection)
                {
                    if(table_Selection_Text == Item.Selection.Type)
                    {
                        //������� ����� � ������.
                        var rowIndex = Item.Selection.StartPos.Pos.Row;
                        var colIndex = Item.Selection.StartPos.Pos.Cell;
                        if(rowIndex < Item.Content.length)
                        {
                            var row = Item.Content[rowIndex];
                            if(colIndex < row.Content.length)
                            {
                                var cell = row.Content[colIndex];
                                this.CopyDocument(oDomTarget, cell.Content, bUseSelection);
                            }
                        }
                    }
                    else if(table_Selection_Cell == Item.Selection.Type)
                    {
                        //������� �������� �����
						var aSelectedRows = new Array();
						var oRowElems = new Object();
						if(Item.Selection.Data.length > 0)
						{
							for(var i = 0, length = Item.Selection.Data.length; i < length; ++i)
							{
								var elem = Item.Selection.Data[i];
								var rowElem = oRowElems[elem.Row];
								if(null == rowElem)
								{
									rowElem = {row: elem.Row, gridStart: null, gridEnd: null, indexStart: null, indexEnd: null, after: null, before: null, cells: {}};
									oRowElems[elem.Row] = rowElem;
									aSelectedRows.push(rowElem);
								}
								if(null == rowElem.indexEnd || elem.Cell > rowElem.indexEnd)
									rowElem.indexEnd = elem.Cell;
								if(null == rowElem.indexStart || elem.Cell < rowElem.indexStart)
									rowElem.indexStart = elem.Cell;
								rowElem.cells[elem.Cell] = 1;
							}
						}
						aSelectedRows.sort(function(a,b){
							return a.row - b.row;
						});
						var nMinGrid = null;
						var nMaxGrid = null;
						var nPrevStartGrid = null;
						var nPrevEndGrid = null
						var nPrevRowIndex = null;
						for(var i = 0, length = aSelectedRows.length; i < length; ++i)
						{
							var elem = aSelectedRows[i];
							var nRowIndex = elem.row;
							if(null != nPrevRowIndex)
							{
								if(nPrevRowIndex + 1 != nRowIndex)
								{
									nMinGrid = null;
									nMaxGrid = null;
									break;
								}
							}
							nPrevRowIndex = nRowIndex;
							var row = Item.Content[nRowIndex];
							var cellFirst = row.Get_Cell(elem.indexStart);
							var cellLast = row.Get_Cell(elem.indexEnd);
							var nCurStartGrid = cellFirst.Metrics.StartGridCol;
							var nCurEndGrid = cellLast.Metrics.StartGridCol + cellLast.Get_GridSpan() - 1;
							if(null != nPrevStartGrid && null != nPrevEndGrid)
							{
								//учитываем вертикальный merge, раздвигаем границы
								if(nCurStartGrid > nPrevStartGrid)
								{
									for(var j = elem.indexStart - 1; j >= 0; --j)
									{
										var cellCur = row.Get_Cell(j);
										if(vmerge_Continue == cellCur.Get_VMerge())
										{
											var nCurGridCol = cellCur.Metrics.StartGridCol;
											if(nCurGridCol >= nPrevStartGrid)
											{
												nCurStartGrid = nCurGridCol;
												elem.indexStart = j;
											}
											else
												break;
										}
										else
											break;
									}
								}
								if(nCurEndGrid < nPrevEndGrid)
								{
									for(var j = elem.indexEnd + 1; j < row.Get_CellsCount(); ++j)
									{
										var cellCur = row.Get_Cell(j);
										if(vmerge_Continue == cellCur.Get_VMerge())
										{
											var nCurGridCol = cellCur.Metrics.StartGridCol + cellCur.Get_GridSpan() - 1;
											if(nCurGridCol <= nPrevEndGrid)
											{
												nCurEndGrid = nCurGridCol;
												elem.indexEnd = j;
											}
											else
												break;
										}
										else
											break;
									}
								}
							}
							elem.gridStart = nPrevStartGrid = nCurStartGrid;
							elem.gridEnd = nPrevEndGrid = nCurEndGrid;
							if(null == nMinGrid || nMinGrid > nCurStartGrid)
								nMinGrid = nCurStartGrid;
							if(null == nMaxGrid || nMaxGrid < nCurEndGrid)
								nMaxGrid = nCurEndGrid;
						}
						if(null != nMinGrid && null != nMaxGrid)
						{
							//выставляем after, before
							for(var i = 0 ,length = aSelectedRows.length; i < length; ++i)
							{
								var elem = aSelectedRows[i];
								elem.before = elem.gridStart - nMinGrid;
								elem.after = nMaxGrid - elem.gridEnd;
							}
							this.oBinaryFileWriter.copyParams.bLockCopyElems++;
							this.CopyTable(oDomTarget, Item, aSelectedRows);
							this.oBinaryFileWriter.copyParams.bLockCopyElems--;
							this.oBinaryFileWriter.CopyTable(Item, aSelectedRows, nMinGrid, nMaxGrid);
						}
                    }
                }
                else
				{
					this.oBinaryFileWriter.copyParams.bLockCopyElems++;
                    this.CopyTable(oDomTarget, Item, null);
					this.oBinaryFileWriter.copyParams.bLockCopyElems--;
					this.oBinaryFileWriter.CopyTable(Item, null);
				}
            }
            else if ( type_Paragraph === Item.GetType() )
            {
                //todo ����� ������ ��� �������� ������ ���� Index == End
				this.oBinaryFileWriter.CopyParagraph(Item, bUseSelection);
                this.CopyParagraph(oDomTarget, Item, Index == End, bUseSelection, oDocument.Content, Index);
            }
        }
        this.CommitList(oDomTarget);
    },
    Start : function(node)
    {
		this.oBinaryFileWriter.CopyStart();
        var oDocument = this.oDocument;
        if(g_bIsDocumentCopyPaste)
        {
			if(!this.api.DocumentReaderMode)
			{
				var Def_pPr = oDocument.Styles.Default.ParaPr;
				if(docpostype_HdrFtr === oDocument.CurPos.Type)
				{
					if(null != oDocument.HdrFtr && null != oDocument.HdrFtr.CurHdrFtr && null != oDocument.HdrFtr.CurHdrFtr.Content)
						oDocument = oDocument.HdrFtr.CurHdrFtr.Content;
				}
				if ( oDocument.CurPos.Type == docpostype_FlowObjects )
				{
					var oData = oDocument.Selection.Data.FlowObject;
					switch ( oData.Get_Type() )
					{
						case flowobject_Image:
						{
							this.Para = document.createElement( "p" );
							//������ ���� �������� ������ ���� ��������
							this.InitRun();
							this.ParseItem(oData);
							var oImg = oData;
							var sSrc = oImg.Img;
							if(sSrc.length > 0)
							{
								sSrc = this.getSrc(sSrc);
								this.aInnerHtml.push("<img width=\""+Math.round(oImg.W * g_dKoef_mm_to_pix)+"\" height=\""+Math.round(oImg.H * g_dKoef_mm_to_pix)+"\" src=\""+sSrc+"\" />");
							}
							this.CommitSpan(false);
							this.ElemToSelect.appendChild( this.Para );
							return;
						}
						case flowobject_Table:
						{
							if(null != oData.Table && null != oData.Table.CurCell && null != oData.Table.CurCell.Content)
								oDocument = oData.Table.CurCell.Content;
							break;
						}
					}
				}

                if(oDocument.CurPos.Type === docpostype_DrawingObjects)
                {
                    switch(oDocument.DrawingObjects.curState.id)
                    {
                        case STATES_ID_TEXT_ADD:
                        {
                            var text_object = oDocument.DrawingObjects.curState.textObject;
                            if(text_object != null && text_object.GraphicObj != null && text_object.GraphicObj.textBoxContent != null)
                                oDocument = text_object.GraphicObj.textBoxContent;
                            break;
                        }
                        case STATES_ID_TEXT_ADD_IN_GROUP:
                        {
                            text_object = oDocument.DrawingObjects.curState.textObject;
                            if(text_object != null &&  text_object.textBoxContent != null)
                                oDocument = text_object.textBoxContent;
                            break;
                        }

                        case STATES_ID_GROUP:
                        {
                            var s_arr = oDocument.DrawingObjects.curState.group.selectionInfo.selectionArray;

                            this.Para = document.createElement( "p" );
                            this.InitRun();
							
							if(copyPasteUseBinery)
							{
								var newArr = null;
								var tempAr = null;
								if(s_arr)
								{
									tempAr = [];
									for(var k = 0; k < s_arr.length; k++)
									{
										tempAr[k] = s_arr[k].absOffsetY;
									}
								}
								tempAr.sort(function(a,b){return a-b;})
								newArr = [];
								for(var k = 0; k < tempAr.length; k++)
								{
									var absOffsetY = tempAr[k];
									for(var i = 0; i < s_arr.length; i++)
									{
										if(absOffsetY == s_arr[i].absOffsetY)
										{
											newArr[k] = s_arr[i];
										}
									}
								}
								if(newArr != null)
									s_arr = newArr;
							}
		
                            for(var i = 0; i < s_arr.length; ++i)
                            {
                                var cur_element = s_arr[i];
                                if(isRealObject(cur_element.parent))
                                {
                                    var base64_img = cur_element.parent.getBase64Img();
									
									if(copyPasteUseBinery)
									{
										var wrappingType = cur_element.parent.wrappingType;
										var DrawingType = cur_element.parent.DrawingType;
										var tempParagraph = new Paragraph(oDocument, oDocument, 0, 0, 0, 0, 0);
										var index = 0;
										tempParagraph.Content.unshift(new ParaDrawing());
										tempParagraph.Content[index].wrappingType = wrappingType;
										tempParagraph.Content[index].DrawingType = DrawingType;
										tempParagraph.Content[index].GraphicObj = cur_element
										tempParagraph.Selection.EndPos = index + 1;
										tempParagraph.Selection.StartPos = index;
										tempParagraph.Selection.Use = true;
										
										this.oBinaryFileWriter.CopyParagraph(tempParagraph, true);
									}
									
                                    var src = this.getSrc(base64_img);
                                    var alt_content = cur_element.parent.writeToBinaryForCopyPaste();

                                    this.aInnerHtml.push("<img width=\""+Math.round(/*cur_element.W*/5 * g_dKoef_mm_to_pix)+"\" height=\""+Math.round(/*cur_element.H*/5 * g_dKoef_mm_to_pix)+"\" src=\""+src+"\" alt=\""+alt_content+"\" />");

                                    this.ElemToSelect.appendChild( this.Para );
                                }

                            }
                            this.CommitSpan(false);
							
							if(copyPasteUseBinery)
							{
								this.oBinaryFileWriter.CopyEnd();
								var sBase64 = this.oBinaryFileWriter.GetResult();
								if(this.ElemToSelect.children && this.ElemToSelect.children.length == 1 && window.USER_AGENT_SAFARI_MACOS)
								{
									$(this.ElemToSelect.children[0]).css("font-weight", "normal");;
									$(this.ElemToSelect.children[0]).wrap(document.createElement("b"));
								}
								if(this.ElemToSelect.children[0])
									$(this.ElemToSelect.children[0]).addClass("docData;" + sBase64);
							}
							
                            return;
                        }
                        default :
                        {
                            var gr_objects = oDocument.DrawingObjects;
                            var selection_array = gr_objects.selectionInfo.selectionArray;

                            this.Para = document.createElement( "span" );
                            this.InitRun();
							var selectionTrue;
							var selectIndex;
                            for(var i = 0; i < selection_array.length; ++i)
                            {
                                var cur_element = selection_array[i];
                                var base64_img = cur_element.getBase64Img();
                                var src = this.getSrc(base64_img);
                                var alt_content = cur_element.writeToBinaryForCopyPaste();

                                this.aInnerHtml.push("<img width=\""+Math.round(/*cur_element.W*/5 * g_dKoef_mm_to_pix)+"\" height=\""+Math.round(/*cur_element.H*/5 * g_dKoef_mm_to_pix)+"\" src=\""+src+"\" alt=\""+alt_content+"\" />");

                                this.ElemToSelect.appendChild( this.Para );
								
								if(copyPasteUseBinery)
								{
									var index = cur_element.Parent.Index;
									selectionTrue = 
									{
										EndPos : oDocument.Content[index].Selection.EndPos,
										StartPos: oDocument.Content[index].Selection.StartPos
									};
									var inIndex;
									for(var k = 0; k < oDocument.Content[index].Content.length;k++)
									{
										if(oDocument.Content[index].Content[k] == cur_element)
										{
											inIndex = k;
											break;
										}
									}
									//меняем Selection
									oDocument.Content[index].Selection.EndPos = inIndex + 1;
									oDocument.Content[index].Selection.StartPos = inIndex;
									oDocument.Content[index].Selection.Use = true;
									
									this.oBinaryFileWriter.CopyParagraph(oDocument.Content[index], true);
									
									//возвращаем Selection
									oDocument.Content[index].Selection.StartPos = selectionTrue.StartPos;
									oDocument.Content[index].Selection.EndPos = selectionTrue.EndPos;
								}
                            }
							
                            this.CommitSpan(false);
						
							if(copyPasteUseBinery)
							{
								this.oBinaryFileWriter.CopyEnd();
								var sBase64 = this.oBinaryFileWriter.GetResult();
								if(this.ElemToSelect.children && this.ElemToSelect.children.length == 1 && window.USER_AGENT_SAFARI_MACOS)
								{
									$(this.ElemToSelect.children[0]).css("font-weight", "normal");;
									$(this.ElemToSelect.children[0]).wrap(document.createElement("b"));
								}
								if(this.ElemToSelect.children[0])
									$(this.ElemToSelect.children[0]).addClass("docData;" + sBase64);
							}
                            return;
                        }

                    }
                }
				if ( true === oDocument.Selection.Use )
				{
					this.CopyDocument(this.ElemToSelect, oDocument, true);
				}
			}
			else
				this.CopyDocument(this.ElemToSelect, oDocument, false);
        }
        else
        {
            var presentation = editor.WordControl.m_oLogicDocument;
            var pasteString = "";
            switch(editor.WordControl.Thumbnails.FocusObjType)
            {
                case FOCUS_OBJECT_MAIN:
                {
                    var slide = presentation.Slides[presentation.CurPage];
                    var graphicObjects = slide.graphicObjects;

                    switch(graphicObjects.State.id)
                    {
                        case STATES_ID_TEXT_ADD:
                        case STATES_ID_TEXT_ADD_IN_GROUP:
                        {
                            if(graphicObjects.State.textObject instanceof CShape)
                            {
                                this.oPresentationWriter.WriteString2("TeamLab1");
                                this.oPresentationWriter.WriteString2(editor.DocumentUrl);
                                this.oPresentationWriter.WriteDouble(presentation.Width);
                                this.oPresentationWriter.WriteDouble(presentation.Height);
                                this.CopyPresentationText(this.ElemToSelect, graphicObjects.State.textObject.txBody.content, true);
                            }
                            else  if(graphicObjects.State.textObject instanceof CGraphicFrame)
                            {
                                var table = graphicObjects.State.textObject.graphicObject;
                                switch(table.Selection.Type)
                                {
                                    case table_Selection_Text:
                                    {
                                        this.oPresentationWriter.WriteString2("TeamLab1");
                                        this.oPresentationWriter.WriteString2(editor.DocumentUrl);
                                        this.oPresentationWriter.WriteDouble(presentation.Width);
                                        this.oPresentationWriter.WriteDouble(presentation.Height);
                                        this.CopyPresentationText(this.ElemToSelect, graphicObjects.State.textObject.graphicObject.CurCell.Content, true);
                                        break;
                                    }
                                    case table_Selection_Cell:
                                    {
                                        this.oPresentationWriter.WriteString2("TeamLab4");
                                        this.oPresentationWriter.WriteString2(editor.DocumentUrl);
                                        this.oPresentationWriter.WriteDouble(presentation.Width);
                                        this.oPresentationWriter.WriteDouble(presentation.Height);
                                        this.CopyPresentationTableCells(this.ElemToSelect, graphicObjects.State.textObject);
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                        default :
                        {
                            if(graphicObjects.selectedObjects.length > 0)
                            {
                                this.aInnerHtml = [];
                                this.Para = document.createElement("p");
                                this.oPresentationWriter.WriteString2("TeamLab2");
                                this.oPresentationWriter.WriteString2(editor.DocumentUrl);
                                this.oPresentationWriter.WriteDouble(presentation.Width);
                                this.oPresentationWriter.WriteDouble(presentation.Height);
                                this.oPresentationWriter.WriteULong(graphicObjects.selectedObjects.length);
                                for(var i = 0; i < graphicObjects.selectedObjects.length; ++i)
                                {
                                    if(!(graphicObjects.selectedObjects[i] instanceof CGraphicFrame))
                                    {
                                        this.oPresentationWriter.WriteBool(true);
                                        this.CopyGraphicObject(this.ElemToSelect, graphicObjects.selectedObjects[i]);
                                    }
                                    else
                                    {
                                        this.oPresentationWriter.WriteBool(false);
                                        this.CopyPresentationTableFull(this.ElemToSelect, graphicObjects.selectedObjects[i]);
                                    }
                                }

                                this.CommitSpan(false);
                                for(var i = 0; i < this.Para.childNodes.length; i++)
                                    this.ElemToSelect.appendChild( this.Para.childNodes[i].cloneNode(true));
                            }
                            break;
                        }
                    }
                    break;
                }
                case FOCUS_OBJECT_THUMBNAILS :
                {
                    var selected_slides = editor.WordControl.Thumbnails.GetSelectedArray();
                    if(selected_slides.length > 0)
                    {
                        this.aInnerHtml = [];
                        this.Para = document.createElement( "p" );
                        this.oPresentationWriter.WriteString2("TeamLab3");
                        this.oPresentationWriter.WriteString2(editor.DocumentUrl);
                        this.oPresentationWriter.WriteDouble(presentation.Width);
                        this.oPresentationWriter.WriteDouble(presentation.Height);
                        this.oPresentationWriter.WriteULong(selected_slides.length);
                        var layouts_map = {};
                        var layout_count = 0;
                        for(var i = 0; i < selected_slides.length; ++i)
                        {
                            this.CopySlide(this.ElemToSelect, editor.WordControl.m_oLogicDocument.Slides[selected_slides[i]]);
                            if(!layouts_map[editor.WordControl.m_oLogicDocument.Slides[selected_slides[i]].Layout.Get_Id()])
                                ++layout_count;
                            layouts_map[editor.WordControl.m_oLogicDocument.Slides[selected_slides[i]].Layout.Get_Id()] = editor.WordControl.m_oLogicDocument.Slides[selected_slides[i]].Layout;
                        }

                        this.oPresentationWriter.WriteULong(layout_count);
                        var arr_layouts_id = [];
                        var t = 0;
                        for(var key in layouts_map)
                        {
                            this.CopyLayout(layouts_map[key]);
                            arr_layouts_id[t] = layouts_map[key];
                            ++t;
                        }

                        var arr_ind = [];
                        for(var i = 0; i < selected_slides.length; ++i)
                        {
                            for(t = 0; t < arr_layouts_id.length; ++t)
                            {
                                if(editor.WordControl.m_oLogicDocument.Slides[selected_slides[i]].Layout === arr_layouts_id[t])
                                {
                                    arr_ind[i] = t;
                                    break;
                                }
                            }
                        }
                        for(var i = 0; i < arr_ind.length; ++i)
                        {
                            this.oPresentationWriter.WriteULong(arr_ind[i]);
                        }
                        this.CommitSpan(false);
                        for(var i = 0; i < this.Para.childNodes.length; i++)
                            this.ElemToSelect.appendChild( this.Para.childNodes[i].cloneNode(true) );
                    }
                    break;
                }
            }
            var sBase64 = this.oPresentationWriter.GetBase64Memory();
            sBase64 = "" + this.oPresentationWriter.pos + ";" + sBase64;
			if(this.ElemToSelect.children && this.ElemToSelect.children.length == 1 && window.USER_AGENT_SAFARI_MACOS)
			{
				$(this.ElemToSelect.children[0]).css("font-weight", "normal");;
				$(this.ElemToSelect.children[0]).wrap(document.createElement("b"));
			}
            if(this.ElemToSelect.children[0])
                $(this.ElemToSelect.children[0]).addClass("docData;" + sBase64);
        }
		this.oBinaryFileWriter.CopyEnd();
		if(copyPasteUseBinery && this.oBinaryFileWriter.copyParams.itemCount > 0)
		{
			var sBase64 = this.oBinaryFileWriter.GetResult();
			if(this.ElemToSelect.children && this.ElemToSelect.children.length == 1 && window.USER_AGENT_SAFARI_MACOS)
			{
				$(this.ElemToSelect.children[0]).css("font-weight", "normal");;
				$(this.ElemToSelect.children[0]).wrap(document.createElement("b"));
			}
			if(this.ElemToSelect.children[0])
				$(this.ElemToSelect.children[0]).addClass("docData;" + sBase64);
		}
    },


    CopySlide: function(oDomTarget, slide)
    {
        var sSrc = slide.getBase64Img();
        //if(sSrc.length > 0)
        {
           // sSrc = this.getSrc(sSrc);
            var _bounds_cheker = new CSlideBoundsChecker();
            slide.draw(_bounds_cheker, 0);
            this.aInnerHtml.push("<img width=\""+Math.round((_bounds_cheker.Bounds.max_x - _bounds_cheker.Bounds.min_x + 1) * g_dKoef_mm_to_pix)+"\" height=\""+Math.round((_bounds_cheker.Bounds.max_y - _bounds_cheker.Bounds.min_y + 1) * g_dKoef_mm_to_pix)+"\" src=\""+sSrc+"\" />");
            this.oPresentationWriter.WriteString2(slide.Layout.Get_Id());
            var table_styles_ids = [];
            var sp_tree = slide.cSld.spTree;
            for(var i = 0; i < sp_tree.length; ++i)
            {
                if(sp_tree[i] instanceof CGraphicFrame)
                {
                    table_styles_ids.push(sp_tree[i].graphicObject.styleIndex);
                    sp_tree[i].graphicObject.styleIndex = -1;
                }
            }
            this.oPresentationWriter.WriteULong(table_styles_ids.length);
            for(var i = 0; i < table_styles_ids.length; ++i)
            {
                this.oPresentationWriter.WriteBool(isRealNumber(table_styles_ids[i]) && table_styles_ids[i] > -1)
                if(isRealNumber(table_styles_ids[i]) && table_styles_ids[i] > -1)
                {
                    this.oPresentationWriter.WriteULong(table_styles_ids[i]);
                }
            }
            this.oPresentationWriter.WriteSlide(slide);

            var  j = 0;
            for(var i = 0; i < sp_tree.length; ++i)
            {
                if(sp_tree[i] instanceof CGraphicFrame)
                {
                    sp_tree[i].graphicObject.styleIndex = table_styles_ids[j];
                    ++j;
                }
            }
        }
    },

    CopyLayout: function(layout)
    {
        this.oPresentationWriter.WriteSlideLayout(layout);
    },


    CopyPresentationTableCells: function(oDomTarget, graphicFrame)
    {
        var aSelectedRows = new Array();
        var oRowElems = new Object();
        var Item = graphicFrame.graphicObject;
        if(Item.Selection.Data.length > 0)
        {
            for(var i = 0, length = Item.Selection.Data.length; i < length; ++i)
            {
                var elem = Item.Selection.Data[i];
                var rowElem = oRowElems[elem.Row];
                if(null == rowElem)
                {
                    rowElem = {row: elem.Row, gridStart: null, gridEnd: null, indexStart: null, indexEnd: null, after: null, before: null, cells: {}};
                    oRowElems[elem.Row] = rowElem;
                    aSelectedRows.push(rowElem);
                }
                if(null == rowElem.indexEnd || elem.Cell > rowElem.indexEnd)
                    rowElem.indexEnd = elem.Cell;
                if(null == rowElem.indexStart || elem.Cell < rowElem.indexStart)
                    rowElem.indexStart = elem.Cell;
                rowElem.cells[elem.Cell] = 1;
            }
        }
        aSelectedRows.sort(function(a,b){
            return a.row - b.row;
        });
        var nMinGrid = null;
        var nMaxGrid = null;
        var nPrevStartGrid = null;
        var nPrevEndGrid = null;
        var nPrevRowIndex = null;
        for(var i = 0, length = aSelectedRows.length; i < length; ++i)
        {
            var elem = aSelectedRows[i];
            var nRowIndex = elem.row;
            if(null != nPrevRowIndex)
            {
                if(nPrevRowIndex + 1 != nRowIndex)
                {
                    nMinGrid = null;
                    nMaxGrid = null;
                    break;
                }
            }
            nPrevRowIndex = nRowIndex;
            var row = Item.Content[nRowIndex];
            var cellFirst = row.Get_Cell(elem.indexStart);
            var cellLast = row.Get_Cell(elem.indexEnd);
            var nCurStartGrid = cellFirst.Metrics.StartGridCol;
            var nCurEndGrid = cellLast.Metrics.StartGridCol + cellLast.Get_GridSpan() - 1;
            if(null != nPrevStartGrid && null != nPrevEndGrid)
            {
                //учитываем вертикальный merge, раздвигаем границы
                if(nCurStartGrid > nPrevStartGrid)
                {
                    for(var j = elem.indexStart - 1; j >= 0; --j)
                    {
                        var cellCur = row.Get_Cell(j);
                        if(vmerge_Continue == cellCur.Get_VMerge())
                        {
                            var nCurGridCol = cellCur.Metrics.StartGridCol;
                            if(nCurGridCol >= nPrevStartGrid)
                            {
                                nCurStartGrid = nCurGridCol;
                                elem.indexStart = j;
                            }
                            else
                                break;
                        }
                        else
                            break;
                    }
                }
                if(nCurEndGrid < nPrevEndGrid)
                {
                    for(var j = elem.indexEnd + 1; j < row.Get_CellsCount(); ++j)
                    {
                        var cellCur = row.Get_Cell(j);
                        if(vmerge_Continue == cellCur.Get_VMerge())
                        {
                            var nCurGridCol = cellCur.Metrics.StartGridCol + cellCur.Get_GridSpan() - 1;
                            if(nCurGridCol <= nPrevEndGrid)
                            {
                                nCurEndGrid = nCurGridCol;
                                elem.indexEnd = j;
                            }
                            else
                                break;
                        }
                        else
                            break;
                    }
                }
            }
            elem.gridStart = nPrevStartGrid = nCurStartGrid;
            elem.gridEnd = nPrevEndGrid = nCurEndGrid;
            if(null == nMinGrid || nMinGrid > nCurStartGrid)
                nMinGrid = nCurStartGrid;
            if(null == nMaxGrid || nMaxGrid < nCurEndGrid)
                nMaxGrid = nCurEndGrid;
        }
        if(null != nMinGrid && null != nMaxGrid)
        {
            //выставляем after, before
            for(var i = 0 ,length = aSelectedRows.length; i < length; ++i)
            {
                var elem = aSelectedRows[i];
                elem.before = elem.gridStart - nMinGrid;
                elem.after = nMaxGrid - elem.gridEnd;
            }
            this.CopyTable(oDomTarget, Item, aSelectedRows);
        }
        var is_on = History.Is_On();
        if(is_on)
        {
            History.TurnOff();
        }
        var graphic_frame = new CGraphicFrame(graphicFrame.parent);
        var grid = [];

        for(var i = nMinGrid; i <= nMaxGrid; ++i)
        {
            grid.push(graphicFrame.graphicObject.TableGrid[i]);
        }
        var table = new CTable(editor.WordControl.m_oDrawingDocument, graphicFrame, false, 0, 0, 0, 0, 0, aSelectedRows.length, nMaxGrid - nMinGrid+1, grid);
        table.setStyleIndex(graphicFrame.graphicObject.styleIndex);
        graphic_frame.setGraphicObject(table);
        graphic_frame.setXfrm(0, 0, 20, 30, 0, false, false);
        var  b_style_index = false;
        if(isRealNumber(graphic_frame.graphicObject.styleIndex) && graphic_frame.graphicObject.styleIndex > -1)
        {
            b_style_index = true;
        }

        this.oPresentationWriter.WriteULong(1);
        this.oPresentationWriter.WriteBool(false);
        this.oPresentationWriter.WriteBool(b_style_index);
        if(b_style_index)
        {
            this.oPresentationWriter.WriteULong(graphic_frame.graphicObject.styleIndex);
        }
        var old_style_index = graphic_frame.graphicObject.styleIndex;
        graphic_frame.graphicObject.styleIndex = -1;
        this.oPresentationWriter.WriteTable(graphic_frame);
        graphic_frame.graphicObject.styleIndex = old_style_index;
        if(is_on)
        {
            History.TurnOn();
        }
        this.oBinaryFileWriter.copyParams.itemCount = 0;
    },

    CopyPresentationTableFull: function(oDomTarget, graphicFrame)
    {
        var aSelectedRows = new Array();
        var oRowElems = new Object();
        var Item = graphicFrame.graphicObject;

        this.CopyTable(oDomTarget, Item, null);
        var b_style_index = false;
        if(isRealNumber(graphicFrame.graphicObject.styleIndex) && graphicFrame.graphicObject.styleIndex > -1)
        {
            b_style_index = true;
        }

        this.oPresentationWriter.WriteBool(b_style_index);
        if(b_style_index)
        {
            this.oPresentationWriter.WriteULong(graphicFrame.graphicObject.styleIndex);
        }
        var old_style_index = graphicFrame.graphicObject.styleIndex;

        graphicFrame.graphicObject.styleIndex = -1;
        this.oPresentationWriter.WriteTable(graphicFrame);
        graphicFrame.graphicObject.styleIndex = old_style_index;
        this.oBinaryFileWriter.copyParams.itemCount = 0;
    },

    CopyPresentationText : function(oDomTarget, oDocument, bUseSelection)
    {
        var Start = 0;
        var End = 0;
        if(bUseSelection)
        {
            if ( true === oDocument.Selection.Use)
            {
                if ( selectionflag_DrawingObject === oDocument.Selection.Flag )
                {
                    this.Para = document.createElement( "p" );
                    //������ ���� �������� ������ ���� ��������
                    this.InitRun();
                    this.ParseItem(oDocument.Selection.Data.DrawingObject);
                    this.CommitSpan(false);

                    for(var i = 0; i < this.Para.childNodes.length; i++)
                        this.ElemToSelect.appendChild( this.Para.childNodes[i].cloneNode(true) );
                }
                else
                {
                    Start = oDocument.Selection.StartPos;
                    End = oDocument.Selection.EndPos;
                    if ( Start > End )
                    {
                        var Temp = End;
                        End = Start;
                        Start = Temp;
                    }
                }
            }
        }
        else
        {
            Start = 0;
            End = oDocument.Content.length - 1;
        }
        // HtmlText

        this.oPresentationWriter.WriteULong(End - Start + 1);
        for ( var Index = Start; Index <= End; Index++ )
        {
            var Item = oDocument.Content[Index];
            var selectStart = Item.Selection.Use ? Item.Selection.StartPos : 0;
            var selectEnd = Item.Selection.Use ? Item.Selection.EndPos : Item.Content.length;

            if ( selectStart > selectEnd )
            {
                var Temp = selectEnd;
                selectEnd = selectStart;
                selectStart = Temp;
            }
            this.oPresentationWriter.StartRecord(0);
            this.oPresentationWriter.WriteParagraph(Item, selectStart, selectEnd);
            this.oPresentationWriter.EndRecord();

            this.CopyParagraph(oDomTarget, Item, Index == End, bUseSelection, oDocument.Content, Index);
        }
        this.CommitList(oDomTarget);
    },


    CopyGraphicObject: function(oDomTarget, oGraphicObj)
    {
        var sSrc = oGraphicObj.getBase64Img();
        if(sSrc.length > 0)
        {
            sSrc = this.getSrc(sSrc);
            var _bounds_cheker = new CSlideBoundsChecker();
            oGraphicObj.draw(_bounds_cheker, 0);
            this.aInnerHtml.push("<img width=\""+Math.round((_bounds_cheker.Bounds.max_x - _bounds_cheker.Bounds.min_x + 1) * g_dKoef_mm_to_pix)+"\" height=\""+Math.round((_bounds_cheker.Bounds.max_y - _bounds_cheker.Bounds.min_y + 1) * g_dKoef_mm_to_pix)+"\" src=\""+sSrc+"\" />");
            if(oGraphicObj instanceof CShape)
            {
                this.oPresentationWriter.WriteShape(oGraphicObj);
            }
            else if(oGraphicObj instanceof CImageShape)
            {
                this.oPresentationWriter.WriteImage(oGraphicObj);
            }
            else if(oGraphicObj instanceof CGroupShape)
            {
                this.oPresentationWriter.WriteGroupShape(oGraphicObj);
            }
        }
    }
};

function Editor_Paste_GetElem(api, bClean)
{
    var oWordControl = api.WordControl;
    var pastebin = document.getElementById(PASTE_ELEMENT_ID);
    if(!pastebin){
        pastebin = document.createElement("div");
        pastebin.setAttribute( 'id', PASTE_ELEMENT_ID );

        if (/MSIE/g.test(navigator.userAgent))
            pastebin.style.position = 'fixed';
        else
            pastebin.style.position = 'absolute';

        pastebin.style.top = '-100px';
        pastebin.style.left = '0px';
        pastebin.style.width = '10000px';
        pastebin.style.height = '100px';
        pastebin.style.overflow = 'hidden';
        pastebin.style.zIndex = -1000;
        //��������� ������ ������������, ����� �������� �������� ����� pastebin �������� span � ������� ��� �������� ������ � computedStyle ���������� ����������� ��������� ��������� �� ���������
        var Def_rPr = oWordControl.m_oLogicDocument.Styles.Default.TextPr;
        pastebin.style.fontFamily = Def_rPr.FontFamily.Name;

        if (!api.DocumentReaderMode)
            pastebin.style.fontSize = Def_rPr.FontSize + "pt";
        else
        {
            api.DocumentReaderMode.CorrectDefaultFontSize(Def_rPr.FontSize);
            pastebin.style.fontSize = "1em";
        }
        pastebin.style.lineHeight = "1px";//todo FF ������ ���������� computedStyle � px, ������� ����� ���� ������� default ���������
        pastebin.style.MozUserSelect = "text";
        pastebin.style["-khtml-user-select"] = "text";
        pastebin.style["-o-user-select"] = "text";
        pastebin.style["user-select"] = "text";
        pastebin.style["-webkit-user-select"] = "text";
        pastebin.setAttribute("contentEditable", true);

        document.body.appendChild( pastebin );
    }
    else if(bClean){
        //������� ����������
        var aChildNodes = pastebin.childNodes;
        for (var length = aChildNodes.length, i = length - 1; i >= 0; i--)
        {
            pastebin.removeChild(aChildNodes[i]);
        }
    }

    if (!window.USER_AGENT_SAFARI_MACOS)
    {
        pastebin.onpaste = function(e){
            if (!window.GlobalPasteFlag)
                return;

            Body_Paste(api,e);
            pastebin.onpaste = null;
        };
    }

    return pastebin;
}
function Editor_Paste_Button(api)
{
    if(/MSIE/g.test(navigator.userAgent))
    {
        document.body.style.MozUserSelect = "text";
        delete document.body.style["-khtml-user-select"];
        delete document.body.style["-o-user-select"];
        delete document.body.style["user-select"];
        document.body.style["-webkit-user-select"] = "text";

        var pastebin = Editor_Paste_GetElem(api, true);

        pastebin.style.display  = "block";
        pastebin.focus();

        var selection = window.getSelection();
        var rangeToSelect = document.createRange();
        rangeToSelect.selectNodeContents (pastebin);
        selection.removeAllRanges ();
        selection.addRange(rangeToSelect);

        //rangeToSelect.execCommand("paste", false);
        document.execCommand("paste");

        if (!window.USER_AGENT_SAFARI_MACOS)
            pastebin.blur();

        pastebin.style.display  = ELEMENT_DISPAY_STYLE;

        document.body.style.MozUserSelect = "none";
        document.body.style["-khtml-user-select"] = "none";
        document.body.style["-o-user-select"] = "none";
        document.body.style["user-select"] = "none";
        document.body.style["-webkit-user-select"] = "none";

		History.Create_NewPoint();
        Editor_Paste(api, false);
        return true;
    }
	else
	{
		var ElemToSelect = document.getElementById( COPY_ELEMENT_ID );
		if(ElemToSelect)
		{
			History.Create_NewPoint();
			Editor_Paste_Exec(api, ElemToSelect);
		}
		return true;
	}
    return false;
}
function CanPaste(oDocument)
{
    //����� ������ paste ����� Select � ���������
    //�� ����� ������ paste ����� select �� ������� �������, ��������, ������
    var oTargetDoc = oDocument;
    if(g_bIsDocumentCopyPaste)
    {
        if ( docpostype_HdrFtr === oTargetDoc.CurPos.Type )
        {
            if(null != oTargetDoc.HdrFtr.CurHdrFtr)
                oTargetDoc = oTargetDoc.HdrFtr.CurHdrFtr.Content;
            else
                return false;
        }
        if ( docpostype_FlowObjects == oTargetDoc.CurPos.Type )
        {
            var nType = oTargetDoc.Selection.Data.FlowObject.Get_Type();
            if(flowobject_Table == nType)
            {
                var oTable = oTargetDoc.Selection.Data.FlowObject.Table;
                if(true == oTable.Selection.Use && table_Selection_Cell == oTable.Selection.Type)
                    return false;
                if(null != oTable.CurCell && null != oTable.CurCell.Content)
                {
                    oTargetDoc = oTable.CurCell.Content;
                }
            }
        }
    }
    else
    {
        if ( docpostype_FlowObjects == oTargetDoc.CurPos.Type )
        {
            var _cur_slide_elements = oTargetDoc.Slides[oTargetDoc.CurPage].elementsManipulator;
            if(_cur_slide_elements.obj != undefined && _cur_slide_elements.obj.txBody && _cur_slide_elements.obj.txBody.content)
            {
                return true;
            }
        }
    }
    return true;
};
function Editor_Paste(api, bClean)
{
    window.GlobalPasteFlagCounter = 1;
    var oWordControl = api.WordControl;
    oWordControl.bIsEventPaste = false;
    var oDocument = oWordControl.m_oLogicDocument;
	if(isOnlyLocalBufferSafariWord && navigator.userAgent.toLowerCase().indexOf('safari') > -1 && navigator.userAgent.toLowerCase().indexOf('mac'))
	{
		 var ElemToSelect = document.getElementById( "SelectId" );
		 if(ElemToSelect)
            Editor_Paste_Exec(api, ElemToSelect);
		return;
	}
    if(false == CanPaste(oDocument))
        return;

    document.body.style.MozUserSelect = "text";
    delete document.body.style["-khtml-user-select"];
    delete document.body.style["-o-user-select"];
    delete document.body.style["user-select"];
    document.body.style["-webkit-user-select"] = "text";

    if (window.USER_AGENT_IE)
    {
        // не убирать!!! это для ие. чтобы не селектились элементы
        document.onselectstart= function() {
        }
    }

    var Text;
    var pastebin = Editor_Paste_GetElem(api, bClean);
    pastebin.style.display  = "block";
    pastebin.focus();
    // Safari requires a filler node inside the div to have the content pasted into it. (#4882)
    pastebin.appendChild( document.createTextNode( '\xa0' ) );

    if (window.getSelection) {  // all browsers, except IE before version 9
        var selection = document.defaultView.getSelection ();
        selection.removeAllRanges ();
        var rangeToSelect = document.createRange ();
        rangeToSelect.selectNodeContents (pastebin);

        selection.removeAllRanges ();
        selection.addRange (rangeToSelect);
    } else {
        if (document.body.createTextRange) {    // Internet Explorer
            var rangeToSelect = document.body.createTextRange ();
            rangeToSelect.moveToElementText (pastebin);
            rangeToSelect.select ();
        }
    }
    //���� ���������� paste
    var func_timeout = function() {

        if (window.USER_AGENT_SAFARI_MACOS)
        {
            if (window.GlobalPasteFlagCounter != 2 && !window.GlobalPasteFlag)
            {
                window.setTimeout(func_timeout, 10);
                return;
            }
        }

        document.body.style.MozUserSelect = "none";
        document.body.style["-khtml-user-select"] = "none";
        document.body.style["-o-user-select"] = "none";
        document.body.style["user-select"] = "none";
        document.body.style["-webkit-user-select"] = "none";

        if (window.USER_AGENT_IE)
        {
            // не убирать!!! это для ие. чтобы не селектились элементы
            document.onselectstart= function() {
                return false;
            }
        }

        if (!window.USER_AGENT_SAFARI_MACOS)
            pastebin.onpaste = null;

        if(!oWordControl.bIsEventPaste)
        {
            Editor_Paste_Exec(api, pastebin);
        }
        else
            pastebin.style.display  = ELEMENT_DISPAY_STYLE;
    };

    var _interval_time = window.USER_AGENT_MACOS ? 200 : 0;
    window.setTimeout( func_timeout, _interval_time );
};
function CopyPasteCorrectString(str)
{
    var res = str;
    res = res.replace(/&/g,'&amp;');
    res = res.replace(/</g,'&lt;');
    res = res.replace(/>/g,'&gt;');
    res = res.replace(/'/g,'&apos;');
    res = res.replace(/"/g,'&quot;');
    return res;
};
function Body_Paste(api, e)
{
    var oWordControl = api.WordControl;
    if (e && e.clipboardData && e.clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
        var bExist = false;
        //������ chrome ��������� �������� 'text/html', safari ������ 'text/plain'
        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        var sHtml = null;

        var fPasteHtml = function(sHtml)
        {
            if(null != sHtml)
            {
                //���������� html � IFrame
                var ifr = document.getElementById("pasteFrame");
                if (!ifr)
                {
                    ifr = document.createElement("iframe");
                    ifr.name = "pasteFrame";
                    ifr.id = "pasteFrame";
                    ifr.style.position = 'absolute';
                    ifr.style.top = '-100px';
                    ifr.style.left = '0px';
                    ifr.style.width = '10000px';
                    ifr.style.height = '100px';
                    ifr.style.overflow = 'hidden';
                    ifr.style.zIndex = -1000;
                    document.body.appendChild(ifr);
                }
                ifr.style.display  = "block";
                var frameWindow = window.frames["pasteFrame"];
                if(frameWindow)
                {
                    frameWindow.document.open();
                    frameWindow.document.write(sHtml);
                    frameWindow.document.close();
					if(null != frameWindow.document && null != frameWindow.document.body)
					{
						Editor_Paste_Exec(api, frameWindow.document.body);
						bExist = true;
					}
                }
                ifr.style.display  = ELEMENT_DISPAY_STYLE;
            }

            if(bExist)
            {
                oWordControl.bIsEventPaste = true;
                if (e.preventDefault)
                {
                    e.stopPropagation();
                    e.preventDefault();
                }
                return false;
            }
        }
		var fTest = function(types, sPattern)
		{
            if (!types)
                return false;
			for(var i = 0, length = types.length; i < length; ++i)
			{
				if(sPattern == types[i])
					return true;
			}
			return false;
		}
        if (fTest(e.clipboardData.types, "text/html"))
        {
            var sHtml = e.clipboardData.getData('text/html');
            //������ ��� ��������� ����� ������ �� Word � chrome ��������� ���������� ������� ����� </html>, �������� ��.
            var nIndex = sHtml.lastIndexOf("</html>");
            if(-1 != nIndex)
                sHtml = sHtml.substring(0, nIndex + "</html>".length);
        }
        else if (is_chrome && fTest(e.clipboardData.types, "text/plain"))
        {
            bExist = true;
            var sText = e.clipboardData.getData('text/plain');
            sHtml = "<html><body>";
            var sCurPar = "";
            var nParCount = 0;
            for ( var i = 0, length = sText.length; i < length; i++ )
            {
                var Char = sText.charAt(i);
                var Code = sText.charCodeAt(i);
                var Item = null;

                if ( '\n' === Char )
                {
                    if("" == sCurPar)
                        sHtml += "<p><span>&nbsp;</span></p>";
                    else
                    {
                        sHtml += "<p><span>" + sCurPar + "</span></p>";
                        sCurPar = "";
                    }
                    nParCount++;
                }
                else if ( 13 === Code )
                {
                    continue;
                }
                else
                {
                    if(32 == Code || 160 == Code) //160 - nbsp
                        sCurPar += " ";
                    else if ( 9 === Code )
                        sCurPar += "<span style='mso-tab-count:1'>    </span>";
                    else
                        sCurPar += CopyPasteCorrectString(Char);
                }
            }
            if("" != sCurPar)
            {
                if(0 == nParCount)
                    sHtml += "<span>" + sCurPar + "</span>";
                else
                    sHtml += "<p><span>" + sCurPar + "</span></p>";
                sCurPar = "";
            }
            sHtml += "</body></html>";
        }
        if(sHtml)
            fPasteHtml(sHtml);
        else
        {
            var items = e.clipboardData.items;
            if(null != items)
            {
                for (var i = 0; i < items.length; ++i) {
                    if (items[i].kind == 'file' &&
                        items[i].type.indexOf('image/') !== -1) {

                        var blob = items[i].getAsFile();
                        var reader = new FileReader();

                        reader.onload = function(evt) {
                            fPasteHtml("<html><body><img src=\"" + evt.target.result + "\"/></body></html>");
                        };
                        reader.readAsDataURL(blob);
                    }
                }
            }
        }
    }
}
function Editor_Paste_Exec(api, pastebin)
{
    var oPasteProcessor = new PasteProcessor(api, true, true, false);
    oPasteProcessor.Start(pastebin);
};
function trimString( str ){
    return str.replace(/^\s+|\s+$/g, '') ;
};
function PasteProcessor(api, bUploadImage, bUploadFonts, bNested)
{
    this.oRootNode = null;
    this.api = api;
    this.bIsDoublePx  = api.WordControl.bIsDoublePx;
    this.oDocument = api.WordControl.m_oLogicDocument;
    this.oLogicDocument = this.oDocument;
    this.oRecalcDocument = this.oDocument;
    this.map_font_index = api.FontLoader.map_font_index;
    this.bUploadImage = bUploadImage;
    this.bUploadFonts = bUploadFonts;
    this.bNested = bNested;//��� ���������� � ��������
    this.oFonts = new Object();
    this.oImages = new Object();

    //��� ������� ������ � ������, ��� ����������� �� word � chrome ���������� ������ ������� ��� <p>
    this.bIgnoreNoBlockText = false;

    this.oCurPar = null;
	this.oCurParContentPos = 0;
    this.oCur_rPr = new CTextPr();

    //Br ������� ������ ��� ���� ������ ����� �� ���� �������� br, ���� �� � ������������.
    this.nBrCount = 0;
    //bInBlock ��������� ������� �� �������(��������������� ������ �������� �������� �� child)
    //���� ����� ��������� ������� true != this.bInBlock ������ ��������� ������� �� �������� � �� ���� ��������� ����� ��������
    this.bInBlock = null;

    //������ �������� � ������� ��������� �������� ��� ������
    this.dMaxWidth = Page_Width - X_Left_Margin - X_Right_Margin;
    //���������� ������(�������� ��� ������� ������� �������, ������ ��� ������� ����������� ������ � ��������� � ������� ����� �������� ���� �����������)
    this.dScaleKoef = 1;
    this.bUseScaleKoef = false;

    this.MsoStyles = {"mso-style-type": 1, "mso-pagination": 1, "mso-line-height-rule": 1, "mso-style-textfill-fill-color": 1, "mso-tab-count": 1,
        "tab-stops": 1, "list-style-type": 1, "mso-special-character": 1, "mso-padding-alt": 1, "mso-border-insidev": 1,
        "mso-border-insideh": 1, "mso-row-margin-left": 1, "mso-row-margin-right": 1, "mso-cellspacing": 1, "mso-border-alt": 1,
        "mso-border-left-alt": 1, "mso-border-top-alt": 1, "mso-border-right-alt": 1, "mso-border-bottom-alt": 1, "mso-border-between": 1};
    this.oBorderCache = new Object();
}
PasteProcessor.prototype =
{
    _GetTargetDocument : function(oDocument)
    {
        if(g_bIsDocumentCopyPaste)
        {
            if(docpostype_HdrFtr === oDocument.CurPos.Type)
            {
                if(null != oDocument.HdrFtr && null != oDocument.HdrFtr.CurHdrFtr && null != oDocument.HdrFtr.CurHdrFtr.Content)
                {
                    oDocument = oDocument.HdrFtr.CurHdrFtr.Content;
                    this.oRecalcDocument = oDocument;
                }
            }
            
            if ( docpostype_FlowObjects == oDocument.CurPos.Type )
            {
                var oData = oDocument.Selection.Data.FlowObject;
                switch ( oData.Get_Type() )
                {
                    case flowobject_Table:
                    {
                        if(null != oData.Table && null != oData.Table.CurCell && null != oData.Table.CurCell.Content)
                        {
                            oDocument = this._GetTargetDocument(oData.Table.CurCell.Content);
                            this.oRecalcDocument = oDocument;
                            this.dMaxWidth = this._CalcMaxWidthByCell(oData.Table.CurCell);
                        }
                        break;
                    }
                }
            }

            if(oDocument.CurPos.Type === docpostype_DrawingObjects)
            {
                switch(oDocument.DrawingObjects.curState.id)
                {
                    case STATES_ID_TEXT_ADD:
                    {
                        var text_object = oDocument.DrawingObjects.curState.textObject;
                        if(text_object != null && text_object.GraphicObj != null && text_object.GraphicObj.textBoxContent != null)
                            oDocument = text_object.GraphicObj.textBoxContent;
                        break;
                    }
                    case STATES_ID_TEXT_ADD_IN_GROUP:
                    {
                        text_object = oDocument.DrawingObjects.curState.textObject;
                        if(text_object != null &&  text_object.textBoxContent != null)
                            oDocument = text_object.textBoxContent;
                        break;
                    }
                }
            }

            //��� ��������� ������
            var Item = oDocument.Content[oDocument.CurPos.ContentPos];
            if( type_Table == Item.GetType() && null != Item.CurCell)
            {
                this.dMaxWidth = this._CalcMaxWidthByCell(Item.CurCell);
                oDocument = this._GetTargetDocument(Item.CurCell.Content);
            }
        }
        else
        {

        }
        return oDocument;
    },
    _CalcMaxWidthByCell : function(cell)
    {
        var row = cell.Row;
        var table = row.Table;
        var grid = table.TableGrid;
        var nGridBefore = 0;
        if(null != row.Pr && null != row.Pr.GridBefore)
            nGridBefore = row.Pr.GridBefore;
        var nCellIndex = cell.Index;
        var nCellGrid = 1;
        if(null != cell.Pr && null != cell.Pr.GridSpan)
            nCellGrid = cell.Pr.GridSpan;
        var nMarginLeft = 0;
        if(null != cell.Pr && null != cell.Pr.TableCellMar && null != cell.Pr.TableCellMar.Left && tblwidth_Mm == cell.Pr.TableCellMar.Left.Type && null != cell.Pr.TableCellMar.Left.W)
            nMarginLeft = cell.Pr.TableCellMar.Left.W;
        else if(null != table.Pr && null != table.Pr.TableCellMar && null != table.Pr.TableCellMar.Left && tblwidth_Mm == table.Pr.TableCellMar.Left.Type && null != table.Pr.TableCellMar.Left.W)
            nMarginLeft = table.Pr.TableCellMar.Left.W;
        var nMarginRight = 0;
        if(null != cell.Pr && null != cell.Pr.TableCellMar && null != cell.Pr.TableCellMar.Right && tblwidth_Mm == cell.Pr.TableCellMar.Right.Type && null != cell.Pr.TableCellMar.Right.W)
            nMarginRight = cell.Pr.TableCellMar.Right.W;
        else if(null != table.Pr && null != table.Pr.TableCellMar && null != table.Pr.TableCellMar.Right && tblwidth_Mm == table.Pr.TableCellMar.Right.Type && null != table.Pr.TableCellMar.Right.W)
            nMarginRight = table.Pr.TableCellMar.Right.W;
        var nPrevSumGrid = nGridBefore;
        for(var i = 0; i < nCellIndex; ++i)
        {
            var oTmpCell = row.Content[i];
            var nGridSpan = 1;
            if(null != cell.Pr && null != cell.Pr.GridSpan)
                nGridSpan = cell.Pr.GridSpan;
            nPrevSumGrid += nGridSpan;
        }
        var dCellWidth = 0;
        for(var i = nPrevSumGrid, length = grid.length; i < nPrevSumGrid + nCellGrid && i < length; ++i)
            dCellWidth += grid[i];

        if(dCellWidth - nMarginLeft - nMarginRight <= 0)
            dCellWidth = 4;
        else
            dCellWidth -= nMarginLeft + nMarginRight;
        return dCellWidth;
    },
    InsertInDocument : function()
    {
        var oDocument = this.oDocument;

        if(false == this.bNested)
        {
            //������� ���������
            this.oRecalcDocument.Remove(1, true, true);
        }

        var nInsertLength = this.aContent.length;
        if(nInsertLength > 0)
        {
            this.InsertInPlace(oDocument, this.aContent);
            if(false == g_bIsDocumentCopyPaste)
            {
                oDocument.Recalculate();
                if(oDocument.Parent != null && oDocument.Parent.txBody != null)
                {
                    oDocument.Parent.txBody.recalculate();
                }
            }
        }

        if(false == this.bNested && nInsertLength > 0)
        {
            this.oRecalcDocument.Recalculate();
            this.oLogicDocument.Document_UpdateInterfaceState();
        }

        window.GlobalPasteFlagCounter = 0;
    },
    InsertInPlace : function(oDoc, aNewContent)
    {
        if(!g_bIsDocumentCopyPaste)
            return;
        var nNewContentLength = aNewContent.length;
        //����� ���� �� Document.Add_NewParagraph
        var Item = oDoc.Content[oDoc.CurPos.ContentPos];

        if( type_Paragraph == Item.GetType() )
        {
            if(true != this.bInBlock && 1 == nNewContentLength && type_Paragraph == aNewContent[0].GetType())
            {
                //������� ������ � ��������
                var oInsertPar = aNewContent[0];
                var nContentLength = oInsertPar.Content.length;
                if(nContentLength > 2)
                {
                    var TextPr = Item.Internal_CalculateTextPr(Item.CurPos.ContentPos);
					var nContentPos = Item.CurPos.ContentPos;
                    for(var i = 0; i < nContentLength - 2; ++i)// -2 �� ����������� ����� ���������
                    {
                        var oCurInsItem = oInsertPar.Content[i];
                        if(para_Numbering != oCurInsItem.Type)
						{
                            Item.Internal_Content_Add(nContentPos, oCurInsItem);
							nContentPos++;
						}
                    }
                    Item.Internal_Content_Add(nContentPos, new ParaTextPr(TextPr));
                }
				Item.RecalcInfo.Set_Type_0(pararecalc_0_All);
				Item.RecalcInfo.Set_Type_0_Spell(pararecalc_0_Spell_All);
                this.oRecalcDocument.ContentLastChangePos = this.oRecalcDocument.CurPos.ContentPos;
            }
            else
            {
                var LastPos = this.oRecalcDocument.CurPos.ContentPos;
                var LastPosCurDoc = oDoc.CurPos.ContentPos;
                //����� ��������� ��������
                var oSourceFirstPar = Item;
                var oSourceLastPar = new Paragraph(oDoc.DrawingDocument, oDoc, 0, 50, 50, X_Right_Field, Y_Bottom_Field );
				if(true !== oSourceFirstPar.Cursor_IsEnd())
					oSourceFirstPar.Split(oSourceLastPar);
                var oInsFirstPar = aNewContent[0];
                var oInsLastPar = null;
                if(nNewContentLength > 1)
                    oInsLastPar = aNewContent[nNewContentLength - 1];

                var nStartIndex = 0;
                var nEndIndex = nNewContentLength - 1;

                if(type_Paragraph == oInsFirstPar.GetType())
                {
                    //�������� �������� ������� ������������ ��������� � ������ �������� ��������
                    //CopyPr_Open - ������� � �������, �.�. ���� �������� ��� � ���������
                    oInsFirstPar.CopyPr_Open( oSourceFirstPar );
                    //�������� ���������� ������������ ���������
                    oSourceFirstPar.Concat(oInsFirstPar);
                    //�������� ��������� ������ ����� ������ �� ��������� ���� ��������
                    nStartIndex++;
                }
                else if(type_Table == oInsFirstPar.GetType())
                {
                    //���� ��������� ������� � ������ ��������, �� �� ��������� ���
                    if(oSourceFirstPar.IsEmpty())
                    {
                        oSourceFirstPar = null;
                    }
                }
                //���� �� ���������� ������ ����� ���������, �� ��������� ���������� ���������� ��������� � ������ ������ �������� ��������� ���������
                if(null != oInsLastPar && type_Paragraph == oInsLastPar.GetType() && true != this.bInBlock)
                {
                    var nNewContentPos = oInsLastPar.Content.length - 2;
                    //�������� �������� ���������� ��������� ��������� � ���������  ����������� ��������
                    //CopyPr - �� ������� � �������, �.�. � ������� ��������� ������� ����� ��������� � ��������
                    if(null != oInsLastPar)
                        oSourceLastPar.CopyPr( oInsLastPar );
                    oInsLastPar.Concat(oSourceLastPar);
                    oInsLastPar.CurPos.ContentPos = nNewContentPos;
                    oSourceLastPar = oInsLastPar;
                    nEndIndex--;
                }
                //���������
                for(var i = nStartIndex; i <= nEndIndex; ++i )
                {
                    var oElemToAdd = aNewContent[i];
                    LastPosCurDoc++;
                    oDoc.Internal_Content_Add(LastPosCurDoc, oElemToAdd);
                }
                if(null != oSourceLastPar)
                {
                    //��������� ��������� ��������
                    LastPosCurDoc++;
                    oDoc.Internal_Content_Add(LastPosCurDoc, oSourceLastPar);
                }
                if(null == oSourceFirstPar)
                {
                    //������� ������ ��������, ������ ��� ����� ������ ���� � ��������� �� ����� �� ������ ���������
                    oDoc.Internal_Content_Remove(LastPosCurDoc, 1);
                    LastPosCurDoc--;
                }
                this.oRecalcDocument.ContentLastChangePos = LastPos;
				Item.RecalcInfo.Set_Type_0(pararecalc_0_All);
				Item.RecalcInfo.Set_Type_0_Spell(pararecalc_0_Spell_All);
                oDoc.CurPos.ContentPos = LastPosCurDoc;
            }
        }

    },


    insertInPlace2: function(oDoc, aNewContent)
    {
        var nNewContentLength = aNewContent.length;
        //����� ���� �� Document.Add_NewParagraph
        var Item = oDoc.Content[oDoc.CurPos.ContentPos];

        if( type_Paragraph == Item.GetType() )
        {
            if(true != this.bInBlock && 1 == nNewContentLength && type_Paragraph == aNewContent[0].GetType())
            {
                //������� ������ � ��������
                var oInsertPar = aNewContent[0];
                var nContentLength = oInsertPar.Content.length;
                if(nContentLength > 2)
                {
                    var TextPr = Item.Internal_CalculateTextPr(Item.CurPos.ContentPos);
                    var nContentPos = Item.CurPos.ContentPos;
                    for(var i = 0; i < nContentLength - 2; ++i)// -2 �� ����������� ����� ���������
                    {
                        var oCurInsItem = oInsertPar.Content[i];
                        if(para_Numbering != oCurInsItem.Type)
                        {
                            Item.Internal_Content_Add(nContentPos, oCurInsItem);
                            nContentPos++;
                        }
                    }
                    Item.Internal_Content_Add(nContentPos, new ParaTextPr(TextPr));
                }
                Item.RecalcInfo.Set_Type_0(pararecalc_0_All);
                Item.RecalcInfo.Set_Type_0_Spell(pararecalc_0_Spell_All);
                this.oRecalcDocument.ContentLastChangePos = this.oRecalcDocument.CurPos.ContentPos;
            }
            else
            {
                var LastPos = this.oRecalcDocument.CurPos.ContentPos;
                var LastPosCurDoc = oDoc.CurPos.ContentPos;
                //����� ��������� ��������
                var oSourceFirstPar = Item;
                var oSourceLastPar = new Paragraph(oDoc.DrawingDocument, oDoc, 0, 50, 50, X_Right_Field, Y_Bottom_Field );
                if(true !== oSourceFirstPar.Cursor_IsEnd())
                    oSourceFirstPar.Split(oSourceLastPar);
                var oInsFirstPar = aNewContent[0];
                var oInsLastPar = null;
                if(nNewContentLength > 1)
                    oInsLastPar = aNewContent[nNewContentLength - 1];

                var nStartIndex = 0;
                var nEndIndex = nNewContentLength - 1;

                if(type_Paragraph == oInsFirstPar.GetType())
                {
                    //�������� �������� ������� ������������ ��������� � ������ �������� ��������
                    //CopyPr_Open - ������� � �������, �.�. ���� �������� ��� � ���������
                    oInsFirstPar.CopyPr_Open( oSourceFirstPar );
                    //�������� ���������� ������������ ���������
                    oSourceFirstPar.Concat(oInsFirstPar);
                    if(isRealObject(oInsFirstPar.bullet))
                    {
                        oSourceFirstPar.setPresentationBullet(oInsFirstPar.bullet.createDuplicate());
                    }
                    //�������� ��������� ������ ����� ������ �� ��������� ���� ��������
                    nStartIndex++;
                }
                else if(type_Table == oInsFirstPar.GetType())
                {
                    //���� ��������� ������� � ������ ��������, �� �� ��������� ���
                    if(oSourceFirstPar.IsEmpty())
                    {
                        oSourceFirstPar = null;
                    }
                }
                //���� �� ���������� ������ ����� ���������, �� ��������� ���������� ���������� ��������� � ������ ������ �������� ��������� ���������
                if(null != oInsLastPar && type_Paragraph == oInsLastPar.GetType() && true != this.bInBlock)
                {
                    var nNewContentPos = oInsLastPar.Content.length - 2;
                    //�������� �������� ���������� ��������� ��������� � ���������  ����������� ��������
                    //CopyPr - �� ������� � �������, �.�. � ������� ��������� ������� ����� ��������� � ��������
                    if(null != oInsLastPar)
                        oSourceLastPar.CopyPr( oInsLastPar );
                    oInsLastPar.Concat(oSourceLastPar);
                    oInsLastPar.CurPos.ContentPos = nNewContentPos;
                    oSourceLastPar = oInsLastPar;
                    nEndIndex--;
                }
                //���������
                for(var i = nStartIndex; i <= nEndIndex; ++i )
                {
                    var oElemToAdd = aNewContent[i];
                    LastPosCurDoc++;
                    oDoc.Internal_Content_Add(LastPosCurDoc, oElemToAdd);
                }
                if(null != oSourceLastPar)
                {
                    //��������� ��������� ��������
                    LastPosCurDoc++;
                    oDoc.Internal_Content_Add(LastPosCurDoc, oSourceLastPar);
                }
                if(null == oSourceFirstPar)
                {
                    //������� ������ ��������, ������ ��� ����� ������ ���� � ��������� �� ����� �� ������ ���������
                    oDoc.Internal_Content_Remove(LastPosCurDoc, 1);
                    LastPosCurDoc--;
                }
                this.oRecalcDocument.ContentLastChangePos = LastPos;
                Item.RecalcInfo.Set_Type_0(pararecalc_0_All);
                Item.RecalcInfo.Set_Type_0_Spell(pararecalc_0_Spell_All);
                oDoc.CurPos.ContentPos = LastPosCurDoc;
            }
        }
    },
    ReadFromBinary : function(sBase64)
	{
		//надо сбросить то, что остался после открытия документа
		window.global_pptx_content_loader.Clear();
		window.global_pptx_content_loader.Start_UseFullUrl();		
		var openParams = {checkFileSize: false, charCount: 0, parCount: 0};
		var oBinaryFileReader = new BinaryFileReader(this.oDocument, openParams);
		oBinaryFileReader.stream = oBinaryFileReader.getbase64DecodedData(sBase64);
		oBinaryFileReader.ReadMainTable();
		var oReadResult = oBinaryFileReader.oReadResult;
		//обрабатываем списки
		for(var i in oReadResult.numToNumClass)
		{
			var oNumClass = oReadResult.numToNumClass[i];
			var documentANum = this.oDocument.Numbering.AbstractNum;
			//проверка на уже существующий такой же AbstractNum
			var isAlreadyContains = false;
			for(var n in documentANum)
			{
				var isEqual = documentANum[n].isEqual(oNumClass);
				if(isEqual == true)
				{
					isAlreadyContains = true;
					break;
				}
			}
			if(!isAlreadyContains)
			{
				this.oDocument.Numbering.Add_AbstractNum(oNumClass);
			}
			else
				oReadResult.numToNumClass[i] = documentANum[n];
				
		}
		for(var i = 0, length = oReadResult.paraNumPrs.length; i < length; ++i)
		{
			var numPr = oReadResult.paraNumPrs[i];
			var oNumClass = oReadResult.numToNumClass[numPr.NumId];
			if(null != oNumClass)
				numPr.NumId = oNumClass.Get_Id();
			else
				numPr.NumId = 0;
		}
		//обрабатываем стили
		var isAlreadyContainsStyle;
		var api = this.api;
		var oStyleTypes = {par: 1, table: 2, lvl: 3};
		var addNewStyles = false;
		var fParseStyle = function(aStyles, oDocumentStyles, oReadResult, nStyleType)
		{
			if(aStyles == undefined)
				return;
			for(var i = 0, length = aStyles.length; i < length; ++i)
			{
				var elem = aStyles[i];
				var stylePaste = oReadResult.styles[elem.style];
				var isEqualName = null;
				if(null != stylePaste && null != stylePaste.style)
				{
					for(var j in oDocumentStyles.Style)
					{
						var styleDoc = oDocumentStyles.Style[j];
						isAlreadyContainsStyle = styleDoc.isEqual(stylePaste.style);
						if(styleDoc.Name == stylePaste.style.Name)
							isEqualName = j;
						if(isAlreadyContainsStyle)
						{
							if(oStyleTypes.par == nStyleType)
								elem.pPr.PStyle = j;
							else if(oStyleTypes.table == nStyleType)
								elem.pPr.Set_TableStyle2(j);
							else
								elem.pPr.PStyle = j;
							break;
						}
					}
					if(!isAlreadyContainsStyle && isEqualName != null)//если нашли имя такого же стиля
					{
						if(nStyleType == oStyleTypes.par || nStyleType == oStyleTypes.lvl)
							elem.pPr.PStyle = isEqualName;
						else if (nStyleType == oStyleTypes.table)
							elem.pPr.Set_TableStyle2(isEqualName);
					}
					else if(!isAlreadyContainsStyle && isEqualName == null)//нужно добавить новый стиль
					{
						//todo править и BaseOn
						var nStyleId = oDocumentStyles.Add(stylePaste.style);
						if(nStyleType == oStyleTypes.par || nStyleType == oStyleTypes.lvl)
							elem.pPr.PStyle = nStyleId;
						else if (nStyleType == oStyleTypes.table)
							elem.pPr.Set_TableStyle2(nStyleId);
						addNewStyles = true;
					}
				}
			}
		}
		
		fParseStyle(oBinaryFileReader.oReadResult.paraStyles, this.oDocument.Styles, oBinaryFileReader.oReadResult, oStyleTypes.par);
		fParseStyle(oBinaryFileReader.oReadResult.tableStyles, this.oDocument.Styles, oBinaryFileReader.oReadResult, oStyleTypes.table);
		fParseStyle(oBinaryFileReader.oReadResult.lvlStyles, this.oDocument.Styles, oBinaryFileReader.oReadResult, oStyleTypes.lvl);
		var aContent = oBinaryFileReader.oReadResult.DocumentContent;
		for(var i = 0, length = oBinaryFileReader.oReadResult.aPostOpenStyleNumCallbacks.length; i < length; ++i)
			oBinaryFileReader.oReadResult.aPostOpenStyleNumCallbacks[i].call();
		if(oReadResult.bLastRun)
			this.bInBlock = false;
		else
			this.bInBlock = true;
		//создаем список используемых шрифтов
		var AllFonts = new Object();
		this.oDocument.Numbering.Document_Get_AllFontNames(AllFonts);
		this.oDocument.Styles.Document_Get_AllFontNames(AllFonts);
        for ( var Index = 0, Count = aContent.length; Index < Count; Index++ )
            aContent[Index].Document_Get_AllFontNames( AllFonts );
		var aPrepeareFonts = [];
		for(var i in AllFonts)
			aPrepeareFonts.push(new CFont(i, 0, "", 0));
		//создаем список используемых картинок
		var oPastedImagesUnique = {};		var aPastedImages = window.global_pptx_content_loader.End_UseFullUrl();		for(var i = 0, length = aPastedImages.length; i < length; ++i)		{			var elem = aPastedImages[i];			oPastedImagesUnique[elem.Url] = 1;		}		var aPrepeareImages = [];
		for(var i in oPastedImagesUnique)
			aPrepeareImages.push(i);
		return {content: aContent, fonts: aPrepeareFonts, images: aPrepeareImages, bAddNewStyles: addNewStyles, aPastedImages: aPastedImages};
	},
	Start : function(node)
    {
		if(g_bIsDocumentCopyPaste)
        {

            var oThis = this;
            this.oDocument = this._GetTargetDocument(this.oDocument);
            // var oPasteContent = this.ReadFromBinary(node.innerText);
            // this.aContent = oPasteContent.content;
            // oThis.api.pre_Paste(oPasteContent.fonts, oPasteContent.images, function(){
            // if(false == oThis.bNested)
            // {
            // oThis.InsertInDocument();
            // node.blur();
            // node.style.display  = ELEMENT_DISPAY_STYLE;
            // }
            // });
            // return;

            if(copyPasteUseBinery)
            {
                var base64 = null;
                var classNode;
                if(node.children[0] && node.children[0].getAttribute("class") != null)
                    classNode = node.children[0].getAttribute("class");
                else if(node.children[0] && node.children[0].children[0] && node.children[0].children[0].getAttribute("class") != null)
                    classNode = node.children[0].children[0].getAttribute("class");
                if( classNode != null ){
                    cL = classNode.split(" ");
                    for (var i = 0; i < cL.length; i++){
                        if(cL[i].indexOf("docData;") > -1)
                        {
                            base64 = cL[i].split('docData;')[1];
                        }
                    }
                }
                var aContent;
                if(base64 != null)
                    aContent = this.ReadFromBinary(base64);
                if(aContent)
                {
                    var fPrepasteCallback = function(){
                        if(false == oThis.bNested)
                        {
                            editor.WordControl.m_oLogicDocument.DrawingObjects.calculateAfterOpen(true);
                            oThis.InsertInDocument();
                            node.blur();
                            node.style.display  = ELEMENT_DISPAY_STYLE;
                            if(aContent.bAddNewStyles)
                                oThis.api.GenerateStyles();
                        }
                    }
                    this.aContent = aContent.content;
                    var oImagesToDownload = {};
                    if(aContent.aPastedImages.length > 0)
                    {
                        for(var i = 0, length = aContent.aPastedImages.length; i < length; ++i)
                        {
                            var imageElem = aContent.aPastedImages[i];
                            var src = imageElem.Url;
                            if(false == (0 == src.indexOf("data:") || 0 == src.indexOf(documentOrigin + this.api.DocumentUrl) || 0 == src.indexOf(this.api.DocumentUrl)))
                                oImagesToDownload[src] = 1;
                        }
                    }
                    var aImagesToDownload = [];
                    for(var i in oImagesToDownload)
                        aImagesToDownload.push(i);
                    if(aImagesToDownload.length > 0)
                    {
                        var rData = {"id":documentId, "c":"imgurls", "data": JSON.stringify(aImagesToDownload)};
                        sendCommand( this.api, function(incomeObject){
                            if(incomeObject && "imgurls" == incomeObject.type)
                            {
                                var oFromTo = JSON.parse(incomeObject.data);
                                for(var i = 0, length = aContent.images.length; i < length; ++i)
                                {
                                    var sFrom = aContent.images[i];
                                    var sTo = oFromTo[sFrom];
                                    if(sTo)
                                        aContent.images[i] = sTo;
                                }
                                for(var i = 0, length = aContent.aPastedImages.length; i < length; ++i)
                                {
                                    var imageElem = aContent.aPastedImages[i];
                                    if(null != imageElem)
                                    {
                                        var sNewSrc = oFromTo[imageElem.Url];
                                        if(null != sNewSrc)
                                            imageElem.SetUrl(sNewSrc);
                                    }
                                }
                            }
                            oThis.api.pre_Paste(aContent.fonts, aContent.images, fPrepasteCallback);
                        }, JSON.stringify(rData) );
                    }
                    else
                        oThis.api.pre_Paste(aContent.fonts, aContent.images, fPrepasteCallback);
                    return;
                }
            }

            var presentation = editor.WordControl.m_oLogicDocument;
            this.oRootNode = node;
            this._Prepeare(node,
                function(){
                    oThis.aContent = new Array();
                    //�� ����� ���������� �������� ��� ������� ��������� �������
                    oThis._Execute(node, {}, true, true, false);

                    oThis._AddNextPrevToContent(oThis.oDocument);
                    if(false == oThis.bNested)
                    {
                        oThis.InsertInDocument();
                    }

                    node.blur();
                    node.style.display  = ELEMENT_DISPAY_STYLE;

                });
        }
        else
        {
            var oThis = this;
            var presentation = editor.WordControl.m_oLogicDocument;
            if(copyPasteUseBinery)
            {
                var base64 = null;
                var classNode;
                if(node.children[0] && node.children[0].getAttribute("class") != null)
                    classNode = node.children[0].getAttribute("class");
                else if(node.children[0] && node.children[0].children[0] && node.children[0].children[0].getAttribute("class") != null)
                    classNode = node.children[0].children[0].getAttribute("class");
                if( classNode != null ){
                    cL = classNode.split(" ");
                    for (var i = 0; i < cL.length; i++){
                        if(cL[i].indexOf("docData;") > -1)
                        {
                            base64 = cL[i].split('docData;')[1];
                        }
                    }
                }
                if(typeof base64 === "string")
                {
                    var _stream = CreateBinaryReader(base64, 0, base64.length);
                    var stream = new FileStream(_stream.data, _stream.size);
                    var first_string = stream.GetString2();
                    var p_url = stream.GetString2();
                    var p_width = stream.GetULong()/100000;
                    var p_height = stream.GetULong()/100000;
                    var kw = presentation.Width/p_width;
                    var kh = presentation.Height/p_height;
                    switch(first_string)
                    {
                        case "TeamLab1":
                        {
                            var shape = this.ReadPresentationText(stream);
                            var fonts = [];
                            shape.getAllFonts(fonts);
                            var presentation = editor.WordControl.m_oLogicDocument;
                            oThis = this;
                            var paste_callback = function()
                            {
                                if(false == oThis.bNested)
                                {
                                    var slide = presentation.Slides[presentation.CurPage];
                                    switch(slide.graphicObjects.State.id)
                                    {
                                        case STATES_ID_TEXT_ADD:
                                        case STATES_ID_TEXT_ADD_IN_GROUP:
                                        {
                                            if(presentation.Document_Is_SelectionLocked(changestype_Drawing_Props) === false)
                                            {
                                                History.Create_NewPoint();
                                                oThis.insertInPlace2(slide.graphicObjects.State.textObject.txBody.content, shape.txBody.content.Content);
                                                slide.graphicObjects.State.textObject.txBody.bRecalculateNumbering = true;
                                                var content = slide.graphicObjects.State.textObject.txBody.content.Content;
                                                for(var j = 0; j < content.length; ++j)
                                                {
                                                    content[j].RecalcInfo.Set_Type_0(pararecalc_0_All);
                                                    content[j].Set_Parent(slide.graphicObjects.State.textObject.txBody.content);
                                                }
                                                slide.graphicObjects.State.textObject.recalcInfo.recalculateContent = true;
                                                slide.graphicObjects.State.textObject.recalcInfo.recalculateTransformText = true;
                                                editor.WordControl.m_oLogicDocument.recalcMap[slide.graphicObjects.State.textObject.Id] = slide.graphicObjects.State.textObject;
                                            }
                                            break;
                                        }
                                        default :
                                        {
                                            if(presentation.Document_Is_SelectionLocked(changestype_Drawing_Props) === false)
                                            {
                                                slide.addToSpTreeToPos(slide.cSld.spTree.length, shape);
                                                var w =  shape.txBody.getRectWidth(presentation.Width*2/3);
                                                var h = shape.txBody.getRectHeight(2000, w);
                                                shape.setXfrm((presentation.Width - w)/2, (presentation.Height - h)/2, w, h, null, null, null);
                                                editor.WordControl.m_oLogicDocument.recalcMap[shape.Id] = shape;
                                            }
                                            break;
                                        }
                                    }
                                    presentation.Recalculate();
                                    node.blur();
                                    node.style.display  = ELEMENT_DISPAY_STYLE;
                                }
                            };

                            this.api.pre_Paste(fonts, [],paste_callback);
                            return;
                        }
                        case "TeamLab2":
                        {
                            var arr_shapes = this.ReadPresentationShapes(stream);
                            var presentation = editor.WordControl.m_oLogicDocument;
                            oThis = this;
                            var fonts = [];
                            for(var i = 0; i < arr_shapes.length; ++i)
                            {
                                if(arr_shapes[i].getAllFonts)
                                    arr_shapes[i].getAllFonts(fonts);
                            }
                            var paste_callback = function()
                            {
                                if(false == oThis.bNested)
                                {
                                    var slide = presentation.Slides[presentation.CurPage];
                                    if(presentation.Document_Is_SelectionLocked(changestype_Drawing_Props) === false)
                                    {
                                        History.Create_NewPoint();
                                        for(var i = 0; i < arr_shapes.length; ++i)
                                        {
                                            arr_shapes[i].changeSize(kw, kh);
                                            slide.addToSpTreeToPos(slide.cSld.spTree.length, arr_shapes[i]);
                                        }
                                    }
                                    presentation.Recalculate();
                                    node.blur();
                                    node.style.display  = ELEMENT_DISPAY_STYLE;
                                }
                            };

                            this.api.pre_Paste(fonts, [],paste_callback);
                            return;
                        }
                        case "TeamLab3":
                        {
                            var arr_layouts_id = [];
                            var arr_slides = [];
                            var loader = new BinaryPPTYLoader();
                            loader.stream = stream;
                            loader.presentation = editor.WordControl.m_oLogicDocument;
                            var slide_count = stream.GetULong();
                            for(var i = 0; i < slide_count; ++i)
                            {
                                arr_layouts_id[i] = stream.GetString2();
                                var table_style_ids_len = stream.GetULong();
                                var table_style_ids = [];
                                for(var j = 0; j < table_style_ids_len; ++j)
                                {
                                    if(stream.GetBool())
                                        table_style_ids.push(stream.GetULong());
                                    else
                                        table_style_ids.push(-1);
                                }
                                arr_slides[i] = loader.ReadSlide(0);
                                var sp_tree = arr_slides[i].cSld.spTree;
                                var t = 0;
                                for(var s = 0; s < sp_tree.length; ++s)
                                {
                                    if(sp_tree[s] instanceof CGraphicFrame)
                                    {
                                        sp_tree[s].graphicObject.setStyleIndex(table_style_ids[t]);
                                        ++t;
                                    }
                                }
                            }

                            var arr_layouts = [];
                            var master = presentation.Slides[presentation.CurPage].Layout.Master;
                            if(editor.DocumentUrl !== p_url)
                            {
                                var layouts_count = stream.GetULong();
                                for(var i = 0; i < layouts_count; ++i)
                                {
                                    arr_layouts[i] = loader.ReadSlideLayout()
                                }
                                var arr_indexes = [];
                                for(var i = 0; i < slide_count; ++i)
                                {
                                    arr_indexes.push(stream.GetULong());
                                }

                                for(var i = 0; i < layouts_count; ++i)
                                {
                                    arr_layouts[i].setMaster(master);
                                    arr_layouts[i].changeSize(kw, kh);
                                    arr_layouts[i].Width = presentation.Width;
                                    arr_layouts[i].Height = presentation.Height;
                                    master.addLayout(arr_layouts[i]);
                                }
                                for(var i =0; i < slide_count; ++i)
                                {
                                    arr_slides[i].changeSize(kw, kh);
                                    arr_slides[i].setLayout(arr_layouts[arr_indexes[i]]);
                                    arr_slides[i].Width = presentation.Width;
                                    arr_slides[i].Height = presentation.Height;
                                }
                            }
                            else
                            {
                                var arr_matched_layout = [];
                                var b_read_layouts = false;
                                for(var i = 0; i < arr_layouts_id.length; ++i)
                                {
                                    if(!isRealObject(g_oTableId.Get_ById(arr_layouts_id[i])))
                                    {
                                        b_read_layouts = true;
                                        break;
                                    }
                                }
                                if(b_read_layouts)
                                {
                                    var layouts_count = stream.GetULong();
                                    for(var i = 0; i < layouts_count; ++i)
                                    {
                                        arr_layouts[i] = loader.ReadSlideLayout()
                                    }
                                    var arr_indexes = [];
                                    for(var i = 0; i < slide_count; ++i)
                                    {
                                        arr_indexes.push(stream.GetULong());
                                    }

                                    var addedLayouts = [];
                                    for(var i = 0; i < slide_count; ++i)
                                    {
                                        if(isRealObject(g_oTableId.Get_ById(arr_layouts_id[i])))
                                        {
                                            arr_slides[i].changeSize(kw, kh);
                                            arr_slides[i].setLayout(g_oTableId.Get_ById(arr_layouts_id[i]));
                                        }
                                        else
                                        {
                                            arr_slides[i].changeSize(kw, kh);
                                            arr_slides[i].setLayout(arr_layouts[arr_indexes[i]]);
                                            for(var j = 0; j < addedLayouts.length; ++j)
                                            {
                                                if(addedLayouts[j] === arr_layouts[arr_indexes[i]])
                                                    break;
                                            }
                                            if(j === addedLayouts.length)
                                            {
                                                addedLayouts.push(arr_layouts[arr_indexes[i]]);
                                                arr_layouts[arr_indexes[i]].setMaster(master);
                                                arr_layouts[arr_indexes[i]].changeSize(kw, kh);
                                                arr_layouts[arr_indexes[i]].Width = presentation.Width;
                                                arr_layouts[arr_indexes[i]].Height = presentation.Height;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    for(var i =0; i < slide_count; ++i)
                                    {
                                        arr_slides[i].changeSize(kw, kh);
                                        arr_slides[i].setLayout(g_oTableId.Get_ById(arr_layouts_id[i]));
                                        arr_slides[i].Width = presentation.Width;
                                        arr_slides[i].Height = presentation.Height;
                                    }
                                }
                            }


                            var presentation = editor.WordControl.m_oLogicDocument;
                            oThis = this;
                            var fonts = [];
                            for(var i = 0; i < arr_slides.length; ++i)
                            {
                                if(arr_slides[i].getAllFonts)
                                    arr_slides[i].getAllFonts(fonts);
                            }
                            var paste_callback = function()
                            {
                                if(false == oThis.bNested)
                                {
                                    var slide = presentation.Slides[presentation.CurPage];
                                    History.Create_NewPoint();
                                    for(var i = 0; i < arr_slides.length; ++i)
                                    {
                                        presentation.insertSlide(presentation.CurPage + i, arr_slides[i]);
                                    }
                                    presentation.Recalculate();
                                    node.blur();
                                    node.style.display  = ELEMENT_DISPAY_STYLE;
                                }
                            };

                            this.api.pre_Paste(fonts, [],paste_callback);
                            return;
                        }
                        case "TeamLab4":
                        {
                            var arr_shapes = this.ReadPresentationShapes(stream);
                            var presentation = editor.WordControl.m_oLogicDocument;
                            oThis = this;
                            var fonts = [];
                            for(var i = 0; i < arr_shapes.length; ++i)
                            {
                                if(arr_shapes[i].getAllFonts)
                                    arr_shapes[i].getAllFonts(fonts);
                            }
                            var paste_callback = function()
                            {
                                if(false == oThis.bNested)
                                {
                                    var slide = presentation.Slides[presentation.CurPage];
                                    if(presentation.Document_Is_SelectionLocked(changestype_Drawing_Props) === false)
                                    {
                                        History.Create_NewPoint();
                                        for(var i = 0; i < arr_shapes.length; ++i)
                                        {
                                            arr_shapes[i].changeSize(kw, kh);
                                            slide.addToSpTreeToPos(slide.cSld.spTree.length, arr_shapes[i]);
                                        }
                                    }
                                    presentation.Recalculate();
                                    node.blur();
                                    node.style.display  = ELEMENT_DISPAY_STYLE;
                                }
                            };

                            this.api.pre_Paste(fonts, [],paste_callback);
                            return;
                        }
                    }
                }
            }

            this.oRootNode = node;
            this._Prepeare(node,
                function(){
                     oThis.aContent = new Array();
                     //�� ����� ���������� �������� ��� ������� ��������� �������
                     oThis._Execute(node, {}, true, true, false);


                     oThis._AddNextPrevToContent(oThis.oDocument);
                     if(false == oThis.bNested)
                     {
                         oThis.InsertInDocument();
                     }

                     node.blur();
                     node.style.display  = ELEMENT_DISPAY_STYLE;

                });

        }
    },


    ReadPresentationText: function(stream)
    {
        var loader = new BinaryPPTYLoader();
        loader.stream = stream;
        loader.presentation = editor.WordControl.m_oLogicDocument;
        var presentation = editor.WordControl.m_oLogicDocument;
        var shape = new CShape(presentation.Slides[presentation.CurPage]);
        shape.setTextBody(new CTextBody(shape));
        var count = stream.GetULong();
        shape.txBody.content.Internal_Content_RemoveAll();
        for(var i = 0; i < count; ++i)
        {
            loader.stream.Skip2(1); // must be 0
            var _paragraph = loader.ReadParagraph(shape.txBody.content);
            shape.txBody.content.Internal_Content_Add(shape.txBody.content.Content.length, _paragraph);
        }
        return shape;
    },

    ReadPresentationShapes: function(stream)
    {
        var loader = new BinaryPPTYLoader();
        loader.stream = stream;
        loader.presentation = editor.WordControl.m_oLogicDocument;
        var presentation = editor.WordControl.m_oLogicDocument;
        var count = stream.GetULong();
        var arr_shapes = [];
        for(var i = 0; i < count; ++i)
        {
            loader.TempMainObject = presentation.Slides[presentation.CurPage];
            var style_index = null;
            if(!loader.stream.GetBool())
            {
                if(loader.stream.GetBool())
                {
                    style_index = stream.GetULong();
                }
            }
            arr_shapes.push(loader.ReadGraphicObject());
            if(isRealNumber(style_index))
            {
                if(arr_shapes[arr_shapes.length - 1] instanceof  CGraphicFrame)
                {
                    if(loader.presentation.globalTableStyles[style_index])
                    {
                        arr_shapes[arr_shapes.length - 1].graphicObject.setStyleIndex(style_index);
                    }
                }
            }
        }
        return arr_shapes;
    },

    ReadPresentationSlides: function(stream)
    {
        var loader = new BinaryPPTYLoader();
        loader.stream = stream;
        loader.presentation = editor.WordControl.m_oLogicDocument;
        var presentation = editor.WordControl.m_oLogicDocument;
        var count = stream.GetULong();
        var arr_slides = [];
        for(var i = 0; i < count; ++i)
        {
            arr_slides.push(loader.ReadSlide(0));
        }
        return arr_slides;
    },


    ReadSlide: function(stream)
    {
        var loader = new BinaryPPTYLoader();
        loader.stream = stream;
        loader.presentation = editor.WordControl.m_oLogicDocument;
        var presentation = editor.WordControl.m_oLogicDocument;
        return loader.ReadSlide(0);
    },

    _Prepeare : function(node, fCallback)
    {
		var oThis = this;
        if(true == this.bUploadImage || true == this.bUploadFonts)
        {
            //����������� �� ��������� �������� ������ ������� � ��������.
            this._Prepeare_recursive(node, true);

            var aPrepeareFonts = new Array();
            for(font_family in this.oFonts)
            {
                //todo ��������� �����, ������ �� ��������
                var oFontItem = this.oFonts[font_family];
                //���� ����� ����� �������
                var index = this.map_font_index[oFontItem.Name];
                if(null != index)
                {
                    this.oFonts[font_family].Index = index;
                    aPrepeareFonts.push(new CFont(oFontItem.Name, 0, "", 0));
                }
                else
                {
                    this.oFonts[font_family] = {Name:"Arial", Index: -1};
                    aPrepeareFonts.push(new CFont("Arial", 0, "", 0));
                }
            }
			var aImagesToDownload = new Array();
			for(image in this.oImages)
            {
				var src = this.oImages[image];
				if(0 == src.indexOf("file:"))
					this.oImages[image] = "local";
				else if(false == (0 == src.indexOf("data:") || 0 == src.indexOf(documentOrigin + this.api.DocumentUrl) || 0 == src.indexOf(this.api.DocumentUrl)))
					aImagesToDownload.push(src);
			}
			var oPrepeareImages = new Object();
			if(aImagesToDownload.length > 0)
			{
				var rData = {"id":documentId, "c":"imgurls", "data": JSON.stringify(aImagesToDownload)};
				sendCommand( this.api, function(incomeObject){
						if(incomeObject && "imgurls" == incomeObject.type)
						{
							var oFromTo = JSON.parse(incomeObject.data);
							for(var i = 0, length = aImagesToDownload.length; i < length; ++i)
							{
								var sFrom = aImagesToDownload[i];
								var sTo = oFromTo[sFrom];
								if(sTo)
								{									oThis.oImages[sFrom] = sTo;									oPrepeareImages[i] = sTo;								}							}
						}
						oThis.api.pre_Paste(aPrepeareFonts, oPrepeareImages, fCallback);
					}, JSON.stringify(rData) );
			}
			else
				this.api.pre_Paste(aPrepeareFonts, oPrepeareImages, fCallback);
        }
        else
            fCallback();
    },
    _Prepeare_recursive : function(node, bIgnoreStyle)
    {
        //����������� �� ����� ������, �������� ��� ������ � ��������
        var nodeName = node.nodeName.toLowerCase();
        var nodeType = node.nodeType;
        if(!bIgnoreStyle)
        {
            if(Node.TEXT_NODE == nodeType)
            {
                var defaultView = node.ownerDocument.defaultView;
                var computedStyle = defaultView.getComputedStyle( node.parentNode, null );
                if ( computedStyle )
                {
                    var fontFamily = computedStyle.getPropertyValue( "font-family" );
                    var sNewFF;
                    var nIndex = fontFamily.indexOf(",");
                    if(-1 != nIndex)
                        sNewFF = fontFamily.substring(0, nIndex);
                    else
                        sNewFF = fontFamily;
                    //trim ' � "
                    var nLength = sNewFF.length
                    if(nLength >= 2)
                    {
                        var nStart = 0;
                        var nStop = nLength;
                        var cFirstChar = sNewFF[0];
                        var cLastChar = sNewFF[nLength - 1];
                        var bTrim = false;
                        if('\'' == cFirstChar || '\"' == cFirstChar)
                        {
                            bTrim = true;
                            nStart = 1;
                        }
                        if('\'' == cLastChar || '\"' == cLastChar)
                        {
                            bTrim = true;
                            nStop = nLength - 1;
                        }
                        if(bTrim)
                            sNewFF = sNewFF.substring(nStart, nStop);
                    }
                    this.oFonts[fontFamily] = {Name:sNewFF, Index: -1};
                }
            }
            else
            {
                var src = node.getAttribute("src");
                if(src)
                    this.oImages[src] = src;

                var binary_shape = node.getAttribute("alt");
                if(binary_shape)
                {
                    var b_history_is_on = History.Is_On();
                    if(b_history_is_on)
                        History.TurnOff();

                    var sub;
                    if(typeof binary_shape === "string")
                        sub = binary_shape.substr(0, 12);
                    if(typeof binary_shape === "string" &&( sub === "TeamLabShape" || sub === "TeamLabImage" || sub === "TeamLabChart" || sub === "TeamLabGroup"))
                    {
                        var reader = CreateBinaryReader(binary_shape, 12, binary_shape.length);
                        var first_string = null;
                        if(reader !== null && typeof  reader === "object")
                        {
                            first_string = sub;
                        }
                        var Drawing;
                        var src_string;
                        switch(first_string)
                        {
                            case "TeamLabImage":
                            case "TeamLabChart":
                            {
                                Drawing = CreateParaDrawingFromBinary(reader, true);
                                if(isRealObject(Drawing)
                                    && isRealObject(Drawing.GraphicObj)
                                    && isRealObject(Drawing.GraphicObj.blipFill)
                                    && typeof Drawing.GraphicObj.blipFill.RasterImageId === "string")
                                {
                                    src_string = Drawing.GraphicObj.blipFill.RasterImageId;
                                    if(typeof src_string === "string")
										this.oImages[src_string] = src_string;
                                }
                                break;
                            }
                            case "TeamLabShape":
                            case "TeamLabGroup":
                            {
                                Drawing = CreateParaDrawingFromBinary(reader, true);
                                if(isRealObject(Drawing) && isRealObject(Drawing.GraphicObj))
                                {
                                    if(typeof Drawing.GraphicObj.isShape === "function" && Drawing.GraphicObj.isShape() === true)
                                    {
                                        if(isRealObject(Drawing.GraphicObj.spPr)
                                            && isRealObject(Drawing.GraphicObj.spPr.Fill)
                                            && isRealObject(Drawing.GraphicObj.spPr.Fill.fill)
                                            && typeof Drawing.GraphicObj.spPr.Fill.fill.RasterImageId === "string")
                                        {
                                            src_string = Drawing.GraphicObj.spPr.Fill.fill.RasterImageId;
                                            if(typeof src_string === "string")
												this.oImages[src_string] = src_string;
                                        }
                                    }
                                    if(typeof Drawing.GraphicObj.isGroup === "function" && Drawing.GraphicObj.isGroup() === true)
                                    {
                                        var sp_tree = Drawing.GraphicObj.getSpTree2();
                                        if(Array.isArray(sp_tree))
                                        {
                                            for(var index = 0; index < sp_tree.length; ++index)
                                            {
                                                var sp = sp_tree[index];
                                                if(isRealObject(sp))
                                                {
                                                    if(typeof sp.isImage === "function" && sp.isImage())
                                                    {
                                                        if(isRealObject(sp.blipFill) && typeof sp.blipFill.RasterImageId === "string")
                                                        {
                                                            src_string = sp.blipFill.RasterImageId;

                                                            if(typeof src_string === "string")
																this.oImages[src_string] = src_string;
                                                        }
                                                    }
                                                    if(typeof sp.isShape === "function" && sp.isShape())
                                                    {
                                                        if(isRealObject(sp.spPr)
                                                            && isRealObject(sp.spPr.Fill)
                                                            && isRealObject(sp.spPr.Fill.fill)
                                                            && typeof sp.spPr.Fill.fill.RasterImageId === "string")
                                                        {
                                                            src_string = sp.spPr.Fill.fill.RasterImageId;
                                                            if(typeof src_string === "string")
																this.oImages[src_string] = src_string;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }

                    if(b_history_is_on)
                        History.TurnOn();
                }
            }
        }
        for(var i = 0, length = node.childNodes.length; i < length; i++)
        {
            var child = node.childNodes[i];
            var child_nodeType = child.nodeType;
            if(!(Node.ELEMENT_NODE == child_nodeType || Node.TEXT_NODE == child_nodeType))
                continue;
            //�������� ������� ��������� ������ �� \t,\n,\r
            if( Node.TEXT_NODE == child.nodeType)
            {
                var value = child.nodeValue;
                if(!value)
                    continue;
                value = value.replace(/(\r|\t|\n)/g, '');
                if("" == value)
                    continue;
            }
            this._Prepeare_recursive(child, false);
        }
    },
    _IsBlockElem : function(name)
    {
        if( "p" == name || "div" == name || "ul" == name || "ol" == name || "li" == name || "table" == name || "tbody" == name || "tr" == name || "td" == name || "th" == name ||
            "h1" == name || "h2" == name || "h3" == name || "h4" == name || "h5" == name || "h6" == name || "center" == name)
            return true;
        return false;
    },
    _ValueToMm : function(value)
    {
        var obj = this._ValueToMmType(value);
        if(obj && "%" != obj.type && "none" != obj.type)
            return obj.val;
        return null;
    },
    _ValueToMmType : function(value)
    {
        var oVal = parseFloat(value);
        var oType;
        if(NaN != oVal)
        {
            if(-1 != value.indexOf("%"))
            {
                oType = "%";
                oVal /= 100;
            }
            else if(-1 != value.indexOf("px"))
            {
                oType = "px";
                oVal *= g_dKoef_pix_to_mm;
            }
            else if(-1 != value.indexOf("in"))
            {
                oType = "in";
                oVal *= g_dKoef_in_to_mm;
            }
            else if(-1 != value.indexOf("cm"))
            {
                oType = "cm";
                oVal *= 10;
            }
            else if(-1 != value.indexOf("mm"))
            {
                oType = "mm";
            }
            else if(-1 != value.indexOf("pt"))
            {
                oType = "pt";
                oVal *= g_dKoef_pt_to_mm;
            }
            else if(-1 != value.indexOf("pc"))
            {
                oType = "pc";
                oVal *= g_dKoef_pc_to_mm;
            }
            else
                oType = "none";
            return {val: oVal, type: oType};
        }
        return null;
    },
    _ParseColor : function(color)
    {
        if(!color || color.length == 0)
            return null;
        if("transparent" == color)
            return null;
        if("aqua" == color)
            return new CDocumentColor(0, 255, 255);
        else if("black" == color)
            return new CDocumentColor(0, 0, 0);
        else if("blue" == color)
            return new CDocumentColor(0, 0, 255);
        else if("fuchsia" == color)
            return new CDocumentColor(255, 0, 255);
        else if("gray" == color)
            return new CDocumentColor(128, 128, 128);
        else if("green" == color)
            return new CDocumentColor(0, 128, 0);
        else if("lime" == color)
            return new CDocumentColor(0, 255, 0);
        else if("maroon" == color)
            return new CDocumentColor(128, 0, 0);
        else if("navy" == color)
            return new CDocumentColor(0, 0, 128);
        else if("olive" == color)
            return new CDocumentColor(128, 128, 0);
        else if("purple" == color)
            return new CDocumentColor(128, 0, 128);
        else if("red" == color)
            return new CDocumentColor(255, 0, 0);
        else if("silver" == color)
            return new CDocumentColor(192, 192, 192);
        else if("teal" == color)
            return new CDocumentColor(0, 128, 128);
        else if("white" == color)
            return new CDocumentColor(255, 255, 255);
        else if("yellow" == color)
            return new CDocumentColor(255, 255, 0);
        else
        {
            if(0 == color.indexOf("#"))
            {
                var hex = color.substring(1);
                if(hex.length == 3)
                    hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2);
                if(hex.length == 6)
                {
                    var r = parseInt("0x" + hex.substring(0,2));
                    var g = parseInt("0x" + hex.substring(2,4));
                    var b = parseInt("0x" + hex.substring(4,6));
                    return new CDocumentColor(r, g, b);
                }
            }
            if(0 == color.indexOf("rgb"))
            {
                var nStart = color.indexOf('(');
                var nEnd = color.indexOf(')');
                if(-1 != nStart && -1 != nEnd && nStart < nEnd)
                {
                    var temp = color.substring(nStart + 1, nEnd);
                    var aParems = temp.split(',');
                    if(aParems.length >= 3)
                    {
                        if(aParems.length >= 4)
                        {
                            var oA = this._ValueToMmType(aParems[3]);
                            if(0 == oA .val)//��������� ����������
                                return null;
                        }
                        var oR = this._ValueToMmType(aParems[0]);
                        var oG = this._ValueToMmType(aParems[1]);
                        var oB = this._ValueToMmType(aParems[2]);
                        var r,g,b;
                        if(oR && "%" == oR.type)
                            r = parseInt(255 * oR.val / 100);
                        else
                            r = oR.val;
                        if(oG && "%" == oG.type)
                            g = parseInt(255 * oG.val / 100);
                        else
                            g = oG.val;
                        if(oB && "%" == oB.type)
                            b = parseInt(255 * oB.val / 100);
                        else
                            b = oB.val;
                        return new CDocumentColor(r, g, b);
                    }
                }
            }
        }
        return null;
    },
    _isEmptyProperty : function(prop)
    {
        var bIsEmpty = true;
        for(var i in prop)
        {
            if(null != prop[i])
            {
                bIsEmpty = false;
                break;
            }
        }
        return bIsEmpty;
    },
    _set_pPr : function(node, Para,  pNoHtmlPr)
    {
        //����������� ����� �� ������ � ������� �������� ��������
        if(node != this.oRootNode)
        {
            while(false == this._IsBlockElem(node.nodeName.toLowerCase()))
            {
                if(this.oRootNode != node.parentNode)
                    node = node.parentNode;
                else
                    break;
            }
        }
        var oDocument = this.oDocument;
        //Heading
        if(null != pNoHtmlPr.hLevel)
            Para.Style_Add(oDocument.Styles.Get_Default_Heading(pNoHtmlPr.hLevel));

        var pPr = Para.Pr;

        //Borders
        var oNewBorder = {Left: null, Top: null, Right: null, Bottom: null, Between: null};
        var sBorder = pNoHtmlPr["mso-border-alt"];
        if(null != sBorder)
        {
            var oNewBrd = this._ExecuteParagraphBorder(sBorder);
            oNewBorder.Left = oNewBrd;
            oNewBorder.Top = oNewBrd.Copy();
            oNewBorder.Right = oNewBrd.Copy();
            oNewBorder.Bottom = oNewBrd.Copy();
        }
        else
        {
            sBorder = pNoHtmlPr["mso-border-left-alt"];
            if(null != sBorder)
            {
                var oNewBrd = this._ExecuteParagraphBorder(sBorder);
                oNewBorder.Left = oNewBrd;
            }
            sBorder = pNoHtmlPr["mso-border-top-alt"];
            if(null != sBorder)
            {
                var oNewBrd = this._ExecuteParagraphBorder(sBorder);
                oNewBorder.Top = oNewBrd;
            }
            sBorder = pNoHtmlPr["mso-border-right-alt"];
            if(null != sBorder)
            {
                var oNewBrd = this._ExecuteParagraphBorder(sBorder);
                oNewBorder.Right = oNewBrd;
            }
            sBorder = pNoHtmlPr["mso-border-bottom-alt"];
            if(null != sBorder)
            {
                var oNewBrd = this._ExecuteParagraphBorder(sBorder);
                oNewBorder.Bottom = oNewBrd;
            }
        }
        sBorder = pNoHtmlPr["mso-border-between"];
        if(null != sBorder)
        {
            var oNewBrd = this._ExecuteParagraphBorder(sBorder);
            oNewBorder.Between = oNewBrd;
        }

        var defaultView = node.ownerDocument.defaultView;
        var computedStyle = defaultView.getComputedStyle( node, null );
        if (computedStyle)
        {
            //Ind
            var Ind = new CParaInd();
            var margin_left = computedStyle.getPropertyValue( "margin-left" );
            if(margin_left && null != (margin_left = this._ValueToMm(margin_left)))
                Ind.Left = margin_left;
            var margin_right = computedStyle.getPropertyValue( "margin-right" );
            if(margin_right && null != (margin_right = this._ValueToMm(margin_right)))
                Ind.Right = margin_right;
            //scale
            // if(null != pPr.Ind.Left && true == this.bUseScaleKoef)
            // pPr.Ind.Left = pPr.Ind.Left * this.dScaleKoef;
            // if(null != pPr.Ind.Right && true == this.bUseScaleKoef)
            // pPr.Ind.Right = pPr.Ind.Right * this.dScaleKoef;
            //�������� ����� ������ margin �� ������� �� ����� ��� �� ����������� ����� ��� �� �������
            if(null != Ind.Left && null != Ind.Right)
            {
                //30 ����������� ��� � �� �������
                var dif = Page_Width - X_Left_Margin - X_Right_Margin - Ind.Left - Ind.Right;
                if(dif < 30)
                    Ind.Right = Page_Width - X_Left_Margin - X_Right_Margin - Ind.Left - 30;
            }
            var text_indent = computedStyle.getPropertyValue( "text-indent" );
            if(text_indent && null != (text_indent = this._ValueToMm(text_indent)))
                Ind.FirstLine = text_indent;
            // if(null != pPr.Ind.FirstLine && true == this.bUseScaleKoef)
            // pPr.Ind.FirstLine = pPr.Ind.FirstLine * this.dScaleKoef;
            if(false == this._isEmptyProperty(Ind))
                Para.Set_Ind(Ind);
            //Jc
            var text_align = computedStyle.getPropertyValue( "text-align" );
            if(text_align)
            {
                //����� ��������� -webkit-right
                var Jc = null;
                if(-1 != text_align.indexOf('center'))
                    Jc = align_Center;
                else if(-1 != text_align.indexOf('right'))
                    Jc = align_Right;
                else if(-1 != text_align.indexOf('justify'))
                    Jc = align_Justify;
                if(null != Jc)
                    Para.Set_Align(Jc, false);
            }
            //Spacing
            var line_height = computedStyle.getPropertyValue( "line-height" );
            if(null != line_height)
            {
                var Spacing = new CParaSpacing();
                if("normal" == line_height || "1px" == line_height)
                {
                    Spacing.Line = 1;
                    Spacing.LineRule = linerule_Auto;
                }
                else
                {
                    var obj = this._ValueToMmType(line_height);
                    if(obj)
                    {
                        if("%" == obj.type)
                        {
                            Spacing.Line = obj.val;
                            Spacing.LineRule = linerule_Auto;
                        }
                        else
                        {
                            // pPr.Spacing.LineRule = linerule_AtLeast;
                            // pPr.Spacing.Line = obj.val;

                            //Todo ����� ����� ��������� � ���������, ���� ��� ������������.
                            //����� �������� � chrome ��� �������� line-height:100% � ��������� �� ��������� � �������
                            //� �������� html ��������� ���. ��������� �������� ��� �� �� �����
                            //�� ���� � ��������� ���� ������ ������������ ����������� ��������, �� ����������� ��� �������� �� ������ ������
                            var font_size = computedStyle.getPropertyValue( "font-size" );
                            if(font_size && null != (font_size = this._ValueToMm(font_size)))
                            {
                                if(font_size != 0)
									Spacing.Line = parseInt(100 * obj.val / font_size) / 100;
                                Spacing.LineRule = linerule_Auto;
                            }
                        }
                    }
                }
            }
            // if("exactly" == pNoHtmlPr["mso-line-height-rule"])
                // Spacing.LineRule = linerule_Exact;
            var margin_top = computedStyle.getPropertyValue( "margin-top" );
            if(margin_top && null != (margin_top = this._ValueToMm(margin_top)))
                Spacing.Before = margin_top;
            var margin_bottom = computedStyle.getPropertyValue( "margin-bottom" );
            if(margin_bottom && null != (margin_bottom = this._ValueToMm(margin_bottom)))
                Spacing.After = margin_bottom;
            if(false == this._isEmptyProperty(Spacing))
                Para.Set_Spacing(Spacing);
            //Shd
            //background-color �� ����������� ��������� ��������, ���� �������� ������������ ��������
            var background_color = null;
            var oTempNode = node;
            while(true)
            {
                var tempComputedStyle = defaultView.getComputedStyle( oTempNode, null );
                if(null == tempComputedStyle)
                    break;
                background_color = tempComputedStyle.getPropertyValue( "background-color" );
                if(null != background_color && (background_color = this._ParseColor(background_color)))
                    break;
                oTempNode = oTempNode.parentNode;
                if(this.oRootNode == oTempNode || "body" == oTempNode.nodeName.toLowerCase() || true == this._IsBlockElem(oTempNode.nodeName.toLowerCase()))
                    break;
            }
            if(g_bIsDocumentCopyPaste)
            {
                if(background_color)
                {
                    var Shd = new CDocumentShd();
                    Shd.Value = shd_Clear;
                    Shd.Color = background_color;
                    Para.Set_Shd(Shd);
                }
            }

            if(null == oNewBorder.Left)
                oNewBorder.Left = this._ExecuteBorder(computedStyle, node, "left", "Left", false);
            if(null == oNewBorder.Top)
                oNewBorder.Top = this._ExecuteBorder(computedStyle, node, "top", "Top", false);
            if(null == oNewBorder.Right)
                oNewBorder.Right = this._ExecuteBorder(computedStyle, node, "left", "Left", false);
            if(null == oNewBorder.Bottom)
                oNewBorder.Bottom = this._ExecuteBorder(computedStyle, node, "bottom", "Bottom", false);
        }
        if(false == this._isEmptyProperty(oNewBorder))
            Para.Set_Borders(oNewBorder);

        //KeepLines , WidowControl
        var pagination = pNoHtmlPr["mso-pagination"];
        if(pagination)
        {
            //todo WidowControl
            if("none" == pagination)
                ;//pPr.WidowControl = !Def_pPr.WidowControl;
            else if(-1 != pagination.indexOf("widow-orphan") && -1 != pagination.indexOf("lines-together"))
                Para.Set_KeepLines(true);
            else if(-1 != pagination.indexOf("none") && -1 != pagination.indexOf("lines-together"))
            {
                ;//pPr.WidowControl = !Def_pPr.WidowControl;
                Para.Set_KeepLines(true);
            }
        }
        //todo KeepNext
        if("avoid" == pNoHtmlPr["page-break-after"])
            ;//pPr.KeepNext = !Def_pPr.KeepNext;
        //PageBreakBefore
        if("always" == pNoHtmlPr["page-break-before"])
            Para.Set_PageBreakBefore(true);
        //Tabs
        var tab_stops = pNoHtmlPr["tab-stops"]
        if(tab_stops && "" != pNoHtmlPr["tab-stops"])
        {
            var aTabs = tab_stops.split(' ');
            var nTabLen = aTabs.length;
            if(nTabLen > 0)
            {
                var Tabs = new CParaTabs();
                for(var i = 0; i < nTabLen; i++)
                {
                    var val = this._ValueToMm(aTabs[i]);
                    if(val)
                        Tabs.Add(new CParaTab(tab_Left, val));
                }
                Para.Set_Tabs(Tabs);
            }
        }

        //num
        if(g_bIsDocumentCopyPaste)
        {
            if(true == pNoHtmlPr.bNum)
            {
                var num = numbering_numfmt_Bullet;
                if(null != pNoHtmlPr.numType)
                    num = pNoHtmlPr.numType;
                var type = pNoHtmlPr["list-style-type"];
                if(type)
                {
                    switch(type)
                    {
                        case "disc": num = numbering_numfmt_Bullet;break;
                        case "decimal": num = numbering_numfmt_Decimal;break;
                        case "lower-roman": num = numbering_numfmt_LowerRoman;break;
                        case "upper-roman": num = numbering_numfmt_UpperRoman;break;
                        case "lower-alpha": num = numbering_numfmt_LowerLetter;break;
                        case "upper-alpha": num = numbering_numfmt_UpperLetter;break;
                    }
                }
                //����� ���� ����������� �� Document.Set_ParagraphNumbering
                
                //������� ����������� ��������, ���� ��� ������ ���������, �� ����� ��� ������ �� ����������� ���������
                var NumId = null;
                if(this.aContent.length > 1)
                {
                    var prevElem = this.aContent[this.aContent.length - 2];
                    if(null != prevElem && type_Paragraph === prevElem.GetType())
                    {
                        var PrevNumPr = prevElem.Numbering_Get();
                        if ( null != PrevNumPr && true === this.oLogicDocument.Numbering.Check_Format( PrevNumPr.NumId, PrevNumPr.Lvl, num ) )
                            NumId  = PrevNumPr.NumId;
                    }
                }
                if(null == NumId)
                {
                    //������� ����� ������
                    NumId  = this.oLogicDocument.Numbering.Create_AbstractNum();
                    NumLvl = 0;

                    var AbstractNum = this.oLogicDocument.Numbering.Get_AbstractNum( NumId );
                    AbstractNum.Create_Default_Bullet();
                    switch(num)
                    {
                        case numbering_numfmt_Decimal: AbstractNum.Set_Lvl_Numbered_2(0);break;
                        case numbering_numfmt_LowerRoman: AbstractNum.Set_Lvl_Numbered_5(0);break;
                        case numbering_numfmt_UpperRoman: AbstractNum.Set_Lvl_Numbered_9(0);break;
                        case numbering_numfmt_LowerLetter: AbstractNum.Set_Lvl_Numbered_8(0);break;
                        case numbering_numfmt_UpperLetter: AbstractNum.Set_Lvl_Numbered_6(0);break;
                    }
                    //��������� ��������� ������ ����� �� ���������� ������� ���������� ��������
                    var oFirstTextChild = node;
                    while(true)
                    {
                        var bContinue = false;
                        for(var i = 0, length = oFirstTextChild.childNodes.length; i < length; i++)
                        {
                            var child = oFirstTextChild.childNodes[i];
                            var nodeType = child.nodeType;

                            if(!(Node.ELEMENT_NODE == nodeType || Node.TEXT_NODE == nodeType))
                                continue;
                            //�������� ������� ��������� ������ �� \t,\n,\r
                            if( Node.TEXT_NODE == child.nodeType)
                            {
                                var value = child.nodeValue;
                                if(!value)
                                    continue;
                                value = value.replace(/(\r|\t|\n)/g, '');
                                if("" == value)
                                    continue;
                            }
                            if(Node.ELEMENT_NODE == nodeType)
                            {
                                oFirstTextChild = child;
                                bContinue = true;
                                break;
                            }
                        }
                        if(false == bContinue)
                            break;
                    }
                    if(node != oFirstTextChild)
                    {
                        var oLvl = AbstractNum.Lvl[0];
						var oTextPr = this._read_rPr(oFirstTextChild);
						if(numbering_numfmt_Bullet == num)
                            oTextPr.RFonts = oLvl.TextPr.RFonts.Copy();
                        //�������� ��������� �� node
						AbstractNum.Apply_TextPr(0, oTextPr);
                    }
                }
                Para.Numbering_Add( NumId, 0 );
            }
            else
            {
                var numPr = Para.Numbering_Get();
                if(numPr)
                    Para.Numbering_Remove();
            }
        }
        else
        {
            if(true == pNoHtmlPr.bNum)
            {
                var num = numbering_presentationnumfrmt_Char;
                if(null != pNoHtmlPr.numType)
                    num = pNoHtmlPr.numType;
                var type = pNoHtmlPr["list-style-type"];
                if(type)
                {
                    switch(type)
                    {
                        case "disc": num = numbering_presentationnumfrmt_Char;break;
                        case "decimal": num = numbering_presentationnumfrmt_ArabicPeriod;break;
                        case "lower-roman": num = numbering_presentationnumfrmt_RomanLcPeriod;break;
                        case "upper-roman": num = numbering_presentationnumfrmt_RomanUcPeriod;break;
                        case "lower-alpha": num = numbering_presentationnumfrmt_AlphaLcPeriod;break;
                        case "upper-alpha": num = numbering_presentationnumfrmt_AlphaUcPeriod;break;
                        default:
                        {
                            num = numbering_presentationnumfrmt_Char;
                        }
                    }
                }
                var _bullet = new CPresentationBullet();
                _bullet.m_nType = num;
                if(num == numbering_presentationnumfrmt_Char)
                {
                    _bullet.m_sChar = "�";
                }
                _bullet.m_nStartAt = 1;
                Para.Add_PresentationNumbering2(_bullet );
            }
            else
            {
               Para.Remove_PresentationNumbering();
            }
        }
        Para.CompiledPr.NeedRecalc = true;
    },
    _commit_rPr : function(node)
    {
        var rPr = this._read_rPr(node);
        //���� ��������� ��������� ���������� ��������� �������
        if(false == Common_CmpObj2(this.oCur_rPr, rPr))
        {
            this._Paragraph_Add( new ParaTextPr( rPr ) );
            this.oCur_rPr = rPr;
        }
    },
    _read_rPr : function(node)
    {
        var oDocument = this.oDocument;
        var rPr = new CTextPr();
        if(false == g_bIsDocumentCopyPaste)
        {
            rPr.Set_FromObject({
                Bold       : false,
                Italic     : false,
                Underline  : false,
                Strikeout  : false,
				RFonts :
				{
					Ascii: {
						Name  : "Arial",
						Index : -1
					},
					EastAsia: {
						Name  : "Arial",
						Index : -1
					},
					HAnsi: {
						Name  : "Arial",
						Index : -1
					},
					CS: {
						Name  : "Arial",
						Index : -1
					}
				},
                FontSize   : 11,
                Color      :
                {
                    r : 0,
                    g : 0,
                    b : 0
                },
                VertAlign : vertalign_Baseline,
                HighLight : highlight_None
            });
        }
        var defaultView = node.ownerDocument.defaultView;
        var computedStyle = defaultView.getComputedStyle( node, null );
        if ( computedStyle )
        {
            var font_family = computedStyle.getPropertyValue( "font-family" );
            if(font_family && "" != font_family)
            {
                var oFontItem = this.oFonts[font_family];
                if(null != oFontItem && null != oFontItem.Name)
				{
					rPr.RFonts.Ascii = {Name: oFontItem.Name, Index: oFontItem.Index};
					rPr.RFonts.HAnsi = {Name: oFontItem.Name, Index: oFontItem.Index};
					rPr.RFonts.CS = {Name: oFontItem.Name, Index: oFontItem.Index};
					rPr.RFonts.EastAsia = {Name: oFontItem.Name, Index: oFontItem.Index};
				}
            }
            var font_size = node.style.fontSize;
            if(!font_size)
                font_size = computedStyle.getPropertyValue( "font-size" );
            if(font_size)
            {
                var obj = this._ValueToMmType(font_size);
                if(obj && "%" != obj.type && "none" != obj.type)
                {
                    font_size = obj.val;
                    //���� ������� �� ������������ ������� ������� �������� ���������� ������, ��� ���������� ��� ������� 8, 11, 14, 20, 26pt
                    if("px" == obj.type && false == this.bIsDoublePx)
                        font_size = Math.round(font_size * g_dKoef_mm_to_pt);
                    else
                        font_size = Math.round(2 * font_size * g_dKoef_mm_to_pt) / 2;//���������� �������� ���������.
                    rPr.FontSize = font_size;
                }
            }
            var font_weight = computedStyle.getPropertyValue( "font-weight" );
            if(font_weight)
            {
                if("bold" == font_weight || "bolder" == font_weight || 400 < font_weight)
                    rPr.Bold = true;
            }
            var font_style = computedStyle.getPropertyValue( "font-style" );
            if("italic" == font_style)
                rPr.Italic = true;
            var color = computedStyle.getPropertyValue( "color" );
            if(color && (color = this._ParseColor(color)))
            {
                rPr.Color = color;
            }
            //������� �� ��������, ������� �� �����������, ���� �������� ������������ ��������
            var background_color = null;
            var underline = null;
            var Strikeout = null;
            var vertical_align = null;
            var oTempNode = node;
            while(true)
            {
                var tempComputedStyle = defaultView.getComputedStyle( oTempNode, null );
                if(null == tempComputedStyle)
                    break;
                if(null == underline || null == Strikeout)
                {
                    text_decoration = tempComputedStyle.getPropertyValue( "text-decoration" );
                    if(text_decoration)
                    {
                        if(-1 != text_decoration.indexOf("underline"))
                            underline = true;
                        if(-1 != text_decoration.indexOf("line-through"))
                            Strikeout = true;
                    }
                }
                if(null == background_color)
                {
                    background_color = tempComputedStyle.getPropertyValue( "background-color" );
                    if(background_color)
                        background_color = this._ParseColor(background_color);
                    else
                        background_color = null;
                }
                if(null == vertical_align || "baseline" == vertical_align)
                {
                    vertical_align = tempComputedStyle.getPropertyValue( "vertical-align" );
                    if(!vertical_align)
                        vertical_align = null;
                }
                if(vertical_align && background_color && Strikeout && underline)
                    break;
                oTempNode = oTempNode.parentNode;
                if(this.oRootNode == oTempNode || "body" == oTempNode.nodeName.toLowerCase()  || true == this._IsBlockElem(oTempNode.nodeName.toLowerCase()))
                    break;
            }
            if(g_bIsDocumentCopyPaste)
            {
                if(background_color)
                    rPr.HighLight = background_color;
            }
            else
                delete rPr.HighLight;
            if(null != underline)
                rPr.Underline = underline;
            if(null != Strikeout)
                rPr.Strikeout = Strikeout;
            switch(vertical_align)
            {
                case "sub": rPr.VertAlign = vertalign_SubScript;break;
                case "super": rPr.VertAlign = vertalign_SuperScript;break;
            }
        }
        return rPr;
    },
    _parseCss : function(sStyles, pPr)
    {
        var aStyles = sStyles.split(';');
        if(aStyles)
        {
            for(var i = 0, length = aStyles.length; i < length; i++)
            {
                var style = aStyles[i];
                var aPair = style.split(':');
                if(aPair && aPair.length > 1)
                {
                    var prop_name = trimString(aPair[0]);
                    var prop_value = trimString(aPair[1]);
                    if(null != this.MsoStyles[prop_name])
                        pPr[prop_name] = prop_value;
                }
            }
        }
    },
    _PrepareContent : function()
    {
        //�� �������� ����� ������� ������������ �� �������, ����� ������ �������� �������� �����
        if(this.aContent.length > 0)
        {
            var last = this.aContent[this.aContent.length - 1];
            if(type_Table == last.GetType())
            {
                this._Add_NewParagraph();
            }
        }
    },
    _AddNextPrevToContent : function(oDoc)
    {
        var prev = null;
        for(var i = 0, length = this.aContent.length; i < length; ++i)
        {
            var cur = this.aContent[i];
            cur.Set_DocumentPrev( prev );
            cur.Parent = oDoc;
            if(prev)
                prev.Set_DocumentNext( cur );
            prev = cur;
        }
    },
    _Paragraph_Add : function(elem)
    {
        if(null != this.oCurPar)
		{
            this.oCurPar.Internal_Content_Add(this.oCurParContentPos, elem);
			this.oCurParContentPos++;
		}
    },
    _Add_NewParagraph : function()
    {
        this.oCurPar = new Paragraph(this.oDocument.DrawingDocument, this.oDocument, 0, 50, 50, X_Right_Field, Y_Bottom_Field );
		this.oCurParContentPos = this.oCurPar.CurPos.ContentPos;
        this.aContent.push(this.oCurPar);
        //���������� ��������� �����
        this.oCur_rPr = new CTextPr();
    },
    _Execute_AddParagraph : function(node, pPr)
    {
        this._Add_NewParagraph();
        //������������� ����� ���������
        this._set_pPr(node, this.oCurPar, pPr);
    },
    _Decide_AddParagraph : function(node, pPr, bParagraphAdded, bCommitBr)
    {
        //���������� ������ ���������(��� ��������, ��� MS), ��������� �������� ������ ����� ������ �����
        if(true == bParagraphAdded)
        {
            if(false != bCommitBr)
                this._Commit_Br(2, node, pPr);//word ���������� 2 ��������� br
            this._Execute_AddParagraph(node, pPr);
        }
        else if(false != bCommitBr)
            this._Commit_Br(0, node, pPr);
        return false;
    },
    _Commit_Br : function(nIgnore, node, pPr)
    {
        for(var i = 0, length = this.nBrCount - nIgnore; i < length; i++)
        {
            if(this.bInBlock)
                this._Paragraph_Add( new ParaNewLine( break_Line ) );
            else
                this._Execute_AddParagraph(node, pPr);
        }
        this.nBrCount = 0;
    },
    _StartExecuteTable : function(node, pPr)
    {
        var oDocument = this.oDocument;
        var defaultView = node.ownerDocument.defaultView;
        var tableNode = node;
        //���� ���� ���� tbody
        for(var i = 0, length = node.childNodes.length; i < length; ++i)
        {
            if("tbody" == node.childNodes[i].nodeName.toLowerCase())
            {
                node = node.childNodes[i];
                break;
            }
        }
        //��������� �����. � ������� �� ����� ���� ����� ��������� �� ����������� ���������� �����.
        var nRowCount = 0;
        var nMinColCount = 0;
        var nMaxColCount = 0;
        var aColsCountByRow = new Array();
        var oRowSums = new Object();
        oRowSums[0] = 0;
        var dMaxSum = 0;
        var nCurColWidth = 0;
        var nCurSum = 0;
        var oRowSpans = new Object();
        var fParseSpans = function()
        {
            var spans = oRowSpans[nCurColWidth];
            while(null != spans && spans.row > 0)
            {
                spans.row--;
                nCurColWidth += spans.col;
                nCurSum += spans.width;
                spans = oRowSpans[nCurColWidth];
            }
        };
        for(var i = 0, length = node.childNodes.length; i < length; ++i)
        {
            var tr = node.childNodes[i];
            if("tr" == tr.nodeName.toLowerCase())
            {
                nCurSum = 0;
                nCurColWidth = 0;
                var nMinRowSpanCount = null;//����������� rowspan ����� ������
                for(var j = 0, length2 = tr.childNodes.length; j < length2; ++j)
                {
                    var tc = tr.childNodes[j];
                    var tcName = tc.nodeName.toLowerCase();
                    if("td" == tcName || "th" == tcName)
                    {
                        fParseSpans();

                        var dWidth = null;
                        var computedStyle = defaultView.getComputedStyle( tc, null );
                        if ( computedStyle )
                        {
                            var computedWidth = computedStyle.getPropertyValue( "width" );
                            if(null != computedWidth && null != (computedWidth = this._ValueToMm(computedWidth)))
                                dWidth = computedWidth;
                        }
                        if(null == dWidth)
                            dWidth = tc.clientWidth * g_dKoef_pix_to_mm;

                        var nColSpan = tc.getAttribute("colspan");
                        if(null != nColSpan)
                            nColSpan = nColSpan - 0;
                        else
                            nColSpan = 1;
                        var nCurRowSpan = tc.getAttribute("rowspan");
                        if(null != nCurRowSpan)
                        {
                            nCurRowSpan = nCurRowSpan - 0;
                            if(null == nMinRowSpanCount)
                                nMinRowSpanCount = nCurRowSpan;
                            else if(nMinRowSpanCount > nCurRowSpan)
                                nMinRowSpanCount = nCurRowSpan;
                            if(nCurRowSpan > 1)
                                oRowSpans[nCurColWidth] = {row: nCurRowSpan - 1, col: nColSpan, width: dWidth};
                        }
                        else
                            nMinRowSpanCount = 0;

                        nCurSum += dWidth;
                        if(null == oRowSums[nCurColWidth + nColSpan])
                            oRowSums[nCurColWidth + nColSpan] = nCurSum;
                        nCurColWidth += nColSpan;
                    }
                }
                fParseSpans();
                //������� ������ rowspan
                if(nMinRowSpanCount > 1)
                {
                    for(var j = 0, length2 = tr.childNodes.length; j < length2; ++j)
                    {
                        var tc = tr.childNodes[j];
                        var tcName = tc.nodeName.toLowerCase();
                        if("td" == tcName || "th" == tcName)
                        {
                            var nCurRowSpan = tc.getAttribute("rowspan");
                            if(null != nCurRowSpan)
                                tc.setAttribute("rowspan", nCurRowSpan - nMinRowSpanCount);
                        }
                    }
                }
                if(dMaxSum < nCurSum)
                    dMaxSum = nCurSum;
                //������� ������ tr
                if(0 == nCurColWidth)
				{
                    node.removeChild(tr);
					length--;
					i--;
				}
                else
                {
                    if(0 == nMinColCount || nMinColCount > nCurColWidth)
                        nMinColCount = nCurColWidth;
                    if(nMaxColCount < nCurColWidth)
                        nMaxColCount = nCurColWidth;
                    nRowCount++;
                    aColsCountByRow.push(nCurColWidth);
                }
            }
        }
        if(nMaxColCount != nMinColCount)
        {
            for(var i = 0, length = aColsCountByRow.length; i < length; ++i)
                aColsCountByRow[i] = nMaxColCount - aColsCountByRow[i];
        }
        if(nRowCount > 0 && nMaxColCount > 0)
        {
            var bUseScaleKoef = this.bUseScaleKoef;
            var dScaleKoef = this.dScaleKoef;
            if(dMaxSum * dScaleKoef > this.dMaxWidth)
            {
                dScaleKoef = dScaleKoef * this.dMaxWidth / dMaxSum;
                bUseScaleKoef = true;
            }
            //������ Grid
            var aGrid = new Array();
            var nPrevIndex = null;
            var nPrevVal = 0;
            for(var i in oRowSums)
            {
                var nCurIndex = i - 0;
                var nCurVal = oRowSums[i];
                var nCurWidth = nCurVal - nPrevVal;
                if(bUseScaleKoef)
                    nCurWidth *= dScaleKoef;
                if(null != nPrevIndex)
                {
                    var nDif = nCurIndex - nPrevIndex;
                    if(1 == nDif)
                        aGrid.push(nCurWidth);
                    else
                    {
                        var nPartVal = nCurWidth / nDif;
                        for(var i = 0; i < nDif; ++i)
                            aGrid.push(nPartVal);
                    }
                }
                nPrevVal = nCurVal;
                nPrevIndex = nCurIndex;
            }
			var CurPage = 0;
            var table = new CTable(oDocument.DrawingDocument, oDocument, true, 0, 0, 0, X_Right_Field, Y_Bottom_Field, 0, 0, aGrid);
			//считаем aSumGrid
			var aSumGrid = new Array();
			aSumGrid[-1] = 0;
			var nSum = 0;
			for(var i = 0, length = aGrid.length; i < length; ++i)
			{
				nSum += aGrid[i];
				aSumGrid[i] = nSum;
			}
            //�������� content
            this._ExecuteTable(tableNode, node, table, aSumGrid, nMaxColCount != nMinColCount ? aColsCountByRow : null, pPr, bUseScaleKoef, dScaleKoef);
            table.Cursor_MoveToStartPos();
            this.aContent.push(table);
        }
    },
    _ExecuteBorder : function(computedStyle, node, type, type2, bAddIfNull)
    {
        var res = null;
        var style = computedStyle.getPropertyValue( "border-"+type+"-style" );
        if(null != style)
        {
            res = new CDocumentBorder();
            if("none" == style)
                res.Value = border_None;
            else
            {
                res.Value = border_Single;
                var width = node.style["border"+type2+"Width"];
                if(!width)
                    computedStyle.getPropertyValue( "border-"+type+"-width" );
                if(null != width && null != (width = this._ValueToMm(width)))
                    res.Size = width;
                var color = computedStyle.getPropertyValue( "border-"+type+"-color" );
                if(null != color && (color = this._ParseColor(color)))
                    res.Color = color;
            }
        }
        if(bAddIfNull && null == res)
            res = new CDocumentBorder();
        return res;
    },
    _ExecuteParagraphBorder : function(border)
    {
        var res = this.oBorderCache[border];
        if(null != res)
            return res.Copy();
        else
        {
            //������� ����� dom ����� �� ������ ������� ������� ������� ������
            //todo ������� ��� dom, ���������� �����.
            res = new CDocumentBorder();;
            var oTestDiv = document.createElement("div");
            oTestDiv.setAttribute("style", "border-left:"+border);
            document.body.appendChild( oTestDiv );
            var defaultView = oTestDiv.ownerDocument.defaultView;
            var computedStyle = defaultView.getComputedStyle( oTestDiv, null );
            if(null != computedStyle)
            {
                res = this._ExecuteBorder(computedStyle, oTestDiv, "left", "Left", true);
            }
            document.body.removeChild( oTestDiv );
            this.oBorderCache[border] = res;
            return res;
        }
    },
    _ExecuteTable : function(tableNode, node, table, aSumGrid, aColsCountByRow, pPr, bUseScaleKoef, dScaleKoef)
    {
		//из-за проблем со вставкой больших таблиц, не вставляем tbllayout_AutoFit
		table.Set_TableLayout(tbllayout_Fixed);
        //Pr
        var Pr = table.Pr;
        var defaultView = tableNode.ownerDocument.defaultView;
        //align ������� � parent tableNode
		var sTableAlign = null;
		if(null != tableNode.align)
			sTableAlign = tableNode.align
        else if(null != tableNode.parentNode && this.oRootNode != tableNode.parentNode)
        {
            computedStyleParent = defaultView.getComputedStyle(tableNode.parentNode, null);
            if(null != computedStyleParent)
            {
                //����� ��������� -webkit-right
                sTableAlign = computedStyleParent.getPropertyValue( "text-align" );
            }
        }
		if(null != sTableAlign)
		{
			if(-1 != sTableAlign.indexOf('center'))
				table.Set_TableAlign(align_Center);
			else if(-1 != sTableAlign.indexOf('right'))
				table.Set_TableAlign(align_Right);
		}
        var spacing = null;
        table.Set_TableBorder_InsideH(new CDocumentBorder());
        table.Set_TableBorder_InsideV(new CDocumentBorder());

        var style = tableNode.getAttribute("style");
        if(style)
        {
            var tblPrMso = new Object();
            this._parseCss(style, tblPrMso);
            var spacing = tblPrMso["mso-cellspacing"];
            if(null != spacing && null != (spacing = this._ValueToMm(spacing)))
                ;
            var padding = tblPrMso["mso-padding-alt"];
            if(null != padding)
            {
                padding = trimString(padding);
                var aMargins = padding.split(" ");
                if(4 == aMargins.length)
                {
                    var top = aMargins[0];
                    if(null != top && null != (top = this._ValueToMm(top)))
                        ;
                    else
                        top = Pr.TableCellMar.Top.W;
                    var right = aMargins[1];
                    if(null != right && null != (right = this._ValueToMm(right)))
                        ;
                    else
                        right = Pr.TableCellMar.Right.W;
                    var bottom = aMargins[2];
                    if(null != bottom && null != (bottom = this._ValueToMm(bottom)))
                        ;
                    else
                        bottom = Pr.TableCellMar.Bottom.W;
                    var left = aMargins[3];
                    if(null != left && null != (left = this._ValueToMm(left)))
                        ;
                    else
                        left = Pr.TableCellMar.Left.W;
                    table.Set_TableCellMar(left, top, right, bottom);
                }
            }
            var insideh = tblPrMso["mso-border-insideh"];
            if(null != insideh)
                table.Set_TableBorder_InsideH(this._ExecuteParagraphBorder(insideh));
            var insidev = tblPrMso["mso-border-insidev"];
            if(null != insidev)
                table.Set_TableBorder_InsideV(this._ExecuteParagraphBorder(insidev));
        }
        var computedStyle = defaultView.getComputedStyle(tableNode, null);
        if(computedStyle)
        {
			if(align_Left == table.Get_TableAlign())
			{
				var margin_left = computedStyle.getPropertyValue( "margin-left" );
				//todo возможно надо еще учесть ширину таблицы
				if(margin_left && null != (margin_left = this._ValueToMm(margin_left)) && margin_left < Page_Width - X_Left_Margin)
					table.Set_TableInd(margin_left);
			}
            background_color = computedStyle.getPropertyValue( "background-color" );
            if(null != background_color && (background_color = this._ParseColor(background_color)))
                table.Set_TableShd(shd_Clear, background_color.r, background_color.g, background_color.b);
            var oLeftBorder = this._ExecuteBorder(computedStyle, tableNode, "left", "Left", false);
            if(null != oLeftBorder)
                table.Set_TableBorder_Left(oLeftBorder);
            var oTopBorder = this._ExecuteBorder(computedStyle, tableNode, "top", "Top", false);
            if(null != oTopBorder)
                table.Set_TableBorder_Top(oTopBorder);
            var oRightBorder = this._ExecuteBorder(computedStyle, tableNode, "right", "Right", false);
            if(null != oRightBorder)
                table.Set_TableBorder_Right(oRightBorder);
            var oBottomBorder = this._ExecuteBorder(computedStyle, tableNode, "bottom", "Bottom", false);
            if(null != oBottomBorder)
                table.Set_TableBorder_Bottom(oBottomBorder);

            if(null == spacing)
            {
                spacing = computedStyle.getPropertyValue( "padding" );
                if(!spacing)
                    spacing = tableNode.style.padding;
                if(!spacing)
                    spacing = null;
                if(spacing && null != (spacing = this._ValueToMm(spacing)))
                    ;
            }
        }

        //content
        var oRowSpans = new Object();
        for(var i = 0, length = node.childNodes.length; i < length; ++i)
        {
            var tr = node.childNodes[i];
            if("tr" == tr.nodeName.toLowerCase())
            {
				var row = table.Internal_Add_Row(table.Content.length, 0);
                this._ExecuteTableRow(tr, row, aSumGrid, spacing, oRowSpans, bUseScaleKoef, dScaleKoef);
            }
        }
    },
    _ExecuteTableRow : function(node, row, aSumGrid, spacing, oRowSpans, bUseScaleKoef, dScaleKoef)
    {
        var oThis = this;
        var table = row.Table;
        if(null != spacing && spacing >= tableSpacingMinValue)
            row.Set_CellSpacing(spacing);
        if(node.style.height)
        {
            var height = node.style.height;
            if(!("auto" == height || "inherit" == height || -1 != height.indexOf("%")) && null != (height = this._ValueToMm(height)))
                row.Set_Height(height, heightrule_AtLeast);
        }
		var bBefore = false;
		var bAfter = false;
        var style = node.getAttribute("style");
        if(null != style)
        {
            var tcPr = new Object();
            this._parseCss(style, tcPr);
            var margin_left = tcPr["mso-row-margin-left"];
            if(margin_left && null != (margin_left = this._ValueToMm(margin_left)))
                bBefore = true;
            var margin_right = tcPr["mso-row-margin-right"];
            if(margin_right && null != (margin_right = this._ValueToMm(margin_right)))
                bAfter = true;
        }

        //content
        var nCellIndex = 0;
        var nCellIndexSpan = 0;
        var fParseSpans = function()
        {
            var spans = oRowSpans[nCellIndexSpan];
            while(null != spans)
            {
				var oCurCell = row.Add_Cell(row.Get_CellsCount(), row, null, false);
				oCurCell.Set_VMerge(vmerge_Continue);
                if(spans.col > 1)
                    oCurCell.Set_GridSpan(spans.col);
                spans.row--;
				if(spans.row <= 0)
					delete oRowSpans[nCellIndexSpan];
                nCellIndexSpan += spans.col;
                spans = oRowSpans[nCellIndexSpan];
            }
        };
		var oBeforeCell = null;
		var oAfterCell = null;
		if(bBefore || bAfter)
		{
			for(var i = 0, length = node.childNodes.length; i < length; ++i)
			{
				var tc = node.childNodes[i];
				var tcName = tc.nodeName.toLowerCase();
				if("td" == tcName || "th" == tcName)
				{
					if(bBefore && null != oBeforeCell)
						oBeforeCell = tc;
					else if(bAfter)
						oAfterCell = tc;
				}
			}
		}
        for(var i = 0, length = node.childNodes.length; i < length; ++i)
        {
            //����� ����� ���� ��� ��� ����� ����������� td, ������ ��� ����������� ���������� ������ ����������� � dom
            fParseSpans();

            var tc = node.childNodes[i];
            var tcName = tc.nodeName.toLowerCase();
            if("td" == tcName || "th" == tcName)
            {
                var nColSpan = tc.getAttribute("colspan");
                if(null != nColSpan)
                    nColSpan = nColSpan - 0;
                else
                    nColSpan = 1;
				if(tc == oBeforeCell)
					row.Set_Before(nColSpan);
				else if(tc == oAfterCell)
					row.Set_After(nColSpan);
				else
				{
					var oCurCell = row.Add_Cell(row.Get_CellsCount(), row, null, false);
					if(nColSpan > 1)
						oCurCell.Set_GridSpan(nColSpan);
					var width = aSumGrid[nCellIndexSpan + nColSpan - 1] - aSumGrid[nCellIndexSpan - 1];
					oCurCell.Set_W(new CTableMeasurement(tblwidth_Mm, width));
					var nRowSpan = tc.getAttribute("rowspan");
					if(null != nRowSpan)
						nRowSpan = nRowSpan - 0;
					else
						nRowSpan = 1;
					if(nRowSpan > 1)
						oRowSpans[nCellIndexSpan] = {row: nRowSpan - 1, col: nColSpan};
					this._ExecuteTableCell(tc, oCurCell, bUseScaleKoef, dScaleKoef, spacing);
				}
                nCellIndexSpan+=nColSpan;
            }
        }
        fParseSpans();
    },
    _ExecuteTableCell : function(node, cell, bUseScaleKoef, dScaleKoef, spacing)
    {
        //Pr
        var Pr = cell.Pr;
        var bAddIfNull = false;
        if(null != spacing)
            bAddIfNull = true;
        var defaultView = node.ownerDocument.defaultView;
        var computedStyle = defaultView.getComputedStyle(node, null);
        if(null != computedStyle)
        {
            background_color = computedStyle.getPropertyValue( "background-color" );
            if(null != background_color && (background_color = this._ParseColor(background_color)))
            {
                var Shd = new CDocumentShd();
                Shd.Value = shd_Clear;
                Shd.Color = background_color;
                cell.Set_Shd(Shd);
            }
            var border = this._ExecuteBorder(computedStyle, node, "left", "Left", bAddIfNull);
            if(null != border)
                cell.Set_Border(border, 3);
            var border = this._ExecuteBorder(computedStyle, node, "top", "Top", bAddIfNull);
            if(null != border)
                cell.Set_Border(border, 0);
            var border = this._ExecuteBorder(computedStyle, node, "right", "Right", bAddIfNull);
            if(null != border)
                cell.Set_Border(border, 1);
            var border = this._ExecuteBorder(computedStyle, node, "bottom", "Bottom", bAddIfNull);
            if(null != border)
                cell.Set_Border(border, 2);

            var top = computedStyle.getPropertyValue( "padding-top" );
            if(null != top && null != (top = this._ValueToMm(top)))
                cell.Set_Margins({ W : top, Type : tblwidth_Mm }, 0);
            var right = computedStyle.getPropertyValue( "padding-right" );
            if(null != right && null != (right = this._ValueToMm(right)))
                cell.Set_Margins({ W : right, Type : tblwidth_Mm }, 1);
            var bottom = computedStyle.getPropertyValue( "padding-bottom" );
            if(null != bottom && null != (bottom = this._ValueToMm(bottom)))
                cell.Set_Margins({ W : bottom, Type : tblwidth_Mm }, 2);
            var left = computedStyle.getPropertyValue( "padding-left" );
            if(null != left && null != (left = this._ValueToMm(left)))
                cell.Set_Margins({ W : left, Type : tblwidth_Mm }, 3);
        }

        //content
        var oPasteProcessor = new PasteProcessor(this.api, false, false, true);
        oPasteProcessor.oFonts = this.oFonts;
        oPasteProcessor.oImages = this.oImages;
        oPasteProcessor.oDocument = cell.Content;
        oPasteProcessor.bIgnoreNoBlockText = true;
        oPasteProcessor.dMaxWidth = this._CalcMaxWidthByCell(cell);
        if(true == bUseScaleKoef)
        {
            oPasteProcessor.bUseScaleKoef = bUseScaleKoef;
            oPasteProcessor.dScaleKoef = dScaleKoef;
        }
        oPasteProcessor.Start(node);
        oPasteProcessor._PrepareContent();
        oPasteProcessor._AddNextPrevToContent(cell.Content);
        if(0 == oPasteProcessor.aContent.length)
        {
            var oDocContent = cell.Content;
            var oNewPar = new Paragraph(oDocContent.DrawingDocument, oDocContent, 0, 50, 50, X_Right_Field, Y_Bottom_Field );
            //���������� ��������� ��������� - ����� ��� ����������� �� ������ � ������ ���� ��� ����������� ������ ������
            oNewPar.Set_Spacing({After: 0, Before: 0, Line: linerule_Auto});
            oPasteProcessor.aContent.push(oNewPar);
        }
        //��������� ����� ���������
        for(var i = 0, length = oPasteProcessor.aContent.length; i < length; ++i)
            cell.Content.Internal_Content_Add(i + 1, oPasteProcessor.aContent[i]);
        //������� ��������, ������� ��������� � ������� �� ���������
        cell.Content.Internal_Content_Remove(0, 1);
    },
    _Execute : function(node, pPr, bRoot, bAddParagraph, bInBlock)
    {
        //bAddParagraph ���� �������� �� ������� _Decide_AddParagraph, ��������� �������� ��� ���.
        //bAddParagraph ������������ � true, ����� ���������� ������� ������� � �� ��������� �������� ��������
        var oDocument = this.oDocument;
        var bRootHasBlock = false;//���� root ���� ������� �������, �� ���� ��� child ������� �����������
        //��� Root node �� ������� ����� � �� ��������� �����
        if(true == bRoot)
        {
            //���� ������� ��������� ���, �� �������� ����
            var bExist = false;
            for(var i = 0, length = node.childNodes.length; i < length; i++)
            {
                var child = node.childNodes[i];
                var bIsBlockChild = this._IsBlockElem(child.nodeName.toLowerCase());
                if(true == bIsBlockChild)
                {
                    bRootHasBlock = true;
                    bExist = true;
                    break;
                }
            }
            if(false == bExist && true == this.bIgnoreNoBlockText)
                this.bIgnoreNoBlockText = false;
        }
        else
        {
            if(Node.TEXT_NODE == node.nodeType)
            {
                if(false == this.bIgnoreNoBlockText || true == bInBlock)
                {
                    var value = node.nodeValue;
                    if(!value)
                        value = "";
                    //������� � ����� �������� \r|\t|\n, � �������� ������ �������� �� �� �������
                    //������ ���(�������� ������ chrome ��� ������� ��������� ������ � ������� \n)
                    value = value.replace(/^(\r|\t|\n)+|(\r|\t|\n)+$/g, '') ;
                    value = value.replace(/(\r|\t|\n)/g, ' ');
                    if(value.length > 0)
                    {
                        bAddParagraph = this._Decide_AddParagraph(node.parentNode, pPr, bAddParagraph);

                        //��������� ������� ����� ���� �� ���������
                        this._commit_rPr(node.parentNode);
                        for(var i = 0, length = value.length; i < length; i++)
                        {
                            var Char = value.charAt(i);
                            var Code = value.charCodeAt(i);
                            var Item;
                            if(32 == Code || 160 == Code) //160 - nbsp
                                Item = new ParaSpace();
                            else
                                Item = new ParaText( value[i] );
                            this._Paragraph_Add( Item );
                        }
                    }
                }
                return bAddParagraph;
            }
            var sNodeName = node.nodeName.toLowerCase();
            if("table" == sNodeName)
            {
                if(g_bIsDocumentCopyPaste)
                {
                    this._StartExecuteTable(node, pPr);
                    return bAddParagraph;
                }
                else
                    return false;
            }

            //�������� �� html �������� ���������(�� ��� ������ �������� �� getComputedStyle)
            var style = node.getAttribute("style");
            if(style)
                this._parseCss(style, pPr);

            if("h1" == sNodeName)
                pPr.hLevel = 0;
            else if("h2" == sNodeName)
                pPr.hLevel = 1;
            else if("h3" == sNodeName)
                pPr.hLevel = 2;
            else if("h4" == sNodeName)
                pPr.hLevel = 3;
            else if("h5" == sNodeName)
                pPr.hLevel = 4;
            else if("h6" == sNodeName)
                pPr.hLevel = 5;

            if("ul" == sNodeName || "ol" == sNodeName || "li" == sNodeName)
            {
                pPr.bNum = true;
                if(g_bIsDocumentCopyPaste)
                {
                    if("ul" == sNodeName)
                        pPr.numType = numbering_numfmt_Bullet;
                    else if("ol" == sNodeName)
                        pPr.numType = numbering_numfmt_Decimal;
                }
                else
                {
                    if("ul" == sNodeName)
                        pPr.numType = numbering_presentationnumfrmt_Char;
                    else if("ol" == sNodeName)
                        pPr.numType = numbering_presentationnumfrmt_ArabicPeriod;
                }
            }

            if("img" == sNodeName)
            {
                if(g_bIsDocumentCopyPaste)
                {
                    bAddParagraph = this._Decide_AddParagraph(node, pPr, bAddParagraph);
                    
                    var nWidth = parseInt(node.getAttribute("width"));
                    var nHeight = parseInt(node.getAttribute("height"));
                    if(!nWidth || !nHeight)
                    {
                        var defaultView = node.ownerDocument.defaultView;
                        var computedStyle = defaultView.getComputedStyle( node, null );
                        if ( computedStyle )
                        {
                            nWidth = parseInt(computedStyle.getPropertyValue("width"));
                            nHeight = parseInt(computedStyle.getPropertyValue("height"));
                        }
                    }
                    var sSrc = node.getAttribute("src");
                    if(isNaN(nWidth) || isNaN(nHeight) || !(typeof nWidth === "number") || !(typeof nHeight === "number")
                        ||  nWidth === 0 ||  nHeight === 0)
                    {
                        var img_prop = new CImgProperty();
                        img_prop.put_ImageUrl(sSrc);
                        var or_sz = img_prop.get_OriginSize(editor);
                        nWidth = or_sz.Width / g_dKoef_pix_to_mm;
                        nHeight = or_sz.Height / g_dKoef_pix_to_mm;
                    }
                    if(nWidth && nHeight && sSrc)
                    {
                        var sSrc = this.oImages[sSrc];
                        if(sSrc)
                        {
                            nWidth = nWidth * g_dKoef_pix_to_mm;
                            nHeight = nHeight * g_dKoef_pix_to_mm;
                            //��������� � this.dMaxWidth
                            var bUseScaleKoef = this.bUseScaleKoef;
                            var dScaleKoef = this.dScaleKoef;
                            if(nWidth * dScaleKoef > this.dMaxWidth)
                            {
                                dScaleKoef = dScaleKoef * this.dMaxWidth / nWidth;
                                bUseScaleKoef = true;
                            }
                            if(bUseScaleKoef)
                            {
                                var dTemp = nWidth;
                                nWidth *= dScaleKoef;
                                nHeight *= dScaleKoef;
                            }
                            var oTargetDocument = this.oDocument;
                            var oDrawingDocument = this.oDocument.DrawingDocument;
                            if(oTargetDocument && oDrawingDocument)
                            {
                                var binary_shape = node.getAttribute("alt");
                                var sub;
                                if(typeof binary_shape === "string")
                                    sub = binary_shape.substr(0, 12);
                                if(typeof binary_shape === "string" &&( sub === "TeamLabShape" || sub === "TeamLabImage" || sub === "TeamLabChart" || sub === "TeamLabGroup"))
                                {
                                    var reader = CreateBinaryReader(binary_shape, 12, binary_shape.length);
                                    if(isRealObject(reader))
                                        reader.oImages = this.oImages;
                                    var first_string = null;
                                    if(reader !== null && typeof  reader === "object")
                                    {
                                        first_string = sub;
                                    }
                                    var Drawing;
                                    switch(first_string)
                                    {
                                        case "TeamLabImage":
                                        case "TeamLabChart":
                                        {
                                            Drawing = CreateParaDrawingFromBinary(reader);
                                            break;
                                        }
                                        case "TeamLabShape":
                                        case "TeamLabGroup":
                                        {
                                            if(this.oDocument.Is_TopDocument())
                                                Drawing = CreateParaDrawingFromBinary(reader);
                                            else
                                            {
                                                var cur_parent = this.oDocument;
                                                if(cur_parent.Is_TableCellContent())
                                                {
                                                    while(isRealObject(cur_parent) && cur_parent.Is_TableCellContent())
                                                    {
                                                        cur_parent = cur_parent.Parent.Row.Table.Parent;
                                                    }
                                                }
                                                if(cur_parent.Parent instanceof WordShape)
                                                {
                                                    Drawing = CreateImageFromBinary(sSrc);
                                                }
                                                else
                                                {
                                                    Drawing = CreateParaDrawingFromBinary(reader);
                                                }
                                            }

                                            break;
                                        }
                                        default :
                                        {
                                            Drawing = CreateImageFromBinary(sSrc);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    Drawing = CreateImageFromBinary(sSrc);
                                    // oTargetDocument.DrawingObjects.Add( Drawing );
                                }

                                this._Paragraph_Add( Drawing );

                                    //oDocument.Add_InlineImage(nWidth, nHeight, img);
                            }
                        }
                    }
                    else if (nWidth && nHeight && (/msie/i.test(navigator.userAgent)))
                    {
                        var binary_shape = node.getAttribute("alt");
                        if (typeof binary_shape === "string")
                        {
                            nWidth = nWidth * g_dKoef_pix_to_mm;
                            nHeight = nHeight * g_dKoef_pix_to_mm;
                            //��������� � this.dMaxWidth
                            var bUseScaleKoef = this.bUseScaleKoef;
                            var dScaleKoef = this.dScaleKoef;
                            if(nWidth * dScaleKoef > this.dMaxWidth)
                            {
                                dScaleKoef = dScaleKoef * this.dMaxWidth / nWidth;
                                bUseScaleKoef = true;
                            }
                            if(bUseScaleKoef)
                            {
                                var dTemp = nWidth;
                                nWidth *= dScaleKoef;
                                nHeight *= dScaleKoef;
                            }
                            var oTargetDocument = this.oDocument;
                            var oDrawingDocument = this.oDocument.DrawingDocument;
                            if(oTargetDocument && oDrawingDocument)
                            {
                                var sub;
                                if(typeof binary_shape === "string")
                                    sub = binary_shape.substr(0, 12);
                                if(typeof binary_shape === "string" &&( sub === "TeamLabShape" || sub === "TeamLabImage" || sub === "TeamLabChart" || sub === "TeamLabGroup"))
                                {
                                    var reader = CreateBinaryReader(binary_shape, 12, binary_shape.length);
                                    if(isRealObject(reader))
                                        reader.oImages = this.oImages;
                                    var first_string = null;
                                    if(reader !== null && typeof  reader === "object")
                                    {
                                        first_string = sub;
                                    }
                                    var Drawing;
                                    switch(first_string)
                                    {
                                        case "TeamLabImage":
                                        case "TeamLabChart":
                                        {
                                            Drawing = CreateParaDrawingFromBinary(reader);

                                            this._Paragraph_Add( Drawing );
                                            break;
                                        }
                                        case "TeamLabShape":
                                        case "TeamLabGroup":
                                        {
                                            if(this.oDocument.Is_TopDocument())
                                                Drawing = CreateParaDrawingFromBinary(reader);
                                            else
                                            {
                                                var cur_parent = this.oDocument;
                                                if(cur_parent.Is_TableCellContent())
                                                {
                                                    while(isRealObject(cur_parent) && cur_parent.Is_TableCellContent())
                                                    {
                                                        cur_parent = cur_parent.Parent.Row.Table.Parent;
                                                    }
                                                }
                                                if(cur_parent.Parent instanceof WordShape)
                                                {
                                                    Drawing = CreateParaDrawingFromBinary(reader);
                                                    var bounds = Drawing.getBounds();
                                                    Drawing =  CreateImageFromBinary2(Drawing.GraphicObj.getBase64Img(), bounds.r - bounds.l, bounds.b - bounds.t);
                                                }
                                                else
                                                {
                                                    Drawing = CreateParaDrawingFromBinary(reader);
                                                }
                                            }
                                            this._Paragraph_Add( Drawing );
                                            break;
                                        }
                                      /*  default :
                                        {
                                            Drawing = CreateImageFromBinary(sSrc);
                                            break;
                                        }  */
                                    }

                                }

                                //oDocument.Add_InlineImage(nWidth, nHeight, img);
                            }
                        }
                    }
                    return bAddParagraph;
                }
                else
                    return false;
            }

            //��������� linebreak, ���� �� �� ��������� ������� �������� � �� ����� ��� ������� �������
            if("br" == sNodeName || "always" == node.style.pageBreakBefore)
            {
                if("always" == node.style.pageBreakBefore)
                {
                    bAddParagraph = this._Decide_AddParagraph(node.parentNode, pPr, bAddParagraph);
                    bAddParagraph = true;
                    this._Commit_Br(0, node, pPr);
                    this._Paragraph_Add( new ParaNewLine( break_Page ) );
                }
                else
                {
                    bAddParagraph = this._Decide_AddParagraph(node.parentNode, pPr, bAddParagraph, false);
                    this.nBrCount++;//this._Paragraph_Add( new ParaNewLine( break_Line ) );
                    if("line-break" == pPr["mso-special-character"])
                        this._Commit_Br(0, node, pPr);
					return bAddParagraph;
                }
            }

            //�������� �� tab
            if("span" == sNodeName)
            {
                var nTabCount = parseInt(pPr["mso-tab-count"] || 0);
                if(nTabCount > 0)
                {
                    bAddParagraph = this._Decide_AddParagraph(node, pPr, bAddParagraph);
                    this._commit_rPr(node);
                    for(var i = 0; i < nTabCount; i++)
                        this._Paragraph_Add( new ParaTab() );
                    return bAddParagraph;
                }
            }
        }
        //���������� �������� ��� childNodes
        for(var i = 0, length = node.childNodes.length; i < length; i++)
        {
            var child = node.childNodes[i];
            var nodeType = child.nodeType;
            //��� ����������� �� word ����� ����������� ����������� �� �������
            //����������� ����������, ������ ������ ������
            if(Node.COMMENT_NODE == nodeType)
            {
                var value = child.nodeValue;
                var bSkip = false;
                if(value)
                {
                    if(-1 != value.indexOf("supportLists"))
                    {
                        //todo ���������� ��� ������
                        pPr.bNum = true;
                        bSkip = true;
                    }
                    if(-1 != value.indexOf("supportLineBreakNewLine"))
                        bSkip = true;
                }
                if(true == bSkip)
                {
                    //���������� ��� �� �������������� �����������
                    var j = i + 1;
                    for(; j < length; j++)
                    {
                        var tempNode = node.childNodes[j];
                        var tempNodeType = tempNode.nodeType;
                        if(Node.COMMENT_NODE == tempNodeType)
                        {
                            var tempvalue = tempNode.nodeValue;
                            if(tempvalue && -1 != tempvalue.indexOf("endif"))
                                break;
                        }
                    }
                    i = j;
                    continue;
                }
            }

            if(!(Node.ELEMENT_NODE == nodeType || Node.TEXT_NODE == nodeType))
                continue;
            //�������� ������� ��������� ������ �� \t,\n,\r
            if( Node.TEXT_NODE == child.nodeType)
            {
                var value = child.nodeValue;
                if(!value)
                    continue;
                value = value.replace(/(\r|\t|\n)/g, '');
                if("" == value)
                    continue;
            }
            var sChildNodeName = child.nodeName.toLowerCase();
            var bIsBlockChild = this._IsBlockElem(sChildNodeName);
            if(bRoot)
                this.bInBlock = false;
            if(bIsBlockChild)
            {
                bAddParagraph = true;
                this.bInBlock = true;
            }

            var bHyperlink = false;
            if("a" == sChildNodeName)
            {
                var href = child.href;
                if(null != href)
                {
                    var sDecoded;
                    //decodeURI ����� �������� malformed exception, ������ ��� ��� ���� � utf8, � ��������� ����� ����� ���������� url � ����� ���������(�������� windows-1251)
                    try
                    {
                        sDecoded = decodeURI(href);
                    }
                    catch(e) { sDecoded = href; }
                    href = sDecoded;
                    bHyperlink = true;
                    var title = child.getAttribute("title");

                    bAddParagraph = this._Decide_AddParagraph(child, pPr, bAddParagraph);
                    var oHyperlink = new ParaHyperlinkStart();
                    oHyperlink.Set_Value( href );
                    if(null != title)
                        oHyperlink.Set_ToolTip(title);
                    this._Paragraph_Add( oHyperlink );
                }
            }

            bAddParagraph = this._Execute(child, Common_CopyObj(pPr), false, bAddParagraph, bIsBlockChild || bInBlock);
            if(bIsBlockChild)
                bAddParagraph = true;
            if("a" == sChildNodeName && true == bHyperlink)
                this._Paragraph_Add( new ParaHyperlinkEnd() );
        }
        if(bRoot)
        {
            this._Commit_Br(2, node, pPr);//word ���������� 2 ��������� br
        }
        return bAddParagraph;
    }
};

function SafariIntervalFocus()
{
    if (window.editor && window.editor.WordControl && window.editor.WordControl.IsFocus && (!window.editor.WordControl.TextBoxInputFocus))
    {
        var pastebin = document.getElementById(COPY_ELEMENT_ID);
        if (pastebin)
            pastebin.focus();
        else
        {
            // create
            Editor_CopyPaste_Create(window.editor);
        }
    }
}

function Editor_CopyPaste_Create(api)
{
    var ElemToSelect = document.createElement("div");
    ElemToSelect.id = COPY_ELEMENT_ID;
    ElemToSelect.style.position = "absolute";

    ElemToSelect.style.left = '0px';
    ElemToSelect.style.top = '-100px';
    ElemToSelect.style.width = '10000px';
    ElemToSelect.style.height = '100px';
    ElemToSelect.style.overflow = 'hidden';
    ElemToSelect.style.zIndex = -1000;
    ElemToSelect.style.MozUserSelect = "text";
    ElemToSelect.style["-khtml-user-select"] = "text";
    ElemToSelect.style["-o-user-select"] = "text";
    ElemToSelect.style["user-select"] = "text";
    ElemToSelect.style["-webkit-user-select"] = "text";
    ElemToSelect.setAttribute("contentEditable", true);

    var Def_rPr = api.WordControl.m_oLogicDocument.Styles.Default.TextPr;
    ElemToSelect.style.fontFamily = Def_rPr.FontFamily.Name;

    if (!api.DocumentReaderMode)
        ElemToSelect.style.fontSize = Def_rPr.FontSize + "pt";
    else
    {
        api.DocumentReaderMode.CorrectDefaultFontSize(Def_rPr.FontSize);
        ElemToSelect.style.fontSize = "1em";
    }

    ElemToSelect.style.lineHeight = "1px";

    ElemToSelect.onpaste = function(e){
        if (!window.GlobalPasteFlag)
            return;

        // тут onpaste не обрубаем, так как он в сафари под macos приходить должен
        if (window.GlobalPasteFlagCounter == 1)
        {
            Body_Paste(api,e);

            if (window.GlobalPasteFlag)
                window.GlobalPasteFlagCounter = 2;
        }
    };

    var TIME_PREVIOS_ONBEFORE_EVENTS = new Date().getTime();
    // не разобравшись не править!
    // эти евенты приходят дважды. так как операции могут быть длительтными,
    // то смотрится время окончания предыдущего и начало следующего евента

    ElemToSelect["onbeforecut"] = function(e){
        var _time = new Date().getTime();
        if (_time - TIME_PREVIOS_ONBEFORE_EVENTS < 100)
            return;

        api.WordControl.m_oLogicDocument.Create_NewHistoryPoint();
        Editor_Copy(api,true);

        TIME_PREVIOS_ONBEFORE_EVENTS = new Date().getTime();
    };

    ElemToSelect["onbeforecopy"] = function(e){
        var _time = new Date().getTime();
        if (_time - TIME_PREVIOS_ONBEFORE_EVENTS < 100)
            return;

        Editor_Copy(api,false);

        TIME_PREVIOS_ONBEFORE_EVENTS = new Date().getTime();
    };

    document.body.appendChild( ElemToSelect );
}