/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 David Heidrich, BowlingX <me@bowlingx.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
'use strict';

require('styles/main.scss');

import {Client, EVENT_MESSAGE} from 'Client';
import Marklib from 'marklib';
import Selector from 'Selector';

const ATTR_COMMENTP = 'data-commentp';

document.addEventListener('DOMContentLoaded', () => {

    const node = document.querySelector(`[${ATTR_COMMENTP}]`);
    if (node) {
        const channel = node.getAttribute(ATTR_COMMENTP);
        Client.connect(channel).then((client) => {

            const selectorApp = new Selector(node, global.document);


            client.on(EVENT_MESSAGE, (msg) => {
                // FIXME: Create a queue :)
                const renderer = new Marklib.Rendering(document, 'marking-remote', node);
                renderer.renderWithResult(msg);
            });
        });
    }


});

export default Client;

@@ -1367,63 +1367,59 @@
 p';%0A
-%0Adocument.addEventListener('DOMContentLoaded', () =%3E %7B%0A
+const DOM_COMPLETE = 'complete';%0A%0Afunction init() %7B
 %0A   
@@ -1603,17 +1603,16 @@
 t) =%3E %7B%0A
-%0A
         
@@ -1672,18 +1672,16 @@
 ument);%0A
-%0A%0A
         
@@ -1940,13 +1940,166 @@
   %7D%0A
+%7D
 %0A%0A
-%7D);
+if (global.document.readyState === DOM_COMPLETE) %7B%0A    init();%0A%7D else %7B%0A    document.addEventListener('DOMContentLoaded', () =%3E %7B%0A        init();%0A    %7D);%0A%7D
 %0A%0Aex

Load Application as soon as DOM is completly loaded