(function()
{

    var rendererOptions = {
        antialiasing: false,
        transparent: true
    };

    var cont = document.getElementById('view_cont');

    var rendererStage;
    try {
        rendererStage = new PIXI.WebGLRenderer(screen.availWidth, screen.height - 3, rendererOptions);
        cont.appendChild(rendererStage.view);

        var detectDlC = [1, 1, 1];

        try {
            require('../hardcore/js/init.js');
        }
        catch (err) {
            detectDlC[0] = 0;
        }

        try {
            require('../hardcore_x2/js/init.js');
        }
        catch (err) {
            detectDlC[1] = 0;
        }

        try {
            require('../hardcore_x2ev/js/init.js');
        }
        catch (err) {
            detectDlC[2] = 0;
        }

        var contBanner = new PIXI.Container();

        var bw = PIXI.Sprite.fromImage('./img/header.png');
        bw.pivot.x = Math.round(bw.width / 2);
        bw.pivot.y = Math.round(bw.height / 2);
        bw.x = Math.round(screen.availWidth / 2) - 365;
        bw.y = Math.round(screen.height / 2) - 118;
        contBanner.addChild(bw);

        bw.interactive = true;
        bw.buttonMode = true;
        bw.click = clickBanner.bind(null, 0);

        bw = PIXI.Sprite.fromImage('./img/header_01.png');
        bw.pivot.x = Math.round(bw.width / 2);
        bw.pivot.y = Math.round(bw.height / 2);
        bw.x = Math.round(screen.availWidth / 2) - 120;
        bw.y = Math.round(screen.height / 2) - 118;
        contBanner.addChild(bw);

        bw.interactive = true;
        bw.buttonMode = true;
        bw.click = clickBanner.bind(null, 1);

        if (detectDlC[0] || detectDlC[1]) {
            bw = PIXI.Sprite.fromImage('./img/border_play.png');
            bw.pivot.x = Math.round(bw.width / 2);
            bw.pivot.y = Math.round(bw.height / 2);
            bw.x = Math.round(screen.availWidth / 2) - 120;
            bw.y = Math.round(screen.height / 2) - 118;
            contBanner.addChild(bw);
        }

        bw = PIXI.Sprite.fromImage('./img/header_02.png');
        bw.pivot.x = Math.round(bw.width / 2);
        bw.pivot.y = Math.round(bw.height / 2);
        bw.x = Math.round(screen.availWidth / 2) + 125;
        bw.y = Math.round(screen.height / 2) - 118;
        contBanner.addChild(bw);

        bw.interactive = true;
        bw.buttonMode = true;
        bw.click = clickBanner.bind(null, 2);

        if (detectDlC[1]) {
            bw = PIXI.Sprite.fromImage('./img/border_play.png');
            bw.pivot.x = Math.round(bw.width / 2);
            bw.pivot.y = Math.round(bw.height / 2);
            bw.x = Math.round(screen.availWidth / 2) + 125;
            bw.y = Math.round(screen.height / 2) - 118;
            contBanner.addChild(bw);
        }

        bw = PIXI.Sprite.fromImage('./img/header_03.png');
        bw.pivot.x = Math.round(bw.width / 2);
        bw.pivot.y = Math.round(bw.height / 2);
        bw.x = Math.round(screen.availWidth / 2) - 365;
        bw.y = Math.round(screen.height / 2) + 10;
        contBanner.addChild(bw);

        bw.interactive = true;
        bw.buttonMode = true;
        bw.click = clickBanner.bind(null, 3);

        if (detectDlC[2]) {
            bw = PIXI.Sprite.fromImage('./img/border_play.png');
            bw.pivot.x = Math.round(bw.width / 2);
            bw.pivot.y = Math.round(bw.height / 2);
            bw.x = Math.round(screen.availWidth / 2) - 365;
            bw.y = Math.round(screen.height / 2) +10;
            contBanner.addChild(bw);
        }

        bw = PIXI.Sprite.fromImage('./img/header_04.png');
        bw.pivot.x = Math.round(bw.width / 2);
        bw.pivot.y = Math.round(bw.height / 2);
        bw.x = Math.round(screen.availWidth / 2) - 120;
        bw.y = Math.round(screen.height / 2) + 10;
        contBanner.addChild(bw);

        bw.interactive = true;
        bw.buttonMode = true;
        bw.click = clickBanner.bind(null, 4);

        bw = PIXI.Sprite.fromImage('./img/exit.png');
        bw.pivot.x = 32;
        bw.x = Math.round(screen.availWidth / 2);
        bw.y = Math.round(screen.height / 2) + 140;
        contBanner.addChild(bw);

        bw.interactive = true;
        bw.buttonMode = true;
        bw.click = closePage;

        setTimeout(function () {
            rendererStage.render(contBanner);
        }, 500);


        function clickBanner(_index) {
            if (_index === 0) document.location.href = '../main/index.html';
            else if (_index === 1) {
                if (detectDlC[1]) {
                    document.location.href = '../hardcore_x2/index.html';
                }
                else if (detectDlC[0] === 0) {
                    require('nw.gui').Shell.openExternal("http://store.steampowered.com/app/547560/");
                }
                else {
                    document.location.href = '../hardcore/index.html';
                }
            }
            else if (_index === 2) {
                if (detectDlC[1]) {
                    document.location.href = '../hardcore_x2/index.html';
                }
                else {
                    require('nw.gui').Shell.openExternal("http://store.steampowered.com/app/547561/");
                }
            }
            else if (_index === 3) {
                if (detectDlC[2]) {
                    document.location.href = '../hardcore_x2ev/index.html';
                }
                else {
                    require('nw.gui').Shell.openExternal("http://store.steampowered.com/app/547562/");
                }
            }
            else if (_index === 4) {
                require('nw.gui').Shell.openExternal("http://store.steampowered.com/app/556950/");
            }
        }
    }
    catch (err)
    {
        rendererStage = new PIXI.autoDetectRenderer(screen.availWidth, screen.height - 3, rendererOptions);
        cont.appendChild(rendererStage.view);

        var stage = new PIXI.Container();

        var basicText = new PIXI.Text('This graphic card does not support webGL.',{fill:'#F7EDCA'});
        basicText.anchor.set(0.5,0.5);
        basicText.x = Math.round(screen.availWidth / 2);
        basicText.y = Math.round(screen.height / 2);

        stage.addChild(basicText);
        rendererStage.render(stage);

        basicText.interactive = true;
        basicText.buttonMode = true;
        basicText.click = closePage;
    }

    function closePage(){
        require('nw.gui').Window.get().close();
    }

//    var stats = new Stats();
//    stats.setMode(0);
//
//    stats.domElement.style.position = 'absolute';
//    stats.domElement.style.left = '0px';
//    stats.domElement.style.top = '0px';
//    document.body.appendChild(stats.domElement);
//
//    return {renderStage:rendererStage, stats:stats, rendererOptions:rendererOptions}
})(this);
