$(function() {
	// FadeIN quand la page est chargée
	$('#container > *, #header > *').fadeTo(800, 1);		
	
	// target _blank sur rel="external"
	$("a[rel$='external']").click(function(){
		this.target = "_blank";
	});
	
    // Affichage des blocs sidebar et blocs titres
    $('#section .bloc, #footer .baseline .bloc').lettering('words').removeClass('bloc').children().addClass('bloc');
    
    // Hover article-img : Masquage du titre des articles
    $('.article-img').hover(function() {
        $(this).prev().stop(true,true).fadeTo(300, 0);
    }, function() {
        $(this).prev().stop(true,true).fadeTo(300, 1);
    });
    
    // legende-lien références (home)
    $("#keur .scrollable .item").bind({
            mouseenter:
               function()
               {
                   if ($(this).is('.fii')) {
                       $(this).children('.legende-rea').animate({bottom: '0'}, 200);
                   };
               },
            mouseleave:
               function()
               {
                   if ($(this).is('.fii')) {
                       $(this).children('.legende-rea').animate({bottom: '-56px'}, 200);
                   };
               }
           }
        );
    
    // Main_nav
    // $('.li-main_nav:not(.fii) .li-sub-main_nav').hide();
    var logoBG	= $('#header .logo img').css('backgroundColor');
	
    $('.li-main_nav:not(.fii)').hoverIntent(function() {	
		if ( $(this).is('.go-a-propos') ) {
			var coulBG = '#F0F41E';
		};
		if ( $(this).is('.go-realisations') ) {
			var coulBG = '#ABFF3D';
		};
		if ( $(this).is('.go-formations') ) {
			var coulBG = '#EF5BE7';
		};
		if ( $(this).is('.go-blog') ) {
			var coulBG = '#FFB433';
		};
		$('#header .logo img').stop(true,true).animate({backgroundColor: coulBG}, 400);
		// $('.sub-main_nav', this).css('marginBottom', '16px');		
		var subli = $('.li-sub-main_nav', this);
        var i = 0;
        $(function affsubli(){
            subli.eq(i++).fadeTo(200,1,affsubli);
        });
    }, function() {	
        var subli = $('.li-sub-main_nav', this);
        var j = subli.size();
		$('#header .logo img').stop(true,true).animate({backgroundColor: logoBG}, 400);
		$('.sub-main_nav', this).css('marginBottom', '0px');
        $(function delsubli(){
            if (j > 0) {subli.eq(--j).fadeTo(200,0).hide(0,delsubli);}
        });
    });

	// position fixed for nav_section
	if ( $('body').is('#a-propos') ) {
		var yNavSection = $('.nav_section').offset().left;
		var xNavSection = $('.nav_section').offset().top;
		$('.nav_section').css({position : 'fixed', top : xNavSection + "px", left : yNavSection + "px" });
		// fii sur li_nav_section
		var inview = $('.titre:in-viewport:first').parent().attr('id'),
			// $liNavSection = $('.nav_section a[href=#' + inview + ']').parent();
			$aNavSection = $('.nav_section a[href=#' + inview + ']');
			$aNavSection.parent().addClass('fii');
	};
    // WHEN SCROLL : fixed Header & nav_section
    var HeightHeader    = $('#header').outerHeight();
    var HeightSection   = $('#section').outerHeight();
    var limit           = HeightSection - HeightHeader;
    
	$(window).scroll(function() {
			// comportement Main_nav fixed <=> absolute
	        var y = $(this).scrollTop();        
	        if (y >= limit) {
	          $('#header').css({position: 'absolute', top: limit});
	        } else {
	          $('#header').removeAttr("style");
	        }
            // add .fii to li-nav_section with viewport
            var inview = $('.titre:in-viewport:first').parent().attr('id'),
                    // $liNavSection = $('.nav_section a[href=#' + inview + ']').parent();
            	$aNavSection = $('.nav_section a[href=#' + inview + ']');
            if ( $aNavSection.is(':not(fii)') ) {
                    $('.li-nav_section').removeClass('fii');
                    $aNavSection.parent().addClass('fii');        
            }
	    });

    
    // biggerlist hgoup liste réa
    $('.li-references .hgroup')
        .hover(function() { $(this).toggleClass("hover"); })
        .click(function() { window.location=$(this).find("a").attr("href");
                            return false;
                            });
							
	// Realisations / blog : sub_main_nav Tabs
	$('.tabs').tabs('.panes > div', {
		current: 'fii',
		effect: 'fade',
		onClick: function() {
			$.scrollTo('0px',400,{axis:'y'});
		}
	});
	
	// Mise en valeur de la catégorie selectionnée (liste de réalisations par cat)
	var pageCat = $('.hgroup-category').size();
	if (pageCat == 1) {
		var	titreCat = $('h2.categorie').text();
		$('.meta-references a:contains('+ titreCat +')').addClass('fii');
	}
	
	// Links ScrollTo 
    $('.scrollTo').click(function() {
        var ancre = $(this.hash);
        $.scrollTo(ancre,800,{axis:'y'});
		return false;
    });
 	
	// hr footnotes
	$('p.footnote:first').before('<hr class="separateur separateur-frise" />');
});

// Quand la page est chargée : galleries scrollable
$(window).bind("load", function() {
    // scrolable references
	$('.li-nav_sidebar.li-galerie_scrollable:first').addClass('fii');
    $('.scrollable').scrollable({
        vertical: true,
        circular: true,
        speed: 1000,
        onSeek: function () {
            var position = this.getIndex();
            $('.scrollable .item').not('.cloned').removeClass('fii').eq(position).addClass('fii');
        }
    }).navigator({
        navi: ".nav-galerie",
        activeClass: "fii"
    }).autoscroll({
        interval: 5000
    });

	//Highlight Prévisualisation commentaire
	$('#cpreview').animate({'borderLeftColor': '#FFD999','borderTopColor': '#FFD999','borderBottomColor': '#FFD999','borderRightColor': '#FFD999' }, 1000);
});


