function varargout = show_calls(varargin)
% SHOW_CALLS M-file for show_calls.fig
%      SHOW_CALLS, by itself, creates a new SHOW_CALLS or raises the existing
%      singleton*.
%
%      H = SHOW_CALLS returns the handle to a new SHOW_CALLS or the handle to
%      the existing singleton*.
%
%      SHOW_CALLS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SHOW_CALLS.M with the given input arguments.
%
%      SHOW_CALLS('Property','Value',...) creates a new SHOW_CALLS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before show_calls_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to show_calls_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help show_calls

% Last Modified by GUIDE v2.5 05-May-2005 13:06:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @show_calls_OpeningFcn, ...
    'gui_OutputFcn',  @show_calls_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before show_calls is made visible.
function show_calls_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to show_calls (see VARARGIN)

% Choose default command line output for show_calls
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes show_calls wait for user response (see UIRESUME)
% uiwait(handles.figure1);

handles.prefs=varargin{1};



for n=1:length(handles.prefs.filenames)
    filename=strcat(handles.prefs.proj_dir,'seg_data_',strrep(handles.prefs.filenames{n},'.wav','')); 
    load(filename)
    if n==1
        handles.disp_data=seg_data.reg_arr(:,1:2);
        handles.disp_data(:,3)=n;
    else
        seg_data.reg_arr(:,3)=n;
        handles.disp_data=cat(1,handles.disp_data,seg_data.reg_arr(:,1:3));
    end
end



ValCall = [handles.prefs.CO handles.prefs.CN handles.prefs.CA handles.prefs.CB ...
        handles.prefs.CC handles.prefs.CD handles.prefs.CE handles.prefs.CF handles.prefs.CG handles.prefs.CH];

handles.sct_var=find(ValCall);

filename = strcat(handles.prefs.proj_dir,'Class.mat')
exist(filename)
if exist(filename)
    load(filename)
else
    num_calls = handles.prefs.NoOfCalls;
    call_class = ones(num_calls,1);
end

handles.data_class = call_class;

n_t = 1;

    for n=1:size(handles.disp_data,1)
    
        hold_class = handles.data_class(n); 
        if ValCall(hold_class) == 1
            if n_t == 1
                hold_data = handles.disp_data(n,1:3);
                handles.call_no(n_t) = n;
                n_t = n_t+1;
            else
                hold_data = cat(1,hold_data,handles.disp_data(n,1:3));
                handles.call_no(n_t) = n;
                n_t = n_t+1;
            end
         end
    end
    
    % set axes handles
    handles.AH(1)=handles.axes1;
    handles.AH(2)=handles.axes2;
    handles.AH(3)=handles.axes3;
    handles.AH(4)=handles.axes4;
    handles.AH(5)=handles.axes5;
    handles.AH(6)=handles.axes6;
    handles.AH(7)=handles.axes7;
    handles.AH(8)=handles.axes8;
    handles.AH(9)=handles.axes9;
    handles.AH(10)=handles.axes10;
    handles.AH(11)=handles.axes11;
    handles.AH(12)=handles.axes12;
    handles.AH(13)=handles.axes13;
    handles.AH(14)=handles.axes14;
    handles.AH(15)=handles.axes15;
    handles.AH(16)=handles.axes16;
    
if size(hold_data,1) ~= 0   
    handles.disp_data = [];
    handles.disp_data = hold_data;
    
    handles.no_of_screens=ceil(size(handles.disp_data,1)/16);
    handles.start_no = handles.call_no(1);

    handles.screen_no=1;

    if size(handles.call_no,2)>((handles.screen_no-1)*16 + 16)
        handles.axes_assn=handles.call_no((handles.screen_no-1)*16+1:(handles.screen_no-1)*16+16);
    else
        handles.axes_assn=handles.call_no((handles.screen_no-1)*16+1:size(handles.call_no,2));
    end

    refresh_show_calls(handles,hObject)
    guidata(hObject, handles);
else
    for k=1:16
       axes(handles.AH(k));
       cla; 
       axis off;
       title('')
       set(handles.AH(k),'Visible','off');
    end
    
    dial.text1='Error: No calls selected';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
end

% --- Outputs from this function are returned to the command line.
function varargout = show_calls_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in next.
function next_Callback(hObject, eventdata, handles)
% hObject    handle to next (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if handles.screen_no+1<=handles.no_of_screens
    handles.screen_no=handles.screen_no+1;
    if size(handles.call_no,2)>((handles.screen_no-1)*16 + 16)
        handles.axes_assn=handles.call_no((handles.screen_no-1)*16+1:(handles.screen_no-1)*16+16);
    else
        handles.axes_assn=handles.call_no((handles.screen_no-1)*16+1:size(handles.call_no,2));
    end
    
    refresh_show_calls(handles,hObject)
    guidata(hObject, handles);
end

% --- Executes on button press in previous.
function previous_Callback(hObject, eventdata, handles)
% hObject    handle to previous (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if handles.screen_no-1>0
    handles.screen_no=handles.screen_no-1;
    if size(handles.call_no,2)>((handles.screen_no-1)*16 + 16)
        handles.axes_assn=handles.call_no((handles.screen_no-1)*16+1:(handles.screen_no-1)*16+16);
    else
        handles.axes_assn=handles.call_no((handles.screen_no-1)*16+1:size(handles.call_no,2));
    end
    
    refresh_show_calls(handles,hObject)
    guidata(hObject, handles);
end

% --- Executes on button press in done.
function done_Callback(hObject, eventdata, handles)
% hObject    handle to done (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%bob = handles.prefs

close(handles.output)
%cd(handles.prefs.func_dir)
main(handles.prefs)
