$.extend($.facebox.settings, {
    loadingImage: '/media/css/images/loading.gif',
    closeImage: '/media/css/images/closelabel.gif'
})


function show_correct(correct, callback) {
    $('#panel > div').hide();
    correct.show(callback);
}

function move_footer() {
    $('#footer').animate({
           bottom: $('#panel').height()
    }, "slow")
}

function show_panel() {
    move_footer();
    $('#panel').show("slide");
}

function hide_panel() {
    $('#footer').animate({
        bottom: 0
    }, "slow")
    $('#panel').hide("slide");
}

function validate_comment_form(data, form) {
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: data,
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                $('div#facebox .body').css({
                    height: '',
                    width: ''
                });
                $.facebox('Your comment was sent.');                    

            } else {
                $('p.error', form).fadeOut().remove();
                for (x = 0; x < data.errors.length; x++) {
                    input = $('input[name=' + data.errors[x][0] + ']', form);
                    error = $('<p style="display: none" class="error">' + data.errors[x][1] + '</p>');
                    input.after(error);
                    
                    error.fadeIn("slow");
                }                
            }
        }
    });
}

function validate_form(data, form) {    
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: data,
        dataType: 'json',
        beforeSend: function (XMLHttpRequest) {
            $.facebox('Sending entry...');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $.facebox(textStatus);
        },
        success: function (data) {
            if (data.success) {
                $.facebox('Entry sent.');
                hide_panel();
            } else {
                $('p.error', form).fadeOut().remove();
                for (x = 0; x < data.errors.length; x++) {
                    input = $('#' + data.errors[x][0]);
                    
                    error = $('<p style="display: none" class="error">' + data.errors[x][1] + '</p>');
                       
                    input.after(error);
                    
                    error.fadeIn("slow");
                }
                
                move_footer();
                
                $.facebox('some errors');
            }
        }
    });
}

function comment_form() {
    $.facebox({ div: this.href });

    page = $(this).parents('div.page');
    content = $('.content img', page);
    o = content.offset()
    $('div#facebox').css({
        top: o.top,
        left: o.left
    });

    $('div#facebox .body').css({
        height: content.height() - 19,
        width: content.width() - 38
    });
}

$(document).ready(function () {
    $('#panel a.close').click(function() {
        hide_panel();
        return false;
    })
    
    $('a.out').attr({ target: "_blank" });
    
    $('a[rel*=facebox], a.comment_form').click(comment_form);

    $('#suggest_site').click(function() {
        if ($('#panel').is(':visible') && $('#entry_form').is(':visible')) {
            hide_panel();
        } else if($('#panel').is(':visible') && !$('#entry_form').is(':visible')) {
            hide_panel();
            show_correct($('#entry_form'), show_panel);
        } else {
            show_correct($('#entry_form'), show_panel);
        }
        return false;
    });
   
    $('#about').click(function() {
        if ($('#panel').is(':visible') && $('#info').is(':visible')) {
            hide_panel();
        } else if($('#panel').is(':visible') && !$('#info').is(':visible')) {
            hide_panel();
            show_correct($('#info'), show_panel);
        } else {
            show_correct($('#info'), show_panel);
        }
        return false;
    }); 

    $('.vote_input').rating({
        required: 'hide',
        callback: function (value, link) {
            var form = this.form;
            $.ajax({
                type: 'POST',
                url: form.action,
                data: {
                    'vote': value
                },
                dataType: 'json',
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //$.facebox(textStatus);
                },
                success: function (data) {
                    if (data.success) {
                        if (data.changed)
                            $.facebox('Your vote was changed.');
                        else
                            $.facebox('Your vote was sent.');
                    } else {
                        $.facebox(data.error_message);
                    }
                }
            });
        }
    });
    
    $("#entry_form form").submit(function() {
        form = $("#entry_form form");
        data = form.formSerialize();
        validate_form(data, form);
        return false;
    });

    $("form.comment").live('submit', function() {
        form = $(this);
        data = form.formSerialize();
        validate_comment_form(data, form);
        return false;
    });
});
