
  function ForumHandler()
  {
    this.discussionFeedLength = 20;
  }

  ForumHandler.prototype.fetchDiscussions = function(moreResults)
  {
    fph.discussionFeedLength += 20;
    $(".feedLoading").removeClass("noShow");
    $(".moreResultsLink").addClass("noShow");
    //Load the feed
    var randomnumber=Math.floor(Math.random()*10000)
    $(".ForumDiscussionItems").load("/index_FetchFeedForum.php?c=" + $("#categoryID").val() + "&rnd="+randomnumber+"&l="+fph.discussionFeedLength,null,function () { $(".feedLoading").addClass("noShow"); $(".moreResultsLink").removeClass("noShow"); } );
  }
  
  ForumHandler.prototype.postMessage = function(categoryId,discussionId,profileId,newsId,eventID)
  {
    var title = '';
    var body = $('#writeSomething').val();
    body = body.replace(/\+/g, "&#43");
    body = body.replace(/\\/g, "&#92");
    var emailNotify = $('#emailNotify').val();
    document.getElementById("postLoadingSection").className = "";
    $("#btnPostMessage").addClass("noShow");
    $.ajax({ 
        type: 'POST',
        url: '/kingtipping/Forum_Post_Add.php', 
        cache: false, 
        data: "c=" + escape(categoryId) + "&d=" + escape(discussionId) + "&p=" + escape(profileId) + "&n=" + escape(newsId) + "&ev=" + escape(eventID) + "&t=" + escape(title) + "&b=" + escape(body) + "&e=" + escape(emailNotify),
        success: function(data,textStatus) { 
          var splitResponse = data.split(',');
          if (splitResponse.length > 0 && splitResponse[0] == 1)
          {
            //It depends on which way the page is ordered!
            if ($('#ForumPostsOrder').val() == 1)
            {
              $('.ForumPostsOuter').prepend(data.substring(splitResponse[0].length+1));
              $('.ForumPostsOuter').prepend($('#ForumCreate'));
            }
            else
            {
              $('.ForumPostsOuter').append(data.substring(splitResponse[0].length+1));
              $('.ForumPostsOuter').append($('#ForumCreate'));
            }
            $('.ForumPostBottom').addClass('ForumPostBottomBorder');
            $('#writeSomething').val('');
            $('#writeSomething').hint();
            $('#NoComments').addClass('noShow');
          }
          else
          {
            //Failed
            var message = 'An unknown error occurred while attempting to add this post.<br/><br/>Please refresh the page and try again.';
            if (splitResponse.length > 1 && splitResponse[0] == 0)
            {
              message = data.substring(splitResponse[0].length+1);
            }
            showFacebox('Error',
              '<div class="error">' + message + '</div>',
              '',
              false);
          }
          $("#btnPostMessage").removeClass("noShow");
          document.getElementById("postLoadingSection").className = "noShow";
        },
        error: function() { 
          showFacebox('Error',
              '<div class="error">' + message + '</div>',
              '',
              false);
          $("#btnPostMessage").removeClass("noShow");
          document.getElementById("postLoadingSection").className = "noShow";
        }
    });
    pageTracker._trackEvent('Forum', 'Post');
  }

  ForumHandler.prototype.loadOuterCut = function()
  {
    $(".feedLoading").removeClass("noShow");
    $(".middleLoaderLink").addClass("noShow");
    var discussionId = $("#discussionID").val();
    var totalNumberOfPosts = $("#totalPosts").val();
    $.ajax({ 
        type: 'GET',
        url: '/kingtipping/Forum_OuterCut.php', 
        cache: true, 
        data: "d=" + escape(discussionId) + "&t=" + escape(totalNumberOfPosts),
        success: function(data,textStatus) { 
          var splitResponse = data.split(',');
          if (splitResponse.length > 0 && splitResponse[0] == 1)
          {
            $('.moreResultsOuter').html(data.substring(2));
            $('.ForumPostBottom').addClass('ForumPostBottomBorder');
            $('.moreResultsOuter').removeClass('moreResultsOuter');
          }
          else
          {
            //Failed
            var message = 'An unknown error occurred while attempting to load the additional posts.<br/><br/>Please refresh the page and try again.';
            if (splitResponse.length > 1 && splitResponse[0] == 0)
            {
              message = data.substring(splitResponse[0].length+1);
            }
            showFacebox('Error',
              '<div class="error">' + message + '</div>',
              '',
              false);
          }
        },
        error: function() { 
          var message = 'An unknown error occurred while attempting to load the additional posts.<br/><br/>Please refresh your page and try again.';
          showFacebox('Error',
              '<div class="error">' + message + '</div>',
              '',
              false);
        }
    });
    pageTracker._trackEvent('Forum', 'Load Outer Cut');
    return false; //Stop the browser from loading the full page
  }

  ForumHandler.prototype.removePost = function(postID, removeType)
  {
    fph.confirmedRemovePost(postID, removeType);
  }

  ForumHandler.prototype.confirmedRemovePost = function(postID, removeType)
  {
    $.ajax({ 
        url: '/kingtipping/Forum_Post_Remove.php', 
        cache: false, 
        data: "p=" + postID + "&t=" + removeType,
        success: function(data,textStatus) { 
          var splitResponse = data.split(',');
          if (splitResponse.length > 0 && splitResponse[0] == 1 && splitResponse[1] > 0)
          {
            $('#post' + splitResponse[1].toString()).children('.ForumPostRight').children('.ForumPostRightInner').children('.ForumPostContent').prepend('<div class="ForumPostRemovedMessage">' + data.substring(splitResponse[0].length+splitResponse[1].length+2)  + '</div>');
            $('#post' + splitResponse[1].toString()).children('.ForumPostLeft').addClass('ForumPostRemoved');
            $('#post' + splitResponse[1].toString()).children('.ForumPostRight').children('.ForumPostHead').addClass('ForumPostRemoved');
            $('#post' + splitResponse[1].toString()).children('.ForumPostRight').children('.ForumPostBody').addClass('ForumPostRemoved');
            $('#post' + splitResponse[1].toString()).children('.ForumPostRight').children('.ForumPostRightInner').children('.ForumPostContent').children('.ForumPostHeadRight').addClass('noShow');
          }
          else
          {
            //Failed
            var message = 'An unknown error occurred while attempting to remove this post.<br/><br/>Please refresh your page and try again.';
            if (splitResponse.length > 1)
            {
              message = data.substring(splitResponse[0].length+1);
            }
            showFacebox('Error',
                '<div class="error">'+message+'</div>',
                '',
                false);
          }
        },
        error: function() { 
          showFacebox('Error',
              '<div class="error">An unexpected occurred while attempting to remove this post.<br/><br/>Please refresh your page and try again.</div>',
              '',
              false);
        }
    }); 
    pageTracker._trackEvent('Forum', 'Remove Post');
  }

  ForumHandler.prototype.createDiscussion = function(categoryId)
  {
    var name = $("#txtNewDiscussionName").val();
    $("#btnNewDiscussion").addClass("noShow");
    $("#btnNewDiscussionLoading").removeClass("noShow");
    
    $.ajax({ 
        url: '/kingtipping/Forum_Discussion_Add.php', 
        cache: false, 
        data: "n=" + escape(name) + "&c=" + escape(categoryId),
        success: function(data,textStatus) { 
          var splitResponse = data.split(',');
          if (splitResponse.length == 3 && parseInt(splitResponse[0]) && splitResponse[0] == 1)
          {
            discussionId = splitResponse[1];
            var discussionName = $("#txtNewDiscussionName").val();
            $("#btnNewDiscussion").removeClass("noShow");
            $("#btnNewDiscussionLoading").addClass("noShow");
            $("#NewDiscussionItem").clone().prependTo(".ForumDiscussionItems").removeClass("noShow").attr("id","Discussion" + discussionId);
            $("#Discussion" + discussionId+" .newsBody h3 a").html(discussionName);
            $("#Discussion" + discussionId+" .newsBody a.discussionLink").each(function () { $(this).attr("href","/forum/NewDiscussion_"+discussionId+"/"); } );
            $("#txtNewDiscussionName").val('');
          }
          else
          {
            var message = 'An unknown error occurred while attempting to add this discussion.<br/><br/>Please refresh your page and try again.';
            if (splitResponse.length == 2)
            {
              message = splitResponse[1];
            }
            showFacebox('Error',
              '<div class="error">' + message + '</div>',
              '',
              false);
            $("#btnNewDiscussion").removeClass("noShow");
            $("#btnNewDiscussionLoading").addClass("noShow");
          }
        },
        error: function() { 
          $("#btnNewDiscussion").removeClass("noShow");
          $("#btnNewDiscussionLoading").addClass("noShow");
          showFacebox('Error',
            '<div class="error">An error occurred while attempting to add this discussion.<br/><br/>Please refresh your page and try again.</div>',
            '',
            false);
        }
    }); 
  }

  var fph = new ForumHandler();

  $(document).ready(function() 
  {
    $('#writeSomething').autogrow();
    $('#writeSomething').val('');
    $('#writeSomething').hint();
    $("#txtNewDiscussionName").val('');
    $("#txtNewDiscussionName").hint();
    $(".middleLoaderLink").click(fph.loadOuterCut);
  });


