/**
 * Add photo rating
 */
function ratePhoto(id) {
    $('photo_rating_form').hide();
    new Ajax.Request('/photo-rating/add/id/' + id + '/', {
        method: 'post',
        parameters: $('photo_rating_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText;
            if (result == 'success') {
                $('photo_rating_form').innerHTML = 'You have rated this photo.';
                $('photo_rating_form').show();
            } else {
                $('photo_rating_form').show();
                alert(result);
            }
        },
        onFailure: function() {
            $('photo_rating_form').show();
            alert('Error performing request');
        }
    });
}

/**
 * Add photo set rating
 */
function ratePhotoSet(id) {
    $('photo_set_rating_form').hide();
    new Ajax.Request('/photo-set-rating/add/id/' + id + '/', {
        method: 'post',
        parameters: $('photo_set_rating_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText;
            if (result == 'success') {
                $('photo_set_rating_form').innerHTML = 'You have rated this photo set.';
                $('photo_set_rating_form').show();
            } else {
                $('photo_set_rating_form').show();
                alert(result);
            }
        },
        onFailure: function() {
            $('photo_set_rating_form').show();
            alert('Error performing request');
        }
    });
}

/**
 * Report a photo 
 */
function reportPhoto(id) {
    if (confirm('Are you sure you want to report this photo?')) {
        var reason = prompt('Why are you reporting this photo?', '');
        new Ajax.Request('/photos/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    alert('The photo has been reported');
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Alternate comment row colours
 */
function shadeComments() {
    var dark = true;
    $('comments').select('li').each(function(item) {
        if (dark) {
            item.addClassName('comments_dark');
            item.removeClassName('comments');
        } else {
            item.addClassName('comments');
            item.removeClassName('comments_dark');
        }
        dark = !dark;
    });
}

/**
 * Remove a photo comment
 */
function removePhotoComment(id) {
    if (confirm('Are you sure you want to delete this comment?')) {
        new Ajax.Request('/photo-comment/delete/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    $('comment_' + id).remove();
                    shadeComments();
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Remove a photo set comment
 */
function removePhotoSetComment(id) {
    if (confirm('Are you sure you want to delete this comment?')) {
        new Ajax.Request('/photo-set-comment/delete/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    $('comment_' + id).remove();
                    shadeComments();
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add a photo comment
 */
function addPhotoComment(id) {
    $('comment_form_button').disabled = true;
    new Ajax.Request('/photo-comment/add/id/' + id + '/', {
        method: 'post',
        parameters: $('comment_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText.evalJSON(true);
            if (result.success) {
                $('comments').insert({
                    top: new Template($('comment_template').innerHTML).evaluate(result)
                });
                shadeComments();
                $('comment_content').value = '';
            } else {
                alert(result.errors);
            }
            $('comment_form_button').disabled = false;
        },
        onFailure: function() {
            alert('Error performing request');
            $('comment_form_button').disabled = false;
        }
    });
}

/**
 * Add a photo set comment
 */
function addPhotoSetComment(id) {
    $('comment_form_button').disabled = true;
    new Ajax.Request('/photo-set-comment/add/id/' + id + '/', {
        method: 'post',
        parameters: $('comment_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText.evalJSON(true);
            if (result.success) {
                $('comments').insert({
                    top: new Template($('comment_template').innerHTML).evaluate(result)
                });
                shadeComments();
                $('comment_content').value = '';
            } else {
                alert(result.errors);
            }
            $('comment_form_button').disabled = false;
        },
        onFailure: function() {
            alert('Error performing request');
            $('comment_form_button').disabled = false;
        }
    });
}

/**
 * Report a photo comment 
 */
function reportPhotoComment(id) { 
    if (confirm('Are you sure you want to report this comment?')) {
        var reason = prompt('Why are you reporting this comment?', '');
        new Ajax.Request('/photo-comment/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The comment has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}

/**
 * Report a photo set comment 
 */
function reportPhotoSetComment(id) { 
    if (confirm('Are you sure you want to report this comment?')) {
        var reason = prompt('Why are you reporting this comment?', '');
        new Ajax.Request('/photo-set-comment/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The comment has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add blog rating
 */
function rateBlog(id) {
    $('blog_rating_form').hide();
    new Ajax.Request('/blog-rating/add/id/' + id + '/', {
        method: 'post',
        parameters: $('blog_rating_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText;
            if (result == 'success') {
                $('blog_rating_form').innerHTML = 'You have rated this blog.';
                $('blog_rating_form').show();
            } else {
                $('blog_rating_form').show();
                alert(result);
            }
        },
        onFailure: function() {
            $('blog_rating_form').show();
            alert('Error performing request');
        }
    });
}

/**
 * Report a blog 
 */
function reportBlog(id) { 
    if (confirm('Are you sure you want to report this blog?')) {
        var reason = prompt('Why are you reporting this blog?', '');
        new Ajax.Request('/blogs/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    alert('The blog has been reported');
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Remove a blog comment
 */
function removeBlogComment(id) {
    if (confirm('Are you sure you want to delete this comment?')) {
        new Ajax.Request('/blog-comment/delete/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    $('comment_' + id).remove();
                    shadeComments();
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add a blog comment
 */
function addBlogComment(id) {
    $('comment_form_button').disabled = true;
    new Ajax.Request('/blog-comment/add/id/' + id + '/', {
        method: 'post',
        parameters: $('comment_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText.evalJSON(true);
            if (result.success) {
                $('comments').insert({
                    top: new Template($('comment_template').innerHTML).evaluate(result)
                });
                shadeComments();
                $('comment_content').value = '';
            } else {
                alert(result.errors);
            }
            $('comment_form_button').disabled = false;
        },
        onFailure: function() {
            alert('Error performing request');
            $('comment_form_button').disabled = false;
        }
    });
}

/**
 * Report a blog comment 
 */
function reportBlogComment(id) { 
    if (confirm('Are you sure you want to report this comment?')) {
        var reason = prompt('Why are you reporting this comment?', '');
        new Ajax.Request('/blog-comment/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The comment has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}

/**
 * Delete an access
 */
function deleteAccess(username, id) {
    if (confirm('Are you sure you want to remove access for this person?')) {
        $('access_' + id).hide();
        new Ajax.Request('/access/delete/username/' + username + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result != 'success') {
                    $('access_' + id).show();
                    alert(result);
                }
            },
            onFailure: function() {
                $('access_' + id).show();
                alert('Error performing request');
            }
        });
    }
}

/**
 * Delete a favourite
 */
function deleteFavourite(username, id) {
    if (confirm('Are you sure you want to delete this person from your favourites?')) {
        $('favourite_' + id).hide();
        new Ajax.Request('/favourites/delete/username/' + username + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result != 'success') {
                    $('favourite_' + id).show();
                    alert(result);
                }
            },
            onFailure: function() {
                $('favourite_' + id).show();
                alert('Error performing request');
            }
        });
    }
}

/**
 * Delete a friend
 */
function deleteFriend(username, id) {
    if (confirm('Are you sure you want to delete this person from your friends?')) {
        $('friend_' + id).hide();
        new Ajax.Request('/friends/delete/username/' + username + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    new Ajax.Updater('box_myfriends_content', '/account/update-myfriends/', {
                        method: 'get'
                    });
                } else {
                    $('friend_' + id).show();
                    alert(result);
                }
            },
            onFailure: function() {
                $('friend_' + id).show();
                alert('Error performing request');
            }
        });
    }
}

/**
 * Delete a block
 */
function deleteBlock(username, id) {
    if (confirm('Are you sure you want to delete this person from your blocks?')) {
        $('block_' + id).hide();
        new Ajax.Request('/blocks/delete/username/' + username + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    new Ajax.Updater('box_myblocks_content', '/account/update-myblocks/', {
                        method: 'get'
                    });
                } else {
                    $('block_' + id).show();
                    alert(result);
                }
            },
            onFailure: function() {
                $('block_' + id).show();
                alert('Error performing request');
            }
        });
    }
}

/**
 * Delete a mail message
 */
function deleteMail(id) {
    if (confirm('Are you sure you want to delete this mail?')) {
        $('mail_' + id).hide();
        new Ajax.Request('/mail/delete-received/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result != 'success') {
                    $('mail_' + id).show();
                    alert(result);
                }
            },
            onFailure: function() {
                $('mail_' + id).show();
                alert('Error performing request');
            }
        });
    }
}

/**
 * Report a mail message
 */
function reportMail(id) { 
    if (confirm('Are you sure you want to report this mail?')) {
        var reason = prompt('Why are you reporting this mail?', '');
        new Ajax.Request('/mail/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The mail has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}

/**
 * Leave a group
 */
function leaveGroup(id) {
    if (confirm('Are you sure you want to leave this group?')) {
        $('group_' + id).hide();
        new Ajax.Request('/my-groups/leave/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result != 'success') {
                    $('group_' + id).show();
                    alert(result);
                }
            },
            onFailure: function() {
                $('group_' + id).show();
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add user rating
 */
function rateUser(username) {
    $('user_rating_form').hide();
    new Ajax.Request('/user-rating/add/username/' + username + '/', {
        method: 'post',
        parameters: $('user_rating_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText;
            if (result == 'success') {
                $('user_rating_form').innerHTML = 'You have rated this user.';
                $('user_rating_form').show();
                $('right_rating').hide();
            } else {
                $('user_rating_form').show();
                alert(result);
            }
        },
        onFailure: function() {
            $('user_rating_form').show();
            alert('Error performing request');
        }
    });
}

/**
 * Report a user 
 */
function reportUser(username) { 
    if (confirm('Are you sure you want to report this user?')) {
        var reason = prompt('Why are you reporting this user?', '');
        new Ajax.Request('/user/report/username/' + username + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The user has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}

/**
 * Remove a user comment
 */
function removeUserComment(id) {
    if (confirm('Are you sure you want to delete this comment?')) {
        new Ajax.Request('/user-comment/delete/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    $('comment_' + id).remove();
                    shadeComments();
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add a user comment
 */
function addUserComment(username) {
    $('comment_form_button').disabled = true;
    new Ajax.Request('/user-comment/add/username/' + username + '/', {
        method: 'post',
        parameters: $('comment_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText.evalJSON(true);
            if (result.success) {
                $('comments').insert({
                    top: new Template($('comment_template').innerHTML).evaluate(result)
                });
                shadeComments();
                $('comment_content').value = '';
            } else {
                alert(result.errors);
            }
            $('comment_form_button').disabled = false;
        },
        onFailure: function() {
            alert('Error performing request');
            $('comment_form_button').disabled = false;
        }
    });
}

/**
 * Report a user comment 
 */
function reportUserComment(id) { 
    if (confirm('Are you sure you want to report this comment?')) {
        var reason = prompt('Why are you reporting this comment?', '');
        new Ajax.Request('/user-comment/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The comment has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add video rating
 */
function rateVideo(id) {
    $('video_rating_form').hide();
    new Ajax.Request('/video-rating/add/id/' + id + '/', {
        method: 'post',
        parameters: $('video_rating_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText;
            if (result == 'success') {
                $('video_rating_form').innerHTML = 'You have rated this video.';
                $('video_rating_form').show();
            } else {
                $('video_rating_form').show();
                alert(result);
            }
        },
        onFailure: function() {
            $('video_rating_form').show();
            alert('Error performing request');
        }
    });
}

/**
 * Report a video comment 
 */
function reportVideoComment(id) { 
    if (confirm('Are you sure you want to report this comment?')) {
        var reason = prompt('Why are you reporting this comment?', '');
        new Ajax.Request('/video-comment/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The comment has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}

/**
 * Remove a video comment
 */
function removeVideoComment(id) {
    if (confirm('Are you sure you want to delete this comment?')) {
        new Ajax.Request('/video-comment/delete/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    $('comment_' + id).remove();
                    shadeComments();
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add a video comment
 */
function addVideoComment(id) {
    $('comment_form_button').disabled = true;
    new Ajax.Request('/video-comment/add/id/' + id + '/', {
        method: 'post',
        parameters: $('comment_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText.evalJSON(true);
            if (result.success) {
                $('comments').insert({
                    top: new Template($('comment_template').innerHTML).evaluate(result)
                });
                shadeComments();
                $('comment_content').value = '';
            } else {
                alert(result.errors);
            }
            $('comment_form_button').disabled = false;
        },
        onFailure: function() {
            alert('Error performing request');
            $('comment_form_button').disabled = false;
        }
    });
}

/**
 * Report a video 
 */
function reportVideo(id) {
    if (confirm('Are you sure you want to report this video?')) {
        var reason = prompt('Why are you reporting this video?', '');
        new Ajax.Request('/videos/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    alert('The video has been reported');
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Remove a group comment
 */
function removeGroupComment(id) {
    if (confirm('Are you sure you want to delete this comment?')) {
        new Ajax.Request('/group-comment/delete/id/' + id + '/', {
            method: 'post',
            onSuccess: function(transport) {
                result = transport.responseText;
                if (result == 'success') {
                    $('comment_' + id).remove();
                    shadeComments();
                } else {
                    alert(result);
                }
            },
            onFailure: function() {
                alert('Error performing request');
            }
        });
    }
}

/**
 * Add a group comment
 */
function addGroupComment(id) {
    $('comment_form_button').disabled = true;
    new Ajax.Request('/group-comment/add/id/' + id + '/', {
        method: 'post',
        parameters: $('comment_form').serialize(true),
        onSuccess: function(transport) {
            result = transport.responseText.evalJSON(true);
            if (result.success) {
                $('comments').insert({
                    top: new Template($('comment_template').innerHTML).evaluate(result)
                });
                shadeComments();
                $('comment_content').value = '';
            } else {
                alert(result.errors);
            }
            $('comment_form_button').disabled = false;
        },
        onFailure: function() {
            alert('Error performing request');
            $('comment_form_button').disabled = false;
        }
    });
}

/**
 * Report a group comment 
 */
function reportGroupComment(id) { 
    if (confirm('Are you sure you want to report this comment?')) {
        var reason = prompt('Why are you reporting this comment?', '');
        new Ajax.Request('/group-comment/report/id/' + id + '/', {
            method: 'post',
            parameters: {
                'reason': reason
            },
            onSuccess: function(transport){
                result = transport.responseText;
                if (result == 'success') {
                    alert('The comment has been reported');
                }
                else {
                    alert(result);
                }
            },
            onFailure: function(){
                alert('Error performing request');
            }
        });
    }
}