var FullscreenrOptions = {
    width: 1280,
    height: 773,
    bgID: '#bgimg'
};
var bg_delay = 5000; //delai entre changement de background

var carousel_param = {
    direction:"horizontal",
    autoSlide: true,
    loop: true,
    autoSlideInterval: 6000,
    pagination: true,
    animSpeed: "slow",
    slideEasing: "easeOutQuint"
};

var useragent = navigator.userAgent;

if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
} else {
    jQuery.fn.fullscreenr(FullscreenrOptions); //on déclare Fullscreenr
}

function change_bg() {
    $('#bgimg').attr('id', 'old_bgimg'); //on change l'id de l'image active
    if($('#old_bgimg').prev('.bgimg').length == 1) { //si il y a une image avant dans le DOM
        $('#old_bgimg').prev('.bgimg').attr('id', 'bgimg').fullscreenrResizer(FullscreenrOptions); //alors on lui met le bon id et Fullscreenr
        $('#old_bgimg').fadeOut('slow').removeAttr('id'); //on cache l'ancienne image et on vire le vieil id
    } else { //sinon
        $('.bgimg:last').attr('id', 'bgimg').fullscreenrResizer(FullscreenrOptions); //alors on prend la dernière des images et on lui met le bon id et Fullscreenr
        $('#bgimg').fadeIn('slow', function() { //on l'affiche
            $('.bgimg').show(); //après, on affiche toutes les autres
        });
        $('#old_bgimg').removeAttr('id'); //on vire le vieil id
    }
}

$(document).ready(function() {
    if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
    } else {
        setTimeout(change_bg,500); //on lance la fonction une première fois au bout de 500ms
    }

    $("#carouselHome").carousel({
        dispItems: 3, 
        autoSlide: true, 
        autoSlideInterval: 8000, 
        delayAutoSlide: 2000
    });

    $('#billboard').uBillboard({
        speed: 600, 			// animation duration (in miliseconds)
        delay: 3000,			// delay between animations (in miliseconds)
        width: '800px',			// container width
        height: '275px',		// container height
        effect: 'cycle',		// Transition effect: cycle | random | horizontal | vertical | async
        square_resolution: 94,		// resolution of one transition square in pixels
        load_check_interval: 100	// how ofted do we poll for image load completion
    });

    $(".actus").carousel(carousel_param);

    $(".fleche_nav_bottom").click(function(){
        if($(this).parent(".visual_nav_bottom").hasClass("up")){
            $(this).parent(".visual_nav_bottom").removeClass("up");
            $(this).parent(".visual_nav_bottom").animate({
                height: '107px'
            }, 1000);
            $(this).css("background-position", "top");
        }
        else{
            $(this).parent(".visual_nav_bottom").hasClass("onglet_presentation");
            var height = $(this).next().height()+17;
            $(this).parent(".visual_nav_bottom").addClass("up");
            $(this).parent(".visual_nav_bottom").animate({
                height: height+"px"
            }, 1000);
            $(this).css("background-position", "bottom");
        }
    });

    /*var contact_send = false ;
    $("#CONTACT_SEND").click(function(){
          if(($("#CONTACT_FORM").validate().form())&&(!contact_send))
          {
              // envoie du formulaire en AJAX
               var str = $("#CONTACT_FORM").serialize();
                    $.ajax({
                            type: "post",
                            url: "/include/tpl/tpl_ajaxMail.php",
                            data: str,
                            success: function(msg) {
                                $("#message_span").html(msg);
                            }
                    });
          }
    });*/
    $("#CONTACT_FORM").validate();
    

    $.fn.deplacement = function(cible) {
        $(this).append(cible);
        $(this).children().fadeIn();
    }

    $("#carouselHome li img").hover(
        function () {
            var clone = $(this).next().clone();
            $("#temp_img").deplacement(clone);            
        }, function(){
            $("#temp_img a").mouseleave(function(){
                $(this).fadeOut('5000', function(){
                    $("#temp_img").empty();
                });
            	
            });
        }
        );
    
    /*$("#content_nav .onglet_nav_top").hover(
        function(){
            $(".onglet_nav_top .lien").each(function(index, domEle){
                console.log($(this).next().is(".current"));
                if($(this).next().is(".current")){
                    $(this).next().slideUp(400, function(){
                        $(this).css("display", "none");
                        $(this).removeClass("current");
                    });
                    return false;
                }
            });
            var cssObj = {
                "height": $(this).children(".sous_nav").css("height")+"px",
                "width": $(this).children(".sous_nav").css("width")+"px"
            }
            $(this).css(cssObj);
            $(this).children(".sous_nav").addClass("current");
            $(".current").slideDown(400);
        }, 
        function(){                
            $(this).children(".current").slideUp(400, function(){
               $(this).removeClass("current");
            });
        }
    );*/
        
    $("#content_nav > li").hover(function(){
        $(this).addClass('current');
        $(this).children().next().slideDown(400);
    }, function(){
        $(this).removeClass('current');
        $(this).children().next().slideUp(200);
    });
    

    $("#fiche-enseignes").carousel({    
        autoSlide: true,
        loop: true,
        autoSlideInterval: 6000,
        animSpeed: "slow",
        slideEasing: "easeOutQuint"
    });
    
    $("#fiche-img").carousel({    
        autoSlide: true,
        loop: true,
        autoSlideInterval: 6000,
        animSpeed: "slow",
        slideEasing: "easeOutQuint"
    });

    /*$('a.zoombox').zoombox({
        theme       : 'zoombox',        //available themes : zoombox,lightbox, prettyphoto, darkprettyphoto, simple
        opacity     : 0.8,              // Black overlay opacity
        duration    : 800,              // Animation duration
        animation   : true,             // Do we have to animate the box ?
        width       : 600,              // Default width
        height      : 400,              // Default heig
        gallery     : false,             // Allow gallery thumb view
        autoplay : false                // Autoplay for video
    });*/

    $('a.zoombox').zoombox();
    
    $('a.zoombox2').click(function(){
        $.zoombox.html(""+$('#fiche-temoin-text').html()+"", {
            opacity     : 0.8,
            gallery     : false,
            theme: 'lightbox'
        });
        return false;
    });
    
    if('function' == typeof(Map)){
        Map();
    }
    
    $("#map-fiche").click(function(){
        Map_recherche();
        $("#Gmap-fiche").slideDown(400);
    });
    
    /**
 * Gestion du déroulement du menu des fiches lors d'un click dessus
 */

    $(".menu-fiche .niveau2_colLeft").hide();
    
    $(".menu-fiche > ul").css("list-style-image", "url('/images/plus.png')");
    
    $(".menu-fiche ul ul").css("list-style-image", "none");

    $(".menu-fiche .menuDeroulant").click(function() {
          
        if ($(this).next(".menu-fiche ul .niveau2_colLeft").css("display") == "none") {
            $(this).css("list-style-image", "url('/images/moins.png')");
        } else {
            $(this).css("list-style-image", "url('/images/plus.png')");
        }
     
        $(this).nextUntil(".menuDeroulant").slideToggle(500);
        
        return false;
    });

});
