bgneal@312: $(document).ready(function() {
bgneal@312:    var postText = $('#id_body');
bgneal@312:    var postButton = $('#forums-reply-post');
bgneal@312:    postButton.click(function () {
bgneal@312:       $(this).attr('disabled', 'disabled').val('Posting reply...');
bgneal@312: 
bgneal@312:       var attachments = new Array()
bgneal@312:       $('#attachment div input').each(function(index) {
bgneal@312:          attachments[index] = $(this).val();
bgneal@312:       });
bgneal@312: 
bgneal@312:       $.ajax({
bgneal@673:          url: '/forums/quick-reply/',
bgneal@312:          type: 'POST',
bgneal@312:          data: {
bgneal@312:             body : postText.val(),
bgneal@312:             topic_id : $('#id_topic_id').val(),
bgneal@312:             attachment : attachments
bgneal@312:          },
bgneal@312:          traditional: true,
bgneal@312:          dataType: 'html',
bgneal@312:          success: function (data, textStatus) {
bgneal@312:             postText.val('');
bgneal@312:             var lastTr = $('#forum-topic tr:last');
bgneal@379:             var newClass = lastTr.hasClass('odd') ? 'even' : 'odd';
bgneal@379:             lastTr.after(data);
bgneal@379:             lastTr = $('#forum-topic tr:last');
bgneal@379:             lastTr.addClass(newClass);
bgneal@312:             lastTr.hide();
bgneal@312:             lastTr.fadeIn(3000);
bgneal@312:             postButton.removeAttr('disabled').val('Submit Reply');
bgneal@312:             initAttachments();
bgneal@312:          },
bgneal@312:          error: function (xhr, textStatus, ex) {
bgneal@673:             alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312:                xhr.responseText);
bgneal@312:             postButton.removeAttr('disabled').val('Submit Reply');
bgneal@312:             initAttachments();
bgneal@312:          }
bgneal@312:          });
bgneal@312:       return false;
bgneal@312:    });
bgneal@312:    $('a.post-flag').click(function () {
bgneal@312:       var id = this.id;
bgneal@312:       if (id.match(/fp-(\d+)/)) {
bgneal@312:          id = RegExp.$1;
bgneal@312:          if (confirm('Only flag a post if you feel it is spam, abuse, violates site rules, ' +
bgneal@312:                  'or is not appropriate. ' +
bgneal@312:                  'A moderator will be notified and will review the post. ' +
bgneal@312:                  'Are you sure you want to flag this post?')) {
bgneal@312:              $.ajax({
bgneal@312:                url: '/forums/flag-post/',
bgneal@312:                type: 'POST',
bgneal@673:                data: {id: id},
bgneal@312:                dataType: 'text',
bgneal@312:                success: function (response, textStatus) {
bgneal@312:                   alert(response);
bgneal@312:                },
bgneal@312:                error: function (xhr, textStatus, ex) {
bgneal@312:                   alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + xhr.responseText);
bgneal@312:                }
bgneal@312:              });
bgneal@312:          }
bgneal@312:      }
bgneal@312:      return false;
bgneal@312:    });
bgneal@312:    $('a.post-delete').click(function () {
bgneal@312:       var id = this.id;
bgneal@312:       if (id.match(/dp-(\d+)/)) {
bgneal@312:          id = RegExp.$1;
bgneal@312:          if (confirm('Are you sure you want to delete this post?')) {
bgneal@312:              $.ajax({
bgneal@312:                url: '/forums/delete-post/',
bgneal@312:                type: 'POST',
bgneal@673:                data: {id: id},
bgneal@312:                dataType: 'text',
bgneal@312:                success: function (response, textStatus) {
bgneal@312:                   alert(response);
bgneal@312:                   $('#post-' + id).fadeOut(3000);
bgneal@312:                },
bgneal@312:                error: function (xhr, textStatus, ex) {
bgneal@312:                   alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + xhr.responseText);
bgneal@312:                }
bgneal@312:              });
bgneal@312:          }
bgneal@312:      }
bgneal@312:      return false;
bgneal@312:    });
bgneal@312:    $('#forum-mod-del-topic').click(function () {
bgneal@312:          return confirm('Are you sure you want to delete this topic?\n' +
bgneal@312:             'WARNING: all posts will be lost.');
bgneal@312:    });
bgneal@312: 
bgneal@312:    var vid = 0;
bgneal@312:    var vidDiv = $('#attachment');
bgneal@312: 
bgneal@312:    function clearAttachments()
bgneal@312:    {
bgneal@312:       $('#attachment div').remove();
bgneal@312:       $('#attach-another').remove();
bgneal@312:    }
bgneal@312: 
bgneal@673:    function processEmbeds(data, textStatus)
bgneal@312:    {
bgneal@312:       vidDiv.find('img').remove();
bgneal@312:       $.each(data, function(index, value) {
bgneal@312:          var html = '<div id="video-' + index + '">' + value.html +
bgneal@312:             '<span class="link">' +
bgneal@312:             '<img src="/static/icons/television_delete.png" alt="Remove" /> ' +
bgneal@312:             '<a href="#">Remove</a></span>' +
bgneal@312:             '<input type="hidden" name="attachment" value="' + value.id + '" />';
bgneal@312:             '</div>';
bgneal@312:          vidDiv.append(html);
bgneal@312:          $('#video-' + index + ' a').click(function() {
bgneal@312:             $('#video-' + index).remove();
bgneal@312:             relabelAttachLink();
bgneal@312:             return false;
bgneal@312:          });
bgneal@312:       });
bgneal@312:       vid = data.length;
bgneal@312:       $('#video-' + (vid-1)).after('<a id="attach-another" href="#">Attach another video</a>');
bgneal@312:       $('#attach-another').click(function() {
bgneal@312:          addVideo();
bgneal@312:          relabelAttachLink();
bgneal@312:          return false;
bgneal@312:       });
bgneal@312:    }
bgneal@312: 
bgneal@312:    function initAttachments()
bgneal@312:    {
bgneal@312:       clearAttachments();
bgneal@312: 
bgneal@312:       var post_input = $('#id_post_id');
bgneal@312:       var attachments = $("#forums_post_form input:hidden[name='attachment']");
bgneal@312:       if (post_input.length == 1)
bgneal@312:       {
bgneal@312:          post_id = post_input.val();
bgneal@312:          vidDiv.prepend('<img src="/static/icons/ajax_busy.gif" alt="Busy" />');
bgneal@312:          $.ajax({
bgneal@673:             url: '/forums/fetch_attachments/',
bgneal@312:             type: 'GET',
bgneal@312:             data: {
bgneal@312:                pid : post_id
bgneal@312:             },
bgneal@312:             dataType: 'json',
bgneal@312:             success: processEmbeds,
bgneal@312:             error: function (xhr, textStatus, ex) {
bgneal@312:                vidDiv.find('img').remove();
bgneal@673:                alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312:                   xhr.responseText);
bgneal@312:             }
bgneal@312:          });
bgneal@312:       }
bgneal@312:       else if (attachments.length > 0)
bgneal@312:       {
bgneal@312:          vidDiv.prepend('<img src="/static/icons/ajax_busy.gif" alt="Busy" />');
bgneal@312:          var embeds = new Array();
bgneal@312:          attachments.each(function(index) {
bgneal@312:             embeds[index] = $(this).val();
bgneal@312:          });
bgneal@312:          attachments.remove();
bgneal@312:          $.ajax({
bgneal@673:             url: '/oembed/fetch_saved/',
bgneal@312:             type: 'GET',
bgneal@312:             data: {
bgneal@312:                embeds: embeds
bgneal@312:             },
bgneal@312:             traditional: true,
bgneal@312:             dataType: 'json',
bgneal@312:             success: processEmbeds,
bgneal@312:             error: function (xhr, textStatus, ex) {
bgneal@312:                vidDiv.find('img').remove();
bgneal@673:                alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312:                   xhr.responseText);
bgneal@312:             }
bgneal@312:          });
bgneal@312:       }
bgneal@312:       else
bgneal@312:       {
bgneal@312:          vid = 0;
bgneal@312:          var s = '<div id="init-add">' +
bgneal@312:             '<img src="/static/icons/television_add.png" alt="Add" /> ' +
bgneal@312:             '<a href="#">Attach Video</a></div>';
bgneal@312:          vidDiv.prepend(s);
bgneal@312:          $('#attachment a').click(function () {
bgneal@312:             $('#init-add').remove();
bgneal@312:             addVideo();
bgneal@312:             return false;
bgneal@312:          });
bgneal@312:       }
bgneal@312:    }
bgneal@312: 
bgneal@312:    function relabelAttachLink()
bgneal@312:    {
bgneal@312:       var another = $('#attach-another');
bgneal@312:       var n = $('#attachment div').length;
bgneal@312:       if (n == 0)
bgneal@312:       {
bgneal@312:          another.html("Attach a video");
bgneal@312:       }
bgneal@312:       else
bgneal@312:       {
bgneal@312:          another.html("Attach another video");
bgneal@312:       }
bgneal@312:    }
bgneal@312: 
bgneal@312:    function addVideo()
bgneal@312:    {
bgneal@312:       var id = "video-" + vid;
bgneal@312: 
bgneal@312:       var fakeForm = '<div id="' + id + '">' +
bgneal@312:          '<img src="/static/icons/television_add.png" alt="Attach" class="r" /> ' +
bgneal@312:          '<input type="text" size="45" class="r" /> <button type="button" class="r">Attach</button> ' +
bgneal@312:          '<a href="#" class="r">Remove</a><br /></div>';
bgneal@312: 
bgneal@312:       var n = $('#attachment div').length;
bgneal@312: 
bgneal@312:       var another = $('#attach-another');
bgneal@312:       if (n == 0)
bgneal@312:       {
bgneal@312:          if (another.length > 0)
bgneal@312:          {
bgneal@312:             another.before(fakeForm);
bgneal@312:          }
bgneal@312:          else
bgneal@312:          {
bgneal@312:             vidDiv.append(fakeForm);
bgneal@312:          }
bgneal@312:       }
bgneal@312:       else
bgneal@312:       {
bgneal@312:          $('#attachment div:last').after(fakeForm);
bgneal@312:       }
bgneal@312: 
bgneal@312:       $('#' + id + ' a').click(function() {
bgneal@312:          $('#' + id).remove();
bgneal@312:          relabelAttachLink();
bgneal@312:          return false;
bgneal@312:       });
bgneal@312: 
bgneal@312:       var vidText = $('#' + id + ' input');
bgneal@312: 
bgneal@312:       $('#' + id + ' button').click(function() {
bgneal@312:          var button = $(this);
bgneal@312:          button.attr('disabled', 'disabled');
bgneal@312:          $.ajax({
bgneal@673:             url: '/oembed/fetch/',
bgneal@312:             type: 'POST',
bgneal@312:             data: {
bgneal@312:                q : vidText.val()
bgneal@312:             },
bgneal@312:             dataType: 'json',
bgneal@312:             success: function (data, textStatus) {
bgneal@312:                $('#' + id + " .r").remove();
bgneal@312:                var myDiv = $('#' + id);
bgneal@312:                var html = '<span class="link">' +
bgneal@312:                   '<img src="/static/icons/television_delete.png" alt="Remove" /> ' +
bgneal@312:                   '<a href="#">Remove</a></span>' +
bgneal@312:                   '<input type="hidden" name="attachment" value="' + data.id + '" />';
bgneal@312:                myDiv.prepend(html);
bgneal@312:                myDiv.prepend(data.embed);
bgneal@312:                $('#' + id + ' a').click(function() {
bgneal@312:                   myDiv.remove();
bgneal@312:                   relabelAttachLink();
bgneal@312:                   return false;
bgneal@312:                });
bgneal@312:             },
bgneal@312:             error: function (xhr, textStatus, ex) {
bgneal@673:                alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312:                   xhr.responseText);
bgneal@312:                button.removeAttr('disabled');
bgneal@312:             }
bgneal@312:          });
bgneal@312:       });
bgneal@312: 
bgneal@312:       if (vid == 0)
bgneal@312:       {
bgneal@312:          $('#video-0').after('<a id="attach-another" href="#">Attach another video</a>');
bgneal@312:          $('#attach-another').click(function() {
bgneal@312:             addVideo();
bgneal@312:             relabelAttachLink();
bgneal@312:             return false;
bgneal@312:          });
bgneal@312:       }
bgneal@312:       ++vid;
bgneal@312:    }
bgneal@312: 
bgneal@312:    initAttachments();
bgneal@673: 
bgneal@673:    var topicTitle = $('#id_name');
bgneal@673:    var topicSearchButton = $('#search_topics');
bgneal@673:    var searchBusy = $('#search-busy-icon');
bgneal@673:    topicSearchButton.click(function () {
bgneal@673:       var text = $.trim(topicTitle.val());
bgneal@673:       if (!text) return;
bgneal@673: 
bgneal@673:       $(this).attr('disabled', 'disabled');
bgneal@673:       $('#quick-search-results').remove();
bgneal@673:       searchBusy.toggle();
bgneal@673: 
bgneal@673:       $.ajax({
bgneal@673:          url: '/search/ajax/',
bgneal@673:          type: 'GET',
bgneal@673:          data: {
bgneal@673:             q : text,
bgneal@673:             models : 'forums.topic'
bgneal@673:          },
bgneal@673:          traditional: true,
bgneal@673:          dataType: 'html',
bgneal@673:          success: function (data, textStatus) {
bgneal@673:             topicSearchButton.removeAttr('disabled');
bgneal@673:             searchBusy.hide();
bgneal@673:             searchBusy.after(data);
bgneal@673:             $('#hide-search-results').click(function() {
bgneal@673:                var results = $('#quick-search-results');
bgneal@673:                results.fadeOut(1500, function() { results.remove(); });
bgneal@673:             });
bgneal@673:          },
bgneal@673:          error: function (xhr, textStatus, ex) {
bgneal@673:             topicSearchButton.removeAttr('disabled');
bgneal@673:             searchBusy.hide();
bgneal@673:             alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@673:                xhr.responseText);
bgneal@673:          }
bgneal@673:          });
bgneal@673:       return false;
bgneal@673:    });
bgneal@312: });