It hovertexts the details fine, and has buttons for "first", "last" (those both work fine,) "previous" and "next" to playback entries in the message queue, but I've hit what seems an intractable problem with "previous" and "next". They will loop back to the other end of the queue as intended (eg: when there are 6 messages in the list and the current message is 1, 'previous' goes to 6, and when the current message is 6, 'next' goes to 1,) but they're not working within the range of the messages - "next" from message 1 is 1 again, previous from 6 is 6

Could any more experienced scripter (and that's pretty much everyone!) point me where the problem is? Because I've been just going in circles with it for hours (it's probably one tiny thing I've misinterpreted from the Wiki tutorial or scripting reference.) I'd be eternally grateful!
I apologise in advance for what is doubtless rather inelegant and clunky newbie coding generally

integer position;
integer totalnumber;
string avname;
list messageList = [" "]; // list containing messages, each definable by 'position'
list avList = [" "]; // which should match the 'position' of each name in this list
// since they're incremented in the same code block
default
{
state_entry()
{
llListen( 3, "", avname, "" );
llListen( 9, "", NULL_KEY, "" );
integer totalnumber = (llGetListLength(messageList) - 1);
llSetText((string)totalnumber + " messages recorded", <1.0,1.0,1.0>, 0.5);
}
// messages submitted on channel 3
// button prims communicate on channel 9
listen( integer channel, string name, key id, string message )
{
if (channel == 9) {
string messageName = llList2String(avList, position);
if ((string)message == "first"
{ // this button works fineinteger totalnumber = (llGetListLength(messageList) - 1);
integer position = 1;
llSetText("No. " + (string)position + " of " + (string)totalnumber, <1.0,1.0,1.0>, 0.5);
string specMessage = llList2String(messageList, position);
llSay (0, specMessage + " - (" + messageName + "
"
;}
else if ((string)message == "previous"
{integer totalnumber = (llGetListLength(messageList) - 1);
if (position = 1) position = totalnumber; //loop to last message
else position = position - 1; //xxx broken
string specMessage = llList2String(messageList, position);
llSetText("No. " + (string)position + " of " + (string)totalnumber, <1.0,1.0,1.0>, 0.5);
llSay (0, specMessage + " - (" + messageName + "
"
;}
else if ((string)message == "next"
{integer totalnumber = (llGetListLength(messageList) - 1);
if (position = totalnumber) position = 1; //loop to first message
else position = position + 1; // xxx broken
string specMessage = llList2String(messageList, position);
llSetText("No. " + (string)position + " of " + (string)totalnumber, <1.0,1.0,1.0>, 0.5);
llSay (0, specMessage + " - (" + messageName + "
"
;}
else if ((string)message == "last"
{ // this button works finestring messageName = llList2String(avList, position);
integer totalnumber = (llGetListLength(messageList) - 1);
position = totalnumber;
llSetText("No. " + (string)position + " of " + (string)totalnumber, <1.0,1.0,1.0>, 0.5);
string specMessage = llList2String(messageList, position);
llSay (0, specMessage + " - (" + messageName + "
"
;}
}
else if (channel == 3) { // this seems to work fine
string messageName = llKey2Name(id);
avList = avList + messageName;
llSay( 0, "Thankyou, " + (string)messageName + ", your message has been saved." );
messageList = messageList + [message];
integer totalnumber = (llGetListLength(messageList) - 1);
position = totalnumber;
llSetText("No. " + (string)position + " of " + (string)totalnumber, <1.0,1.0,1.0>, 0.5);
}
}
touch_start(integer total_number)
{
// string specMessage = llList2String(messageList, position);
// llSay (0, (string)messageList);
// llSay (0, (string)specMessage);
}
}