<?php

// NOTE: If you want to make changes to this file save it as
// wysiwyg_custom.php and it will get loaded instead of this
// file and won't get overwritten when you upgrade.

// NOTE: You can find the CSS for the wysiwyg in /lib/wysiwyg.css
// and also create a /lib/wysiwyg_custom.css file as well.

// this is called once at the top of the page
function loadWysiwygJavascript() {
  global $SETTINGS;

  // Renders script tag with compressed scripts
  // Note: We're using the direct method here: http://www.tinymce.com/wiki.php/Compressors:PHP
  require_once("{$GLOBALS['CMS_ASSETS_DIR']}/3rdParty/tiny_mce/tiny_mce_gzip.php");
  TinyMCE_Compressor::renderTag(array(
      // Load Plugins: ***NOTE*** plugins must be added both loadWysiwygJavascript() and initWysiwyg() (twice in this page, search for "plugins:")
     'plugins'   => 'inlinepopups,contextmenu,table,fullscreen,paste,media,spellchecker,lists,visualblocks,advimage',
     'languages' => $SETTINGS['wysiwyg']['wysiwygLang'],
     'themes'    => 'advanced',
     'url'       => "{$GLOBALS['CMS_ASSETS_URL']}/3rdParty/tiny_mce/tiny_mce_gzip.php",
  ));

}

// this is called once for each wysiwyg editor on the page
function initWysiwyg($fieldname, $uploadBrowserCallback) {
  global $SETTINGS;
  $includeDomainsInLinks = $SETTINGS['wysiwyg']['includeDomainInLinks'] ? "remove_script_host : false, // domain name won't be removed from absolute links" : '';
  $programUrl            = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
  $programUrl            = preg_replace("/ /", "%20", $programUrl);

  // load either wysiwyg_custom.css (if exists) or wysiwyg.css
  $wysiwygCssFilename    = file_exists( dirname(__FILE__) .'/wysiwyg_custom.css' ) ? 'wysiwyg_custom.css' : 'wysiwyg.css';
  $wysiwygCssModified    = filemtime( dirname(__FILE__) ."/$wysiwygCssFilename" );

  // call custom wysiwyg functions named: initWysiwyg_sectionName_fieldName() or initWysiwyg_sectionName()
  if (__FUNCTION__ == 'initWysiwyg') {
    $fieldnameWithoutPrefix = preg_replace("/^field_/", '', $fieldname);
    $fieldSpecificFunction   = "initWysiwyg_{$GLOBALS['tableName']}_$fieldnameWithoutPrefix";
    $sectionSpecificFunction = "initWysiwyg_{$GLOBALS['tableName']}";

    if (function_exists($fieldSpecificFunction))   { return call_user_func($fieldSpecificFunction, $fieldname, $uploadBrowserCallback); }
    if (function_exists($sectionSpecificFunction)) { return call_user_func($sectionSpecificFunction, $fieldname, $uploadBrowserCallback); }
	
	//see if we have design based styles file - this becomes a bit tricky
	//in that we don't know what the active and development designs are, so for
	//now we will use "/design/custom/styles.css" and "/design/dev/styles.css"
	
	//this may need to be changed on a customer by customer basis depending on the name
	//of their current active design
	// example of using a google font for WYSIWYG $extracss = ",https://fonts.googleapis.com/css?family=Open+Sans%3A400%2C600";
	$extracss = "";
	$docroot = $_SERVER['DOCUMENT_ROOT'];
	
	$srvrname = explode(".",$_SERVER['SERVER_NAME']);
	if ($srvrname[0]=="dev") {
		$designFolder = "custom";
	} else {
		$designFolder = "custom";
	}
	if (file_exists($docroot . "/design/{$designFolder}/fonts.css")) {
		$extracss .= ",/design/{$designFolder}/fonts.css?dt=" . time();
	}
	if (file_exists($docroot . "/design/{$designFolder}/editor.css")) {
		$extracss .= ",/design/{$designFolder}/editor.css?dt=" . time();
	}
	$extracss .= ",https://fonts.googleapis.com/css?family=Open+Sans%3A400%2C600%2C700";
	$extracss .= ",https://fonts.googleapis.com/css?family=Titillium+Web%3A400%2C600%2C700";
  }

  // display field
  print <<<__HTML__

  <script language="javascript" type="text/javascript"><!--
  tinyMCE.init({
    mode : "exact",
    theme : "advanced",
    language: "{$SETTINGS['wysiwyg']['wysiwygLang']}",
	
	formats : {
		divwrap : {block : 'div', wrapper : 1}
	},
	
    // Define buttons: Button codes: http://www.tinymce.com/wiki.php/Buttons/controls
    theme_advanced_buttons1 : "formatselect,fontsizeselect,bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,outdent,indent,|,removeformat,fullscreen",
    theme_advanced_buttons2 : "forecolor,backcolor,|,link,unlink,anchor,|,blockquote,hr,image,media,table,visualaid,|,pastetext,pasteword,|,code,|,spellchecker",
      theme_advanced_buttons3 : "styleselect,fontselect,fontsizeselect,|,sub,sup,|,forecolorpicker,backcolorpicker,|,charmap,visualblocks",

    // set formats to show with formatselect
    theme_advanced_blockformats : "p,address,pre,h1,h2,h3,h4,h5,h6,divwrap",
	theme_advanced_font_sizes : "8px,9px,10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,26px,30px,36px,0.2em,0.4em,0.6em,0.8em,1.0em,1.2em,1.4em,1.6em,1.8em,2.0em,2.4em,2.8em.3.0em",

    // set classes to show with styleselect - if you don't set this classes will be imported from the file loaded by 'content_css' below (usually wysiwyg.css).
    //theme_advanced_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1",

    // Load Plugins: ***NOTE*** plugins must be added both loadWysiwygJavascript() and initWysiwyg() (twice in this page, search for "plugins:")
    plugins: "inlinepopups,contextmenu,table,fullscreen,paste,media,spellchecker,lists,visualblocks,advimage",

    // Paste Settings - Docs: http://www.tinymce.com/wiki.php/Plugin:paste
    paste_text_sticky: true, // if user clicks "paste as text" don't unselect it after they paste, wait for them to click it again
    setup: function(ed) {
      ed.onInit.add(function(ed) { // from: http://tinymce.moxiecode.net/punbb/viewtopic.php?pid=73053#p73053
       ed.pasteAsPlainText = true;
       ed.controlManager.setActive("pastetext", true);
      });
    },

    // v2.50 - allow style in body (invalid XHTML but required to style html emails since many email clients won't display remote styles or styles from head)
    // Reference: http://www.tinymce.com/wiki.php/Configuration:valid_children
    valid_children : "+body[style]",

    // Spellchecker settings - see: http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
    spellchecker_languages: "+{$SETTINGS['wysiwyg']['wysiwygLang']}={$SETTINGS['wysiwyg']['wysiwygLang']}",
    spellchecker_rpc_url:   "{$GLOBALS['CMS_ASSETS_URL']}/3rdParty/tiny_mce/plugins/spellchecker/rpc.php",

    // Force <br> instead of <p> - see: http://www.tinymce.com/wiki.php/TinyMCE_FAQ#TinyMCE_produce_P_elements_on_enter.2Freturn_instead_of_BR_elements.3F
    // Uncomment these lines to enable this for new records
    //forced_root_block : false,
    //force_br_newlines : true,
    //force_p_newlines : false,

    // Allow user to drag the editor to resize it - see "theme_advanced_resizing*" options here: http://wiki.moxiecode.com/index.php/TinyMCE:Configuration
    // Uncommon these lines to enable this.
    //theme_advanced_statusbar_location : 'none',
    //theme_advanced_statusbar_location : 'bottom',
    //theme_advanced_resizing : true,

    //
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    elements: '$fieldname',
    file_browser_callback : "$uploadBrowserCallback",
    relative_urls : false,
    document_base_url: "/",

    $includeDomainsInLinks
    entity_encoding : "raw", // don't store extended chars as entities (&ntilde) or keyword searching won't match them
	  extended_valid_elements : "script[src|type|language]", 

    verify_html : false, // allow all tags and attributes

    // add file modified time on end of url so updated files won't be cached by the browser
    content_css : "$programUrl/lib/$wysiwygCssFilename?$wysiwygCssModified{$extracss}"
  });

  //--></script>

__HTML__;
}

?>