// JavaScript Document
J(document).ready( function () {	
    //Récupération du formulaire de commentaire
    J("#joomlacommentform").insertBefore("#col1afcomment");
    J("#joomlacommentform").css('display', 'block');

    if(J("#Comments :first-child").size()== 0)J("#titre_commentaires").css('display', 'none');
    	
    //Récupération du formulaire de commentaire
    if(J("#col1afnav").size()!=0){
    	if(J(".blocArticle").size()!=0)J("#col1afnav").insertBefore(".blocArticle");
        J(".navactu").insertBefore("#col1afnav");
    }else{
        J(".navactu").css("display","none");
    }
    //Récupération des liens print et print pdf
    J("a[title=Imprimer][rel=nofollow]").hide();
    J("a[title=PDF][rel=nofollow]").hide();
    J(".addattach").hide();
    //J(".attachmentsList").hide();
    //PDF
	if(J("a[title=PDF][rel=nofollow]").size()==1){
		var onclick = J("a[title=PDF][rel=nofollow]").attr('onclick');
		var href = J("a[title=PDF][rel=nofollow]").attr('href');
		J("#pdf_header").attr('href', href);
		J("#pdf_header").click(onclick);
		J("#pdf_header").show();
	}
    //PRINT
	if(J("a[title=Imprimer][rel=nofollow]").size()==1){
		var onclick = J("a[title=Imprimer][rel=nofollow]").attr('onclick');
		var href = J("a[title=Imprimer][rel=nofollow]").attr('href');
		J("#print_header").attr('href', href);
		J("#print_header").click(onclick);
		J("#print_header").show();
	}
	
	//masquer les attachments dont le display_filename est égal à 'miniature'
	if(J('.attachmentsList')){
		var n = 0;
		J('.at_url').each(function(){
			if(J(this).html().toUpperCase()=='MINIATURE'){
				J(this).parent().parent().hide();
				n++;
			}
		});
		if(J('.attachmentsList tr').length == n){
			J('.attachmentsList').hide();
		}
	}
    
    //Placement de la sidebar
	if(J("#sidebar").size()!=0 && (J(".blocArticle").size()!=0 || J(".blocComm").size()!=0)){
		J('#sidebar').addClass('pimpMySidebar');
		J(".colonne1").addClass('pimpMyColonne1');
		
		var top = J('.colonne1').offset().top - parseFloat(J('.colonne1').css('margin-top').replace(/auto/, 0));
		var bottom = J('#footer').offset().top;
		var colonne1Size = J('.colonne1').height() + parseFloat(J('.colonne1').css('margin-top').replace(/auto/, 0));
		var msie6 = J.browser == 'msie' && J.browser.version < 7;
		
		if(!msie6 && top+colonne1Size<=bottom){
			J(window).bind('resize scroll', function(){
				//quelle est la position de la scroll
				var y = J(window).scrollTop();
				bottom = J('#footer').offset().top;
				var x = J(window).scrollLeft();
				left = J('#menu').offset().left;
				//est-ce que je dépasse ma sidebar
				if(y >= top && y+colonne1Size < bottom){							
					//si oui, je colle ma postion fixed
					J('.colonne1').addClass('sidebarFixed');
					J('.colonne1').css('top','');
					J('.colonne1').css('left',left-x);								
				}else if((y+colonne1Size) >= bottom){
					J('.colonne1').removeClass('sidebarFixed');
					J('.colonne1').css('top',bottom-colonne1Size);
					J('.colonne1').removeAttr('style');
				}else{
					//si non, je l'enlève
					J('.colonne1').removeClass('sidebarFixed');
					J('.colonne1').removeAttr('style');
				}					
			});
		}else if( top+colonne1Size >bottom){
			J('#footer').css('position','absolute');
			J('#footer').css('top',top+colonne1Size+30);
			J('#footer').css('left','0');
		}
	}
});