function [correlation,viaCorrelation]=ridgeCVvia(savePath,data,via,goal,k,ridgeParams,eegOffset)
% ridgeCVvia calculates correlation of ridgeRegression from 'dat' to 'via'
% and correlation going the next step to 'goal'
    load(savePath);
    clear classification;
    eeg=false;
    if strcmp(data,'EEG')
        eeg=true;
        trainingData=trainingDataEEG(eegOffset+1:end,:);
    elseif strcmp(data,'EMG')
        trainingData=trainingDataEMG;
        eegOffset=0; %no offset because EMG is used
    elseif strcmp(data,'LF')
        eeg=true;
        trainingData=trainingDataEEGlf(eegOffset+1:end,:);
    else
        error('only EEG, EMG and LF are valid inputs for data');
    end
    
    % if data is smaller then goal, goal is scaled down
    factor=size(trainingDataEMG,1)/(size(trainingData,1)+eegOffset);

    if strcmp(via,'Autoenc')
        viaData=shiftingMean(synergiesAutoenc,factor);
    elseif strcmp(via,'PCA')
        viaData=shiftingMean(synergiesPCA,factor);
    elseif strcmp(via,'NNMF')
        viaData=shiftingMean(synergiesNNMF,factor);
    else
        error('only Autoenc, PCA nad NNMF are valid inputs for via');
    end

    
    if strcmp(goal,'kin')
        predicted=shiftingMean(kinematics,factor);
    elseif strcmp(goal,'EMG')
        predicted=shiftingSum(trainingDataEMG,factor);
    else
        error('only kin and EMG are valid inputs for goal');
    end

    clear trainingDataEEG;
    clear trainingDataEEGlf;
    clear trainingDataEMG;
    clear kinematics;
    clear synergies*;

    % is eeg Offset applied?
    if eeg
        viaData=viaData(1:end-eegOffset,:);
        predicted=predicted(1:end-eegOffset,:);
    end
    
    %make sure data are of same size
    %disp([size(trainingData),size(viaData),size(predicted)])
    minSize=min([size(trainingData,1),size(viaData,1),size(predicted,1)]);
    if minSize+5<max([size(trainingData,1),size(viaData,1),size(predicted,1)])
	error('data dimensions not matching');
    end
    while minSize<size(trainingData,1)
        trainingData=trainingData(2:end,:);
    end
    while minSize<size(viaData,1)
        viaData=viaData(2:end,:);
    end
    while minSize<size(predicted,1)
        predicted=predicted(2:end,:);
    end
    %disp([size(trainingData),size(viaData),size(predicted)])
    
    viaCorr=zeros([k,size(viaData,2)]);
    finalCorr=zeros([k,size(predicted,2)]);
    
    randMap=randperm(size(trainingData,1));
    for i=1:k %k-fold CV
        leaveData=trainingData(mod(randMap,k)==i-1,:);
        leaveVia=viaData(mod(randMap,k)==i-1,:);
        leavePred=predicted(mod(randMap,k)==i-1,:);
        remainingData=trainingData(mod(randMap,k)~=i-1,:);
        remainingVia=viaData(mod(randMap,k)~=i-1,:);
        remainingPred=predicted(mod(randMap,k)~=i-1,:);
        viaDataPredicted=zeros(size(viaData));
        
        coeffs=zeros([size(trainingData,2)+1,size(viaData,2)]);
        for j=1:size(viaData,2)
            coeffs(:,j)=ridge(remainingVia(:,j),remainingData,ridgeParams(1),0);

            via=coeffs(1,j)+leaveData*coeffs(2:end,j);
            viaCorr(i,j)=corr(leaveVia(:,j),via);
            clear via
            viaDataPredicted(:,j)=coeffs(1,j)+trainingData*coeffs(2:end,j);
        end
        
        clear coeffs
        
        %Take same data as for data-> via
        viaDataPredictedLeave=viaDataPredicted(mod(randMap,k)==i-1,:);
        viaDataPredictedRemaining=viaDataPredicted(mod(randMap,k)~=i-1,:);

        for j=1:size(predicted,2)

            coeffs=ridge(remainingPred(:,j),viaDataPredictedRemaining,ridgeParams(1),0);

            finalCorr(i,j)=ridgeCorrelation(viaDataPredictedLeave,leavePred(:,j),coeffs);

        end
    end
    viaCorrelation=mean(viaCorr,1);
    correlation=mean(finalCorr,1);
end
