var TALENT = {};

$(document).ready(function() {
    if (!$('#name_show').length) {
        return;
    }
    $('.interviews > li').each(function(e) {
        $(this).append($('<span class="report"></span>')
                       .attr('id', 'report_' + this.id)
                       .click(function() {
                           TALENT.sendReport('/report/create.json', {
                               target: 'interview',
                               target_id: this.id,
                               one_time_token: $('input[name="one_time_token"]:first').val()
                           });
                       })
                       .text('[不適切を報告]'))
    });
    $('.official').each(function(e) {
        $(this).append($('<span class="report"></span>')
                       .attr('id', 'report_' + this.id)
                       .click(function() {
                           TALENT.sendReport('/report/create.json', {
                               target: this.id.replace(/^report_/, '').replace(/_[0-9]+$/, ''),
                               target_id: this.id,
                               one_time_token: $('input[name="one_time_token"]:first').val()
                           });
                       })
                       .text('[公式のサイトではないので報告する]'))
    });
});

TALENT.sendReport = function(endPoint, data) {
    $.ajax({
        "type": "POST",
        "url": endPoint,
        "data": data,
        "success": function(json) {
            json = eval('(' + json + ')');
            if (json.target_id) {
                $('#' + json.target_id).hide();
            }
        }
    });
};

TALENT.sendRequest = function(endPoint, data, callback) {
    $.ajax({
        "type": "POST",
        "url": endPoint,
        "data": data,
        "success": function(json) {
            callback(json);
        }
    });
}

TALENT.fan = {};
TALENT.fan.create = function(form) {
    var endPoint = form.action;
    var data = {
        name_id: form.name_id.value,
        one_time_token: form.one_time_token.value
    };
    $.ajax({
        "type": "POST",
        "url": endPoint,
        "data": data,
        "success": function(json) {
            json = eval('(' + json + ')');
            $('#fan-create').toggleClass('show');
            $('#fan-delete').toggleClass('show');
            $('#fan-count').text(json.fan_count);
        }
    });
    return false;
}
TALENT.fan._delete = function(form) {
    var endPoint = form.action;
    var data = {
        name_id: form.name_id.value,
        one_time_token: form.one_time_token.value
    };
    $.ajax({
        "type": "POST",
        "url": endPoint,
        "data": data,
        "success": function(json) {
            json = eval('(' + json + ')');
            $('#fan-create').toggleClass('show');
            $('#fan-delete').toggleClass('show');
            $('#fan-count').text(json.fan_count);
        }
    });
    return false;
}

