Try this sample code -
Basically, the next page is always one more than the current page. When it becomes greater than the maximum (6 in your case) wrap this around to the starting page. Going the other direction, the previous page is always one less than the current page. When it becomes less than the starting page (1) wrap this around to the maximum page.