var columnsPosition = [[120,360],
                      [260,330],
                      [190,355],
                      [390,400],
                      [335,455],
                      [510,465],
                      [600,420],
                      [690,440],
                      [810,455],
                      [855,417],
                      [1010,450],
                      [1030,365],
                      [1150,430],
                      [1150,320],
                      [200,480],
                      [350,300],
                      [900,510],
                      [480,360]]
var aura = null;
var littleAura = null;

var WorldMgr = {
    
    prepare: function() {
    },
    
    init: function() {
        game.stage.backgroundColor = '#2d2d2d';
    },
        
    createColumns: function() {
        maxCol = 18;
        for (i =0; i< maxCol; i++) {
            posX = columnsPosition[i][0];
            posY = columnsPosition[i][1];
            gameData.columns[i] = new Column();
            random = Math.floor((Math.random() * 3) + 1);
            if (random == 1)
                gameData.columns[i].sprite = game.add.sprite(posX, posY, 'pilone1');
            else if (random == 2)
                gameData.columns[i].sprite = game.add.sprite(posX, posY, 'pilone2');
            else if (random == 3)
                gameData.columns[i].sprite = game.add.sprite(posX, posY, 'pilone3');
            gameData.columns[i].x = posX;
            gameData.columns[i].y = posY;
            gameData.columns[i].width = gameData.columns[i].sprite.width;
            gameData.columns[i].height = gameData.columns[i].sprite.height;
        }
    },
    
    create3Archers: function() {
        gameData.placedArcher = 0;
        var index = (gameData.round -1) * 3;
        if (game.cache.checkImageKey('archerStandLvl1') && game.cache.checkImageKey('archerStandLvl2') && game.cache.checkImageKey('archerStandLvl3')) {
            gameData.archers.push(new Archer());
            gameData.archers.push(new Archer());
            gameData.archers.push(new Archer());
            gameData.archers[index + 0].id = (gameData.round-1) * 3 + 0;
            gameData.archers[index + 0].lvl = LvlArchers[gameData.archers[index + 0].id];
            if (gameData.archers[index + 0].lvl == 1)
                gameData.archers[index + 0].sprite = game.add.sprite(MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2, MAX_SCREEN_Y - 175, 'archerStandLvl1');
            else if (gameData.archers[index + 0].lvl == 2)
                gameData.archers[index + 0].sprite = game.add.sprite(MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2, MAX_SCREEN_Y - 175, 'archerStandLvl2');
            else {
                gameData.archers[index + 0].sprite = game.add.sprite(MAX_SCREEN_X / 4 - (myCache.archerStandLvl_width*1.1) /2, MAX_SCREEN_Y - 190, 'archerStandLvl3');
                gameData.archers[index + 0].sprite.scale.set(1.1);
            }
            gameData.archers[index + 0].sprite.inputEnabled = true;
            gameData.archers[index + 0].sprite.input.enableDrag();
            gameData.archers[index + 0].sprite.input.useHandCursor = true;
            gameData.archers[index + 0].sprite.events.onDragStart.add(startMoveArcher, {archerCtxt: gameData.archers[index + 0]});
            gameData.archers[index + 0].sprite.events.onDragUpdate.add(updateMoveArcher, {archerCtxt: gameData.archers[index + 0]});
            gameData.archers[index + 0].sprite.events.onDragStop.add(stopMoveArcher, {archerCtxt: gameData.archers[index + 0]});
            gameData.archers[index + 0].sprite.events.onInputOver.add(watchArcher, {archerCtxt: gameData.archers[index + 0]});
            gameData.archers[index + 0].sprite.events.onInputOut.add(stopWatchArcher, {archerCtxt: gameData.archers[index + 0]});
            anim1 = gameData.archers[index + 0].sprite.animations.add('stand');
            anim1.play(25, true); 
            
            gameData.archers[index + 1].id = (gameData.round-1) * 3 + 1;
            gameData.archers[index + 1].lvl = LvlArchers[gameData.archers[index + 1].id];
            if (gameData.archers[index + 1].lvl == 1)
                gameData.archers[index + 1].sprite = game.add.sprite(2* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2, MAX_SCREEN_Y - 175, 'archerStandLvl1');
            else if (gameData.archers[index + 1].lvl == 2)
                gameData.archers[index + 1].sprite = game.add.sprite(2* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2, MAX_SCREEN_Y - 175, 'archerStandLvl2');
            else {
                gameData.archers[index + 1].sprite = game.add.sprite(2* MAX_SCREEN_X / 4 - (myCache.archerStandLvl_width*1.1) /2, MAX_SCREEN_Y - 190, 'archerStandLvl3');
                gameData.archers[index + 1].sprite.scale.set(1.1);
            }
            gameData.archers[index + 1].sprite.inputEnabled = true;
            gameData.archers[index + 1].sprite.input.enableDrag();
            gameData.archers[index + 1].sprite.input.useHandCursor = true;
            gameData.archers[index + 1].sprite.events.onDragStart.add(startMoveArcher, {archerCtxt: gameData.archers[index + 1]});
            gameData.archers[index + 1].sprite.events.onDragUpdate.add(updateMoveArcher, {archerCtxt: gameData.archers[index + 1]});
            gameData.archers[index + 1].sprite.events.onDragStop.add(stopMoveArcher, {archerCtxt: gameData.archers[index + 1]});
            gameData.archers[index + 1].sprite.events.onInputOver.add(watchArcher, {archerCtxt: gameData.archers[index + 1]});
            gameData.archers[index + 1].sprite.events.onInputOut.add(stopWatchArcher, {archerCtxt: gameData.archers[index + 1]});
            anim2 = gameData.archers[index + 1].sprite.animations.add('stand');
            anim2.play(25, true);
            
            gameData.archers[index + 2].id = (gameData.round-1) * 3 + 2;
            gameData.archers[index + 2].lvl = LvlArchers[gameData.archers[index + 2].id];
            if (gameData.archers[index + 2].lvl == 1)
                gameData.archers[index + 2].sprite = game.add.sprite(3* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2, MAX_SCREEN_Y - 175, 'archerStandLvl1');
            else if (gameData.archers[index + 2].lvl == 2)
                gameData.archers[index + 2].sprite = game.add.sprite(3* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2, MAX_SCREEN_Y - 175, 'archerStandLvl2');
            else {
                gameData.archers[index + 2].sprite = game.add.sprite(3* MAX_SCREEN_X / 4 - (myCache.archerStandLvl_width*1.1) /2, MAX_SCREEN_Y - 190, 'archerStandLvl3');
                gameData.archers[index + 2].sprite.scale.set(1.1);
            }
            gameData.archers[index + 2].sprite.inputEnabled = true;
            gameData.archers[index + 2].sprite.input.enableDrag();
            gameData.archers[index + 2].sprite.input.useHandCursor = true;
            gameData.archers[index + 2].sprite.events.onDragStart.add(startMoveArcher, {archerCtxt: gameData.archers[index + 2]});
            gameData.archers[index + 2].sprite.events.onDragUpdate.add(updateMoveArcher, {archerCtxt: gameData.archers[index + 2]});
            gameData.archers[index + 2].sprite.events.onDragStop.add(stopMoveArcher, {archerCtxt: gameData.archers[index + 2]});
            gameData.archers[index + 2].sprite.events.onInputOver.add(watchArcher, {archerCtxt: gameData.archers[index + 2]});
            gameData.archers[index + 2].sprite.events.onInputOut.add(stopWatchArcher, {archerCtxt: gameData.archers[index + 2]});
            anim3 = gameData.archers[index + 2].sprite.animations.add('stand');
            anim3.play(25, true);
        } else {
            mLog('image not found');
        }
    },
    
    fireArcher: function () {
        if (fireArcherData.isFire == false) {
            prepareFireArcher(gameData.columns);
        }
        return updateArrows();
    },
    
    fireArcherLegendary: function () {
        if (fireArcherLegData.isFire == false) {
            prepareFireArcherLeg(gameData.columns);
        }
        return updateArrowsLeg();
    },
    
    fireDragon: function () {
        if (fireDragonData.isFire == false) {
            prepareFireDragon(gameData.columns);
        }
        return updateFireDragon();
    },
    
    update: function() {
        /*if (gameData.archers[0] != null)
            game.debug.spriteInfo(gameData.archers[0].sprite, 32, 32);*/
        //game.debug.pointer( game.input.activePointer );
        //game.debug.spriteInfo(myCache.dragonHead, 32, 32);
    }
    
}

function startMoveArcher () {
    rand = Math.floor(Math.random() *2 +1);
    if (rand == 1)
        sound_hmm.play();
    else
        sound_hmm2.play();
}

function stopWatchArcher() {
    aura.visible = false;
}

function watchArcher(sprite) {
    if (aura == null)
        aura = game.add.sprite(sprite.x, sprite.y + 134, 'aura');
    sprite.bringToTop();
    aura.visible = true;
    aura.x = sprite.x - 3 + 4*(this.archerCtxt.lvl == 3);
    aura.y = sprite.y + 119 + 15 * (this.archerCtxt.lvl == 3);
}

function updateMoveArcher (sprite, pointer, dragX, dragY, snapPoint) {
    if (pointer.y < MAX_SCREEN_Y - 215 && this.archerCtxt.isDraggingTop == false) {
        if (this.archerCtxt.lvl == 1 && sprite.key != 'littleArcherLvl1') {
            sprite.scale.set(0.4);
            sprite.loadTexture('littleArcherStandLvl1', 0, false);
        } else if (this.archerCtxt.lvl == 2 && sprite.key != 'littleArcherLvl2') {
            sprite.scale.set(0.4);
            sprite.loadTexture('littleArcherStandLvl2', 0, false);
        } else if (this.archerCtxt.lvl == 3 && sprite.key != 'littleArcherLvl3') {
            sprite.scale.set(0.4);
            sprite.loadTexture('littleArcherMixLvl3', 0, false);
        }
        sprite.x = pointer.x - sprite.width/2;
        sprite.y = pointer.y - sprite.height/2;
        sprite.input._dragPoint.x = - sprite.width/2;
        sprite.input._dragPoint.y = - sprite.height/2;
        this.archerCtxt.isDraggingTop = true;
    } else if (pointer.y > MAX_SCREEN_Y - 215 && this.archerCtxt.isDraggingTop == true) {
        if (this.archerCtxt.lvl == 1 && sprite.key != 'archerStandLvl1') {
            sprite.scale.set(1);
            sprite.loadTexture('archerStandLvl1', 0, false);
        } else if (this.archerCtxt.lvl == 2 && sprite.key != 'archerStandLvl2') {
            sprite.scale.set(1);
            sprite.loadTexture('archerStandLvl2', 0, false);
        } else if (this.archerCtxt.lvl == 3 && sprite.key != 'archerStandLvl3') {
            sprite.scale.set(1.1);
            sprite.loadTexture('archerStandLvl3', 0, false);
        }
        sprite.x = pointer.x - sprite.width/2;
        sprite.y = pointer.y - sprite.height/2;
        sprite.input._dragPoint.x = - sprite.width/2;
        sprite.input._dragPoint.y = - sprite.height/2;
        this.archerCtxt.isDraggingTop = false;
    } else {
        column = getColumn(pointer);
        if (column != null) {
            //TODO Aura de sélection
        }
    }
    column = getColumn(pointer);
    if (column != null) {
        if (littleAura == null) {
            littleAura = game.add.sprite(0,0, 'aura');
            littleAura.scale.set(0.7);
        }
        littleAura.visible = true;
        littleAura.x = column.x -6;
        littleAura.y = column.y -5 ;
    } else  if (littleAura != null)
        littleAura.visible = false;
}

function stopMoveArcher (sprite, pointer) {
    if (pointer.y > MAX_SCREEN_Y - 215) {
        //on replace l'archer a sa place
        if (this.archerCtxt.id % 3 == 0) {
            sprite.x = MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2;
            sprite.y = MAX_SCREEN_Y - sprite.height - 25;
        } else if (this.archerCtxt.id % 3 == 1) {
            sprite.x = 2* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2;
            sprite.y = MAX_SCREEN_Y - sprite.height - 25;
        } else if (this.archerCtxt.id % 3 == 2) {
            sprite.x = 3* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2;
            sprite.y = MAX_SCREEN_Y - sprite.height - 25;
        }
    } else {
        column = getColumn(pointer);
        if (column != null && column.archer == null && column.destroyed == false) {
            sprite.x = column.x + 5;
            sprite.y = column.y-45;
            sprite.inputEnabled = false;
            sprite.input.disableDrag();
            sprite.input.useHandCursor = false;
            gameData.placedArcher++;
            column.archer = this.archerCtxt;
        } else { //placé nul part, on replace l'archer a sa place
            if (this.archerCtxt.lvl == 1 && sprite.key != 'archerStandLvl1') {
                sprite.scale.set(1);
                sprite.loadTexture('archerStandLvl1', 0, false);
            } else if (this.archerCtxt.lvl == 2 && sprite.key != 'archerStandLvl2') {
                sprite.scale.set(1);
                sprite.loadTexture('archerStandLvl2', 0, false);
            } else if (this.archerCtxt.lvl == 3 && sprite.key != 'archerStandLvl3') {
                sprite.scale.set(1.1);
                sprite.loadTexture('archerStandLvl3', 0, false);
            }
            if (this.archerCtxt.id % 3 == 0) {
                sprite.x = MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2;
                sprite.y = MAX_SCREEN_Y - sprite.height - 25;
            } else if (this.archerCtxt.id % 3 == 1) {
                sprite.x = 2* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2;
                sprite.y = MAX_SCREEN_Y - sprite.height - 25;
            } else if (this.archerCtxt.id % 3 == 2) {
                sprite.x = 3* MAX_SCREEN_X / 4 - myCache.archerStandLvl_width /2;
                sprite.y = MAX_SCREEN_Y - sprite.height - 25;
            }
        } 
        if (gameData.placedArcher == 3) {
            gameData.userPlaying = false;
        }
    }
}