wmd_options = { autostart: false, buttons: "bold italic | link blockquote code image | ol ul heading" };

var wmdInstance = [];

$(function(){  
  checkEditablesOnPage( true );
  
  checkAddNewPageControls();
  
  checkRenamePageControls();
  
  setupPopupMessage();
});

function setupPopupMessage(){
  $("#popupMessage").hide();
  
  $("#popupMessage").click( function(){
    $(this).fadeOut();
  });
}

function showMessageBox( message ){
  $("#popupMessage .message").html( message );
  $("#popupMessage").slideDown("slow");
}

function checkAddNewPageControls( ){
  if( $("#CreateNewPage").length ){
    $("#AddNewPage").hide();
	
	$("#AddNewPage input[type=submit]").click( function(){
	  var postUrl = "/Admin/NewPage";
	  var postValues = $("#AddNewPage").serialize();
	  
	  $.post( postUrl, postValues, function(){}, "json" );
	});
	
	$("#CreateNewPage").click( function(){
	  $("#CreateNewPage").hide();
	  $("#AddNewPage").show();
	  
	  return false;
	});
  }
}

function checkRenamePageControls(){
  if( $(".RenamePage").length ){
    $(".RenamePage form").hide();

    $(".RenamePage a").click( function(){
      $(this).parent().find("form").toggle();
      $(this).parent().find("form input[type=submit]").click( function(){
        var postUrl = "Admin/RenamePage";
        var postValues = $(this).parent().serialize();
        
        $.post( postUrl, postValues, function(){}, "json" );
      });
      
      return false;
    });
  }
}

function checkEditablesOnPage( isNotJsonCall ){

  //if there are in place editors on the page
  if( $(".EditInPlace").length ){

    if( isNotJsonCall ){
      $(".EditInPlace form").hide();
      $(".EditInPlace form textarea").hide();
    }
    
    //Setup the click event for the submit button
    $(".EditInPlace form input[type=submit]").click( function(){
       var editId = $(this).parent().parent().attr('id').replace("Editor", "");
       var postUrl = "/Edit/" + editId;
       
       var parentId = $(this).parent().parent().attr('id');
       
       var postValues = getPostValues(parentId);
       
       //Does the json call and calls "processEditData" method when done
       $.post( postUrl, postValues, processEditData, "json" );
       
       return false;
    });
    
    //Makes the edit toggle button show and hide the form to edit
    $(".EditInPlace .EditToggle").click( function(){
      if( $(this).parent().find( 'form' ).is(':visible') ){
        $(this).parent().find( 'form' ).slideUp( "normal", function(){
          $(".EditInPlace form textarea").hide();
          destroyWmdInstance();
        });
      } else {
        $(".EditInPlace form textarea").show();
        $(this).parent().find( 'form' ).slideDown();
        createWmdInstance();
      }
    });
  }
}

function getPostValues( parentId ){
  return $( "#" + parentId + " form" ).serialize();
}

function processEditData( data ){

  destroyWmdInstance();
  
  var id = data.ObjectId;
  var message = data.Message;

  var result = data.Result;
  
  $("#" + id + "PageContent").html( result );
  
  showMessageBox( message );
  
  checkEditablesOnPage( false );
  
  createWmdInstance();
}

function createWmdInstance() {
  /***** Make sure WMD has finished loading *****/
  if (!Attacklab || !Attacklab.wmd) {
    return;
  } else {
    // get the dom elements
    var textarea = document.getElementById("MarkdownEditBox");
    var previewDiv = document.getElementById("WmdPreview");

    /***** build the preview manager *****/
    var panes = {input:textarea, preview:previewDiv, output:null};
    var previewManager = new Attacklab.wmd.previewManager(panes);

    /***** build the editor and tell it to refresh the preview after commands *****/
    var editor = new Attacklab.wmd.editor(textarea,previewManager.refresh);
    
    wmdInstance.push({ed:editor, pm:previewManager});
  }
}

function destroyWmdInstance(){
  var inst = wmdInstance.pop();
  
  if (inst){
    /***** destroy the editor and preview manager *****/
    inst.pm.destroy();
    inst.ed.destroy();
  }
}
