function Phase_Portrait_Figure(hndl)

% створюємо глобальну змінну TS, куди будуть записуватись дані
global kindOfMovingWindow; kindOfMovingWindow=0;
global windowMovingStep;windowMovingStep=0;
global windowWidth;windowWidth=0;
global I_Left;I_Left = 0;
global I_Right;I_Right = 0;
global numIR;numIR = 0;
global numIL;numIL = 0;
% визначаємо поточну розподільчу здатність, встановивши одиниці в PIXELS
set(0,'Units','pixels');
SCREEN_SIZE=get(0,'ScreenSize');
% SCREEN_SIZE = [left bottom width height]
SCRHeight=SCREEN_SIZE(1,4);

% створюємо фігуру розміром 300 x 300, що знаходиться у верхному лівому
% куті з відступом у 10 пікселів зверху та зліва
fg=figure('Position',[30,SCRHeight-330,470,300], 'Units','pixels', ...
          'HandleVisibility','on', 'MenuBar','none', 'Name','Динамический ФП', ...
          'Resize','off', 'Tag','Phase Portrait figure', ...
          'NumberTitle','off', 'DeleteFcn',@DeleteFcn_Callback);
      
% записуємо handle фігури
handles.rootFigure=fg;
% записуємо handle фігури, з якої відбувся виклик
handles.baseFigure=hndl;

% створюємо меню користувача
%menuRoot=uimenu('Parent',fg, 'Label','Файл', 'Tag', 'Menu File');
%menuTemp=uimenu('Parent',menuRoot, 'Label','Читать', 'Tag','Menu File Load', 'Callback',@MenuFileLoad_Callback);
      
% створюємо статичний текст та едіт для ширини вікна
handles.SeriesFirstListBox=uicontrol(fg, 'Style','listbox', 'Units','pixels','Position',[3,270,225,25], ...
          'String','', 'FontName','Times New Roman', ...
          'FontSize', 10, 'FontAngle', 'normal', 'FontWeight', 'normal', ...
          'Tag', 'ListBoxTS', 'ListboxTop', 5);

handles.SeriesSecondListBox=uicontrol(fg, 'Style','listbox', 'Units','pixels','Position',[240,270,225,25], ...
          'String','', 'FontName','Times New Roman', ...
          'FontSize', 10, 'FontAngle', 'normal', 'FontWeight', 'normal', ...
          'Tag', 'ListBoxTS', 'ListboxTop', 5);
      
      
uicontrol(fg, 'Style','text', 'Tag','', 'Units','pixels', 'Position',[90,230,225,25], ...
          'String','Начальная ширина окна:', ...
          'FontName','Times New Roman', 'FontSize', 12, 'FontAngle', 'normal', 'FontWeight', 'normal');
handles.beginWidthMovingWindowEdit=uicontrol(fg, 'Style','edit', 'Tag','beginWidthMovingWindowEdit', ...
          'BackgroundColor',[1.0 1.0 1.0], 'Units','pixels', 'Position',[300,230,72,25], ...
          'String','100', 'FontName','Courier New', ...
          'FontSize', 12, 'FontAngle', 'normal', 'FontWeight', 'normal');
% створюємо статичний текст та едіт для кроку вікна
handles.windowMovingText =uicontrol(fg, 'Style','text', 'Tag','', 'Units','pixels', 'Position',[90,205,225,25], ...
          'String','Шаг перемещения окна:', ...
          'FontName','Times New Roman', 'FontSize', 12, 'FontAngle', 'normal', 'FontWeight', 'normal');
handles.windowMovingEdit=uicontrol(fg, 'Style','edit', 'Tag','windowMovingEdit', ...
          'BackgroundColor',[1.0 1.0 1.0], 'Units','pixels', 'Position',[300,205,72,25], ...
          'String','10', 'FontName','Courier New', ...
          'FontSize', 12, 'FontAngle', 'normal', 'FontWeight', 'normal');
handles.LagText =uicontrol(fg, 'Style','text', 'Tag','', 'Units','pixels', 'Position',[90,180,225,25], ...
          'String','Временной лаг:', ...
          'FontName','Times New Roman', 'FontSize', 12, 'FontAngle', 'normal', 'FontWeight', 'normal');
handles.LagEdit=uicontrol(fg, 'Style','edit', 'Tag','LagEdit', ...
          'BackgroundColor',[1.0 1.0 1.0], 'Units','pixels', 'Position',[300,180,72,25], ...
          'String','1', 'FontName','Courier New', ...
          'FontSize', 12, 'FontAngle', 'normal', 'FontWeight', 'normal');
handles.kindOfMovingWindowPushbutton = uicontrol(fg,'Style','pushbutton','Tag','kindOfMovingWindowPushbutton','Units','pixels','Position',[150,105,170,45],...
           'String', 'Перемещение окна',...
           'FontName','Time New Roman','Fontsize',12,'FontAngle','normal','FontWeight','bold','Max', 1, 'Min', 0, 'Value', 0,'CallBack',@kindOfMovingWindowPushbutton_Callback);
handles.calcDynamicPPPushbutton=uicontrol(fg, 'Style','pushbutton', 'Tag','calcDynamicPPButton', ...
          'Units','pixels', 'Position',[150,25,170,45], 'String','Динамический ФП', ...
          'FontName','Times New Roman', 'FontSize', 12, 'FontAngle', 'normal', 'FontWeight', 'bold', ...
          'Callback',@calcDynamicPPButton_Callback);
      

guidata(fg,handles);

%handles=guidata(gcbo);
global SERIESINDEX
handlesBase=guidata(handles.baseFigure);
ListBoxTS=handlesBase.listbox1;
i = SERIESINDEX;
while (i>0)
    STRINGS=get(ListBoxTS,'String');
    set(handles.SeriesFirstListBox,'String',STRINGS);
    set(handles.SeriesSecondListBox,'String',STRINGS);
    i = i-1;
end


% =============================================================================================================================
% Функція, що виконується при закритті фігури
function DeleteFcn_Callback(h,eventdata,handles)

% очищення пам"яті, що використовувалась для локальних змінних
%global TSTemp;
%clear TSTemp;

% =============================================================================================================================
% якщо клацнули по кнопці ">>"
function kindOfMovingWindowPushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to kindOfMovingWindowPushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles=guidata(gcbo);
if (CmpStr(get(handles.kindOfMovingWindowPushbutton,'String'),'Перемещение окна')==0)
    set(handles.kindOfMovingWindowPushbutton,'String','Увеличение окна');
    set(handles.windowMovingText,'String','Шаг увеличения окна');
else
    set(handles.kindOfMovingWindowPushbutton,'String','Перемещение окна');
    set(handles.windowMovingText,'String','Шаг перемещения окна');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function calcDynamicPPButton_Callback(hObject, eventdata, handles)
% hObject    handle to plotDynamicPPButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global SERIES
global SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT
global I_Left;I_Left=0;
global I_Right;I_Right=0;
global numIR;numIR = 0;
global numIL;numIL = 0;

handles=guidata(gcbo);
handlesBase=guidata(handles.baseFigure);

hF = figure('Name', 'Dynamic Phase Portrait', 'NumberTitle', 'off','MenuBar','none','Tag','mainwin',...
    'DeleteFcn',@DeleteFcn_Callback);
handles.A = axes('NextPlot','replacechildren','Tag','mainax','Position', [0.1 0.2 0.8 0.7]);
handles.BtnLeftPlot = uicontrol('Style', 'pushbutton', ...
     'Position', [20 20 120 30],...
    'String', 'Moving right',...
    'Callback', @BtnLeftPlot_Callback);
handles.BtnLeftOneWPlot = uicontrol('Style', 'pushbutton', ...
     'Position', [145 10 120 30],...
    'String', 'To the right for 1 window',...
    'Callback', @BtnLeftOneWPlot_Callback);
handles.BtnRightPlot = uicontrol('Style', 'pushbutton', ...
     'Position', [400 20 120 30],...
    'String', 'Moving left',...
    'Callback', @BtnRightPlot_Callback);
handles.BtnRightOneWPlot = uicontrol('Style', 'pushbutton', ...
     'Position', [270 10 125 30],...
    'String', 'To the left for 1 window',...
    'Callback', @BtnRightOneWPlot_Callback);

handles.rootFigure=hF;

mnSizeGrid=uimenu('Label','Axes','Callback',@SizeGrid_Callback,'Tag','mnSizeGrid');

mnGrid=uimenu('Label','Grid','Tag','mnGrid');
  
mnXGrid=uimenu(mnGrid,'Label','XGrid','Callback',@XGrid_Callback,'Tag','mnXGrid');
  
mnYGrid=uimenu(mnGrid,'Label','YGrid','Callback',@YGrid_Callback,'Tag','mnYGrid');
cmnAxes=uicontextmenu('Tag','cmnAxes');
  
cmnXGrid=uimenu(cmnAxes,'Label','XGrid','Callback',@XGrid_Callback,'Tag','cmnXGrid');
  
cmnYGrid=uimenu(cmnAxes,'Label','YGrid','Callback',@YGrid_Callback,'Tag','cmnYGrid');
set(handles.A,'UIContextMenu',cmnAxes)
      
guidata(hF,handles); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function SizeGrid_Callback(hObject, eventdata, handles)
global SERIES
global SERIESNAMES
global SERIESINDEX
global SERIESTYPE

handles=guidata(gcbo);
handlesBase=guidata(handles.baseFigure);

index1=get(handles.SeriesFirstListBox,'Value');
index2=get(handles.SeriesSecondListBox,'Value');
X=SERIES{index1};
Y=SERIES{index2};
axis([min(X) max(X) min(Y) max(Y)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function BtnLeftPlot_Callback(hObject, eventdata, handles)
global SERIES
global SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT
global I_Left;I_Left=0;
global I_Right;I_Right=0;

handles=guidata(gcbo);
handlesBase=guidata(handles.baseFigure);

if (CmpStr(get(handles.kindOfMovingWindowPushbutton,'String'),'Перемещение окна')==0)
    kindOfMovingWindow=0;
else
    kindOfMovingWindow=1;
end
windowWidth=str2num(get(handles.beginWidthMovingWindowEdit,'String'));
windowMovingStep=str2num(get(handles.windowMovingEdit,'String'));
lag = str2num(get(handles.LagEdit,'String'));
index1=get(handles.SeriesFirstListBox,'Value');
index2=get(handles.SeriesSecondListBox,'Value');
X=SERIES{index1};
Y=SERIES{index2};
indexStart=1;
[m1,n1]=size(X);
[m2,n2]=size(Y);
if (n1==1) X=X'; [m1,n1]=size(X); end
if (n2==1) Y=Y'; [m2,n2]=size(Y); end
if ((n1~=n2)||(m1~=m2)) error('Sizes of series must be the same!'); end;
while (indexStart+windowWidth<n1)
    axis([min(X) max(X) min(Y) max(Y)]);
    XResTemp=X(1,indexStart:lag:indexStart+windowWidth-2);
    YResTemp=Y(1,indexStart+lag:lag:indexStart+windowWidth-2+lag);
    %for i = indexStart:indexStart+windowWidth-2
    %    plot(XResTemp(i),YResTemp(i),'or')
    %    hold on
    %end
    
    plot(XResTemp,YResTemp,'b');
    hold on
    if (kindOfMovingWindow==0)
        indexStart=indexStart+windowMovingStep;
    else
        windowWidth=windowWidth+windowMovingStep;
    end
        pause(0.01);
  end

hold off
clear XResTemp YResTemp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function BtnRightPlot_Callback(hObject, eventdata, handles)

global SERIES
global SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT
global I_Left;I_Left=0;
global I_Right;I_Right=0;

handles=guidata(gcbo);
handlesBase=guidata(handles.baseFigure);

if (CmpStr(get(handles.kindOfMovingWindowPushbutton,'String'),'Перемещение окна')==0)
    kindOfMovingWindow=0;
else
    kindOfMovingWindow=1;
end
windowWidth=str2num(get(handles.beginWidthMovingWindowEdit,'String'));
windowMovingStep=str2num(get(handles.windowMovingEdit,'String'));
lag = str2num(get(handles.LagEdit,'String'));

index1=get(handles.SeriesFirstListBox,'Value');
index2=get(handles.SeriesSecondListBox,'Value');
X=SERIES{index1};
Y=SERIES{index2};
indexStart=1;
[m1,n1]=size(X);
[m2,n2]=size(Y);
if (n1==1) X=X'; [m1,n1]=size(X); end
if (n2==1) Y=Y'; [m2,n2]=size(Y); end
if ((n1~=n2)||(m1~=m2)) error('Sizes of series must be the same!'); end;
indexStart=n1;

while ((indexStart - windowWidth)>1)
        axis([min(X) max(X) min(Y) max(Y)]);
        %XResTemp=X(1,indexStart:indexStart-windowWidth+2);
        %YResTemp=X(1,indexStart+1:indexStart-windowWidth+1);
        j=1;
        for i = indexStart:-lag:indexStart-windowWidth+2
            XResTemp(j)=X(i);
            YResTemp(j)=Y(i-1);
            j = j+1;
        end    
        plot(XResTemp,YResTemp,'r');
        hold on
        if (kindOfMovingWindow==0)
           indexStart=indexStart-windowMovingStep;
        else
           windowWidth=windowWidth-windowMovingStep;
        end
        pause(0.01);
  end
hold off
clear XResTemp YResTemp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function BtnLeftOneWPlot_Callback(hObject, eventdata, handles)

global SERIES
global SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT
global I_Left;
global I_Right;
%global numIL;
%global numIR; 

I_Left = I_Left+1;
handles=guidata(gcbo);
handlesBase=guidata(handles.baseFigure);

if (CmpStr(get(handles.kindOfMovingWindowPushbutton,'String'),'Перемещение окна')==0)
    kindOfMovingWindow=0;
else
    kindOfMovingWindow=1;
end
windowWidth=str2num(get(handles.beginWidthMovingWindowEdit,'String'));
windowMovingStep=str2num(get(handles.windowMovingEdit,'String'));

index1=get(handles.SeriesFirstListBox,'Value');
index2=get(handles.SeriesSecondListBox,'Value');
X=SERIES{index1};
Y=SERIES{index2};
indexStart=1;
[m1,n1]=size(X);
[m2,n2]=size(Y);
if (n1==1) X=X'; [m1,n1]=size(X); end
if (n2==1) Y=Y'; [m2,n2]=size(Y); end
if ((n1~=n2)||(m1~=m2)) error('Sizes of series must be the same!'); end;
indexStart=1;
indx=0;
%if ((numIL==1)&&(I_Right~=0)) I_Left = I_Right+1;end
while ((indx~=I_Left)&&((indexStart+windowWidth)<n1))
    %axis([min(X) max(X) min(X) max(X)]);
    XResTemp=X(1,indexStart:indexStart+windowWidth-2);
    YResTemp=Y(1,indexStart+1:indexStart+windowWidth-1);
    %for i = indexStart:indexStart+windowWidth-2
    %    plot(XResTemp(i),YResTemp(i),'or')
    %    hold on
    %end
    
    plot(XResTemp,YResTemp,'g');
    hold on
    if (kindOfMovingWindow==0)
        indexStart=indexStart+windowMovingStep;
    else
        windowWidth=windowWidth+windowMovingStep;
    end
        indx=indx+1;
  end

t = ['Window for part of series ' num2str(indexStart) ':' num2str(indexStart+windowWidth-1)];
title(t)
hold off
clear XResTemp YResTemp

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function BtnRightOneWPlot_Callback(hObject, eventdata, handles)

global SERIES
global SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT
global I_Right;
global I_Left;
%global numIR;
%global numIL;

%numIL = 1;
%numIR = numIR+1;
I_Right=I_Right+1;
handles=guidata(gcbo);
handlesBase=guidata(handles.baseFigure);

if (CmpStr(get(handles.kindOfMovingWindowPushbutton,'String'),'Перемещение окна')==0)
    kindOfMovingWindow=0;
else
    kindOfMovingWindow=1;
end
windowWidth=str2num(get(handles.beginWidthMovingWindowEdit,'String'));
windowMovingStep=str2num(get(handles.windowMovingEdit,'String'));

index1=get(handles.SeriesFirstListBox,'Value');
index2=get(handles.SeriesSecondListBox,'Value');
X=SERIES{index1};
Y=SERIES{index2};
indexStart=1;
[m1,n1]=size(X);
[m2,n2]=size(Y);
if (n1==1) X=X'; [m1,n1]=size(X); end
if (n2==1) Y=Y'; [m2,n2]=size(Y); end
if ((n1~=n2)||(m1~=m2)) error('Sizes of series must be the same!'); end;
indexStart=n1;
indx=0;
%if ((numIR==1)&&(I_Left~=0)) I_Right = I_Left-1;end
while ((indx~=I_Right)&&((indexStart-windowWidth)>1))
        %XResTemp=X(1,indexStart:indexStart-windowWidth+2);
        %YResTemp=X(1,indexStart+1:indexStart-windowWidth+1);
        %axis([min(X) max(X) min(X) max(X)]);
        j=1;
        for i = indexStart:-1:indexStart-windowWidth+2
            XResTemp(j)=X(i);
            YResTemp(j)=Y(i-1);
            j = j+1;
        end    
        plot(XResTemp,YResTemp,'r');
        hold on
        if (kindOfMovingWindow==0)
           indexStart=indexStart-windowMovingStep;
        else
           windowWidth=windowWidth-windowMovingStep;
        end
        indx=indx+1;
  end
  
  t = ['Window for part of series ' num2str(indexStart) ':' num2str(indexStart-windowWidth+1)];
  title(t)
  hold off
clear XResTemp YResTemp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function XGrid_Callback(src, evt)
   
handles=guihandles;
   
state=get(src,'Checked') 
   
if isequal(state,'off')
      
    set(handles.mnXGrid,'Checked','on')
    set(handles.cmnXGrid,'Checked','on')    
       
    set(gca,'XGrid','on')
else
      
    set(handles.mnXGrid,'Checked','off')
    set(handles.cmnXGrid,'Checked','off')    
       
    set(gca,'XGrid','off')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function YGrid_Callback(src, evt)
   
handles=guihandles;
  
state=get(src,'Checked')
  
if isequal(state,'off')
     
    set(handles.mnYGrid,'Checked','on')
    set(handles.cmnYGrid,'Checked','on')    
       
    set(gca,'YGrid','on')
else
       
    set(handles.mnYGrid,'Checked','off')
    set(handles.cmnYGrid,'Checked','off')    
      
    set(gca,'YGrid','off')
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
