$(document).ready( function() {

    bindClicks();

    $(document).keypress( function(evt) {
        if ($('td.selected').length == 0) {
            return;
        }
//        alert(evt.hasOwnProperty('keyCode'));
        code = evt.charCode ? evt.charCode : evt.keyCode;
        code = code - 48 ;
        if (code <= 9 & code >= 1) {
            $.post(
                'index.php', {
                    number: code,
                    id: $('td.selected').attr('id')
                },
                onInputSuccess
            );
//            $('td.selected').html(code);
        } else {
            $('td.selected').html('&nbsp;');
        }
    });

    $('#newgame').click( function() {
        if (!confirm('Закончить эту игру и начать новую?')) {
            return;
        }
        $.post(
            'index.php', {
                regenerate: 1
            },
            onNewgameSuccess
        );
    });

    $('#reveal').click( function() {
        if (!confirm('Открыть поле?')) {
            return;
        }
        $.post(
            'index.php', {
                reveal: 1
            },
            onRevealSuccess
        );
    });

    $('#hint').click( function() {
        if (!confirm('Использовать подсказку?')) {
            return;
        }
        $.post(
            'index.php', {
                hint: 1
            },
            onHintSuccess
        );
    });

    $('.switcher').click( function() {
	    var level;
        if ($(this).attr('id') == 'plus') {
            level = 1;
        } else {
            level = -1;
        }
        $.post(
            'index.php', {
                level: level
            },
            onLevelSuccess
        );
    });
});

function bindClicks() {
    $('.clickable').click( function() {
        $('.clickable').removeClass('selected');
        $(this).addClass('selected');
    });
}

function onNewgameSuccess(data) {
    $('#puzzle').html(data);
    $('#hint, #reveal').removeAttr('disabled');
    bindClicks();
}

function onRevealSuccess(data) {
    $('#puzzle').html(data);
    $('#hint, #reveal').attr('disabled', 'disabled');
    $('#newgame').focus();
}

function onHintSuccess(data) {
    $('#puzzle').html(data);
    if ($('.clickable').length == $('.correct').length) {
        $('#hint, #reveal').attr('disabled', 'disabled');
        $('#newgame').focus();
        alert('Поздравляем. Вы открыли все поле!');
    } else {
        bindClicks();
    }
}

function onInputSuccess(data) {

    info = eval('(' + data + ')');
    //number, correct, cell
    coord = info.cell.split('|');
    id = parseInt(coord[0])*9 + parseInt(coord[1]) + 1;
    if (info.correct == '1') {
        st = 'correct';
    } else {
        st = 'mistake'
    }
    str = '<span class="' + st + '">' + info.number + '</span>';
    $('#' + id).html(str);

    if (info.win == '1') {
        alert('Вы победили!');
        $('.clickable').removeClass('selected');
        $('.clickable').unbind('click');
        $('#hint, #reveal').attr('disabled', 'disabled');
        $('#newgame').focus();
    }
}

function onLevelSuccess(data) {
    $('#level').val(data + '/6');
}