// tiny MCE -
//    jQuery plugin for accessible, unobtrusive WYSIWYG HTML editing
// v .1
// by Alton Crossley
// changed by Volker Nötzold
// http://www.nogahidebootstrap.com/jtinymce/
// Free beer and free speech. Enjoy!
// The cool part is oh-so-simple

//if (!jQuery.browser.opera){
if (true){
  $.fn.tinymceRemove = function(options) {
    return this.each(function() {
      $('#editTextButton').hide();
      $(this)
      .unbind('click')
      .Tooltip();
  //    .unbind('hover')	// geht nicht ...
      if ($.browser.msie && $.browser.version<7){
        $(this).hover( function(){$(this).removeClass('editHover');}, function(){$(this).removeClass('editHover');});
      }
    });
  }
}


$.fn.tinymce = function(options) {
  return this.each(function() {
    var tmp=this.id;
    $(this)
    .attr('title','Click to edit this')
    .Tooltip({track: true,delay: 0,opacity: 1})
    .click(function(event) {
      toogleEditorMode(tmp);
      $('#editTextButton').val("Normal view").unbind().fadeIn('slow').click(function(){
        $(this).fadeOut('slow',function(){
          $(this).val("Save").fadeIn('slow').unbind().click(function(){
            $.post("/ajax.php",{content: $("#"+tmp).html(),id: tmp, type:"updateDBContent"},function(txt,textStatus){
              if(txt){$("#"+tmp).html(txt);}
            });
            $(this).val("Saving").fadeOut('slow');
          });
        }) ;
        toogleEditorMode(tmp);
        $("#"+tmp).removeClass('editHover');
      });
    });
    if ($.browser.msie && $.browser.version<7){
      $(this).hover( function(){$(this).addClass('editHover');}, function(){$(this).removeClass('editHover');}) ;
    }
  });
}

// this is where we decide the toggle of 'on' (true) or 'off' (false)
// init an array to keep each id's state seperate
var tinyMCEmode = new Array();
// this is called by the click method
function toogleEditorMode(sEditorID) {
  if(tinyMCEmode[sEditorID]) {
    try {
      tinyMCE.execCommand('mceRemoveControl',false,sEditorID);
      tinyMCEmode[sEditorID] = false;
      $("#"+sEditorID) .css('height','auto');
      $("#eventLinkbox").show();
    } catch(e) {
      alert( "REMOVE:" + sEditorID + ':' + e.message);
    }
  } else {
    try {
      $("#"+sEditorID).css('height',$("#"+sEditorID).height()+300);
      $("#eventLinkbox").hide();
      tinyMCE.execCommand('mceAddControl',false,sEditorID);
      tinyMCEmode[sEditorID] = true;
    } catch(e) {
      alert( "ADD:" + sEditorID + ': ' + e.message);
    }
  }
}
function initMCE() {
  tinyMCE.init({ 
    mode : "none",
    editor_deselector : "mceNoEditor" ,
    theme : "advanced",
    theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,undo,redo,fontselect,fontsizeselect,forecolor,sub,sup,|,link,image,cleanup,|,formatselect",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    valid_elements : "@[id|class|style|title|lang],a[name|!href|!title],b/strong,i/em,u,#p[align],-ol[type|compact],-ul[type|compact],-li,br,img[!src|!alt|title|width|height|align],-sub,-sup,-div,-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr,-font[face|size|color]",
    convert_fonts_to_spans : true,
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom"
  });
}


initMCE();


/*

valid_elements : "@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
+ "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
+ "onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|"
+ "name|href|target|title|class|onfocus|onblur],strong/b,em/i,strike,u,"
+ "#p[align],-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|"
+ "src|border|alt=|title|hspace|vspace|width|height|align],-sub,-sup,"
+ "-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|"
+ "height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|"
+ "height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,"
+ "#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor"
+ "|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,"
+ "-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face"
+ "|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],"
+ "object[classid|width|height|codebase|*],param[name|value|_value],embed[type|width"
+ "|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,"
+ "button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|"
+ "valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],"
+ "input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value],"
+ "kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],"
+ "q[cite],samp,select[disabled|multiple|name|size],small,"
+ "textarea[cols|rows|disabled|name|readonly],tt,var,big"


@[id|class|style|title|lang],a[name|href|target|title],b/strong,i/em,u,#p[align],-ol[type|compact],-ul[type|compact],-li,br,img[src|alt=|title|width|height|align],-sub,-sup,-div,-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr,-font[face|size|color]






*/


