/**
 * jQuery TinyMCE (http://mktgdept.com/jquery-tinymce-plugin)
 * A jQuery plugin for unobtrusive TinyMCE
 *
 * v0.0.2 - 28 August 2009
 *
 * Copyright (c) 2009 Chad Smith (http://twitter.com/chadsmith)
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-license.php
 *
 * Add TinyMCE to an element using: $(element).tinymce([settings]);
 *
 **/
;jQuery.fn.tinymce=jQuery.fn.tinyMCE=jQuery.fn.TinyMCE=function(d,e){return this.each(function(i){var a,b,c=this.id=this.id||this.name||(this.className||'jMCE')+i;if(d&&Object===d.constructor){e=d;d=null}if(!d&&tinyMCE.get(c)){d='remove';b=true}switch(d){case'remove':a='mceRemoveControl';break;case'toggle':a='mceToggleEditor';break;default:a='mceAddControl'}tinyMCE.settings=e;tinyMCE.execCommand(a,false,c);if(b)$(this).tinyMCE(e)})};

/**
 * init tinymce on given selector/element or on default textareas with class 'tinymce'
 *
 * @param element selector or element
 * @return void
 */
$.setupTinymce = function (element) {
  var default_options = {
    relative_urls: false,
    remove_script_host: true,
    // Location of TinyMCE script
    script_url : '/tiny_mce/tiny_mce.js',
    content_css : "/css/tinyview_styles.css",
    // General options
    //TODO get current language from dom
    language: "de",
    theme : "advanced",
    skin : "fre", 
    mode: "exact",
    plugins : "advlist, paste", 
    // Theme options
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sup,|,formatselect,|,cut,copy,paste,|,bullist,|,link,unlink,|,outdent,indent,|,cleanup,|,code",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_path : false,
    theme_advanced_resizing : true,
    theme_advanced_resize_horizontal : false,
    theme_advanced_blockformats : "p,h1,h2",
    theme_advanced_link_targets : "advanced_dlg.link_target_modal=modal",
    theme_advanced_styles : "Redaktionelles Special=editorialSpecial", 
    advlist_bullet_styles: [
      {
        'title': 'advlist.def',
        'styles': {
          'listStyleType': ''
        },
        'class': 'genericList'
      },
      {
        'title': 'advlist.checklist',
        'styles': {
          'listStyleType': ''
        },
        'class': 'checklist'
      },
      {
        'title': 'advlist.downloadDocs',
        'styles': {
          'listStyleType': ''
        },
        'class': 'downloadDocs'
      }
    ],
    // paste plugin options
    paste_auto_cleanup_on_paste: false, 
    paste_remove_styles: true, 
    paste_remove_styles_if_webkit: true,
    paste_strip_class_attributes: "mso", 
    paste_remove_spans: false, 
    paste_convert_middot_lists: true, 
    paste_retain_style_properties: "none", 
    // debug off
    debug: false
  };
  var simple_options = {
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sup,|,formatselect,|,cut,copy,paste,|,link,unlink,|,code"
  };
  var simpleinline_options = {
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sup,|,cut,copy,paste,|,link,unlink,|,code",
    convert_newlines_to_brs: true,
    force_p_newlines : false,
    force_br_newlines : true,
    forced_root_block : ''
  };
  var table_options = {
    plugins : "table, advlist, paste",
    theme_advanced_buttons1 : "table,|,row_props,|,cell_props,|,col_after,col_before,row_after,row_before|,delete_col,delete_row,delete_table",
    theme_advanced_buttons2 : "bold,italic,underline,strikethrough,sup,|,formatselect,|,cut,copy,paste,|,bullist,|,link,unlink,|,code"
  };
  var forum_options = {
    bd_user_frontend : true,
    plugins : "emotions, inlinepopups",
    dialog_type : "modal",
    inlinepopups_skin: "clearlooks2", 
    invalid_elements : "table, object,embed,iframe",
    theme_advanced_buttons1 : "bold,italic,underline,|,fontsizeselect,forecolor,|,link,unlink,|,emotions"
  };
  var justformat_options = {
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sup,|,cut,copy,paste,|,code",
    convert_newlines_to_brs: true,
    force_p_newlines : false,
    force_br_newlines : true,
    forced_root_block : ''
  };
  var mail_options = {
    theme_advanced_buttons1 : "styleselect,|,cut,copy,paste,|,code",
    element_format : 'html', 
    convert_fonts_to_spans : false, 
    style_formats : [
            {title : 'Fett', inline : 'b'},
            {title : 'Kursiv', inline : 'i'},
            {title : 'Unterstrichen', inline : 'u'},
            {title : 'Überschrift', inline : 'font', attributes: { face: 'Arial,Helvetica,sans-serif', size: '4' } }], 
    convert_newlines_to_brs: true,
    force_p_newlines : false,
    force_br_newlines : true,
    forced_root_block : ''
  };
  
  var o = {};
  if(element) {
    if($(element).hasClass('tinymce')) {
      o = default_options;
      $(element).tinymce(o);
    }
    else if($(element).hasClass('tinymce_simple')) {
      o = $.extend({}, default_options, simple_options);
      $(element).tinymce(o);
    }
    else if($(element).hasClass('tinymce_simpleinline')) {
      o = $.extend({}, default_options, simpleinline_options);
      $(element).tinymce(o);
    }
    else if($(element).hasClass('tinymce_table')) {
      o = $.extend({}, default_options, table_options);
      $(element).tinymce(o);
    }
    else if($(element).hasClass('tinymce_forum')) {
      o = $.extend({}, default_options, forum_options);
      $(element).tinymce(o);
    }
    else if($(element).hasClass('tinymce_justformat')) {
      o = $.extend({}, default_options, justformat_options);
      $(element).tinymce(o);
    }
    else if($(element).hasClass('tinymce_mail')) {
      o = $.extend({}, default_options, mail_options);
      $(element).tinymce(o);
    }
  }
  else {
    $('textarea.tinymce').tinymce(default_options);
    o = $.extend({}, default_options, simple_options);
    $('textarea.tinymce_simple').tinymce(o);
    o = $.extend({}, default_options, simpleinline_options);
    $('textarea.tinymce_simpleinline').tinymce(o);
    o = $.extend({}, default_options, justformat_options);
    $('textarea.tinymce_justformat').tinymce(o);
    o = $.extend({}, default_options, table_options);
    $('textarea.tinymce_table').tinymce(o);
    o = $.extend({}, default_options, forum_options);
    $('textarea.tinymce_forum').tinymce(o);
    o = $.extend({}, default_options, mail_options);
    $('textarea.tinymce_mail').tinymce(o);
  }
};

function init_tinymce(element)
{
  $.setupTinymce(element);
}

$(document).ready(function() {
  init_tinymce();
});

