<?php 
require("config.php");

$Photos = "actif";

$afficher_galeries = mysqli_query($mysqlLink, "SELECT * FROM `galerie` WHERE `rewrite` = '".$_GET['galeries']."' ");
while($afficher_galerie = mysqli_fetch_array($afficher_galeries))
{
    $afficher_galerie_id = $afficher_galerie['id'];
    $afficher_galerie_nom = $afficher_galerie['nom'];
    $afficher_galerie_titre = $afficher_galerie['titre'];
    $afficher_galerie_rewrite = $afficher_galerie['rewrite'];
    $afficher_galerie_meta_title = $afficher_galerie['meta_title'];
    $afficher_galerie_meta_description = $afficher_galerie['meta_description'];
    $afficher_galerie_contenu = $afficher_galerie['contenu'];
    $afficher_galerie_vue = $afficher_galerie['vue']+1;

    mysqli_query($mysqlLink, "UPDATE `galerie` SET `vue` = '".$afficher_galerie_vue."' WHERE rewrite = '".$afficher_galerie_rewrite."' ");
}

//Si requête ajax chargement photos
if(isset($_GET['ajax']) && isset($_GET['limit']) && !empty($_GET['limit']) && is_numeric($_GET['limit']))
{
    $selection_photos = mysqli_query($mysqlLink, "SELECT * FROM galerie_photos WHERE id_rubrique = '$afficher_galerie_id' AND titre != '' ORDER by id_photo_new DESC LIMIT ".intval($_GET['limit']).", 60");
    
    if(isset($_GET['checkNextPage']))
    {
        echo mysqli_num_rows($selection_photos);
        exit();
    }

    $return_json = array();

    while($photo = mysqli_fetch_assoc($selection_photos))
    {
        //Séléction de la moyenne
        $selection_moyenne_note = mysqli_query($mysqlLink, "SELECT SUM(note) AS total_note, COUNT(note) AS nb_notes FROM galerie_photos_notes WHERE id_photo_new = '".$photo['id_photo_new']."'");
        $moyenne_note = mysqli_fetch_assoc($selection_moyenne_note);

        $return_json[] = array(
            'galerie_rewrite' => $afficher_galerie_rewrite,
            'photo' => $photo,
            'nb_notes' => $moyenne_note['nb_notes'],
            'total_note' => $moyenne_note['total_note']
        );
    }

    echo json_encode($return_json);

    exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> 
    <title>Idée <?php echo $afficher_galerie_nom; ?> , Galerie photo de <?php echo $afficher_galerie_titre; ?> - <?php echo $jour; ?></title>
    <meta name="description" content="<?php echo $afficher_galerie_meta_description; ?>">
    <meta charset="utf-8">
    <link rel="icon" href="/images/favicon.ico" type="image/x-icon">    
    <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/css/jquery.rateyo.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <script src="/js/jquery.js"></script>
    <script src="/js/superfish.js"></script>
    <script src="/js/jquery.hoverIntent.minified.js"></script>
    <script src="/js/jquery.easing.1.3.js"></script>
    <script src="/js/jquery.mobilemenu.js"></script>
    <script src="/js/jquery.ui.totop.js"></script>
    <script src="/js/jquery.rateyo.min.js"></script> 
    <script src="/js/script.js"></script>   

    <style>
    #grid[data-columns]::before {
        content: '3 .column.size-1of3';
    }

    @media screen and (max-width: 480px){
        #grid[data-columns]::before {
            content: '2 .column.size-1of2';
        }
    }

    @media screen and (min-width: 481px) and (max-width: 768px) {
        #grid[data-columns]::before {
            content: '2 .column.size-1of2';
        }
    }
    @media screen and (min-width: 769px) {
        #grid[data-columns]::before {
            content: '3 .column.size-1of3';
        }
    }

    /* These are the classes that are going to be applied: */
    .column { float: left; margin-left: 10px; }
    .size-1of1 { width: 100%; }
    .size-1of2 { width: 46%; }
    .size-1of3 { width: 31.5%; }
    </style>

    <script>
    jQuery(function(){
        $().UItoTop({ easingType: 'easeOutQuart' });


        var checkNextPage = function() {

            $.ajax({
                url: "/galeries.php?galeries=<?php echo $afficher_galerie_rewrite; ?>",
                type: 'get',
                data: 'ajax&checkNextPage&limit=' + $('.itemArticle').size(),
 
                //Succès de la requête
                success: function(data) {

                    if(data == 0)
                        $("#chargeMorePhotos").remove();

                }
            });                

        }

        $("#chargeMorePhotos").click(function() {

            $.ajax({
                url: "/galeries.php?galeries=<?php echo $afficher_galerie_rewrite; ?>",
                type: 'get',
                dataType: 'json',
                data: 'ajax&limit=' + $('.itemArticle').size(),
 
                //Succès de la requête
                success: function(data) {

                    var grid = document.querySelector('#grid');
                    var item = document.createElement('article');

                    $(data).each(function (i, Photo) {

                        var moyenne_note = (Photo.photo.nb_notes > 0 ? (Photo.photo.total_note / Photo.photo.nb_notes) : '0');

                        var h = '<article class="itemArticle">';
                        h += '<div class="inner-block" style="background: url(/pixel.png) repeat;">';
                            h += '<div class="page1-box2">';
  
                                h += '<figure class="page1-img2"><a href="http://www.bestcakes.fr/idees-cakedesign/' + Photo.galerie_rewrite + '/' + Photo.photo.id_photo_new + '_' + Photo.photo.rewrite + '/" title="' + Photo.photo.titre + '"><img src="http://www.bestcakes.fr/photo/' + Photo.galerie_rewrite + '/' + Photo.photo.id_photo + '/thumb_' + Photo.photo.rewrite + '-' + Photo.photo.id_photo + '.jpg" alt="' + Photo.photo.titre + '" /></a></figure>';
                                
                                for(i = 1; i <= moyenne_note; i++)
                                {
                                    h += '<svg fill="#f39c12" height="20px" width="20px" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 12.705 512 486.59" x="0px" y="0px" xml:space="preserve"><polygon points="256.814,12.705 317.205,198.566 512.631,198.566 354.529,313.435 414.918,499.295 256.814,384.427 98.713,499.295 159.102,313.435 1,198.566 196.426,198.566 "/></svg>';
                                }

                                for(i = 1; i <= (5 - moyenne_note); i++)
                                {
                                    h += '<svg fill="#fff" height="20px" width="20px" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 12.705 512 486.59" x="0px" y="0px" xml:space="preserve"><polygon points="256.814,12.705 317.205,198.566 512.631,198.566 354.529,313.435 414.918,499.295 256.814,384.427 98.713,499.295 159.102,313.435 1,198.566 196.426,198.566 "/></svg>';
                                }

                                h += '- ' + moyenne_note + ' / 5'

                                h += '<span class="text4">' + Photo.photo.titre + '</span>';
                                h += '<a href="http://www.bestcakes.fr/idees-cakedesign/' + Photo.galerie_rewrite + '/' + Photo.photo.id_photo_new + '_' + Photo.photo.rewrite + '/" title="' + Photo.photo.titre + '" class="link2"></a>';

                            h += '</div>';
                        h += '</div>';
                        h += '</article>';
                        salvattore['append_elements'](grid, [item])
                        item.outerHTML = h;

                    });

                    checkNextPage();

                }
            });

            return false;

        });

    });
    </script>
<!--[if lt IE 8]>
   <div style=' clear: both; text-align:center; position: relative;'>
     <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
       <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today." />
    </a>
  </div>
<![endif]-->
<!--[if lt IE 9]>
    <script src="js/html5.js"></script>
    <link rel="stylesheet" href="css/ie.css"> 
<![endif]-->
</head>
<body>
<div class="bg">
<!--==============================header=================================-->
    <header>
        <div class="head-box1">
        <div class="container_16">
            <article class="grid_16">
                <div class="inner-block">
                    
                    <?php include("menu.php"); ?>
                    
            </div>
            </article>
        </div>
        </div>  
        <div class="container_16">
            <article class="grid_16 p6">
                <div class="inner-block"><h1><a href="http://www.bestcakes.fr" title="Best Cakes <?php echo $afficher_galerie_titre; ?>" class="logo">Best Cakes</a></h1></div>                
            </article>
            <div class="clear"></div>
        </div>        
    </header>
    
    <!--==============================content================================-->
    <section id="content">      
        <div class="container_16">
                <article class="grid_4 suffix_1">
                    <div class="inner-block">
                        <h3 style="font-size:25px;">idées <span>Cake Design</span></h3>
                        <ul class="list1">

                            <?php include("nav2.php"); ?>
                    
                        </ul>
                    </div>
                </article>
                <article class="grid_12">
                    <div class="inner-block">

                        <h2 class="p12">
                            <span class="text6">Idée <?php echo $afficher_galerie_nom; ?> , Galerie photo de <?php echo $afficher_galerie_titre; ?></span>
                        </h2>

                        <div id="grid" data-columns>
                            <?php
                            $selection_photos = mysqli_query($mysqlLink, "SELECT * FROM galerie_photos WHERE id_rubrique = '$afficher_galerie_id' AND titre != '' ORDER by id_photo_new DESC LIMIT 0, 60");
                            while($photo = mysqli_fetch_assoc($selection_photos))
                            {
                                //Séléction de la moyenne
                                $selection_moyenne_note = mysqli_query($mysqlLink, "SELECT SUM(note) AS total_note, COUNT(note) AS nb_notes FROM galerie_photos_notes WHERE id_photo_new = '".$photo['id_photo_new']."'");
                                $moyenne_note = mysqli_fetch_assoc($selection_moyenne_note);

                                $moyenne_note = round($moyenne_note['nb_notes'] > 0 ? ($moyenne_note['total_note'] / $moyenne_note['nb_notes']) : '0');
                            ?>
                            <article class="itemArticle">
                                <div class="inner-block" style="background: url(/pixel.png) repeat;">
                                    <div class="page1-box2">
                                        <figure class="page1-img2" style="margin-bottom:10px;">
                                            <a href="http://www.bestcakes.fr/idees-cakedesign/<?php echo $afficher_galerie_rewrite; ?>/<?php echo $photo['id_photo_new']; ?>_<?php echo $photo['rewrite']; ?>/" title="<?php echo stripslashes($photo['titre']); ?>">
                                                <img src="http://www.bestcakes.fr/photo/<?php echo $afficher_galerie_rewrite; ?>/<?php echo $photo['id_photo']; ?>/thumb_<?php echo $photo['rewrite']; ?>-<?php echo $photo['id_photo']; ?>.jpg" />
                                            </a>
                                        </figure>
                                        <?php
                                        for($i = 1; $i <= $moyenne_note; $i++)
                                        {
                                            echo '<svg fill="#f39c12" height="20px" width="20px" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 12.705 512 486.59" x="0px" y="0px" xml:space="preserve"><polygon points="256.814,12.705 317.205,198.566 512.631,198.566 354.529,313.435 414.918,499.295 256.814,384.427 98.713,499.295 159.102,313.435 1,198.566 196.426,198.566 "/></svg>';
                                        }

                                        for($i = 1; $i <= (5 - $moyenne_note); $i++)
                                        {
                                            echo '<svg fill="#fff" height="20px" width="20px" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 12.705 512 486.59" x="0px" y="0px" xml:space="preserve"><polygon points="256.814,12.705 317.205,198.566 512.631,198.566 354.529,313.435 414.918,499.295 256.814,384.427 98.713,499.295 159.102,313.435 1,198.566 196.426,198.566 "/></svg>';
                                        }
                                        ?>
                                        - <?php echo $moyenne_note; ?> / 5
                                        <span class="text4"><?php echo stripslashes($photo['titre']); ?></span>
                                        <a href="http://www.bestcakes.fr/idees-cakedesign/<?php echo $afficher_galerie_rewrite; ?>/<?php echo $photo['id_photo_new']; ?>_<?php echo $photo['rewrite']; ?>/" title="<?php echo stripslashes($photo['titre']); ?>" class="link2"></a>
                                    </div>
                                </div>
                            </article>
                            <?php
                            }
                            ?>
                        </div>

                        <?php 
                        $selection_nb_photos = mysqli_query($mysqlLink, "SELECT * FROM galerie_photos WHERE id_rubrique = '$afficher_galerie_id' AND titre != ''");
                        if(mysqli_num_rows($selection_nb_photos) > 28)
                        {
                        ?>
                        <a href="#" class="button1" id="chargeMorePhotos">Afficher plus</a>
                        <?php
                        }
                        ?>

                        <p style="clear:both;"><?php echo $afficher_galerie_contenu; ?></p>

                    </div>
                </article>
            <div class="clear"></div>
        </div>
    </section>

    <?php include("footer.php"); ?>

</div>
    
    <script src="/js/salvattore.js"></script>  

</body>
</html>