/* Plugin för att visa innehåll i en tinymce editor.
   Ange vilket formulär det ska postas till, alla data i querystring kommer att postas till sparafunktionen.
  */
  

(function ($) {
  $.fn.tinyMceDialog = function(settings) {
    return this.each(function() {
      $(this).click(function() {


      $.extend({
            dialogHeight : 1200,
            dialogWidth : 500,
            editorType : 'Large'
         }, settings);


      
      //ta ut querystring för att lägga den till urlen som formuläret postar till.
      var url = $(this).attr('href');
      var qryString = url.substring(url.indexOf('?')+1, url.length);
      var windowTitle = $(this).attr('title');
      var dialogHeight = $(this).attr('data-dialogHeight');
      var dialogWidth = $(this).attr('data-dialogWidth');
      var editorType= $(this).attr('data-editorType');
      
      $contentHtmlShit = $().load($(this).attr('href'));
      
         //Skapa en main div och lägg på draggable.
         var $mainDiv = $('<div></div>')
            .css('position','fixed') 
            .css('color','#000') 
            .css('z-index','2000') 
            .css('top','20%') 
            .css('left','20%')
            .addClass('tinyMceDialogMainWindow');
         
         
         $EditorElement = $('<span></span>').load(url, function(data) { 
               // När data har laddats lägg på tinymce
              if (settings.editorType=='Large') {
                  $('textarea').tinymce({
                     // Location of TinyMCE script
                     script_url : "jscripts/tiny_mce/tiny_mce.js",
                     language: "sv",
                     mode: "textareas",
                     skin: "cirkuit",
                     theme: "advanced",
                     plugins: "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,autosave,imagemanager,filemanager",

                     // Theme options
                     theme_advanced_buttons1: "save,bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,formatselect,forecolor",
                     theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,search,replace,undo,redo,link,unlink,image,insertimage,insertfile,code,fullscreen,print",
                     theme_advanced_buttons3: "tablecontrols,hr,removeformat,charmap,media",
                     theme_advanced_toolbar_location: "top",
                     theme_advanced_toolbar_align: "left",
                     theme_advanced_statusbar_location: "bottom",
                     theme_advanced_resizing: false,
                     theme_advanced_blockformats: "H1=h1, H2=h2, H3=h3, H4=h4, H5=h5",
                     external_link_list_url: "jscripts/link_list.js",

                     height: settings.dialogHeight,
                     width: settings.dialogWidth,

                     content_css: "../includes/css/style.css",
                     theme_advanced_font_sizes: "10px,11px,12px,13px,14px,16px,18px,20px"
                  });            
               } 
               
            });
          $formElement = $('<form></form>')
         .attr('method','post')
         .attr('action','editor_save.asp?action=save&' + qryString)
         .addClass('tinyMceDialogFormElement');
         
         
         //Skapa stäng knappen.
         var $closeButton = $('<a href="#" id="clsButton">&nbsp;</a>')
            .addClass('tinyMceDialogCloseButton')
            .click(function() { 
               var i,t = tinyMCE.editors;
               for (i in t){
                  if (t.hasOwnProperty(i)) {
                     t[i].remove();
                  }
               }
               $mainDiv.remove(); 
            });
            
         var $titleDiv = $('<div></div>')
            .addClass('tinyMceDialogTitleBar')
            .html(windowTitle);
            
         $titleDiv.append($closeButton);
         
         $formElement.wrapInner($EditorElement);
         $mainDiv.wrapInner($titleDiv);
         //$mainDiv.append($formElement);
         $mainDiv.append($EditorElement);
         $mainDiv.draggable();
         
         $('body').append($mainDiv);
         
         return false;
      });
      
    });
  };
})(jQuery);
