

function JBoxitGame() {
    /*JBoardGame.call(this)*/
    
    
}
extendClass(JBoxitGame, JBoardGame);

/************************************************
    Function: setupGame()
************************************************/
JBoxitGame.prototype.setupGame = function() {
    var i;
    var brd;
    var entry;
    jsgamecode = this.name = "boxit";
    this.setBoard(brd = new JBoard());
    
    this.setGameSize( mc_w, mc_h, 80, 80 );

    entry = 1;
        
    for (i = 0; i < mc_dices * mc_dices; i++) {
        var z = '' + (Math.floor(i/mc_dices) + 1) + ((i % mc_dices) +1 );
        var img = jsgamebase + jsgamecode + "/gfx/piece" + z + ".gif";
        this.preloadImage( img, PRELOAD_SPRITE, 0, entry++ );
    }
    this.bgcolor = brd.bgcolor = 0xE7BB90;

    currmouseh=this;
//    enableKeyboardEvents();    

}

/************************************************
    Function: initLevel()
************************************************/
JBoxitGame.prototype.initLevel = function(levelnum) {
    var lev;
    var x, y, n, w, h, i, len;
    //lev = this.levels[levelnum];
    //len = lev.length;
    this.board.clearBoard( 0, LAY_LEVEL );
    
    var x, y;
    for (x=0; x<mc_w; x++)
        for (y=0; y<mc_h; y++) {
            var entry = x + y * mc_w;
            mc_curr[entry] = mc_level[entry];            
            var num = mc_curr[entry];
            this.board.setCell( num , x, y, LAY_LEVEL);
        }
 
    
    this.currlevel = levelnum;
    
//    this.board.flipBoard( this.flips[levelnum] & 1, (this.flips[levelnum] >> 1) & 1, LAY_LEVEL );

    this.board.copyLayer( LAY_LEVEL, LAY_DATA );    
    //this.fxBorder();
    this.board.refreshBoard( true );
    this.setStatus( GAMESTATUS_PLAYING );   
}

/************************************************
    Function: startGame()
************************************************/
JBoxitGame.prototype.startGame = function(firstlevel) {
    if (mc_errsave != -1) getel("scorebox").style.display = 'none';
    getel('mc_restart').disabled = true;       
    this.showInfo(1, '&nbsp;');
    mc_score = 0;
    last_x = last_y = -1;
    mc_tiles = mc_w * mc_h;
    mc_solution = "";
    mc_undo = new Array();
    mc_undo_len = 0;
    mc_undo_top = 0;    
    putScore();
    putPos();
    
    //this.showPlayer(1);
    //this.showBackgroundImage(IMG_NONE);
    this.initLevel(1);
    
}

/************************************************
    Function: unmapCell()
************************************************/
JBoxitGame.prototype.unmapCell = function( v, x, y ) {
    if (v > 0) {
        var id = v & 0xff;
        var hi = v >> 8;
        //v = id * 2;
        if (hi) {
            this.setBgColor(x,y,0x00ffff);
        } else {
            this.setBgColor(x,y,this.bgcolor);
        }
        //v++;
    }
    else
        id = -1;
    return id;
}

/************************************************
    Function: restart()
************************************************/
JBoxitGame.prototype.restart = function() {
    if (this.gamestatus != GAMESTATUS_ANIMATION) {
        this.startGame(1);
    }
}

/************************************************
    Function: parse_undo()
************************************************/
JBoxitGame.prototype.parse_undo = function(s) {
    var res = new Object();
    var a = s.split("-");
    var p1 = a[0].split(':');
    var p2 = a[1].split(':');
    for (var i=0; i<3; i++) { p1[i]*=1; p2[i]*=1; }
    res.a = p1;
    res.b = p2;
    return res;    
}

/************************************************
    Function: undo()
************************************************/
JBoxitGame.prototype.undo = function(dir) {
    var last;
    if (this.gamestatus == GAMESTATUS_PLAYING || this.gamestatus == GAMESTATUS_GAMEOVER ) {
        if (dir == -1 && mc_undo_top > 0) { // UNDO
            mc_undo_top--;
            last = this.parse_undo( mc_undo[mc_undo_top] );
            mc_tiles += 1;
            mc_score -= 1;
            this.setCell( last.a[2], last.a[0], last.a[1]  );
            this.setCell( last.b[2], last.b[0], last.b[1]  );
            if (this.gamestatus == GAMESTATUS_GAMEOVER) {
                // RESUME
                getel("scorebox").style.display = 'none';
                mc_undo_len = mc_undo_top;
                putPos();
                this.showInfo(1, "");
                this.setStatus(GAMESTATUS_PLAYING);
            }
        }
        if (dir == 1 && mc_undo_top < mc_undo_len) { // REDO
            last = this.parse_undo( mc_undo[mc_undo_top] );
            mc_undo_top++;
            mc_tiles -= 1;
            mc_score += 1;
            this.setCell( 0, last.a[0], last.a[1] );
            this.setCell( last.a[2], last.b[0], last.b[1] );
        }      
        last_x = last_y = -1;
        this.board.refreshBoard( false );
        putPos();
        putScore();
    }
}


/************************************************
    Function: checkSol()
************************************************/
JBoxitGame.prototype.checkSol = function() {
    var x, y, i, n1, n2, c1, c2, a, b;
    var n = mc_w * mc_h;
    
    for (x=0; x<mc_w; x++) {
        for (y=0; y<mc_h; y++) {
            a = this.getCell(x, y);
            if (a == 0) continue;
            n1 = (a-1) % mc_dices;
            c1 = Math.floor( (a-1) / mc_dices );
            // check hor
            for (i=0; i<mc_w; i++) {
                if (i==x) continue;
                b = this.getCell(i, y);
                if (b==0) continue;
                n2 = (b-1) % mc_dices;
                c2 = Math.floor( (b-1) / mc_dices );
                if (n1 ==n2 || c1 == c2) {
                    //alert( x+','+y+':'+a+'/'+n1+'/'+c1+' --> '+i+','+y+':'+b+'/'+n2+'/'+c2+' ');
                    return true;
                }
            }
            for (i=0; i<mc_h; i++) {
                if (i==y) continue;
                b = this.getCell(x, i);
                if (b==0) continue;
                n2 = (b-1) % mc_dices;
                c2 = Math.floor( (b-1) / mc_dices );
                if (n1 ==n2 || c1 == c2) {
                    //alert( x+','+y+':'+a+'/'+n1+'/'+c1+' --> '+i+','+y+':'+b+'/'+n2+'/'+c2+' ');
                    return true;
                }
            }        
        }
    }

    return false;    
}

/************************************************
    Function: doGameOver()
************************************************/
JBoxitGame.prototype.doGameOver = function() {
    if (mc_score > mc_hiscore) {
        getel("scorebox").style.display = 'block';
        mc_solution = '';
        for (var i=0; i<mc_undo_len; i++) {
            if (i>0) mc_solution += ",";
            mc_solution += mc_undo[i];
        }
        getel("id_solution").value = mc_solution;
        getel("id_sendscore").value = mc_score;
        getel("id_sendscore2").innerHTML = '' + mc_score;
    }    
    this.setStatus(GAMESTATUS_GAMEOVER);
}


/************************************************
    Function: vblank()
************************************************/
JBoxitGame.prototype.vblank = function() {
    switch (this.gamestatus) {
    case GAMESTATUS_ANIMATION :
        var a = this.getCell(last_x, last_y, LAY_DATA) & 0xff;
        var b = this.getCell(last_x2, last_y2, LAY_DATA) & 0xff;
        var aa = a-1;
        var bb = b-1;
        var n1 = aa % mc_dices;
        var c1 = Math.floor( aa / mc_dices );
        var n2 = bb % mc_dices;
        var c2 = Math.floor( bb / mc_dices );
        
        if ( (last_x==last_x2 || last_y==last_y2)
              && (n1 == n2 || c1 == c2)) {
            
            mc_score += 1;
            putPos();
            putScore();
            mc_curr[ last_x + last_y * mc_w ] = 0;
            mc_curr[ last_x2 + last_y2 * mc_w ] = 0;
            this.setCell(0, last_x, last_y, LAY_DATA);
            this.setCell(a, last_x2, last_y2, LAY_DATA);
            if ( getel("mc_restart").disabled ) {
                this.showInfo(1, '&nbsp;');
                getel("mc_restart").disabled = false;
            }            
            mc_tiles -= 1;
            var s = '' + last_x +':' + last_y + ':' + a;
            s += '-' + last_x2 +':' + last_y2 + ':' + b;
            mc_undo[mc_undo_top++] = s;
            mc_undo_len = mc_undo_top;
            putPos();

        } else {
            this.setCell(a, last_x, last_y, LAY_DATA);
            this.setCell(b, last_x2, last_y2, LAY_DATA);
        }
        last_x = last_y = -1;
        this.board.refreshBoard( false );
        if (mc_tiles == 1) {
            this.showInfo(1, "<b class=\"mcclear\">PERFETTO, GIOCO RISOLTO!!!</b>");
            this.doGameOver();
            return;            
        }
        else 
            if ( ! this.checkSol() ) {
                this.showInfo(1, "NESSUN'ALTRA MOSSA DISPONIBILE");
                this.doGameOver();
                return;
            }
        this.setStatus(GAMESTATUS_PLAYING);
        break;
    }
}

/************************************************
    Function: mouseHandler()
************************************************/
JBoxitGame.prototype.mouseHandler = function(src, x, y) {
    var num, c, hi;
    if (src == 'board') {
        switch (this.gamestatus) {
            case GAMESTATUS_PLAYING : 
                c = this.getCell(x, y, LAY_DATA);
                num = c & 0xff;
                hi = c >> 8;
                if (num == 0) return;
                if (hi) {
                    this.setCell(num, x, y, LAY_DATA);
                    last_x = last_y = -1;
                }
                else {
                    if (last_x >= 0) {
                        last_x2 = x;
                        last_y2 = y;
                        this.setStatus(GAMESTATUS_ANIMATION);
                        setTimeout( "boxitgame.vblank()", 350 );                        

                    } else {
                        last_x = x;
                        last_y = y;
                    }
                    this.setCell(num | 0x100, x, y, LAY_DATA);
                }
                
                this.board.refreshBoard( false );
                break;                
        }
    }
}
