$(function() {
	$("input.balance").change(addBalance);
    $('.js-but').click(function() { switchEdit($(this).attr('id')); });
});

function addBalance() {
	var br = $('<br>');
	var copy = $(this).clone();
	copy.val('');
	$(this).after(br);
	br.after(copy);
	copy.change(addBalance);
	$(this).unbind('change');
}

function switchEdit(id) {
    var div = $('#js-input-' + id + ' .js-val');
    var but = $('#js-input-' + id + ' .js-but');

    var val = div.text();
    
    div.empty();
    div.append('<input id="js-value-' + id + '" type="text" value="' + val + '" />');

    but.attr('src', '/img/ico_ok.gif');

    but.unbind();
    but.click(function() {
        var newVal = $('#js-value-' + id).val();
        $.post('/index/partner-settings/', {name: id, value: newVal}, function(response) {
            div.empty();
            div.append(response);
            but.attr('src', '/img/ico_edit.gif');
            but.unbind();
            but.click(function() { switchEdit(id); });
        }, 'json');
    });
}

function delItem(id) {
    if (!confirm('Вы уверены, что хотитте удалить это объявление?')) {
        return false;
    }

    $.post('/index/partner-del/', {id: id}, function(response) {
        if (response) {
            window.location = '/index/partner/';
        }
    }, 'json');
}
