var $ = require('jquery');
var Logger = require('polyball/shared/Logger');
var Physics = require('physicsjs');
var Model = require('polyball/shared/Model');
var GameRenderer = require('polyball/client/GameRenderer');
var HUD = require('polyball/client/HUD');
var Comms = require('polyball/client/Comms');
var Synchronizer = require('polyball/client/Synchronizer');

$(document).ready(function() {

    var model = new Model();

    var comms = new Comms({
        serverAddress: window.location.href,
        newIDCallback: function (id) { model.setLocalClientID(id); }
    });

    Logger.info('Requesting configuration from ' + window.location.href);

    comms.requestInitialConfig(function (config) {
        Logger.info('Config received:');
        Logger.debug(config);

        var gameRenderer = new GameRenderer({
            model: model
        });

        var width = window.innerWidth,
            height = window.innerHeight;

        Logger.info('Width: ' + width + ' Height: ' + height);

        gameRenderer.resize(width - 25, height - 25);
        gameRenderer.forceFontLoad();
        gameRenderer.renderParticles();

        window.onresize = function () {
            width = window.innerWidth;
            height = window.innerHeight;

            Logger.info('Width: ' + width + ' Height: ' + height);
            gameRenderer.resize(width - 25, height - 25);
        };

        var synchronizer = new Synchronizer({
            comms: comms,
            model: model,
            commandAggregationInterval: config.commandAggregationInterval,
            simulationSyncLargeDelta: config.simulationSyncLargeDelta,
            simulationSyncRapidDecayRate: config.simulationSyncRapidDecayRate,
            simulationSyncSlowDecayRate: config.simulationSyncSlowDecayRate
        });

        var hud = new HUD({ //jshint ignore: line
            comms: comms,
            synchronizer: synchronizer,
            accumulationInterval: config.inputAccumulationInterval
        });

        Physics.util.ticker.on(function (time) {
            gameRenderer.render();
            synchronizer.tick(time);
        });

    });

}); // end on DOM ready

@@ -796,20 +796,16 @@
 nderer =
- new
  GameRen
@@ -809,16 +809,26 @@
 Renderer
+.createNew
 (%7B%0A     
@@ -2084,16 +2084,20 @@
 r.render
+Game
 ();%0A    

use the new renderer in the client. closes #184