Zigzag Support and Resistance

Hi everyone!
Please could I get some help? I'm trying to plot support and resistance lines on the peaks and troughs of the zigzag function. These S/R lines will change with the percent change from Zig. Sorry I can't seem to work out where I'm going wrong here. Basically need it to plot where my plotshapes are plotting. Any help would be great.

Plot(C,"",3,64);
ShowSR	 = ParamToggle("Show Vert S/R","No|Yes", 1);
SRBack	 = Param("S/R Back", 5, 1,15,1);

P = ParamField( "Price field" );
change = Param("% change",5,0.1,25,0.1);
Plot( Zig(P, change), _DEFAULT_NAME(), colorOrange );

troughs     = Cross(Zig(P, change),Ref(Zig(P, change),-1));
peaks       = Cross(Ref(Zig(P, change),-1),Zig(P, change));

Trough_val  = ValueWhen(troughs,L);
peaks_val   = ValueWhen(peaks,H);

PlotShapes( IIf(Peaks, shapeCircle, shapeNone ), colorBlue, 0, H,20);
PlotShapes( IIf(troughs, shapeCircle, shapeNone ), colorYellow, 0, L,-20);

function GetXSupport(Trough_val, change, Back)
{
return ((BarCount - 1) - LastValue(TroughBars(Trough_val, change,Back)));
}
function GetYSupport(Trough_val, change, Back)
{
return (LastValue(Trough(Trough_val, change, back)));
}
function GetXResistance(peaks_val, change, Back)
{
return ((BarCount - 1) -LastValue(PeakBars(peaks_val, change, Back)));
}
function GetYResistance(peaks_val, change, Back)
{
return (LastValue(Peak(peaks_val, change, Back)));
}

if(ShowSR)
{
for(i=1; i<=SRBack; i++)
{
x0 = GetXSupport(Trough_val, change, i);
x1 = BarCount-1;
y0 = GetYSupport(Trough_val, change, i);
x = LineArray(x0, y0, x1, y0, 0);
Plot(x, "", IIf(LastValue(C) > x, colorCustom12,colorCustom11),
styleLine);
x0 = GetXResistance(peaks_val, change, i);
y0 = GetYResistance(peaks_val, change, i);
x = LineArray(x0, y0, x1, y0, 0);
Plot(x, "", IIf(LastValue(C) > x, colorCustom12,colorCustom11),
styleLine);
}
}