<?php 
    
// original Last Modified date: 2013-01-21 17:52:58.222385900
    
if (isset($_GET["source"]) || true) {
        
highlight_file(__FILE__);
        exit;
    }
    
    if (isset(
$_GET["getupdate"])) {
        
$data = file_get_contents("http://www.gmot.nl/?topic=" . intval($_GET["topic"]) . "&action=.xml");
        
        
$parser = xml_parser_create();
        
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
        
xml_parse_into_struct($parser, $data, $values);
        
xml_parser_free($parser);
        
        
$posts = array();
        
        foreach (
$values as $value) {
            switch (
$value["tag"]) {
                case 
"body":
                    if (!
$got_all)
                        
$posts[count($posts) - 1]["body"] = $value["value"];
                    break;
                case 
"id":
                    if (!
$got_all)
                        
$posts[count($posts) - 1]["id"] = $value["value"];
                    break;
                case 
"name":
                    if (
$got_all == 1)
                        
$posts[count($posts) - 1]["name"] = $value["value"];
                    
$got_all++;
                    break;
                case 
"recent-post":
                    if (
$value["type"] == "open") {
                        
$got_all = 0;
                        
$posts[] = array();
                    }
                    break;
            }
        }
        
        
$posts = array_reverse($posts);
        
        echo 
json_encode($posts);
        exit;
    }
    if (!
is_numeric($_GET["topic"])) {
        echo 
"Je ziet nu alle berichten op het forum. Zet ?topic=[topicid] achter de URL om maar van één topic berichten te zien.";
    }
?>
<noscript>Geen Javascript in je browser ingeschakeld, en je verwacht dat dit werkt..?</noscript>
<div id="reply"></div>
<div id="info"></div>
<div id="posts"></div>
<div id="ploeng" style="position: absolute; left: -1000;"></div>
<script src="/rp/js/lib.js"></script>
<script>
    var update_rate = 6000; // Miliseconds between updates
    
    var topic = <?php echo intval($_GET["topic"]); ?>;
    var update_started_time = -1;
    var first_update_time = -1;
    var last_post_id = -1;
    var last_post_time = -1;
    var posts = {};
    
    function update() {
        update_started_time = new Date().getTime();
        aGET("topic-as-chat.php?getupdate&topic=" + topic + "&r=" + Math.random(), update2);
    }
    function update2(data) {
        data = eval(data);
        
        i = 0;
        //alert(parseInt(data[i]["id"]) + " > " + last_post_id);
        while (i < 5) {
            if (parseInt(data[i]["id"]) > last_post_id) {
                if (first_update_time != -1)
                    ploeng();
                append_to_chat("<b>" + data[i]["name"] + ":</b> <span class='post" + data[i]["id"] + "'>" + data[i]["body"] + "</span>");
                last_post_id = parseInt(data[i]["id"]);
                last_post_time = new Date().getTime();
                posts[data[i]["id"]] = data[i]["body"];
            }
            else {
                if (posts[data[i]["id"]] != data[i]["body"]) {
                    document.getElementsByClassName("post" + data[i]["id"])[0].innerHTML = data[i]["body"];
                    posts[data[i]["id"]] = data[i]["body"];
                }
            }
            i++;
        }
        
        // Update the info div
        var d = new Date();
        
        if (first_update_time == -1)
            first_update_time = d.getTime();
        
        if (first_update_time == last_post_time)
            var last_post = "";
        else {
            d = new Date(last_post_time);
            var last_post = "Laatste post: " + getTime(d) + "<br>";
            d = new Date();
        }
        $("info").innerHTML = last_post + "Laatste update: " + getTime(d) + "<br><br>";
        
        setTimeout(update, Math.max(45, update_rate - (new Date().getTime() - update_started_time)));
    }
    function getTime(d) {
        return leadingZeros(d.getHours()) + ":" + leadingZeros(d.getMinutes()) + ":" + leadingZeros(d.getSeconds());
    }
    function leadingZeros(n) {
        if (n < 10)
            return "0" + n;
        else
            return "" + n;
    }
    function append_to_chat(text) {
        $("posts").innerHTML = text + "<br><br>" + $("posts").innerHTML;
    }
    function reply() {
        $("reply").innerHTML = "<a href='javascript: endreply();'><b>Sluiten</b></a><br><iframe src='http://gmot.nl/?action=post&topic=" + topic + "' width='950' height='700'></iframe>";
    }
    function endreply() {
        $("reply").innerHTML = "<a href='javascript: reply();'>Voeg reactie toe</a>";
    }
    function ploeng(clear) {
        if (clear)
            $("ploeng").innerHTML = "";
        else {
            if ($("ploeng").innerHTML == "") {
                $("ploeng").innerHTML = "<iframe src='http://youtube.googleapis.com/v/afuKksl4G6Q?autoplay=1'></iframe>";
                setTimeout("ploeng(true);", update_rate - 250);
            }
        }
    }
    update();
    endreply();
</script>
<style>
    .quote {
        background: #DDD;
    }
</style>