<?php

define( 'JAVES_MIN_PHP_VERSION', '5.3' );

/*-----------------------------------------------------------------------------------*/
/* Enqueue script and styles */
/*-----------------------------------------------------------------------------------*/

if (!function_exists('javes_enqueue_scripts')) {

	function javes_enqueue_scripts() {

		wp_enqueue_style('javes-style', get_stylesheet_uri(), array() );

		$googleFontsArgs = array(
			'family' =>	str_replace('|', '%7C','PT+Serif:700|Poppins:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i'),
			'subset' =>	'latin,latin-ext'
		);

		wp_enqueue_style('google-fonts', add_query_arg ( $googleFontsArgs, "https://fonts.googleapis.com/css" ), array(), '1.0.0' );

		wp_enqueue_style(
			'bootstrap-grid',
			get_template_directory_uri() . '/css/bootstrap-grid.css',
			array(),'4.5.0'
		);

		wp_enqueue_style(
			'owl.carousel',
			get_template_directory_uri() . '/css/owl.carousel.css',
			array(),'2.3.4'
		);

		wp_enqueue_style(
			'owl.theme.default',
			get_template_directory_uri() . '/css/owl.theme.default.css',
			array(),'2.3.4'
		);

		wp_enqueue_script(
			'javes-navigation',
			get_template_directory_uri() . '/js/navigation.js',
			FALSE,
			'1.0',
			TRUE
		);

		wp_enqueue_script(
			'owl.carousel',
			get_template_directory_uri() . '/js/owl.carousel.js',
			array('jquery'),
			'2.3.4',
			TRUE
		);

		wp_enqueue_script(
			'javes-script',
			get_template_directory_uri() . '/js/script.js',
			array('jquery'),
			'1.0',
			TRUE
		);

		if ( is_singular() ) wp_enqueue_script( 'comment-reply' );

		$css = '';

		if ( get_header_image() ) :

			$css .=  '
				header.header {
					background-image: url('.esc_url(get_header_image()).');
					-webkit-background-size: cover !important;
					-moz-background-size: cover !important;
					-o-background-size: cover !important;
					background-size: cover !important;
				}';

		endif;

		if ( get_header_textcolor() ) :

			$css .=  '
				.logo a {
					color: #'.esc_attr(get_header_textcolor()).';
				}';

		endif;

		if ( get_theme_mod('javes_color_overlay') ) :

			$css .=  '
				a:hover,
				a:focus,
				.logo a:hover,
				.logo a:focus,
				#main-menu a:hover,
				#main-menu ul li a:hover,
				#main-menu li:hover > a,
				#main-menu a:focus,
				#main-menu ul li a:focus,
				#main-menu li.focus > a,
				#main-menu li:focus > a,
				#main-menu ul li.current-menu-item > a,
				#main-menu ul li.current_page_item > a,
				#main-menu ul li.current-menu-parent > a,
				#main-menu ul li.current_page_ancestor > a,
				#main-menu ul li.current-menu-ancestor > a {
					color: '.esc_attr(get_theme_mod('javes_color_overlay')).';
				}';

		endif;

		wp_add_inline_style( 'javes-style', $css );

	}

	add_action( 'wp_enqueue_scripts', 'javes_enqueue_scripts' );

}

/*-----------------------------------------------------------------------------------*/
/* Setup theme */
/*-----------------------------------------------------------------------------------*/

if (!function_exists('javes_after_setup_theme')) {

	function javes_after_setup_theme() {

		if ( ! isset( $content_width ) ) $content_width = 900;

		register_nav_menus( array(
			'main-menu' => esc_html__( 'Main menu', 'javes' ),
		));

		add_theme_support('title-tag');
		add_theme_support('automatic-feed-links');
		add_theme_support('post-thumbnails');

		add_image_size(
			'javes_large_thumbnail',
			1110,
			400,
			true
		);

		add_image_size(
			'javes_small_thumbnail',
			730,
			263,
			true
		);

		add_image_size(
			'javes_carousel_thumbnail',
			300,
			300,
			true
		);

		add_theme_support( 'custom-background', array(
		  'default-color' => 'f3f3f3'
		));

		add_theme_support( 'custom-header', array(
			'width' => 1920,
			'height' => 90
		));

		require_once( trailingslashit( get_template_directory() ) . '/core/includes/class-plugin-activation.php' );
		require_once( trailingslashit( get_template_directory() ) . '/core/functions/function-required-plugins.php' );

	}

	add_action( 'after_setup_theme', 'javes_after_setup_theme', 999 );

}

/*-----------------------------------------------------------------------------------*/
/* Get post comments */
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'javes_list_comments' ) ) {

	function javes_list_comments($comment, $args, $depth) {

?>

		<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">

			<div id="comment-<?php comment_ID(); ?>" class="comment-container">

				<div class="comment-avatar">
					<?php echo get_avatar($comment->comment_author_email, 80, 'retro' ); ?>
				</div>

				<div class="comment-text">

					<header class="comment-author">

						<span class="author"><cite><?php printf( esc_html__('%s has written:','javes'), get_comment_author_link());?> </cite></span>

						<time datetime="<?php echo esc_attr(get_comment_date())?>" class="comment-date">

							<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ) ?>">
								<?php printf(esc_html__('%1$s at %2$s','javes'), get_comment_date(),  get_comment_time()) ?>
							</a>

							<?php

								comment_reply_link(
									array_merge(
										$args,
										array(
											'depth' => $depth,
											'max_depth' => $args['max_depth']
										)
									)
								);

								edit_comment_link(esc_html__('(Edit)', 'javes'));

							?>

						</time>

					</header>

					<?php if ($comment->comment_approved == '0') : ?>

						<p><em><?php esc_html_e('Your comment is awaiting approval.','javes') ?></em></p>

					<?php endif; ?>

					<?php comment_text() ?>

				</div>

			</div>

		<?php

	}

}

if (!function_exists('javes_widgets_init')) {

	function javes_widgets_init() {

		register_sidebar(array(

			'name' => esc_html__('Sidebar','javes'),
			'id'   => 'javes-sidebar',
			'description'   => esc_html__('This sidebar will be shown next to the content.', 'javes'),
			'before_widget' => '<div id="%1$s" class="sidebar-widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="title">',
			'after_title'   => '</h4>'

		));

		register_sidebar(array(

			'name' => esc_html__('Footer sidebar','javes'),
			'id'   => 'javes-footer-sidebar',
			'description'   => esc_html__('This sidebar will be shown next at the bottom of your content.', 'javes'),
			'before_widget' => '<div id="%1$s" class="col-md-4 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="title">',
			'after_title'   => '</h4>'

		));

	}

	add_action( 'widgets_init', 'javes_widgets_init' );

}

function javes_customize_register( $wp_customize ) {

	$wp_customize->add_panel('javes_panel', array(
	  'title' => esc_html__( 'Javes settings', 'javes' ),
	));

	$wp_customize->add_section('javes_carousel_settings', array(
	  'title' => esc_html__( 'Carousel settings', 'javes' ),
	  'panel' => 'javes_panel',
	));

	$wp_customize->add_setting( 'javes_carousel_items', array(
	  'default' => 5,
	  'sanitize_callback' => 'absint',
	));

	$wp_customize->add_control( 'javes_carousel_items', array(
	  'type' => 'number',
	  'section' => 'javes_carousel_settings',
	  'label' => esc_html__('Carousel items.', 'javes'),
	  'description' => esc_html__('How many items do you want to display?', 'javes'),
	  'input_attrs' => array(
	    'min'   => 1,
	    'max'   => 10,
	    'step'  => 1,
	  )
  ));

	$wp_customize->add_section('javes_content_settings', array(
	  'title' => esc_html__( 'Content settings', 'javes' ),
	  'panel' => 'javes_panel',
	));

	$wp_customize->add_setting( 'javes_content_info', array(
	  'default' => true,
	  'sanitize_callback' => 'javes_checkbox_sanize',
	));

	$wp_customize->add_control( 'javes_content_info', array(
	  'type' => 'checkbox',
	  'section' => 'javes_content_settings',
	  'label' => esc_html__('Content informations.', 'javes'),
	  'description' => esc_html__('Do you want to display the informations of current content?.', 'javes'),
	));

	$wp_customize->add_section('javes_footer_settings', array(
	  'title' => esc_html__( 'Footer settings', 'javes' ),
	  'panel' => 'javes_panel',
	));

	$wp_customize->add_setting( 'javes_footer_text', array(
	  'default' => '',
	  'sanitize_callback' => 'sanitize_textarea_field',
	));

	$wp_customize->add_control( 'javes_footer_text', array(
	  'type' => 'textarea',
	  'section' => 'javes_footer_settings',
	  'label' => esc_html__('Footer text.', 'javes'),
	  'description' => esc_html__('Insert here the content of footer.', 'javes'),
	));

	$wp_customize->add_setting( 'javes_color_overlay', array(
	  'default' => '#8098D0',
	  'sanitize_callback' => 'sanitize_hex_color',
	));

	$wp_customize->add_control( 'javes_color_overlay', array(
	  'type' => 'color',
	  'section' => 'colors',
	  'label' => esc_html__('Color overlay.', 'javes'),
	  'description' => esc_html__('Select a color on mouse hover.', 'javes'),
	));

	function javes_checkbox_sanize( $input ) {
	  return $input ? true : false;
	}

}

add_action( 'customize_register', 'javes_customize_register' );

?>
