/*
 * Cette fonction pourrait s'appeler $
 */
jQuery(function() {
    // Gestion de la ligne verticale
    $('#contenthomebox').css('opacity', 0.42);
    $('#contenthomebox').css('visibility', 'visible');
    $('#contentprojetsbox').css('opacity', 0.42);
    $('#contentprojetsbox').css('visibility', 'visible');
    $('#contentcontactbox').css('opacity', 0.42);
    $('#contentcontactbox').css('visibility', 'visible');
    $('#contentclientbox').css('opacity', 0.42);
    $('#contentclientbox').css('visibility', 'visible');
    // Voir fonction changeabstract(descriptionf, descriptioni)
    $('.abstractprojetlien').hide();

    // Slideshow sur la page home via a.slideshow.js
    $(document).ready(function(){
        $('#contenthomeslide').slideshow({
            height:300,
            width:600,
            time:3000,
            playframe:false,
            panel:false,
            effect: 'aviable fade',
            playclick:true,
            imgresize:true,
            titleshow:false,
			loadframe:false
        }).playSlide();
    });

    // Gestion du login sur la page client    
    $("p.montrerlogin").click(function() {
        //$("div.login").show("slow");
        $("div.login").css('opacity', 0.7).show("slow");
    });
    
    // Fancybox
    // Permet l'ouverture d'une fenêtre au-dessus. Il faut un lien avec la
    // classe fancybox
    $('a.fancybox').fancybox({
        opacity:true,
        transitionIn: 'elastic',
        transitionOut: 'fade',
        titleShow: false,
        cyclic: true
    });
    $('a.fancyboxprojet').fancybox({
        overlayOpacity: 0.7,
        transitionIn: 'elastic',
        transitionOut: 'fade'
    });    
    $("#data").hide();
    $("a.fancytext").fancybox({
		titlePosition       : 'inside',
		transitionIn		: 'none',
		transitionOut		: 'none',
        onStart             : function() {$('#data').show();},
        onClosed            : function() {$('#data').hide();},
        hideOnContentClick  : true
	});

    // Changement du div de description courte on mouseOver et
    // lancement de la page projet sur le clic
    $(".changeabstract").mouseover(function() {
        var param = $(this).attr("title");
        var junk = param.split(":::");
        var name = junk[1];
        var lien = junk[0] + "/" + junk[1] + "/description-courte.txt";
        $('.abstractprojetdesc').load(lien);        
        $('.abstractprojetlien').show().html(
            "<a href=\"?p=projet&n="+name+"\" class=\"fancybox\">Plus d'infos</a>");
    });
    $(".changeabstract").click(function() {
        var param = $(this).attr("title");
        var junk = param.split(":::");
        var name = junk[1];
        window.location.replace("?p=projet&n="+name);
    });
    
    $(function(){
        makeScrollable(".scrollerwrapper", ".scroller");
    });

});

function changeabstract(descriptionf, descriptioni){
    $('.abstractprojetdesc').load(descriptionf);    
}

/*
 * Sur base du tuto http://valums.com/vertical-scrolling-menu/
 * Permet le scroll vertical ... 
 */
function makeScrollable(wrapper, scrollable){
    // Get jQuery elements
    var wrapper = $(wrapper);
    var scrollable = $(scrollable);

    // Hide images until they are not loaded
    scrollable.hide();
    var loading = $(
        "<div class='loading'>Loading <img src=\"img/loader.gif\" /></div>")
        .appendTo(wrapper);

    // Set function that will check if all images are loaded
    var interval = setInterval(function(){
        var images = scrollable.find("img");
        var completed = 0;
        // Counts number of images that are succesfully loaded
        images.each(function(){
            if (this.complete) completed++;
        });
        if (completed == images.length){
            clearInterval(interval);
            // Timeout added to fix problem with Chrome
            setTimeout(function(){
                loading.hide();
                // Remove scrollbars
                wrapper.css({
                    overflow: "hidden"
                });
                scrollable.slideDown("slow", function(){
                    enable();
                });
            }, 1000);
        }
    }, 100);

    // Fonction pemettant le csrolling sur la zone lorsque les images
    // sont chargées.
    function enable(){
        // height of area at the top at bottom, that don't respond to mousemove
        var inactiveMargin = 100;
        // Cache for performance
        //var wrapperWidth = wrapper.width();
        var wrapperHeight = wrapper.height();        
        // Using outer height to include padding too
        var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;        
        // Do not cache wrapperOffset, because it can change when user resizes window
        // We could use onresize event, but it&#39;s just not worth doing that
        // var wrapperOffset = wrapper.offset();

        //When user move mouse over menu
        wrapper.mousemove(function(e){
            var wrapperOffset = wrapper.offset();
            var top;
            // Scroll menu//          
            top = (e.pageY -  wrapperOffset.top) 
                * (scrollableHeight - wrapperHeight)
                / wrapperHeight - inactiveMargin;
            
            if (top < 0){
                top = 0;
            }
            wrapper.scrollTop(top);
        });
    }
}









