<?php

function kit_setup() {

  load_theme_textdomain( 'kit', get_template_directory() . '/languages' );

  add_theme_support( 'menus' );
  add_theme_support( 'post-thumbnails' );
  add_theme_support( 'custom-logo' );

  register_nav_menus( array(
    'primary' => 'Main Menu',
    'secondary' => 'Footer Menu'
  ) );

};

add_action( 'after_setup_theme', 'kit_setup' );

class kit_custom_main_menu extends Walker_Nav_Menu {

  function start_lvl( &$output, $depth = 0, $args = array() ) {

      // depth dependent classes
      $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
      $display_depth = ( $depth + 1); // because it counts the first submenu as 0
      $classes = array(
          'sub-menu',
          'menu-depth-' . $display_depth
      );
      $class_names = implode( ' ', $classes );

      // build html
      if ($display_depth == 1) {
          $output .= "\n" . $indent . '
            <section class="nav-second-level mega-flyout" style="">
              <div class="content-wrap">
                <div class="container container-3-cols">
            ' . "\n";
      } else {
          $output .= "\n" . $indent . '<ul>' . "\n";
      }
  }

  function end_lvl( &$output, $depth = 0, $args = array() ) {

      $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
      $display_depth = ( $depth + 1); // because it counts the first submenu as 0
      $classes = array(
          'sub-menu',
          'menu-depth-' . $display_depth
      );
      $class_names = implode( ' ', $classes );

      // build html
      if ($display_depth == 1) {
          $output .= "\n" . $indent . '
                </div>
              </div>
            </section>' . "\n";
      } else {
          $output .= "\n" . $indent . '</ul>' . "\n";
      }
  }

    function start_el(&$output, $item, $depth = 0, $args=array(), $id = 0) {
    	$object = $item->object;
    	$type = $item->type;
    	$title = $item->title;
    	$description = $item->description;
    	$permalink = $item->url;

      $theme_template_path = get_template_directory_uri();

      if ($depth == 1) {
        $output .= '
        <div class="column">
          <div class="nav-second-level-group">
            <a class="nav-second-level-group-headline" href="' . $permalink . '">
              <div class="icon"></div>
              <span>
            ';
        $output .= $title;
        $output .= '
              </span>
            </a>
        <div class="nav-second-level-opener">
          <span>Untermenu öffnen / schließen</span>
        </div>';
      } else {
        $output .= "<li class='" .  implode(" ", $item->classes) . "'>";
        $output .= '<div class="nav-first-level-opener">
                      <span>Untermenu öffnen / schließen</span>
                    </div>';
    	  $output .= '<a href="' . $permalink . '">';
        $output .= $title;
      	$output .= '</a>';
      }
    }

    function end_el(&$output, $item, $depth = 0, $args=array(), $id = 0) {
    	$object = $item->object;
    	$type = $item->type;
    	$title = $item->title;
    	$description = $item->description;
    	$permalink = $item->url;

      if ($depth == 1) {
        $output .= '
                  </div>
                </div>';
      } else {
      	$output .= '</li>';
      }
    }

}

function theme_customize_register( $wp_customize ) {

    $wp_customize->add_setting( 'theme_color', array(
      'default'   => '#009982',
      'transport' => 'refresh',
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'theme_color', array(
      'section' => 'colors',
      'label'   => esc_html__( 'Theme color', 'theme' ),
    ) ) );

    $wp_customize->add_setting('contact_mail', array(
        'default'        => '',
        'type'           => 'option',
        'transport' => 'refresh',
    ));

    $wp_customize->add_control('contact_mail', array(
        'label'      => __('Contact mail', 'contact_mail'),
        'section'    => 'core',
        'settings'   => 'contact_mail',
    ));

}

add_action( 'customize_register', 'theme_customize_register' );

function kit_customize_css()
{
    ?>
         <style type="text/css">
             aside .aside-icon {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             nav > ul > li:hover > a,
             nav > ul > li:active > a {
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             nav > ul > li:hover > a::after,
             nav > ul > li:active > a::after {
               border-bottom: 4px solid <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .header-button {
               /* background-image: linear-gradient(0deg, <?php echo get_theme_mod('theme_color', '#009982'); ?>, rgb(0,0,0,0.2)); */
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
               box-shadow: inset 0 14px 16px -16px white;
             }

             .nav-second-level-group-headline {
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .opened > a {
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             nav > ul > li .nav-first-level-opener::before, nav > ul > li .nav-first-level-opener::after {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .nav-second-level .nav-second-level-opener::before, .nav-second-level .nav-second-level-opener::after {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .headline-element h2::after {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             form input[type='submit'] {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .tx-indexedsearch .tx-indexedsearch-searchbox form fieldset .tx-indexedsearch-search-submit button {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .tx-indexedsearch .tx-indexedsearch-browsebox ul li.current a {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .tx-indexedsearch .tx-indexedsearch-browsebox ul li a:hover {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .tx-indexedsearch .tx-indexedsearch-res {
               border-bottom: 1px solid <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .tx-indexedsearch .tx-indexedsearch-res .tx-indexedsearch-title a:hover {
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .tx-indexedsearch .tx-indexedsearch-res .link-more span:hover {
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .tx-indexedsearch .tx-indexedsearch-browsebox ul li .current {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .nav-second-level-group-headline .icon {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .header-lang li a:hover {
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .header-lang li.active span {
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             h2::after {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .page-category-marker {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .teaser-box-color-brand-secondary .teaser-box-category-marker {
               border-bottom-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
               color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

             .teaser-box-color-brand-secondary .teaser-box-category-marker:hover {
               filter: brightness(120%);
             }

             .teaser-box-category-marker .icon {
               background-color: <?php echo get_theme_mod('theme_color', '#009982'); ?>;
             }

         </style>
    <?php
}
add_action( 'wp_head', 'kit_customize_css');


function kit_modify_posts_per_page( $query ) {
  if ( $query->is_search() ) {
    $query->set( 'posts_per_page', '10' );
  }
}
add_action( 'pre_get_posts', 'kit_modify_posts_per_page' );
