/*
 * Initilisation au chargement : on cache tous les CV. 
 */

var attribute;
$(function(){
	init_navigator();
	init_page();
	//$('a').attr('onfocus','this.blur();');alert('ok');
	$('a').focus(function(){$(this).blur();});
});


function init_navigator() {

	if (navigator.userAgent.indexOf('Firefox/3',0)!=-1) attribute='firefox3';
	if (navigator.userAgent.indexOf('Iceweasel',0)!=-1) attribute='linux';
	if (navigator.userAgent.indexOf('Safari',0)!=-1) attribute='safari';
	if (navigator.userAgent.indexOf('MSIE',0)!=-1) attribute='explorer6';
	if (navigator.userAgent.indexOf('Opera',0)!=-1) attribute='opera';
	if(attribute=='explorer6' && navigator.userAgent.indexOf('MSIE 8',0)!=-1) attribute='explorer';

	feuilles=document.getElementsByTagName('link');
	for (var i=0; i<feuilles.length;i++) {

		if((feuilles[i].getAttribute('rev')!=attribute)&&(feuilles[i].getAttribute('rev'))) feuilles[i].disabled='true'; 
	}
}

function init_page(){ 
	  //pour les publications spécifiquement on cache toutes publica 
	  //puis on affiche la premiere (en pensant au menu munéric et adpater la place de la legende.
		$(".fermer").hide();	
		$('.mce').hide();
		$(".close").hide();
		$('.nbsp').hide();
	  $('.publi_img').hide();
	  img_courante = $('.publi_img').eq(0);
	  $(img_courante).show();
	  //on adate la place de la légende en fonction de la hauteur
	  hauteur = $(".legende > p").height();
	  hauteur=hauteur*(-1);
	  //$(".legende").css("margin-top",(hauteur+13)+"px");
	  $("a", "#menu_numeric").eq(0).addClass("selected");
	  $('.background_img').css("display","none");

	//$('a').focus(function(){blur();});
}
function init_page_listing(){ 
	  //on adate la place de la légende en fonction de la hauteur
	  hauteur = $(".legende > p").height();
	  hauteur=hauteur*(-1);
	  //$(".legende").css("margin-top",(hauteur+13)+"px");
	  $("a", "#menu_numeric").eq(0).addClass("selected");
	  $('.background_img').css("display","none");
	  $('.nbsp').hide();
	//$('a').focus(function(){blur();});
}

function display_presentation(elmt){

	//recherche de la realisation parent
	rea = $(elmt).parent().parent().parent();
	$(".mce", rea).fadeTo(200, 0.99);
	$(".open", rea).hide();
	$(".close", rea).show();//on cache les zone de textes au départ et les liens fermer


	  //les fond d'image ne sont pas activés : ils le seront avec onmouseover sur l'image
	  //$('.background_img').css("display","none");

	
	return false;
}


function hide_presentation(elmt){
	//recherche de la realisation parent
	rea = $(elmt).parent().parent().parent();

	$(".mce", rea).fadeOut(50);
	$(".open", rea).show();
	$(".close", rea).hide();
	
	return false;
	
}//fin de la fonction hide_presentation

function display_listing_rea(elmt){
	if(all_closed==1) $('.rubrique').each(function(){$('a',this).eq(0).removeClass('selected');});
	all_closed=0;
	tmp=elmt.href.split(base);
	listing_url=tmp[1];
	//recuperation de la rubrique
	var listing_cible = $(elmt).parent(); 
	//Si une rubrique est ouverte
	if ($('.realisations',listing_cible).html()!='' && $('.realisations',listing_cible).html()!=null) {
		$(elmt).removeClass('selected');
		$('.container',listing_cible).slideUp(300);
		//$('.container',listing_cible).hide();
		$('.container',listing_cible).html('');
	}
	// Sinon
	else {
		$(elmt).addClass('selected');
		$('body').css('cursor','progress');
		// Ajax publi
		$.ajax({
			url:'/ajax/'+listing_url,
			cache:false,
			timeout:5000,
			error: function(){document.location=elmt.href;},
			success: function(content){
				if($('.realisation',listing_cible).html()!='' && $('.realisation',listing_cible).html()!=null) {fade=1;}
				else {fade=0;}
				$('.container',listing_cible).hide();
				$('.container',listing_cible).html(content);
				init_page_listing();
				if(fade==1)$('.container',listing_cible).fadeTo(300, 0.99);
				else $('.container',listing_cible).slideDown(300);
				//else $('.container',listing_cible).show();
				$('body').css('cursor','default');
				$('a', $('.container',listing_cible)).attr('onfocus','this.blur();');
			}
		});
	}

	return false;
}//fin de la fonction display_rea

function display_rea(elmt){
	tmp=elmt.href.split(base);
	rea_url=tmp[1];
	$(".rubrique","#realisations").each(function(){
		if($('.realisation',this).html()!='' && $('.realisation',this).html()!=null) {display_listing_rea(this.getElementsByTagName('a')[0]);}
	});
	//recuperation de la rubrique
	cible = $(elmt).parent().parent().parent().parent().parent(); 
	$('body').css('cursor','progress');
		// Ajax publi
		$.ajax({
			url:'/ajax/'+rea_url,
			cache:false,
			timeout:5000,
			error: function(){document.location=elmt.href;},
			success: function(content_bis){
				$(cible).hide();
				$(cible).html(content_bis);
				init_page();
				$(cible).fadeTo(300, 0.99);
				offset=$(cible).parent().offset();
				$('body').css('cursor','default');
				$('a', cible).focus(function(){$(this).blur();});
				//setTimeout("$('html,body').animate({ scrollTop: offset.top }, 600)",100);
			}
		});
	

	return false;
	
}//fin de display rea

function display_publi(elmt){
	 next_index= $(elmt).attr('rel');

	 $('.publi_img').each(function(i){
			if(i==next_index) $(this).fadeIn(200);
			else $(this).fadeOut(200);
		});
		 $("a", "#menu_numeric").each(function(i){
			if(i==next_index) $(this).addClass("selected");
			else $(this).removeClass("selected");
		});
index=next_index;
	return false;
}//fin de la fonction display_publi

function display_next(elmt){
	
	theparent = $(elmt).parent();
	index = $(theparent).attr('rel');
	last_index = $(".publi_img:last").attr('rel');
	$("a", "#menu_numeric").removeClass("selected");
	
	
	if(last_index == index){
		
		next_index=0;
		
		//on allume le numero de l'image courante
		$("a", "#menu_numeric").removeClass("selected");
		$("a", "#menu_numeric").eq(0).addClass("selected");
			
	}else{
		next_index = Number(index);
		next_index+=1;
		
		//on allume le numero de l'image courante
		$("a", "#menu_numeric").removeClass("selected");
		$("a", "#menu_numeric").eq(next_index+1).addClass("selected");
	
	}
	 $('.publi_img').each(function(i){
		if(i==next_index) $(this).fadeIn(200);
		else if(i==index) $(this).fadeOut(200);
	});
	 $("a", "#menu_numeric").each(function(i){
		if(i==next_index) $(this).addClass("selected");
		else $(this).removeClass("selected");
	});
	
	//on adate la place de la légende en fonction de la hauteur
	hauteur = $(".legende > p", img_courante).eq(0).height();
	hauteur=hauteur*(-1);
	//deplacement du div
	$(".legende", img_courante).eq(0).css("margin-top", hauteur+"px");
	
	return false;
	
}//fin de diplay_next(elmt)


function display_cv(elmt){
	
	theparent = $(elmt).parent().parent();
	$('.lire', theparent).eq(0).hide();
	$('.mce', theparent).eq(0).fadeTo(150,0.99);
	//$('.mce', theparent).eq(0).show();
	$('.fermer', theparent).eq(0).show();
	$('.fermer', theparent).eq(1).show();
	$('.telecharger_cv', theparent).eq(0).fadeTo(150,0.99);
	//$('.telecharger_cv', theparent).show();

	return false;
	
}//fin de show_cv

function hide_cv_last(elmt){
	theparent = $(elmt).parent().parent().parent();
	//connaître la position du fermer pour repositionner l'internaute au début

	pos = $('.fermer', theparent).eq(0).offset();
	thetop = pos.top - 200;
	$('html,body').animate({ scrollTop: thetop }, 300);
	
	
	return hide_cv(elmt);
}

function hide_cv(elmt){
	theparent = $(elmt).parent().parent().parent();

	$('.mce', theparent).eq(0).hide();
	$('.fermer', theparent).eq(0).hide();
	$('.lire', theparent).eq(0).show();
	

	return false;
}//fin de show_cv


function display_background(elmt){
	the_parent = $(elmt).parent();

	$('.background_img', the_parent).fadeOut(50);
	$('.background_img', the_parent).eq(0).fadeTo(50, 0.99);
}

function display_img(elmt){
	$('.background_img').fadeOut(50);
}


