/* global moment, io */
(function (Pingbacks) {

    var container = document.querySelector('.pingbacks tbody');
    container.innerHTML = '';

    Pingbacks.addPingbacks = function (pingbacks) {
        console.info("Adding", pingbacks.length, "pingbacks");
        pingbacks.forEach(function (pingback) {
            var row = document.createElement("tr");
            var dateCell = document.createElement("td");
            dateCell.innerHTML = moment(pingback.date).format("DD/MM/YYYY HH:mm:ss");
            row.appendChild(dateCell);
            var bodyCell = document.createElement("td");
            var content = [];
            for (var key in pingback.params) {
                if (pingback.params.hasOwnProperty(key)) {
                    content.push('<strong>' + key + '</strong>: ' + pingback.params[key]);
                }
            }
            bodyCell.innerHTML = content.join('<br/>');
            row.appendChild(bodyCell);
            container.appendChild(row);
        });
    };

    var socket = io(location.protocol + "//" + location.host);
    socket.on('pingbacks', Pingbacks.addPingbacks);

})(window.Pingbacks = window.Pingbacks || {});