<?php

if (!function_exists('kippis_update_options_css')) {
function kippis_update_options_css()
{
  global $kippis_is_smartphone;
  global $kippis_options;
  global $kippis_sidebar_enable_left;
  global $kippis_sidebar_enable_right;
  global $kippis_sidebars_left;
  global $kippis_sidebars_right;

  $kippis_options = kippis_get_options();
  kippis_enable_sidebars();
  $kippis_option_css_screen = '';
  $kippis_option_css_mobile = '';

  //----------------------------------------------------------------------------------------------------
  //
  //  Standard display.
  //
  //----------------------------------------------------------------------------------------------------
  $content_width = KIPPIS_HEADER_DEFAULT_WIDTH;
  $content_header_width_diff = false;
  if (!empty($kippis_options['kippis_all_width']) && ctype_digit($kippis_options['kippis_all_width'])) {
    $content_width = $kippis_options['kippis_all_width'];
    $content_header_width_diff = true;
  }
  else {
    if (($kippis_options['kippis_header_image_enable'] == 'on') && (!empty($kippis_options['kippis_header_image']['width']))) $content_width = $kippis_options['kippis_header_image']['width' ];
    if (!empty($kippis_options['kippis_header_image_width'])) $content_width = $kippis_options['kippis_header_image_width'];
  }
  $arr = array('background-color' => $kippis_options['kippis_body_background']
              ,'border-color'     => $kippis_options['kippis_body_background']
              ,'color'            => $kippis_options['kippis_body_foreground']
              ,'text-align'       => $kippis_options['kippis_body_text_align']
              );
  if ($kippis_options['kippis_body_image_enable'] == 'on') {
    $arr['background-image'] = $kippis_options['kippis_body_image']['url'];
    if      (($kippis_options['kippis_body_image_repeat_x'] == 'on') && ($kippis_options['kippis_body_image_repeat_y'] != 'on')) $arr['background-repeat'    ] = 'repeat-x';
    else if (($kippis_options['kippis_body_image_repeat_x'] != 'on') && ($kippis_options['kippis_body_image_repeat_y'] == 'on')) $arr['background-repeat'    ] = 'repeat-y';
    else if (($kippis_options['kippis_body_image_repeat_x'] == 'on') && ($kippis_options['kippis_body_image_repeat_y'] == 'on')) $arr['background-repeat'    ] = 'repeat';
    else                                                                                                                         $arr['background-attachment'] = 'fixed';
  }
  $kippis_option_css_screen .= kippis_savecss_get_style('body',$arr);
  $fontdata = kippis_savecss_get_fontdata('kippis_body_font');
  if (!empty($fontdata)) $kippis_option_css_screen .= kippis_savecss_get_style('body',array('#' => $fontdata));
  $kippis_option_css_screen .= kippis_savecss_get_style('#all'
    ,array('#'                => 'width:' . ($content_width) . 'px;margin:10px auto;padding:10px;' . (!empty($kippis_options['kippis_all_background']) ? 'border:1px solid;border-radius:8px;' : '')
          ,'background-color' => $kippis_options['kippis_all_background']
          ,'border-color'     => $kippis_options['kippis_all_background']
          ,'color'            => $kippis_options['kippis_all_foreground']
          ));
  $arr = array('background-color' => $kippis_options['kippis_header_background']
              ,'border-color'     => $kippis_options['kippis_header_background']
              ,'color'            => $kippis_options['kippis_header_foreground']
              );
  if ($kippis_options['kippis_header_image_enable'] == 'on') {
    $arr['background-image'] = $kippis_options['kippis_header_image']['url'];
    if (!empty($kippis_options['kippis_header_image_width'])) {
      $arr['width' ] = $kippis_options['kippis_header_image_width'];
      $arr['height'] = floor((float)$kippis_options['kippis_header_image_width']
                           / (float)$kippis_options['kippis_header_image']['width']
                           * (float)$kippis_options['kippis_header_image']['height']);
      $kippis_option_css_screen .= kippis_savecss_get_style('#pagehead',array('#' => 'background-size:' . $arr['width'] . 'px ' . $arr['height'] . 'px'));
    }
    else {
      $arr['width' ] = $content_width;
      $arr['height'] = (empty($kippis_options['kippis_header_image']['height']) ? KIPPIS_HEADER_MIN_HEIGHT : $kippis_options['kippis_header_image']['height']);
    }
  }
  else {
    if (!empty($kippis_options['kippis_header_image_width'])) {
      $arr['width' ] = $kippis_options['kippis_header_image_width'];
      $arr['height'] = floor((float)$kippis_options['kippis_header_image_width']
                           / (float)KIPPIS_HEADER_MIN_HEIGHT
                           * (float)$kippis_options['kippis_header_image']['height']);
      $kippis_option_css_screen .= kippis_savecss_get_style('#pagehead',array('#' => 'background-size:' . $arr['width'] . 'px ' . $arr['height'] . 'px'));
    }
    else {
      $arr['width' ] = $content_width;
      $arr['height'] = KIPPIS_HEADER_MIN_HEIGHT;
    }
  }
  $kippis_px_width_header  = $arr['width' ];
  $kippis_px_height_header = $arr['height'];
  $kippis_option_css_screen .= kippis_savecss_get_style('#pagehead',$arr);
  if ($content_header_width_diff && ($kippis_options['kippis_header_image_enable'] == 'on')) {
    $kippis_option_css_screen .= kippis_savecss_get_style('#pagehead',array('#' => 'background-position:left;'));
  }
  $kippis_option_css_screen .= kippis_savecss_get_style('#pagehead .search_box' ,array('background-color' => $kippis_options['kippis_body_background']));
  if ($kippis_options['kippis_search_box_header'] == 'no') $kippis_option_css_screen .= kippis_savecss_get_style('#pagehead .search_wrap',array('#'=> 'display:none;'));
  else {
    $px = $kippis_options['kippis_search_box_header'] == 'top' ? '-40' : $kippis_px_height_header - 80;
    $kippis_option_css_screen .= kippis_savecss_get_style('#pagehead .search_wrap',array('#'=> 'position:relative;top:' . $px . 'px;right:0px;'));
  }
  $kippis_option_css_screen .= kippis_savecss_get_style('#searchsubmit'  ,array('border-color'     => $kippis_options['kippis_body_foreground']));
  $kippis_option_css_screen .= kippis_savecss_get_style('#main_menu'     ,array('width'            => $content_width));
  $kippis_option_css_screen .= kippis_savecss_get_style('#secondary_menu',array('width'            => $content_width));
  $kippis_option_css_screen .= kippis_savecss_get_style('#pagebody'      ,array('width'            => $content_width));
  $kippis_option_css_screen .= kippis_savecss_get_style('#pagefoot'      ,array('width'            => $content_width));
  $kippis_option_css_screen .= kippis_savecss_get_style('#content'
    ,array('background-color' => $kippis_options['kippis_content_background']
          ,'border-color'     => $kippis_options['kippis_content_background']
          ,'color'            => $kippis_options['kippis_content_foreground']
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#main_menu'
    ,array('background-color'           => $kippis_options['kippis_main_menu_bar_top_background']
          ,'background-linear-gradient' => $kippis_options['kippis_main_menu_bar_top_background'].','.$kippis_options['kippis_main_menu_bar_bottom_background']
          ));
  $fontsize = '16px';  // Default: 1em.
  if (!empty($kippis_options['kippis_body_font_size'     ])) $fontsize = $kippis_options['kippis_body_font_size'     ];
  if (!empty($kippis_options['kippis_main_menu_font_size'])) $fontsize = $kippis_options['kippis_main_menu_font_size'];
  $kippis_option_css_screen .= kippis_savecss_get_style('#main_menu ul ul',array('#' => 'position:absolute;top:' . (floor(((float)$fontsize) * 1.5)) . 'px;'));
  $kippis_option_css_screen .= kippis_savecss_get_style('#main_menu a'
    ,array('color' => $kippis_options['kippis_main_menu_bar_foreground']
          ,'#'     => kippis_savecss_get_fontdata('kippis_main_menu_font')
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#main_menu ul ul a'
    ,array('background-color' => $kippis_options['kippis_main_menu_dropdown_background']
          ,'color'            => $kippis_options['kippis_main_menu_dropdown_foreground']
          ,'border-color'     => $kippis_options['kippis_main_menu_dropdown_foreground']
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#main_menu li:hover > a,#main_menu ul ul:hover > a,#main_menu li:hover > a,#main_menu a:focus'
    ,array('background-color'           => $kippis_options['kippis_main_menu_hover_top_background']
          ,'background-linear-gradient' => $kippis_options['kippis_main_menu_hover_top_background'].','.$kippis_options['kippis_main_menu_hover_bottom_background']
          ,'color'                      => $kippis_options['kippis_main_menu_hover_foreground'    ]
          ));
  // Not yet.
  //$kippis_option_css_screen .= kippis_savecss_get_style('#main_menu .current_page_ancestor > a',array('color' => $kippis_options['kippis_main_menu_parent_foreground']));
  $kippis_option_css_screen .= kippis_savecss_get_style('#secondary_menu'
    ,array('background-color'           => $kippis_options['kippis_secondary_menu_bar_top_background']
          ,'background-linear-gradient' => $kippis_options['kippis_secondary_menu_bar_top_background'].','.$kippis_options['kippis_secondary_menu_bar_bottom_background']
          ));
  $fontsize = '16px';  // Default: 1em.
  if (!empty($kippis_options['kippis_body_font_size'     ])) $fontsize = $kippis_options['kippis_body_font_size'     ];
  if (!empty($kippis_options['kippis_secondary_menu_font_size'])) $fontsize = $kippis_options['kippis_secondary_menu_font_size'];
  $kippis_option_css_screen .= kippis_savecss_get_style('#secondary_menu ul ul',array('#' => 'position:absolute;top:' . (floor(((float)$fontsize) * 1.5)) . 'px;'));
  $kippis_option_css_screen .= kippis_savecss_get_style('#secondary_menu a'
    ,array('color' => $kippis_options['kippis_secondary_menu_bar_foreground']
          ,'#'     => kippis_savecss_get_fontdata('kippis_secondary_menu_font')
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#secondary_menu ul ul a'
    ,array('background-color' => $kippis_options['kippis_secondary_menu_dropdown_background']
          ,'color'            => $kippis_options['kippis_secondary_menu_dropdown_foreground']
          ,'border-color'     => $kippis_options['kippis_secondary_menu_dropdown_foreground']
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#secondary_menu li:hover > a,#secondary_menu ul ul:hover > a,#secondary_menu li:hover > a,#secondary_menu a:focus'
    ,array('background-color'           => $kippis_options['kippis_secondary_menu_hover_top_background']
          ,'background-linear-gradient' => $kippis_options['kippis_secondary_menu_hover_top_background'].','.$kippis_options['kippis_secondary_menu_hover_bottom_background']
          ,'color'                      => $kippis_options['kippis_secondary_menu_hover_foreground'    ]
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('nav#nav-single, nav#nav-above, nav#nav-below, .article, .search-result'
    ,array('background-color' => $kippis_options['kippis_article_background']
          ,'border-color'     => $kippis_options['kippis_article_background']
          ,'color'            => $kippis_options['kippis_article_foreground']
          ,'border-radius'    => $kippis_options['kippis_article_radius'    ]
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('.wp-caption'
    ,array('background-color' => $kippis_options['kippis_caption_background']
          ,'border-color'     => $kippis_options['kippis_caption_border'    ]
          ,'#'                => kippis_savecss_get_fontdata('kippis_caption_font')
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#comments'
    ,array('background-color' => $kippis_options['kippis_comments_background']
          ,'border-color'     => $kippis_options['kippis_comments_background']
          ,'color'            => $kippis_options['kippis_comments_foreground']
          ,'border-radius'    => $kippis_options['kippis_comments_radius'    ]
          ,'#'                => kippis_savecss_get_fontdata('kippis_comments_font')
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#comments-title'
    ,array('color'            => $kippis_options['kippis_comments_foreground']
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('.thread-odd, .children .odd'
    ,array('background-color' => $kippis_options['kippis_comments_even_border']
          ,'border-color'     => $kippis_options['kippis_comments_odd_border' ]
          ,'border-radius'    => $kippis_options['kippis_comments_radius'     ]
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('.thread-even, .children .even'
    ,array('background-color' => $kippis_options['kippis_comments_odd_border' ]
          ,'border-color'     => $kippis_options['kippis_comments_even_border']
          ,'border-radius'    => $kippis_options['kippis_comments_radius'     ]
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('.comment'
    ,array('background-color' => $kippis_options['kippis_comment_background']
          ,'color'            => $kippis_options['kippis_comment_foreground']
          ,'border-radius'    => $kippis_options['kippis_comments_radius'   ]
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('.sidebar'
    ,array('background-color' => $kippis_options['kippis_sidebars_background']
          ,'border-color'     => $kippis_options['kippis_sidebars_background']
          ,'color'            => $kippis_options['kippis_sidebars_foreground']
          ,'#'                => kippis_savecss_get_fontdata('kippis_sidebars_font')
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('.widget'
    ,array('background-color' => $kippis_options['kippis_widget_background']
          ,'border-color'     => $kippis_options['kippis_widget_background']
          ,'color'            => $kippis_options['kippis_widget_foreground']
          ,'border-radius'    => $kippis_options['kippis_widget_radius'    ]
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('.widget-title'
    ,array('background-color' => $kippis_options['kippis_widget_title_background']
          ,'border-color'     => $kippis_options['kippis_widget_title_background']
          ,'color'            => $kippis_options['kippis_widget_title_foreground']
          ,'border-radius'    => $kippis_options['kippis_widget_radius'] . ' ' . $kippis_options['kippis_widget_radius'] . ' 0 0'
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('h1,h2,h3,h4,h5,h6'
    ,array('background-color' => $kippis_options['kippis_h_background']
          ,'color'            => $kippis_options['kippis_h_foreground']
  ));
  $kippis_option_css_screen .= kippis_savecss_get_style('a'
    ,array('background-color' => $kippis_options['kippis_a_background']
          ,'color'            => $kippis_options['kippis_a_foreground']
  ));
  $kippis_option_css_screen .= kippis_savecss_get_style('h1 a,h2 a,h3 a,h4 a,h5 a,h6 a'
    ,array('background-color' => $kippis_options['kippis_h_a_background']
          ,'color'            => $kippis_options['kippis_h_a_foreground']
          ));
  $kippis_option_css_screen .= kippis_savecss_get_style('#site-title a,#site-description'
    ,array('background-color' => 'transparent'
          ,'color'            => $kippis_options['kippis_sitetitle_foreground']
          ,'text-shadow'      => $kippis_options['kippis_sitetitle_background']
          ,'#'                => kippis_savecss_get_fontdata('kippis_sitetitle_font')
          ));
  $fontsize = '29px';  // Default: 1.8em & 1.2em.
  if (!empty($kippis_options['kippis_sitetitle_font_size'])) $fontsize = $kippis_options['kippis_sitetitle_font_size'];  // site-title
  $kippis_option_css_screen .= kippis_savecss_get_style('#site-description',array('#' => 'font-size:' . (floor(((float)$fontsize) * 0.67)) . 'px;'));
  if ($kippis_options['kippis_sitetitle_enable'] != 'on') $kippis_option_css_screen .= kippis_savecss_get_style('#site-title'      ,array('#'=> 'display:none;'));
  if ($kippis_options['kippis_tagline_enable'  ] != 'on') $kippis_option_css_screen .= kippis_savecss_get_style('#site-description',array('#'=> 'display:none;'));
  $kippis_option_css_screen .= kippis_savecss_get_style('blockquote'
    ,array('background-color' => $kippis_options['kippis_blockquote_background']
          ,'color'            => $kippis_options['kippis_blockquote_foreground']
          ,'#'                => kippis_savecss_get_fontdata('kippis_blockquote_font')
          ));

  if (!empty($kippis_options['kippis_css_textarea'])) {
    $kippis_option_css_screen .= "\n" . $kippis_options['kippis_css_textarea'] . "\n";
  }

  //----------------------------------------------------------------------------------------------------
  //
  //  Smartphone.
  //
  //----------------------------------------------------------------------------------------------------
  $kippis_option_css_mobile .= kippis_savecss_get_style('body, #searchform#a, #main_menu_mobile_toggle, #main_menu_mobile'
    ,array('background-color' => $kippis_options['kippis_mobile_body_background']
          ,'border-color'     => $kippis_options['kippis_mobile_body_background']
          ,'color'            => $kippis_options['kippis_mobile_body_foreground']
          ));
  if (($kippis_options['kippis_mobile_header_image_enable'] == 'on') && (!empty($kippis_options['kippis_mobile_header_image']['width']))) {
    $forced_height = floor(320.0
                        / (float)$kippis_options['kippis_mobile_header_image']['width']
                        * (float)$kippis_options['kippis_mobile_header_image']['height']);
    $kippis_option_css_mobile .= kippis_savecss_get_style('#pagehead'
      ,array('background-image' => $kippis_options['kippis_mobile_header_image']['url'   ]
            ,'#'                => 'background-size:320px ' . $forced_height . 'px;min-height:' . $forced_height . 'px;'
            ));
  }
  $kippis_option_css_mobile .= kippis_savecss_get_style('nav#nav-single, nav#nav-above, nav#nav-below, .article, .search-result'
    ,array('background-color' => $kippis_options['kippis_mobile_article_background']
          ,'border-color'     => $kippis_options['kippis_mobile_article_background']
          ,'color'            => $kippis_options['kippis_mobile_article_foreground']
          ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('#s'           ,array('background-color' => $kippis_options['kippis_mobile_body_background']));
  $kippis_option_css_mobile .= kippis_savecss_get_style('#searchsubmit',array('border-color'     => $kippis_options['kippis_mobile_body_foreground']));
  $kippis_option_css_mobile .= kippis_savecss_get_style('#main_menu_mobile_toggle, #main_menu_mobile'
    ,array('background-color' => $kippis_options['kippis_mobile_body_background']
          ,'border-color'     => $kippis_options['kippis_mobile_body_foreground']
          ,'color'            => $kippis_options['kippis_mobile_body_foreground']
          ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('.widget'
    ,array('background-color' => $kippis_options['kippis_mobile_widget_background']
          ,'border-color'     => $kippis_options['kippis_mobile_widget_background']
          ,'color'            => $kippis_options['kippis_mobile_widget_foreground']
          ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('.widget-title'
    ,array('background-color' => $kippis_options['kippis_mobile_widget_title_background']
          ,'border-color'     => $kippis_options['kippis_mobile_widget_title_background']
          ,'color'            => $kippis_options['kippis_mobile_widget_title_foreground']
          ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('h1,h2,h3,h4,h5,h6'
    ,array('background-color' => $kippis_options['kippis_mobile_h_background']
          ,'color'            => $kippis_options['kippis_mobile_h_foreground']
  ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('a'
    ,array('background-color' => $kippis_options['kippis_mobile_a_background']
          ,'color'            => $kippis_options['kippis_mobile_a_foreground']
  ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('h1 a,h2 a,h3 a,h4 a,h5 a,h6 a'
    ,array('background-color' => $kippis_options['kippis_mobile_h_a_background']
          ,'color'            => $kippis_options['kippis_mobile_h_a_foreground']
          ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('h1#site-title a, #site-description'
    ,array('background-color' => 'transparent'
          ,'color'            => $kippis_options['kippis_mobile_sitetitle_foreground']
          ,'text-shadow'      => $kippis_options['kippis_mobile_sitetitle_background']
          ));
  if ($kippis_options['kippis_mobile_sitetitle_enable'] != 'on') $kippis_option_css_mobile .= kippis_savecss_get_style('#site-title'      ,array('#'=> 'display:none;'));
  if ($kippis_options['kippis_mobile_tagline_enable'  ] != 'on') $kippis_option_css_mobile .= kippis_savecss_get_style('#site-description',array('#'=> 'display:none;'));
  $kippis_option_css_mobile .= kippis_savecss_get_style('.continue-reading'
    ,array('border-color' => $kippis_options['kippis_mobile_a_foreground']
          ));
  $kippis_option_css_mobile .= kippis_savecss_get_style('.comment-reply-link'
    ,array('border-color' => $kippis_options['kippis_mobile_a_foreground']
          ));

  //----------------------------------------------------------------------------------------------------
  //
  //  Save CSS.
  //
  //----------------------------------------------------------------------------------------------------
  $kippis_option_css = array();
  $kippis_option_css['screen'] = $kippis_option_css_screen;
  $kippis_option_css['mobile'] = $kippis_option_css_mobile;
  update_option('kippis_css',$kippis_option_css);
}}

if (!function_exists('kippis_savecss_get_fontdata')) {
function kippis_savecss_get_fontdata($id_prefix)
{
  global $kippis_options;
  $retval = '';
  $id_font   = $id_prefix;
  $id_size   = $id_prefix . '_size';
  $id_bold   = $id_prefix . '_bold';
  $id_italic = $id_prefix . '_italic';
  if ( !empty($kippis_options[$id_font  ])) $retval .= 'font-family:' . $kippis_options[$id_font] . ';';
  if ( !empty($kippis_options[$id_size  ])) $retval .= 'font-size:'   . $kippis_options[$id_size] . ';';
  if ((!empty($kippis_options[$id_bold  ])) && ($kippis_options[$id_bold  ] == 'on')) $retval .= 'font-weight:bold;';
  if ((!empty($kippis_options[$id_italic])) && ($kippis_options[$id_italic] == 'on')) $retval .= 'font-style:italic;';
  return $retval;
}}

if (!function_exists('kippis_savecss_get_style')) {
function kippis_savecss_get_style($item,$para)
{
  $out = '';
  foreach($para as $css => $data) {
    if (empty($data)) {
      switch ($css) {
        case 'background-color':
        case 'border-color':
          $data = 'transparent';
          break;
      }
    }
    if (!empty($data)) {
      switch ($css) {
      case 'background-linear-gradient':
          $out .= 'background:-moz-linear-gradient('    . $data . ');'
               .  'background:-webkit-linear-gradient(' . $data . ');';
          break;
        case 'background-image':
          $out .= $css . ':url("' . $data . '");background-position:center top;background-repeat:no-repeat;';
          break;
        case 'background-attachment':
        case 'background-repeat':
        case 'background-color':
        case 'border-color':
        case 'border-radius':
        case 'color':
        case 'text-align':
          $out .= $css . ':' . $data . ';';
          break;
        case 'text-shadow':
          $out .= $css . ':0 0 0.6em ' . $data . ',0 0 0.6em ' . $data . ',0 0 0.6em ' . $data . ',0 0 0.6em ' . $data . ',0 0 0.6em ' . $data . ',0 0 0.6em ' . $data . ';';
          break;
        case 'height':
        case 'width':
          $out .= $css . ':' . $data . 'px;';
          break;
        default:  // '#'
          if (!empty($data)) $out .= $data;
      }
    }
  }
  if (!empty($out)) return $item . '{' . $out . '}' . "\n";
  return '';
}}

