function $(el){
    return document.getElementById(el);
}

function addClass(el, cl){
	var c = el.className.split(' ');
	c.push(cl);
	el.className = c.join(' ');
}

function removeClass(el, cl){
	var c = el.className.split(' ');
	for(var i=0; i<c.length; i++){
		if(c[i] == cl){
			el.className = (c.slice(0, i).concat(c.slice(i+1, c.length))).join(' ');
			break;
		}
	}
}

function hasClass(el, cl){
	var c = el.className.split(' ');
	for(var i=0; i<c.length; i++){
		if(c[i] == cl){
			return true;
		}
	}
	return false;
}

function Mi(){
    this.lin = 0;
    this.col = 0;
    this.miny = [];
    this.minyP = [];
    this.mode = 1;
	this.mineCount = 6;
}

Mi.prototype.createStage = function(){
    var o = '<table cellpadding="0" cellspacing="0">';
    for (var a = 0; a < this.lin; a++) {
        o += '<tr>';
        for (var b = 0; b < this.col; b++) {
            var id = a * this.col + b;
            o += '<td id="box_' + id + '" onclick="Mi.clicked(' + id + ');" class="hide">' + '-' + '</td>';
            if ((typeof this.miny[a]).toLowerCase() != 'object') {
                this.miny[a] = new Array();
            }
            this.miny[a].push(0);
            
        }
        o += '</tr>';
    }
    o += '</table>';
    return o;
}

Mi.prototype.insertMines = function(poli, min){
    var m = [];
    while (m.length < min) {
        var elId = Math.round(Math.random() * poli);
        var n = 0;
        for (var i = 0; i < m.length; i++) {
            if (m[i] == elId) {
                n = 1;
                break;
            }
        }
        if (n == 0) {
            m.push(elId);
            $('box_' + elId).innerHTML = 'M';
            this.miny[Math.floor(elId / this.col)][elId % this.col] = 1;
        }
    }
}

Mi.prototype.insertNumbers = function(){
    for (var a = 0; a < this.lin; a++) {
        for (var b = 0; b < this.col; b++) {
            if ((typeof this.minyP[a]).toLowerCase() != 'object') {
                this.minyP[a] = new Array();
            }
            var id = a * this.col + b;
            
            if (this.miny[a][b] != 1) {
                var p = 0;
                var M = this.miny;
                if (M[a - 1] && M[a - 1][b] == 1) {
                    p++;
                }
                if (M[a + 1] && M[a + 1][b] == 1) {
                    p++;
                }
                
                if (M[a][b + 1] == 1) {
                    p++;
                }
                
                if (M[a][b - 1] == 1) {
                    p++;
                }
                
                if (M[a - 1] && M[a - 1][b - 1] == 1) {
                    p++;
                }
                
                if (M[a - 1] && M[a - 1][b + 1] == 1) {
                    p++;
                }
                
                if (M[a + 1] && M[a + 1][b - 1] == 1) {
                    p++;
                }
                
                if (M[a + 1] && M[a + 1][b + 1] == 1) {
                    p++;
                }
                this.minyP[a].push(p);
                $('box_' + id).innerHTML = p;
            }
            else {
                this.minyP[a].push('M');
            }
        }
    }
}

Mi.prototype.unhide = function(id){
    id = Number(id);
    var P = this.minyP;
    var a = Math.floor(id / this.col);
    var b = id % this.col;
    
    if (P[a - 1] && P[a - 1][b] == 0) {
        this.clicked(id - this.col);
    }
    if (P[a + 1] && P[a + 1][b] == 0) {
        this.clicked(id + this.col);
    }
    if (P[a][b + 1] == 0) {
        this.clicked(id + 1);
    }
    if (P[a][b - 1] == 0) {
        this.clicked(id - 1);
    }
    
    function d(id){
        var e = $('box_' + id);
        if (e) {
            e.className = '';
            e.onclick = null;
        }
    }
    d(id - this.col);
    d(id + this.col);
    if (P[a][b + 1]) {
        d(id + 1);
    }
    if (P[a][b - 1]) {
        d(id - 1);
    }
    if (P[a - 1] && P[a - 1][b - 1]) {
        d(id - this.col - 1);
    }
    if (P[a + 1] && P[a + 1][b - 1]) {
        d(id + this.col - 1);
    }
    if (P[a - 1] && P[a - 1][b + 1]) {
        d(id - this.col + 1);
    }
    if (P[a + 1] && P[a + 1][b + 1]) {
        d(id + this.col + 1);
    }
}

Mi.prototype.clicked = function(id){
    var el = $('box_' + id);
    if (el) {
        if (this.mode == 1) {
            if (!hasClass(el, 'isMin')) {
                if (this.minyP[Math.floor(id / this.col)][id % this.col] == 'M') {
                    el.className = '';
                    el.onclick = null;
                    showStatus(200, 100, '<b>GAME OVER!</b>')
                }
                else {
                    el.className = '';
                    el.onclick = null;
                    if (this.minyP[Math.floor(id / this.col)][id % this.col] == 0) {
                        this.minyP[Math.floor(id / this.col)][id % this.col] = 'U';
                        this.unhide(id);
                    }
                }
                
            }
        }
        else {
            if (hasClass(el, 'isMin')) {
				removeClass(el, 'isMin');
            }
            else {
				addClass(el, 'isMin');
            }
            
        }
    }
}

Mi.prototype.check = function(){
	var yes = false;
	for (var a = 0; a < this.lin; a++) {
		if(yes){break;}
		for (var b = 0; b < this.col; b++) {
			if(this.minyP[a][b] == 'M'){
				if(!hasClass($('box_'+(a * this.col + b)), 'isMin')){
					yes = true;
					showStatus(200, 150, '<b>Your result is false!</b><p class="note">Note: You must mark all places where are mines, before check!</p>')
					break;
				}
			}
			if(this.minyP[a][b] != 'M' && hasClass($('box_'+(a * this.col + b)), 'isMin')){
				yes = true;
				showStatus(200, 150, '<b>Your result is false!</b><p class="note">Note: You must mark all places where are mines, before check!</p>')
				break;
			}
		}
	}
	if(!yes){
		showStatus(200, 150, '<b>Your result is true!</b>')
	}
}

Mi.prototype.changeMode = function(){
	if(this.mode == 1){
		this.mode = 0;
		$('mode').innerHTML = getButton('UNHIDE');
	}else{
		this.mode = 1;
		$('mode').innerHTML = getButton('MARK AS MINE');
	}
}

Mi.prototype.start = function(){
    this.miny = [];
    this.minyP = [];
    $('page').innerHTML = '<h1>iMines</h1><div id="mine"></div>' + '<div id="mode" onclick="Mi.changeMode();">'+getButton('MARK AS MINE')+'</div>' + '<div onclick="Mi.check();">' + getButton('CHECK') + '</div>' + baToMe;
    this.mode = 1;
    this.lin = 7;
    this.col = 7;
    $('mine').innerHTML = this.createStage();
    Mi.insertMines(this.lin * this.col - 1, this.mineCount);
    Mi.insertNumbers();
}

var Mi = new Mi();
