$().ready(function() {
  $('textarea.tinymce').tinymce({
  //tinyMCE.init({
		// Location of TinyMCE script
		script_url : '/_ext/tinymce/tiny_mce.js',

    // General options
    mode : "textareas",
    theme : "advanced",

  	language : "cs",
    file_browser_callback : 'tinymce_FileBrowser',
    convert_urls : false,
    entities : "160,nbsp",
    entity_encoding : "raw",
    preformatted : true,
    
    //editor_selector : "mceEditor",
    //editor_deselector : "mceNoEditor",
        
    theme_advanced_styles : "Obrázek vpravo=obrR;Obrázek vlevo=obrL;Konec obtékání=cb;Margin top=mt;Margin top 2=mt2;Margin top 3=mt3;Margin top 4=mt4;Slide=slide;Screen only=screen_only;Projection only=projection_only;Otevrit v zalozce Dotaz=open_dotaz_tab",

    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",

  	// Theme options
  	// Unused: fontsizeselect,|,ltr,rtl,|,insertfile,insertimage,spellchecker,iespell,
  	theme_advanced_buttons1 : "code,preview,fullscreen,|,save,newdocument,|,print,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,removeformat,visualaid,|,insertdate,inserttime,|,cleanup,help",
  	theme_advanced_buttons2 : "formatselect,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,hr,advhr,|,pagebreak,|,insertlayer,moveforward,movebackward,absolute",
  	theme_advanced_buttons3 : "styleselect,|,bold,italic,strikethrough,|,forecolor,backcolor,|,sub,sup,|,cite,abbr,acronym,|,del,ins,|,charmap,emotions,visualchars,nonbreaking",
  	theme_advanced_buttons4 : "image,link,unlink,anchor,|,tablecontrols,|,media,|,attribs,styleprops", // ,|,iframe
  	theme_advanced_toolbar_location : "top",
  	theme_advanced_toolbar_align : "left",
  	theme_advanced_statusbar_location : "bottom",
  	theme_advanced_resizing : true,

    content_css : "/_css/css.css",

  	extended_valid_elements : "script[charset|defer|language|src|type],iframe[src|width|height|name|align|style|frameborder],style",

    /*
    // Drop lists for link/image/media/template dialogs
    template_external_list_url : "js/template_list.js",
    external_link_list_url : "js/link_list.js",
    external_image_list_url : "js/image_list.js",
    media_external_list_url : "js/media_list.js",

    // Replace values for the template plugin
    template_replace_values : {
    username : "Some User",
    staffid : "991234"
    }
    */
  });
});

function tinymce_FileBrowser (field_name, url, type, win) {
  //alert("Field_Name: " + field_name + "\nURL: " + url + "\nType: " + type + "\nWin: " + win); // debug/testing

  // If you work with sessions in PHP and your client doesn't accept cookies you might need to carry
  // the session name and session ID in the request string (can look like this: "?PHPSESSID=88p0n70s9dsknra96qhuk6etm5").
  // These lines of code extract the necessary parameters and add them back to the filebrowser URL again.

  //var cmsURL = window.location.toString();    // script URL - use an absolute path!
  var cmsURL = '/?win=file_form'; // script URL - use an absolute path!

  if (cmsURL.indexOf("?") < 0) {
      //add the type as the only query parameter
      cmsURL = cmsURL + "?";
  }
  else {
      //add the type as an additional query parameter
      // (PHP session ID is now included if there is one at all)
      cmsURL = cmsURL + "&";
  }
  cmsURL = cmsURL + "type=" + type;

  tinyMCE.activeEditor.windowManager.open({
      file : cmsURL,
      title : 'Výběr souboru',
      width : 420,  // Your dimensions may differ - toy around with them!
      height : 400,
      resizable : "yes",
      inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
      close_previous : "no"
  }, {
      window : win,
      input : field_name,
      inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
      editor_id : tinyMCE.selectedInstance.editorId
  });
  return false;
}

