<?php
//reset error reporting to eliminate advisory notices
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
//include top of the page
include_once('top.php');
//include magpieRSS
include_once('magpierss/rss_fetch.inc');

//constants
$TRIBE_TOPIC_PATH = 'http://diemaschinen.tribe.net/thread/';
//load rss
$tribe_rss = 'http://diemaschinen.tribe.net/rss';
$rss = fetch_rss($tribe_rss);

//proccess rss
$items = $rss->items;
$topics = array();
foreach($items as $item) {
	$post = array();
	$post['creator'] = $item['dc']['creator'];
	if(date('d/m/Y', time()) == date('d/m/Y', strtotime($item['dc']['date']))) {
		$post['date'] = date('g:iA', strtotime($item['dc']['date']));
	} else {
		$post['date'] = date('g:iA d/m/Y', strtotime($item['dc']['date']));
	}
	$post['link'] = $item['link'];
	$post['description'] = $item['description'];
	$post['topic_nr'] =  substr($item['link'], -73, -37);
	if(!$topics[$post['topic_nr']]) {
		$topics[$post['topic_nr']] = array();
	}
	if(!count($post['topic_nr']['posts'])) {
		$topics[$post['topic_nr']]['name'] = substr($item['title'], 0, 4) == 'Re: ' ? substr($item['title'], 4) : $item['title'];
	}
	if(substr($item['title'], 0, 4) == 'Re: ') {
	$topics[$post['topic_nr']]['name'] = substr($item['title'], 4);
	}
	
	if(!$topics[$post['topic_nr']]['posts']) {
		$topics[$post['topic_nr']]['posts'] = array();
	}
	array_push($topics[$post['topic_nr']]['posts'], $post);	
}
echo '<div id="tribe">';
foreach($topics as $key => $topic) {
	echo '<h3>&gt;&gt; <a href="'.$TRIBE_TOPIC_PATH.$key.'" target="tribe">'.$topic['name'].'</a> &lt;&lt;</h3><ul>';
	foreach($topic['posts'] as $post) {
		echo '<li><a href="'.$post['link'].'" target="tribe">'.$post['creator'].'</a><span class="date"> ['.$post['date'].']</span>';
		echo '<p>'.$post['description'].'</p>';		
		echo '</li>';
	}	
	echo '</ul>';
}
echo '</div>';
//include bottom
include_once('bottom.php');
?>