if (typeof SR == "undefined") { var SR = {}; }

SR.Site = {
    DiscussionBoard: {
        settings: {},
        topics: [],
        
        init: function(view) {
            var global = this;
            
            if (view == 'topic_detail') {
                global.topic = SR.GLOBAL.topic;
                
                global.moderation_init();
                
                $('a.deleteReply').bind('click', function() {
                    var reply_id = $(this).getID();
                    var reply = $('li#reply-'+reply_id);
                    SR.Dialog.createWithPayload({
                        title: 'Delete Reply',
                        content: '<p>Are you sure that you want to delete this reply?</p>',
                        buttons: [
                            { label: 'Delete Reply', type: 'target', callback: function() {
                                $.ajax({ 
                                    url: '/dashboard/ajax/discussion/reply/'+reply_id+'/delete/', 
                                    type: 'POST', 
                                    data: {}, 
                                    beforeSend: function() {
                                        SR.Dialog.util.isLoading();
                                        SR.Dialog.reloadWithPayload({
                                            title: 'Delete Reply',
                                            content: '<p>Deleting reply...</p>'
                                        });
                                    },
                                    complete: function() {
                                        SR.Dialog.util.isDoneLoading();
                                    },
                                    success: function() {
                                        SR.Dialog.destroy();
                                        reply.remove();
                                        // Update the reply count
                                        var replies = $('ul.discussionTopic li').size();
                                        $('h4.repliesCount').text(replies.apnumber()+' Repl'+replies.pluralize('y', 'ies'));
                                    }, 
                                    error: function() { 
                                        SR.Dialog.createWithError('An error occured deleting your reply. Please try again momentarily.');
                                    }
                                }); 
                            }},
                            { label: 'Cancel', callback: function() { SR.Dialog.destroy(); }}
                        ]
                    });
                    return false;
                });
                

            } else {
                var DiscussionTopic = new Class({
                    __init__: function(self, topic, topic_id){
                        self.id = topic_id;
                        self.topic = topic;
                        self.title = self.topic.children('div.title').children('h4').text();
                        self.moderation = self.topic.children('div.meta').children('span.moderation');
                    
                        self.moderation.children('a').bind('click', function(event) {
                            if ($(this).hasClass('unapproveTopic')) {
                                self.unapprove();
                            } else if ($(this).hasClass('approveTopic')) { 
                                self.approve();
                            } else if ($(this).hasClass('deleteTopic')) {
                                SR.Dialog.createWithPayload({
                                    title: 'Delete '+self.title.shorten(40),
                                    content: '<p>Are you sure that you want to delete this topic?</p>',
                                    buttons: [
                                        { label: 'Delete Topic', type: 'target', callback: function() { self.destroy(); }},
                                        { label: 'Cancel', callback: function() { SR.Dialog.destroy(); }}
                                    ]
                                });
                            }
                            return false;
                        });
                    },
                    approve: function(self){
                        $.ajax({
                            url: '/dashboard/ajax/discussion/topic/'+self.id+'/accept/',
                            type: 'POST',
                            data: {},
                            dataType: 'json',
                            success: function(json) {
                                if (json.response == 'success') {
                                    self.topic.removeClass('unapproved').addClass('approved');
                                    self.moderation.children('span.unapproved').hide();
                                    self.moderation.children('a.approveTopic').hide();
                                    self.moderation.children('a.unapproveTopic').show();
                                } else {
                                    SR.Dialog.createWithError('An error occured while approving the topic titled <strong>"'+self.title+'"</strong>.');
                                }
                            },
                            error: function() {
                                SR.Dialog.createWithError('An error occured while approving the topic titled <strong>"'+self.title+'"</strong>. (500)');
                            }
                        });
                    },
                    unapprove: function(self){
                        $.ajax({
                            url: '/dashboard/ajax/discussion/topic/'+self.id+'/reject/',
                            type: 'POST',
                            data: {},
                            dataType: 'json',
                            success: function(json) {
                                if (json.response == 'success') {
                                    self.topic.removeClass('approved').addClass('unapproved');
                                    self.moderation.children('span.unapproved').show();
                                    self.moderation.children('a.approveTopic').show();
                                    self.moderation.children('a.unapproveTopic').hide();
                                } else {
                                    SR.Dialog.createWithError('An error occured while unapproving the topic titled <strong>"'+self.title+'"</strong>.');
                                }
                            },
                            error: function() {
                                SR.Dialog.createWithError('An error occured while unapproving the topic titled <strong>"'+self.title+'"</strong>. (500)');
                            }
                        });
                    },
                    destroy: function(self){
                        $.ajax({
                            url: '/dashboard/ajax/discussion/topic/'+self.id+'/delete/',
                            type: 'POST',
                            data: {},
                            dataType: 'json',
                            beforeSend: function() {
                                self.topic.hide();
                                SR.Dialog.destroy();
                            },
                            success: function(json) {
                                if (json.response == 'success') {
                                    self.topic.remove();
                                    // Gather all of the topics, remove all even and odd classes, and then re-stripe the entire table
                                    $('ul.discussionBoard li.topic').removeClass('even').removeClass('odd');
                                    $('ul.discussionBoard li.topic:even').addClass('odd');
                                    $('ul.discussionBoard li.topic:odd').addClass('even');
                                } else {
                                    self.topic.show();
                                    SR.Dialog.createWithError('An error occured while trying to delete the topic titled <strong>"'+self.title+'"</strong>.');
                                }
                            },
                            error: function() {
                                self.topic.show();
                                SR.Dialog.createWithError('An error occured while trying to delete the topic titled <strong>"'+self.title+'"</strong>. (500)');
                            }
                        });
                    }
                });
            
                $('li.topic').each(function(index) {
                    var topic_id = $(this).getID();
                    var topic = new DiscussionTopic($(this), topic_id);
                    global.topics.push(topic);
                });
            }
        },
        
        moderation_init: function() {
            var global = this;
            
            $('div.unapprovedNotice a').bind('click', function(event) {
                if ($(this).hasClass('approveTopic')) {
                    global.moderation_approve();
                } else if ($(this).hasClass('rejectTopic')) {
                    global.moderation_reject();
                }
                return false;
            });
        },
        
        moderation_approve: function() {
            var global = this;
            
            $.ajax({
                url: '/dashboard/ajax/discussion/topic/'+global.topic.id+'/accept/',
                type: 'POST',
                data: {},
                dataType: 'json',
                success: function(json) {
                    if (json.response == 'success') {
                        $('div.unapprovedNotice').addClass('approved').html(
                            '<p>This topic has been approved. It will now appear to your students and parents.</p>'+
                            '<p><a class="rejectTopic" href="#">Undo Approval (Hide Topic)</a></p>');
                        global.moderation_init();
                    } else {
                        SR.Dialog.createWithError('An error occured while approving this topic.');
                    }
                },
                error: function() {
                    SR.Dialog.createWithError('An error occured while approving this topic. (500)');
                }
            });
        },
        
        moderation_reject: function() {
            var global = this;
            
            $.ajax({
                url: '/dashboard/ajax/discussion/topic/'+global.topic.id+'/reject/',
                type: 'POST',
                data: {},
                dataType: 'json',
                success: function(json) {
                    if (json.response == 'success') {
                        $('div.unapprovedNotice').removeClass('approved').html(
                            '<p>Your discussion board is in moderation mode. You haven\'t approved this topic yet so you are the only one who can see it.</p>'+
                            '<p><a class="approveTopic" href="#">Approve This Topic</a> &bull; <a href="/dashboard/discussion/">Modify Discussion Board Settings</a></p>');
                        global.moderation_init();
                    } else {
                        SR.Dialog.createWithError('An error occured while unapproving this topic.');
                    }
                },
                error: function() {
                    SR.Dialog.createWithError('An error occured while unapproving this topic. (500)');
                }
            });
        }
        
    }
};
