var quoteContent = '';
var quoteAlias = '';
var displayed = false;

function forumTopicWatch (reqId) {
  sendJasonIsAwesome('forumTopicWatch', reqId, undefined, 'forums', 'community');
}
function forumTopicSticky (reqId) {
  sendJasonIsAwesome('forumTopicSticky', reqId, undefined, 'forums', 'community');
}

function deleteForumTopic ( reqId, admin) {
  if ( confirm('WARNING: Deleting a forum thread is irreversible! Are you sure you want to delete your thread?\n\nWARNING: Deleting your thread will also delete all replies to this thread that other people have made. Are you sure you want to do this?') ) {
    if (admin) {
    $('#deleteForumTopicDiv'+reqId).show();
    } else {
      sendJasonIsAwesome('deleteForumTopic', reqId, undefined, 'forums', 'community')
    }
  }
}
function deleteForumTopicThread ( reqId, admin ) {
  if ( confirm('WARNING: Deleting a forum reply is irreversible! Are you sure you want to delete your reply?') ) {
    if (admin) {
    $('#deleteForumTopicThreadDiv'+reqId).show();
    } else {
      sendJasonIsAwesome('deleteForumTopicThread', reqId, undefined, 'forums', 'community');
    }
  }
}

function quoteForumTopicText(quoteA, quoteId){
  showWysiwyg('createForumTopicThread', 'text');
  window.scrollTo(0,100000);
  quoteAlias = quoteA;
  quoteContent = $('div#Topic' + quoteId).html();
  
  if(displayed){
    var oEditor = FCKeditorAPI.GetInstance('text') ;
    FCKeditor_OnComplete(oEditor);
  }
  return true;
}

function quoteForumTopicThreadText(quoteA, quoteId){
  showWysiwyg('createForumTopicThread', 'text');
  window.scrollTo(0,100000);
  quoteAlias = quoteA;
  quoteContent = $('div#TopicThread' + quoteId).html();
  if(displayed){
    var oEditor = FCKeditorAPI.GetInstance('text') ;
    FCKeditor_OnComplete(oEditor);
  }
  return true;
}


function FCKeditor_OnComplete( editorInstance){
  displayed = true;
  if(editorInstance.Name == 'text'){
    if(quoteAlias.length > 1 && quoteContent.length > 1){
      editorInstance.InsertHtml('<blockquote style="background:#F3F3F3 none repeat scroll 0%; border:1px solid #AAAAAA; margin:5px; 	padding:5px;"><strong>' + quoteAlias + '</strong> said:<br /><br />' + quoteContent + '</blockquote><br />'); 
      quoteAlias = '';
      quoteContent = '';
    }
  }
  if($('form#createForumTopicForm input#name').get(0)){
    $('form#createForumTopicForm input#name').focus();
  } else {
    editorInstance.Focus();
  }
}



function showEditForumTopicForm(reqId){
  showWysiwyg("editForumTopicDiv" + reqId, 'text' + reqId);
}

function showEditForumTopicThreadForm(reqId){
  showWysiwyg("editForumTopicThreadDiv" + reqId, 'text' + reqId);
}

function toggleAttachPoll()
{
  var chk = $( 'input#attachPoll' ).get(0);
  var td = $( chk ).parent().get(0);
  var tr = $( td ).parent().get(0);
  var trs = $( tr ).nextAll( 'tr' );
  
  for( var i = 0; i < trs.length - 1; ++i )
  {
    var el = $( trs ).get(i);
   
    if( chk.checked ){
      $( el ).css( "display", "" );
    }
    else{
      $( el ).css( "display", "none" );
    }
  }
  
}

function toggleAddForumTopicPoll( ftId )
{
  document.saveForumTopicPoll.forumTopicId.value = ftId;
  $( 'div#add-forum-topic-poll-div' ).slideToggle();
}

function isForumTopicPollOptionValid()
{ 
  var q = $( 'textarea#questionText' ).get(0);
  if( !q ) return true;
  
  var numNonEmpty = 0;
  var form = q.form;
  
  if( form.attributes.name.value == 'createForumTopic' && form.attachPoll.checked != true ) return true;
  
  var ops = $( 'form[name="' + form.name + '"] input[type="text"]' ).get();
  for( var i = 0; i < ops.length; ++i )
  {
    if( ops[i].name.indexOf( 'pollOptionText' ) != -1 )
    {
      if( $.trim( ops[i].value ) != '' && ++numNonEmpty >= 2  )
        return true;
    }
  }
  
  return false;
}

function sendDeleteForumTopicPoll()
{
  if( !confirm( 'Deleted polls cannot be recovered. Are you sure you want to delete the poll attached to this thread?' ) ) return;
  
  sendJasonIsAwesome('deleteForumTopicPoll', '', undefined, 'forums', 'community');
}

function sendCloseForumTopic()
{
  if( !confirm( "Once closed no more replies will be allowed on this forum post!\nAre you sure you want to close this forum post?" ) ) return;
  sendJasonIsAwesome('closeForumTopic', '', undefined, 'forums', 'community');
}

function sendOpenForumTopic()
{
  sendJasonIsAwesome('openForumTopic', '', undefined, 'forums', 'community');
}

function toggleMoveForumTopic()
{
  $( 'div#move-forum-topic' ).slideToggle();
}