(function(htd, undefined) {
    (function(data, undefined) {

        data.scripts =
        {
            "Util/index": null,
            "Util/PathFinding": null,
            "Util/Hex": null,
            "GameData/index": null,
            "GameData/Enemy": null,
            "GameData/ProjectileEffect": null,
            "GameData/Simulation": null,
            "GameData/Tower": null,
            "GameData/Wave": null,
            "GameData/Level": null,
            "GameData/Root": null,
            "Models/index": null,
            "Models/Cell": null,
            "Models/Enemy": null,
            "Models/Level": null,
            "Models/Projectile": null,
            "Models/Tower": null,
            "Simulation/index": null,
            "Simulation/Root": null,
            "Simulation/SpawnEnemies": null,
            "Simulation/MoveEnemies": null,
            "Simulation/FireTowers": null,
            "Simulation/MoveProjectiles": null,
            "Simulation/Commands/index": null,
            "Simulation/Commands/CreateTower": null,
            "Simulation/Commands/BlockTerrain": null,
            "Simulation/Commands/StartNextWave": null,
            "Views/view.js": null,
            "Views/level.js": null,
            "controller/mouse.js": null,
            "Game": null,
        };

        data.images = 
        {
            "images/projectile.svg": null,
            "images/tower-0.svg": null,
            "images/cell-highlighted.svg": null,
            "images/cell-entry-1.svg": null,
            "images/cell-entry-2.svg": null,
            "images/cell-entry-3.svg": null,
            "images/cell-entry-4.svg": null,
            "images/cell-entry-6.svg": null,
            "images/cell-entry-8.svg": null,
            "images/cell-entry-12.svg": null,
            "images/cell-entry-16.svg": null,
            "images/cell-entry-24.svg": null,
            "images/cell-entry-32.svg": null,
            "images/cell-entry-33.svg": null,
            "images/cell-entry-48.svg": null,
            "images/cell-blocked-0.svg": null,
            "images/cell-blocked-1.svg": null,
            "images/cell-blocked-2.svg": null,
            "images/cell-blocked-3.svg": null,
            "images/cell-blocked-4.svg": null,
            "images/cell-blocked-5.svg": null,
            "images/cell-blocked-6.svg": null,
            "images/cell-blocked-7.svg": null,
            "images/cell-blocked-8.svg": null,
            "images/cell-blocked-9.svg": null,
            "images/cell-blocked-10.svg": null,
            "images/cell-blocked-11.svg": null,
            "images/cell-blocked-12.svg": null,
            "images/cell-blocked-13.svg": null,
            "images/cell-blocked-14.svg": null,
            "images/cell-blocked-15.svg": null,
            "images/cell-blocked-16.svg": null,
            "images/cell-blocked-17.svg": null,
            "images/cell-blocked-18.svg": null,
            "images/cell-blocked-19.svg": null,
            "images/cell-blocked-20.svg": null,
            "images/cell-blocked-21.svg": null,
            "images/cell-blocked-22.svg": null,
            "images/cell-blocked-23.svg": null,
            "images/cell-blocked-24.svg": null,
            "images/cell-blocked-25.svg": null,
            "images/cell-blocked-26.svg": null,
            "images/cell-blocked-27.svg": null,
            "images/cell-blocked-28.svg": null,
            "images/cell-blocked-29.svg": null,
            "images/cell-blocked-30.svg": null,
            "images/cell-blocked-31.svg": null,
            "images/cell-blocked-32.svg": null,
            "images/cell-blocked-33.svg": null,
            "images/cell-blocked-34.svg": null,
            "images/cell-blocked-35.svg": null,
            "images/cell-blocked-36.svg": null,
            "images/cell-blocked-37.svg": null,
            "images/cell-blocked-38.svg": null,
            "images/cell-blocked-39.svg": null,
            "images/cell-blocked-40.svg": null,
            "images/cell-blocked-41.svg": null,
            "images/cell-blocked-42.svg": null,
            "images/cell-blocked-43.svg": null,
            "images/cell-blocked-44.svg": null,
            "images/cell-blocked-45.svg": null,
            "images/cell-blocked-46.svg": null,
            "images/cell-blocked-47.svg": null,
            "images/cell-blocked-48.svg": null,
            "images/cell-blocked-49.svg": null,
            "images/cell-blocked-50.svg": null,
            "images/cell-blocked-51.svg": null,
            "images/cell-blocked-52.svg": null,
            "images/cell-blocked-53.svg": null,
            "images/cell-blocked-54.svg": null,
            "images/cell-blocked-55.svg": null,
            "images/cell-blocked-56.svg": null,
            "images/cell-blocked-57.svg": null,
            "images/cell-blocked-58.svg": null,
            "images/cell-blocked-59.svg": null,
            "images/cell-blocked-60.svg": null,
            "images/cell-blocked-61.svg": null,
            "images/cell-blocked-62.svg": null,
            "images/cell-blocked-63.svg": null,
            "images/cell.svg": null,
            "images/enemy-0.svg": null,
            "images/wall.png": null,
        };
        
    }(window.htd.data = window.htd.data || {}));

    (function(loader, undefined) {

        loader.currentScript = 0;

        loader.Draw = function()
        {
            var progress = this.GetProgress();
            if (!!htd.loader)
            {
                var canvas = document.getElementById("canvas");
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;

                var ctx = canvas.getContext("2d");
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.beginPath();
                ctx.rect(20, canvas.height / 2 - 10, progress * (canvas.width - 40), 20);
                ctx.fillStyle = "red";
                ctx.fill();

                requestAnimationFrame(function step(timestamp) 
                {
                    htd.loader.Draw();
                });
            }
        }
        
        loader.LoadScripts = function()
        {
            var keys = [];
            for (var key in htd.data.scripts) 
            {
                if (!htd.data.scripts.hasOwnProperty(key)) 
                {
                    continue;
                }
                keys.push(key);
            }
            var scr = document.createElement("script");
            scr.src = keys[this.currentScript];
            scr.dataName = keys[this.currentScript];
            scr.onload = function(key, script)
            {
                htd.data.scripts[this.dataName] = this;
                loader.currentScript += 1;
                if (loader.currentScript < keys.length)
                {
                    loader.LoadScripts();
                }
            };
            document.head.appendChild(scr);
        }   
        
        loader.LoadImages = function()
        {
            for (var key in htd.data.images) 
            {
                if (!htd.data.images.hasOwnProperty(key)) 
                {
                    continue;
                }

                var img = new Image();
                img.src = key;
                img.dataName = key;
                img.onload = function()
                {
                    htd.data.images[this.dataName] = this;
                };
            };
        }

        loader.GetProgress = function()
        {
            var length = 0;
            var loaded = 0;
            for (var key in htd.data.scripts) 
            {
                if (!htd.data.scripts.hasOwnProperty(key)) 
                {
                    continue;
                }

                length += 1;
                var obj = htd.data.scripts[key];
                if (obj != null) 
                {
                    loaded += 1;
                }
            };

            for (var key in htd.data.images) 
            {
                if (!htd.data.images.hasOwnProperty(key)) 
                {
                    continue;
                }

                length += 1;
                var obj = htd.data.images[key];
                if (obj != null) 
                {
                    loaded += 1;
                }
            };

            var progress = (loaded / length);
            if (progress >= 1)
            {
                for (var idx in htd.data.scripts)
                {
                    eval(htd.data.scripts[idx]);
                }

                htd.sim.Load();
                htd.view.Load();
                htd.controller.mouse.Load();
                htd.loader = null;
            }
            return progress;
        }

        loader.LoadScripts();
        loader.LoadImages();

        requestAnimationFrame(function step(timestamp) 
        {
            loader.Draw();
        });
    }(window.htd.loader = window.htd.loader || {}));
}(window.htd = window.htd || {}));