function XBRV_StartVideo(varargin)
%XBRV_STARTVIDEO  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   XBRV_StartVideo(varargin)
%
%   Input:
%   varargin =
%
%
%
%
%   Example
%   XBRV_StartVideo
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2009 Deltares
%       Pieter van Geer
%
%       Pieter.vanGeer@Deltares.nl	
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://OpenEarth.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 27 Nov 2009
% Created with Matlab version: 7.7.0.471 (R2008b)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%% retrieve info
h = guidata(varargin{1});
XB = get(h.fig,'UserData');
XB = XB(h.plotspec.currentdataset);
ddd = h.plotspec.ddd;
if ddd
    id = ':';
else
    id = h.plotspec.nyid;
end

%% process input
nr = varargin{end};

%% clear axes and handles
cla(h.plotax);
[h.plothan.hw,...
   h.plothan.hb,...
   h.plothan.hpw,...
   h.plothan.hpb,...
   h.plothan.hpwleg,...
   h.plothan.hpbleg,...
   h.plothan.hrms1,...
   h.plothan.hrms2,...
   h.plothan.hrmsleg,...
   h.plothan.hbwleg] = deal([]);

%% calculate plotlimits
minn = min(min(min(XB.Output.zb(:,id,:))));

%% set xlim
if ~isempty(h.plotspec.xlim)
    xlimm = h.plotspec.xlim;
else
    xlimm = [min(min(XB.Output.x(:,id))) max(max(XB.Output.x(:,id)))];
end

%% set zlim
if ~isempty(h.plotspec.zlim)
    zlimm = h.plotspec.zlim;
else
    limms = [min(min(min(XB.Output.zb(:,id,:)))) max(max(max(XB.Output.zb(:,id,:))))];
    zlimm = [min(limms)-diff(limms)*0.1 max(limms)+diff(limms)*0.1];
end

%% set ylim
if ddd
    if ~isempty(h.plotspec.ylim)
        ylimm = h.plotspec.ylim;
    else
        ylimm = [min(min(XB.Output.y(:,id))) max(max(XB.Output.y(:,id)))];
    end
end

if ddd
    set(h.plotax,...
        'xlim',xlimm,...
        'ylim',ylimm,...
        'zlim',zlimm);
else
    set(h.plotax,...
        'xlim',xlimm,...
        'ylim',zlimm);
end

%% set other axes properties
set(h.plotax,'Color','none');
xlabel(h.plotax,'Cross-shore distance [m]');
ylabel(h.plotax,'height [m]');
title(h.plotax,['it = ' num2str(nr)]);

%% retrieve axes handles
infoax = h.infoax;
plotax = h.plotax;

%% plot 3D video components
if ddd
    view(plotax,3);

    h.plothan.hpw = surf(XB.Output.x,XB.Output.y,XB.Output.zs(:,id,nr),...
        'FaceColor',h.plotspec.WaterColor,...
        'EdgeColor',h.plotspec.WaterEdgeColor,...
        'EdgeAlpha',0.1,...
        'Parent',h.plotax);
    h.plothan.hpb = surf(XB.Output.x,XB.Output.y,XB.Output.zb(:,id,nr),...
        'FaceColor',h.plotspec.SedimentColor,...
        'EdgeColor',h.plotspec.SedimentEdgeColor,...
        'EdgeAlpha',0.1,...
        'Parent',h.plotax);

    h.plothan.hpwleg = surf(XB.Output.x,XB.Output.y,XB.Output.zs(:,id,nr),...
        'FaceColor',h.plotspec.WaterColor,...
        'EdgeColor',h.plotspec.WaterEdgeColor,...
        'EdgeAlpha',0.1,...
        'Parent',h.infoax,...
        'Visible','off');
    h.plothan.hpbleg = surf(XB.Output.x,XB.Output.y,XB.Output.zb(:,id,nr),...
        'FaceColor',h.plotspec.SedimentColor,...
        'EdgeColor',h.plotspec.SedimentEdgeColor,...
        'Parent',h.infoax,...
        'Visible','off',...
        'EdgeAlpha',0.1);
    
    h.plothan.hinitzb = surf(XB.Output.x,XB.Output.y,XB.Output.zb(:,:,1),...
        'FaceColor',h.plotspec.SedimentColor,...
        'EdgeColor',h.plotspec.SedimentEdgeColor,...
        'Parent',plotax,...
        'Visible','on',...
        'EdgeAlpha',0.1,...
        'FaceAlpha',0.5);
    
    h.plothan.hw = [];
    h.plothan.hb = [];
else
%% plot 2D components
    h.plothan.hpwleg = patch([min(XB.Output.x(:,id)); XB.Output.x(:,id); max(XB.Output.x(:,id))],[minn; XB.Output.zs(:,id,nr); minn],...
        h.plotspec.WaterColor,...
        'EdgeColor','none',...
        'Visible','off',...
        'Parent',infoax);
    h.plothan.hpbleg = patch([min(XB.Output.x(:,id)); XB.Output.x(:,id); max(XB.Output.x(:,id))],[minn; XB.Output.zb(:,id,nr); minn],...
        h.plotspec.SedimentColor,...
        'EdgeColor','none',...
        'Visible','off',...
        'Parent',infoax);

    h.plothan.hpw = patch([min(XB.Output.x(:,id)); XB.Output.x(:,id); max(XB.Output.x(:,id))],[minn; XB.Output.zs(:,id,nr); minn],...
        h.plotspec.WaterColor,...
        'EdgeColor','none',...
        'Parent',plotax);
    h.plothan.hw = plot(XB.Output.x(:,id),XB.Output.zs(:,id,nr),...
        'Color',h.plotspec.WaterEdgeColor,...
        'LineWidth',2,...
        'Parent',plotax);
    h.plothan.hpb = patch([min(XB.Output.x(:,id)); XB.Output.x(:,id); max(XB.Output.x(:,id))],[minn; XB.Output.zb(:,id,nr); minn],...
        h.plotspec.SedimentColor,...
        'EdgeColor','none',...
        'Parent',plotax);
    h.plothan.hb = plot(XB.Output.x(:,id),XB.Output.zb(:,id,nr),...
        'Color',h.plotspec.SedimentEdgeColor,...
        'LineWidth',2,...
        'parent',plotax);
    h.plothan.hinitzb = plot(XB.Output.x(:,id),XB.Output.zb(:,id,1),...
        'Color',h.plotspec.SedimentEdgeColor,...
        'LineWidth',2,...
        'Parent',plotax);
end

%% construct legend
h.plothan.hleg = [h.plothan.hpbleg, h.plothan.hpwleg];
h.plothan.legnames = {'Sediment','Water (including long waves)'};

%% plot hrms if needed
if (isfield(XB.Output,'Hrms') || isfield(XB.Output,'H'))&& ~ddd && h.plotspec.hrmson
    if isfield(XB.Output,'Hrms')
        H = XB.Output.Hrms;
    else
        H = XB.Output.H;
    end
    h.plotspec.thin = round(size(XB.Output.x,1)/h.plotspec.nthin);
    h.plothan.hrmsleg = plot(XB.Output.x(1:h.plotspec.thin:end,2),H(1:h.plotspec.thin:end,1)./2+XB.Output.zs(1:h.plotspec.thin:end,nr),...
        'Marker',h.plotspec.HrmsMarker,...
        'LineStyle',h.plotspec.LineStyle,...
        'LineWidth',h.plotspec.HrmsEdgeWidth,...
        'Color',h.plotspec.HrmsEdgeColor,...
        'MarkerFaceColor',h.plotspec.HrmsFaceColor,...
        'Parent',infoax,...
        'Visible','off');
    h.plothan.hleg = [h.plothan.hleg, h.plothan.hrmsleg];
    h.plothan.legnames{end+1} = 'Hrms - range';
        
    h.plothan.hrms1 = plot(XB.Output.x(1:h.plotspec.thin:end,2),H(1:h.plotspec.thin:end,1)./2+XB.Output.zs(1:h.plotspec.thin:end,nr),...
        'Marker',h.plotspec.HrmsMarker,...
        'LineStyle',h.plotspec.LineStyle,...
        'LineWidth',h.plotspec.HrmsEdgeWidth,...
        'Color',h.plotspec.HrmsEdgeColor,...
        'Parent',plotax,...
        'MarkerFaceColor',h.plotspec.HrmsFaceColor);
    h.plothan.hrms2 = plot(XB.Output.x(1:h.plotspec.thin:end,2),-H(1:h.plotspec.thin:end,1)./2+XB.Output.zs(1:h.plotspec.thin:end,nr),...
        'Marker',h.plotspec.HrmsMarker,...
        'LineStyle',h.plotspec.LineStyle,...
        'LineWidth',h.plotspec.HrmsEdgeWidth,...
        'Color',h.plotspec.HrmsEdgeColor,...
        'Parent',plotax,...
        'MarkerFaceColor',h.plotspec.HrmsFaceColor);
end

%% create legend
axes(infoax);
axis off
legend(h.plothan.hleg,h.plothan.legnames,'Location','North');
axes(plotax);

%% set controls
set(h.control.slider,'max',XB.Output.nt,...
    'min',1,...
    'Value',nr,...
    'SliderStep',[1/XB.Output.nt (1/20)]);

set(h.control.startpause,'string','play');

%% store handle information
guidata(h.fig,h);