<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages and that other
 * 'pages' on your WordPress site will use a different template.
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

get_header(); ?>

	<div id="primary" class="content-area">



        <div id="content" class="site-content" role="main">

        <?php
        global $post;


$page_courante = $post;





if($post->post_parent != 0)
{
    $id = $post->post_parent;
    $post_parent = get_post($id);
    //echo "<li class='menu-item'><a href=\"" . get_permalink($post_parent->ID) . "\">" .$post_parent->post_title. "</a></li>";
}
else
{
   $id = $post->ID;
}


$post_ID = $post->ID;
$post_content = apply_filters('the_content', $post->post_content);
preg_match_all('/<h2\s*class=".*titre_ancre[^"]*">([^<]+)<\/h2>/i', $post_content, $matchs, PREG_SET_ORDER);
$anchors = array();
foreach ($matchs as $key => $value) {
    $anchors[] = array('link' => 'anchor' . $key, 'title' => $value[1]);
    $post_content = str_replace($value[0], '<a name="anchor' . $key . '"></a>' . $value[0], $post_content);
}


if($post->post_parent == 164)
    $id = 157;

        if ( is_page())
        {
        ?>
        <div class="bloc_gauche">
            <ul>
        <?php


            $data = new WP_Query();
            $sous_data = new WP_Query();


            $args = array(
                'numberposts' => -1,
                'post_type'   => 'page',
                'post_parent' => $id,
                'order' => 'ASC',
                'orderby' => 'menu_order'
            );
            $data->query($args);


            foreach($data->posts as $key => $valeur)
            {

                    if(htmlentities($valeur->post_title) == htmlentities($page_courante->post_title))
                    {
                        $laclasse = "current-menu-item";
                    }
                    else
                    {
                        $laclasse = "";
                    }
            ?>
                <li class="menu-item <?php echo $laclasse; ?>"><div class="lien_sous_menu"><a href="<?php echo get_permalink($valeur->ID); ?>"><?php echo $valeur->post_title; ?></div></a>

                 <?php
                    if(count($anchors) >= 1 && $post_ID == $valeur->ID)
                    {
                    echo '<ul>';
                    foreach ($anchors as $key => $a)
                    {

                    ?>
                         <li><a href="<?php echo get_permalink($valeur->ID); ?>#<?php echo $a['link'] ?>">&gt; <?php echo $a['title']; ?></a></li>
                    <?php
                    }
                    echo '</ul>';
                    }
                ?>


                </li>
            <?php
            }

       wp_reset_query();
        }
        ?>
            </ul>
        </div>
        <div class="bloc_droite">

			<?php /* The loop */ ?>
			<?php while ( have_posts() ) : the_post(); ?>
             <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
					<h2 class="entry-title"><?php the_title(); ?></h2>
						<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
						<div class="entry-thumbnail">
							<?php the_post_thumbnail(null, array('alt' => '')); ?>
						</div>
						<?php endif; ?>
                    <div class="entry-content">
						<?php
                            if ($post->post_name == 'faq') {
                                the_content();
                            } else if (preg_match('#youtube.com/watch|youtu.be#i', $post_content)) {
                                $patern = '#(https?:\/\/)?(www.)?(youtube.com\/watch\?v=|youtu.be\/)([\w_-]{11})(.*)#i';
                                $iframe = '<iframe width="500" height="282" src="//www.youtube.com/embed/\\4?rel=0" frameborder="0" allowfullscreen></iframe>';
                                echo preg_replace($patern, $iframe, $post_content);
                                //echo $post_content;
                            } else {
                                echo $post_content;
                            }
                        ?>
						<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
					</div><!-- .entry-content -->

					<footer class="entry-meta">
						<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
					</footer><!-- .entry-meta -->
				</article><!-- #post -->

				<?php //comments_template(); ?>
			<?php endwhile; ?>
            <div style="clear: both;"></div>
        </div>
            <?php get_sidebar(); ?>
            <div style="clear: both;"></div>
		</div><!-- #content -->
        <div style="clear:both;"></div>
	</div><!-- #primary -->

<?php get_footer(); ?>