function Su(){
    this.diCache = '';
    this.diFor = null;
    this.diType = 0;
}

Su.prototype.changeDiType = function(el){
    if (this.diType == 0) {
        el.innerHTML = 'insert number';
        this.diType = 1;
        if (this.diFor.pNum) {
            for (var i = 0; i < this.diFor.pNum.length; i++) {
                $('diNum_' + this.diFor.pNum[i]).className = 'diPos_s';
            }
        }
    }
    else {
        for (var i = 1; i < 10; i++) {
            $('diNum_' + i).className = '';
        }
        el.innerHTML = 'possible numbers';
        this.diType = 0;
    }
}


Su.prototype.setNumber = function(el){
    if (this.diType == 0) {
        this.diFor.innerHTML = el.innerHTML;
        this.diFor.pNum = undefined;
        this.hideDi();
    }
    else {
        if (!this.diFor.pNum) {
            this.diFor.pNum = new Array();
        }
        var is = false;
        for (var i = 0; i < this.diFor.pNum.length; i++) {
            if (this.diFor.pNum[i] == el.innerHTML) {
                this.diFor.pNum = this.diFor.pNum.slice(0, i).concat(this.diFor.pNum.slice(i + 1, this.diFor.pNum.length));
                el.className = '';
                
                this.diFor.innerHTML = '<div class="posN">' + this.diFor.pNum.sort().join('</div><div class="posN">') + '</div>';
                is = true;
            }
        }
        if (is == false) {
            if (this.diFor.pNum.length < 8) {
                this.diFor.pNum.push(el.innerHTML);
                el.className = 'diPos_s';
                this.diFor.innerHTML = '<div class="posN">' + this.diFor.pNum.sort().join('</div><div class="posN">') + '</div>';
            }
        }
    }
}


Su.prototype.clearIt = function(){
    this.diFor.innerHTML = '&nbsp;';
    this.hideDi();
}


Su.prototype.hideDi = function(){
    remove($('di'));
    remove($('diBg'));
}

Su.prototype.hideStDi = function(){
    remove($('statusDi'));
    remove($('diBg'));
}

Su.prototype.check = function(){
    var ok = true;
    this.hideDi();
    var r = [];
    for (var i = 0; i < 81; i++) {
        r.push(parseInt($('p' + i).innerHTML));
    }
    
    for (var a = 0; a < 3; a++) {
        if (ok == false) {
            break;
        }
        for (var i = 0; i < 3; i++) {
            if (r.slice(a * 27 + i * 3 + 0, a * 27 + i * 3 + 3).concat(r.slice(a * 27 + i * 3 + 9, a * 27 + i * 3 + 12), r.slice(a * 27 + i * 3 + 18, a * 27 + i * 3 + 21)).sort().join('') != '123456789') {
                ok = false;
                break;
            }
            if ([r[0 + i + a * 9], r[3 + i + a * 9], r[6 + i + a * 9], r[27 + i + a * 9], r[30 + i + a * 9], r[33 + i + a * 9], r[54 + i + a * 9], r[57 + i + a * 9], r[60 + i + a * 9]].sort().join('') != '123456789') {
                ok = false;
                break;
            }
        }
    }
    
    if (ok) {
        this.showStatus('<p class="ou">Your result is true!</p>');
    }
    else {
        this.showStatus('<p class="ou">Your result is false!</p><p class="note">Note: You must fill all blocks before check!</p>');
    }
}



Su.prototype.showDi = function(el){
    this.diType = 0;
    this.hideDi();
    this.diFor = el;
    var di = document.createElement('div');
    di.id = 'di';
    if (this.diCache == '') {
        var o = '<table cellpadding="0" cellspacing="0">';
        for (var a = 0; a < 3; a++) {
            o += '<tr>';
            for (var b = 0; b < 3; b++) {
                o += '<td id="diNum_' + (a * 3 + b + 1) + '" onclick="Su.setNumber(this);">' + (a * 3 + b + 1) + '</td>';
            }
            o += '</tr>';
        }
        o += '<tr><td colspan="3" onclick="Su.clearIt();">clear</td></tr>';
        o += '<tr><td colspan="3" onclick="Su.changeDiType(this);">possible numbers</td></tr>';
        o += '<tr><td colspan="3" onclick="Su.hideDi();">close</td></tr>';
        o += '</table>';
        this.diCache = o;
    }
    di.innerHTML = this.diCache;
    var diBg = document.createElement('div');
    diBg.id = "diBg";
    document.body.appendChild(diBg);
    document.body.appendChild(di);
}


Su.prototype.showStatus = function(text){
    var di = document.createElement('div');
    di.id = 'statusDi';
    di.innerHTML = text + '<div onclick="Su.hideStDi(); menuPage();">' + getButton('MENU') + '</div><div onclick="Su.hideStDi();">' + getButton('BACK TO GAME') + '</div>';
    var diBg = document.createElement('div');
    diBg.id = "diBg";
    document.body.appendChild(diBg);
    document.body.appendChild(di);
}


Su.prototype.generateSudokuHTML = function(set){
    var set = set.split('');
    var o = '<table cellpadding="0" cellspacing="0" align="center">';
    for (var a = 0; a < 3; a++) {
        o += '<tr>';
        for (var b = 0; b < 3; b++) {
            o += '<td><table cellpadding="0" cellspacing="0">';
            for (var c = 0; c < 3; c++) {
                o += '<tr>';
                for (var d = 0; d < 3; d++) {
                    var id = a * 27 + b * 9 + c * 3 + d;
                    var v = set[id];
                    if (v == 'x') {
                        o += '<td class="n_new" onclick="Su.showDi(this);" id="p' + id + '">&nbsp;</td>';
                    }
                    else {
                        o += '<td class="n_old" id="p' + id + '">' + v + '</td>';
                    }
                }
                o += '</tr>';
            }
            o += '</table></td>';
        }
        o += '</tr>';
    }
    o += '</table>';
    return o;
}



Su.prototype.run = function(difficulty){
    var t = this;
    var aj = new Ajax('ajax.php', {
        data: 'action=getSud&d=' + difficulty
    });
    aj.onComplete = function(r){
        $('sudoku').innerHTML = t.generateSudokuHTML(r);
    }
    
}

Su.prototype.startSudoku = function(difficulty){
	var vs = loggedIn==1?'<div id="save" onclick="Su.save();">' + getButton('SAVE') + '</div><div id="load" onclick="Su.load();">' + getButton('LOAD') + '</div>':'';
    $('page').innerHTML = '<div id="sudoku">Loading...</div><div id="check" onclick="Su.check();">' + getButton('CHECK') + '</div>'+vs+'<div onclick="menuPage();">' + getButton('BACK TO MENU') + '</div>';
    this.run(difficulty);
}

Su.prototype.save = function(){
    var s = [];
    for (var i = 0; i < 81; i++) {
        var el = $('p' + i);
        var c = el.className == 'n_new' ? 1 : 0;
        var pn = el.pNum ? el.pNum : '';
        s.push('["' + el.innerHTML.replace('"', '\"') + '", "' + c + '" , "' + pn + '"]');
    }
    var save = '[' + s.join(',') + ']';
    save = save.split('&').join('%26');
    var aj = new Ajax('ajax.php', {
        data: 'action=save&c=' + save
    });
    aj.onComplete = function(r){
		alert('SAVED!');
    }
    
}

Su.prototype.load = function(){
    var aj = new Ajax('ajax.php', {
        data: 'action=load'
    });
    aj.onComplete = function(r){
        r = eval(r);
        for (var i = 0; i < 81; i++) {
            var el = $('p' + i);
            el.className = r[i][1]==0?'n_old':'n_new';
			if(r[i][1]==1){
				el.onclick = function(){
					Su.showDi(this);
				}
			}else{
				el.onclick = null;
			}
            el.pNum = r[i][2].split(',');
            el.innerHTML = r[i][0];
            
        }
		alert('LOADED!');
    }
}

var Su = new Su();
