var answers = null;
var current_answer = -1;
function prev_answer() {
    if(current_answer > 0) {
        show_answer(--current_answer);
    }
}

function next_answer() {
    if(current_answer < answers.length - 1) {
        show_answer(++current_answer);
    }
}

function show_answer(idx) {
    $('#found-reg').fadeOut(400, function() {
        $('#answer-title').text(answers[idx]['tl']);
        $('#answer-text').html(answers[idx]['tt']);
        
        if (idx > 0) {
            $('#prev-button').show();
            $('#prev-button').attr('href', "#a" + (idx-1))
        } else {
            $('#prev-button').hide()
        }
        
        if (idx < answers.length - 1) {
            $('#next-button').show();
            $('#next-button').attr('href', "#a" + (idx+1))
        } else {
            $('#next-button').hide();
        }
        
        $('#show-answers-num').text('Answer ' + (idx+1) + ' of ' + answers.length);
        $('#found-reg').fadeIn(400)
    });
}
function load_answers_bill(id) {
	$.getJSON(
	    '/search/get-answers.json',
	    {'k': id},
		function(data, textStatus) {
			$('#found-answers').text(data.total);
			$('#found-answers-block').show();
			$('#loader').toggle();
		}
	);
}
function load_answers(id) {
	$.getJSON(
	    '/search/get-answers.json',
	    {'k': id},
	    function(data, textStatus) {
	        $('.found-answers').each(function(i) {
	            $(this).text(data.total);
	        });
            $('#found-search-hint').show();
	        $('#loader').toggle();
	        if(data.answers.length > 0) {
	            res = window.location.hash.match(/(?:#|&)a(\d+)/);
                if (res) {
                    current_answer = parseInt(res[1]);
                }
                
	            answers = data.answers;
	            if (current_answer < 0 || current_answer >= answers.length) {
	                current_answer = 0;
	            }
	            
	            show_answer(current_answer);
	        } else {
	            $('#found-unreg').toggle();
	        }
	    }
	);
}
function init_box(name) {
    $(window).scroll(function(e){
        if ($('#' + name + '-content').css('display') != 'none') {
            $('#' + name + '-content').css({
                    'marginTop' : (100 + $(window).scrollTop()) + 'px'
                })
        }
    })
}
function show_box(name) {
    $('#' + name + '-container').show();
}

function hide_box(name) {
    $('#' + name + '-container').hide();
}

function validate(f) {
    if ($(f.comment).val().length < 3) {
        alert("Please, fill comment field!");
        return false;
    }
    
    return true;
}
