$(document).ready(function() {

    $('#slideshow').cycle({
        fx: 'scrollRight',
        timeout:  0,
        pager:   '#nav',
        pagerAnchorBuilder: pagerFactory
    });

    function pagerFactory(idx, slide) {
        var s = idx > 2 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
    };
    
});

$(document).ready(function() {
       hauteurEgale($(".blocs"));
    });

$(document).ready(function(){$("#slideshow-row0").cycle({fx:"fade",timeout:5000,speed:2500,prev:".prev",next:".next"})});

function hauteurEgale(group) {
       tallest = 0;
       group.each(function() {
          thisHeight = $(this).height();
          if(thisHeight > tallest) {
             tallest = thisHeight;
          }
       });
       group.height(tallest);
    }
    
/*CAROUSELS*/

var carouselMoving = false;
var isInfoBulle = null;
var infoBulleTimeout = null;

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
        if(isInfoBulle!=null){
            cancelCloseInfoBulle();
        }
    }, function() {
        carousel.startAuto(0);
         if(isInfoBulle!=null){
            initCloseInfoBulle();
         }
    });

    initInfobulle();
    
};

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        auto: 2,//Définit le temps de scrolling ici 2 secondes
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        onBeforeAnimationCallback : function(carousel){
            hideInfoBulle();
            carouselMoving = true;
        },
        onAfterAnimationCallback : function(carousel){
            carouselMoving = false;
        }
    });    
});

function initInfobulle(){
    var items = $("li.jcarousel-item a");

    items.unbind().click(function(e){
        e.preventDefault();
    })
    .hover(function(){
        if(!carouselMoving) showInfoBulle( $(this) );
    }, function(){
        
    });
}

function showInfoBulle( obj ){
    var index = obj.parent().attr("jcarouselindex");
    if(index == isInfoBulle) return;

    hideInfoBulle();
    cancelCloseInfoBulle();

    var params = $.parseJSON(obj.attr("rel"));
    var url = obj.attr("href");
    var offset = obj.offset();
    var top = Math.round( $("ul#mycarousel").offset().top );
    var center = Math.round(offset.left - ((145-obj.width()))/2);
    

    var infobulle = $('<div id="infoBulle" rel="infoBulle-'+index+'"><dl><dt>' + params.name + '</dt><dd><a href="' + url + '" title="'+ params.text +'">'+ params.text +' »</a></dd></dl></div>');
    infobulle.css({
        'top' : top - 60 + 'px',
        'left' : (center-20) + 'px',
        'opacity' : 0,
        'z-index' : 999
    });
    
    infobulle.appendTo('body')
    .animate({
        'opacity' : 1,
        'left' : center + 'px'
    }, 300, infoBulleReady );
    isInfoBulle = index;
}

function hideInfoBulle(){
    $("#infoBulle").remove();
    isInfoBulle = null;
}
function fadeInfoBulle(){
    $("#infoBulle")
    .fadeOut(250, function(){
        hideInfoBulle();
    });
}

function infoBulleReady(){
    $("#infoBulle").unbind().hover(function(){
        cancelCloseInfoBulle();
    }, function(){
        initCloseInfoBulle();
    });
}

function initCloseInfoBulle(){
    infoBulleTimeout = window.setTimeout("fadeInfoBulle()", 250);
}
function cancelCloseInfoBulle(){
    window.clearTimeout(infoBulleTimeout);
}

//FONCTION QTIP
$(document).ready(function()
{
    /*
     * You'll need your own API key, don't abuse mine please!
     * Get yours here: http://www.websnapr.com/free_services/
     */

    // Notice the use of the each method to gain access to each element individually
    /*$('#mycarousel a').each(function()
    {
		var params = $.parseJSON($(this).attr("rel"));
		var url = $(this).attr("href");
        // Setup the tooltip with the content
        $(this).click(function(e){e.preventDefault()}).qtip(
        {
            content: '<p class="noMargin font13to14">' + params.name + '</p><a class="white noMargin font13to12" href="' + url + '" title="'+ params.text +'">'+ params.text +' »</a>',
            position: {
                corner: {
                    target: ''
                },
                adjust: {
                    x: 0,//Détermine la position de la bulle sur l'axe des abscisses
                    y: -90,//Détermine la position de la bulle sur l'axe des ordonnées
                    screen: true // Keep the tooltip on-screen at all times
                } 
            },
            show: { 
                when: 'click', 
                solo: true // Only show one tooltip at a time
            },
            hide: 'unfocus',//Définit quand la bull disparait ()
            style: {
                tip: false, // Give it a speech bubble tip with automatic corner detection
                //name: 'infoBulle',appel de la classe presente dans le fichier js
                width: 145,
                height: 79,
                padding: '5px 20px 0 20px',
                background: 'url(images/bkg-pop-up-bleu-accueil.png) no-repeat top left',
                color: '#FFFFFF',
                border: 'none'
            }
        });
    });*/
});
