<?php
// This file generates the editor CSS overrides from the admin interface. It will be included only
// when needed.
function weaver_save_editor_css($mce_css_dir) {
    // create a new editor-style.css file (sorry, no rtl) in the upload directory so will be multi-site compatible
    $base_style =  get_template_directory() . '/editor-style.css';	/* the default editor css file */

    if (!weaver_f_file_access_available() || !weaver_f_is_writable($mce_css_dir)) {
        return false;				// just bail and let the default editor-style.css be used
    }

    /* delete current weaver-editor-style.css
       Copy default editor style to editor-style.css
       Add Weaver overrides to that file
       Close
    */

    if (!weaver_f_exists($base_style)) {
	return false;
    }
    
    $contents = weaver_f_get_contents($base_style);	// get the core part

    $filename = trailingslashit($mce_css_dir) . 'weaver-editor-style.css';

    if (!($handle = weaver_f_open($filename, 'w')) ) {		// can't write - blow it off
	return false;
    }

    weaver_f_write($handle, $contents);	// save new editor style css file

    weaver_f_write($handle, sprintf("\n/* Subtheme %s specific editor styling */\n",weaver_getopt('ttw_subtheme')));

    weaver_output_editor_style($handle);

    weaver_f_write($handle,sprintf("/* WEAVER WARNING: Do not edit this file. It is dynamically generated. Any edits you make will be overwritten. */
/* This file generated using %s %s subtheme: %s */\n",WEAVER_THEMENAME, WEAVER_VERSION, weaver_getopt('ttw_subtheme')));
    weaver_f_close($handle);

    return;
}

function weaver_output_editor_style($sout) {
    /* This outputs the CSS overrides. It will output to a file pointer, so it can write to a .css file saved
       in the user's upload directory - just like the saved style .wvr files. It is included via a standard
       css include. It needs to be loaded only for the admin page.
    */

// Put a options that can be overridden by the CSS option at the top!

/*  ttw_contentlist_bullet  */

    $val = weaver_getopt("ttw_contentlist_bullet");       /* add a new lsit bullet */
    if ($val && $val != '' && $val != 'default') {
	if ($val == 'none' || $val == 'circle' || $val == 'disc' || $val == 'square')
	    weaver_f_write($sout, sprintf("ul {list-style:%s;}\n",$val));	// writing default css style
	else
            weaver_f_write($sout, sprintf("ul {list-style:none;list-style-image: url(%s/images/bullets/%s.gif);}\n",
		get_template_directory_uri(),$val));
    }


/*  ttw_content_font  */

    if (($useFont = weaver_get_font_value('ttw_content_font')) != '') {
	weaver_css_fontfamily($sout,'*', $useFont);
    }

/*  ttw_title_font  */

if (($useFont = weaver_get_font_value('ttw_title_font')) != '') {
	weaver_css_fontfamily($sout,
'thead th, tr th', $useFont);
    }


/* need to handle bg color of content area - need to do the cascade ourself */

    if (($optVal = weaver_getopt("ttw_content_bgcolor")) && strcasecmp($optVal,'transparent') != 0) {	/* #content */
	weaver_css_bgcolor($sout,'html .mceContentBody',$optVal);
    } else if (($optVal = weaver_getopt("ttw_container_bgcolor")) && strcasecmp($optVal,'transparent') != 0) {	/* #container */
	weaver_css_bgcolor($sout,'html .mceContentBody',$optVal);
    } else if (($optVal = weaver_getopt('ttw_main_bgcolor')) && strcasecmp($optVal,'transparent') != 0) { /* #main */
	weaver_css_bgcolor($sout,'html .mceContentBody',$optVal);
    } else if (($optVal = weaver_getopt('ttw_page_bgcolor')) && strcasecmp($optVal,'transparent') != 0) { /* #wrapper */
	weaver_css_bgcolor($sout,'html .mceContentBody',$optVal);
    } else if (($name = weaver_getopt('ttw_subtheme')) && strcasecmp($name,'Transparent Dark') == 0) {
	weaver_css_bgcolor($sout,'html .mceContentBody','#222');
    } else if (($name = weaver_getopt('ttw_subtheme')) && strcasecmp($name,'Transparent Light') == 0) {
	weaver_css_bgcolor($sout,'html .mceContentBody','#ccc');
    }

/*  ttw_content_color  - text color of content */
    if (($optVal = weaver_getopt_color('ttw_content_color')) ) {
	weaver_css_color($sout,'*, table,tr td,tr th',$optVal);

    }
    if (($optVal = weaver_getopt('ttw_content_color_css')) ) {
	weaver_css_css($sout,'*, table,tr td,tr th',$optVal);
    }

    /*  ttw_small_content_font  */

    if (!weaver_getopt('ttw_small_content_font')) {
	weaver_f_write($sout, sprintf("* {font-size: 16px; line-height: 1.5; }\n"));  // write content font
	weaver_f_write($sout, sprintf("p,dl,td,th,ul,ol,blockquote {font-size: 16px; line-height: 1.5; }\n")); // line height styling
    }

/*  ttw_hr_color  */

    if (($optVal = weaver_getopt_color('ttw_hr_color'))) {
	weaver_css_bgcolor($sout,'hr',$optVal);
    }
    if (($optVal = weaver_getopt('ttw_hr_color_css'))) {
	weaver_css_css($sout,'hr',$optVal);
    }

/*  ttw_link_color  */
/*  ttw_link_hover_color  */
/*  ttw_link_visited_color  */

    if (($optVal = weaver_getopt_color('ttw_link_color'))) {
	weaver_css_color($sout,'a:link', $optVal);
    }
    if (($optVal = weaver_getopt_color('ttw_link_hover_color'))) {
	weaver_css_color($sout,'a:active, a:hover', $optVal);
    }
    if (($optVal = weaver_getopt_color('ttw_link_visited_color'))) {
	weaver_css_color($sout,'a:visited', $optVal);
    }
    if (($optVal = weaver_getopt('ttw_link_color_css'))) {
	weaver_css_css($sout,'a:link', $optVal);
    }
    if (($optVal = weaver_getopt('ttw_link_hover_color_css'))) {
	weaver_css_css($sout,'a:active, a:hover', $optVal);
    }
    if (($optVal = weaver_getopt('ttw_link_visited_color_css'))) {
	weaver_css_css($sout,'a:visited', $optVal);
    }

    /*  ttw_caption_color  */

    if (($optVal = weaver_getopt_color('ttw_caption_color'))) {
	weaver_css_color($sout,'.wp-caption, .wp-caption p.wp-caption-text',$optVal);
    }
    if (($optVal = weaver_getopt('ttw_caption_color_css'))) {
	weaver_css_css($sout,'.wp-caption, .wp-caption p.wp-caption-text',$optVal);
    }

/*    ttw_media_lib_border    */

    if (($optVal = weaver_getopt('ttw_media_lib_border'))) {
	weaver_f_write($sout, sprintf(".size-full, .size-large, .size-medium, .size-thumbnail,.size-thumbnail {background-color: %s;}\n",
	    $optVal  ));
    }
    if (($optVal = weaver_getopt('ttw_media_lib_border_css'))) {
	weaver_css_css($sout,'.size-full, .size-large, .size-medium, .size-thumbnail',$optVal);
    }

/*    ttw_media_lib_captioned_border    */

    if (($optVal = weaver_getopt('ttw_media_lib_captioned_border'))) {
	weaver_f_write($sout, sprintf(".wp-caption, .wp-caption p.wp-caption-text,
.wp-caption .size-full, .wp-caption .size-large, .wp-caption .size-medium,
.wp-caption .size-thumbnail, .wp-caption .size-thumbnail {background-color: %s;}\n",
	    $optVal  ));
    }
    if (($optVal = weaver_getopt('ttw_media_lib_captioned_border_css'))) {
	weaver_css_css($sout,'.wp-caption, .wp-caption p.wp-caption-text,
.wp-caption .size-full, .wp-caption .size-large, .wp-caption .size-medium,
.wp-caption .size-thumbnail, .wp-caption .size-thumbnail',
	    $optVal);
    }

/*  ttw_text_color  */

    if (($textColor = weaver_getopt_color('ttw_text_color'))) {
	weaver_css_color($sout,'h1, h2, h3, h4, h5, h6, dt, th',$textColor);
    }
    if (($textColor = weaver_getopt('ttw_text_color_css'))) {
	weaver_css_css($sout,'h1, h2, h3, h4, h5, h6, dt, th',$textColor);
    }

/*  ttw_weaver_tables  */
    /* based on code in wvr-generatecss - the standard not 'none' code is in editor-style.css so you
       get a reasonable display when 'None' is the style. Also have to add !important to all borders. */

    $table = weaver_getopt('ttw_weaver_tables');

    if ($table != 'None') {		// Need to write default style values
	if ($table != 'Default') {		// Non-default styles based on Weaver table style
	    weaver_f_write($sout, "table { border: 2px solid #AAA !important; text-align: left; margin: auto; margin-bottom: 5px; width: auto; }
tr th, thead th { color: inherit; background-color: rgba(0,0,0,.1); font-size: medium;
   font-weight: normal; line-height: normal; padding: 5px 10px; }
tr td { border: 1px solid #AAA !important; padding: 5px 10px; }
tr.odd td { background: inherit; }\n");
	}
    }

     if ($table == 'Default') {	// make backward compatible with 1.4 and before when Twenty Ten was default
	weaver_f_write($sout, sprintf("table {border: 1px solid #e7e7e7 !important;margin: 0 -1px 24px 0;text-align: left;width: 100%%;}
tr th, thead th {color: #888;font-size: 12px;font-weight: bold;line-height: 18px;padding: 9px 24px;}
tr td {border-style:none !important; border-top: 1px solid #e7e7e7; padding: 6px 24px;}
tr.odd td {background: #f2f7fc;}\n"));
    } else
    if ($table == 'Bold Headings') {
	weaver_f_write($sout, sprintf("table {border: 2px solid #888;}
tr th, thead th {font-weight: bold;}
tr td {border: 1px solid #888 !important;}\n"));
    } else
    if ($table == 'No Borders') {
	weaver_f_write($sout, sprintf("table {border-style:none;}
tr th, thead th {font-weight: bold;border-bottom: 1px solid #888 !important;background-color:transparent;}
tr td {border-style:none !important;}\n"));
    } else
    if ($table == 'Full Width') {
	weaver_f_write($sout, sprintf("table {width:100%%;}
tr th, thead th {font-weight:bold;}\n"));
    } // else 'Weaver' - handled previously

    if (function_exists('weaver_plus_output_editor_style'))
	weaver_plus_output_editor_style($sout);
}
?>
