function [plateau_onset,longest_plateau]=find_riseplateau(start_point,peak_point,raw_event)
%description: using the EPSC onset time and peak time, presumably obtained from curve fitting, this program will find the longest plateau during the rise phase
%called by: simulated_psc_in_noise
%calls: 
%notes: the 'first method' is a relic, retained in comment form for archaeological purposes
%code confirmed date: july 17, 2012

%first method:  find the two points during the rising phase st the later point is greater (less negative) than the former point, separated by the greatest interval 
% 	for j=taperlength:min_ind
% %         gg=ntemplate(j)
%         afterz=ntemplate((j+1):min_ind);
% %         greaterz=afterz>ntemplate(j)
% %         bin_greaterz=find(afterz>ntemplate(j))
%         long_bin_greaterz=max(find(afterz>ntemplate(j)));
%         if long_bin_greaterz>mm,mm=long_bin_greaterz;end
% 	end

%second method: among rise phase samples, find the longest stretch of points whose group mean is not more negative than than the first value in the group
plateau_onset=0;
longest_plateau=0;
current_plateau_length=0;
for j=start_point:peak_point-1
    for k=peak_point:-1:(j+1)
        if mean(raw_event(j:k))>=raw_event(j)   %the sign here depends critically on whether the template is negative or positive
            current_plateau_length=k-j;
            break
        end
    end
    if current_plateau_length>longest_plateau  %if the current plateau is *as long as* the previous record holder, primacy wins and the previous record holder's onset is the onset that is returned
        plateau_onset=j;
        longest_plateau=current_plateau_length;
    end
    if longest_plateau>(peak_point-(j+2)) %adding 1 for the fact that we're referring to the next cycle of j, and 1 more b/c having *the same* plateau length next time around wouldn't matter, would need to be longer to make any difference
        break    
    end
end