Script Hookcontainer not working when numbers to keep in history > 0

I have been using this script for some years now to create a hookcontainer just after the Top of the Hour:

[code]var
i, iItemCount, iMax, iVol, iToth: integer;
i64Duration: TTimeValue;
sArtist : string;
sMessage, sPlaylist, sTitle : string;
currentItem, ItemHook, ItemHook2: IPlaylistItem;
ads: IContainerPlaylistItem;
adsPlaylist, newPlaylist: IPlaylist;
adsContainerAsPlaylistItem, piNews: IPlaylistItem;
sFileName, sOpener, sCloser, sMiddle: string;
i64HookIn, i64HookFade, i64HookOut: TTimeValue;

const SONG_MIN_LENGTH = 900000000;

procedure startMarkup;
begin

for i := 0 to Playlist(0).GetCount -1 do
begin

if Playlist(0).GetItem(i).GetTitle = ‘Top of the hour’ then begin
iToth := i;
SystemLog(Playlist(0).GetItem(i).GetTitle);
break;
end;
end;

end;

procedure MarkupCurrentPlaylist;
begin

// Set values (REPLACE THESE WITH YOUR OWN)
sOpener := ‘M:\Jingle\NFM\NFM Selected with Passion Mixdown 1.mp3’;
sMiddle := ‘M:\Jingle\Soundeffect\BBC Bleep 2.mp3’;
sCloser := ‘M:\Jingle\NFM\NFM 2014 Jingles 4a.mp3’;

// Reset counters etc.
iMax := iItemCount - 1;
iVol := 0;

// Display our start up message
SystemLog('Hook: ’
+ ‘Checking the playlist, looking for songs with Hooks.’);

ads.GetPlaylist.InsertFile(0, sOpener, [fitMMD, fitTags, fitNativeTags, fitDuration, fitTilde, fitAutoCue, fitDatabaseLookup]);

// Step through each item in the current (selected) Playlist
for i := iToth + 2 to Playlist(0).GetCount -1 do
begin

// Create a local reference to the current item
// This makes all subsequent code easier to read and slightly faster to process
currentItem := Playlist(0).GetItem(i);

// Store the current item’s Artist, Title, and Effective Duration
sArtist := currentItem.GetArtist;
sTitle := currentItem.GetTitle;
i64Duration := currentItem.GetDuration;

// Is this a song?
if currentItem.IsCueable then
begin

// Add the song to the container and set it to a hook.

ItemHook := iPlaylistItem(currentItem.Clone);

i64HookIn := ItemHook.GetCuePosition(ptHookIn);
i64HookFade := ItemHook.GetCuePosition(ptHookFade);
i64HookOut := ItemHook.GetCuePosition(ptHookOut);

if (i64HookIn > 0) then
begin
// SystemLog(‘Added’);
ads.GetPlaylist.Add(ItemHook);
if (iVol < 2) then
begin
ads.GetPlaylist.InsertFile(ads.GetPlaylist.GetCount, sMiddle, [fitMMD, fitTags, fitNativeTags, fitDuration, fitTilde, fitAutoCue, fitDatabaseLookup]);
end;
ItemHook.SetCuePosition(ptCueIn, i64HookIn);
ItemHook.SetCuePosition(ptFadeOut, i64HookFade);
ItemHook.SetCuePosition(ptStartNext, i64HookFade);
ItemHook.SetCuePosition(ptCueOut, i64HookOut);
ItemHook.SetArtist('Coming up: '+ currentItem.GetArtist);
iVol := iVol + 1
end;

if (iVol = 3) then
begin
break;
end;

SystemLog('Hook: ’ + ‘Item ’
+ IntToStr(i + 1)
+ ’ (’
+ Trim(currentItem.GetTitle)
+ ’ / ’
+ Trim(currentItem.GetArtist)
+ ') ’
+ ‘.’);

end

end; // of for… loop

// All Playlist items processed, inform user

 if (ivol < 3) then
 begin
 ads.GetPlaylist.Delete(ads.GetPlaylist.GetCount -1);
 end;

ads.GetPlaylist.InsertFile(ads.GetPlaylist.GetCount, sCloser, [fitMMD, fitTags, fitNativeTags, fitDuration, fitTilde, fitAutoCue, fitDatabaseLookup]);

SystemLog('Hook: ’ + ‘Checking done.’);

end;

begin

// Create a new Container Item
ads := Factory.CreateContainerPlaylistItem;

// create a copy of the reference as an IPlaylistItem interface

adsContainerAsPlaylistItem := IPlaylistItem(ads);
startMarkup;
MarkupCurrentPlaylist;

adsContainerAsPlaylistItem.SetTitle(‘Coming Up Next’);

Playlist(0).Insert(iToth + 1, adsContainerAsPlaylistItem);

end.[/code]

Recently I discovered the option to show the last played items in the playlist, I increased “0” to “3”. When doing so, the Hookcontainer is placed at the top of the playlist instead of straight after the Top of the Hour. Meaning it won’t be aired. It doesn’t make a difference whether the just played items are shown or not, also it doesn’t make a difference changing the number to “1” or “2”, as long as the number is > “0” the container is placed at the top of the playlist.

Does the script need to be adjusted or is this a minor bug?