+
+ /** Return root message of the given thread
+ * @param id INTEGER id of a message
+ */
+ function root($id)
+ {
+ $id_cur = $id;
+ while (true) {
+ $id_parent = $this->overview[$id_cur]->parent;
+ if (is_null($id_parent)) break;
+ $id_cur = $id_parent;
+ }
+ return $id_cur;
+ }
+
+ /** Returns previous thread root index
+ * @param id INTEGER message number
+ */
+ function prevThread($id)
+ {
+ $root = $this->root($id);
+ $last = null;
+ foreach ($this->roots as $i) {
+ if ($i == $root) {
+ return $last;
+ }
+ $last = $i;
+ }
+ return $last;
+ }
+
+ /** Returns next thread root index
+ * @param id INTEGER message number
+ */
+ function nextThread($id)
+ {
+ $root = $this->root($id);
+ $ok = false;
+ foreach ($this->roots as $i) {
+ if ($ok) {
+ return $i;
+ }
+ if ($i == $root) {
+ $ok = true;
+ }
+ }
+ return null;
+ }
+
+ /** Return prev post in the thread
+ * @param id INTEGER message number
+ */
+ function prevPost($id)
+ {
+ $parent = $this->overview[$id]->parent;
+ if (is_null($parent)) {
+ return null;
+ }
+ $last = $parent;
+ foreach ($this->overview[$parent]->children as $child) {
+ if ($child == $id) {
+ return $last;
+ }
+ $last = $child;
+ }
+ return null;
+ }
+
+ /** Return next post in the thread
+ * @param id INTEGER message number
+ */
+ function nextPost($id)
+ {
+ if (count($this->overview[$id]->children) != 0) {
+ return $this->overview[$id]->children[0];
+ }
+
+ $cur = $id;
+ while (true) {
+ $parent = $this->overview[$cur]->parent;
+ if (is_null($parent)) {
+ return null;
+ }
+ $ok = false;
+ foreach ($this->overview[$parent]->children as $child) {
+ if ($ok) {
+ return $child;
+ }
+ if ($child == $cur) {
+ $ok = true;
+ }
+ }
+ $cur = $parent;
+ }
+ return null;
+ }