
$(document).ready(function(){
	
	
	
	$(".btn-more-info").prepend('<span class="icon"></span>');
	
	//#####################################################################################################
	//##  Ajustement du titre de page  ####################################################################
	//alert($("#content-title h1").width()+" x "+$("#content-title h1").height())
	if($("#content-title h1").height() > 60){
		fontSize = parseFloat($("#content-title h1").css("fontSize").replace(/px/g,""));
		$("#content-title h1").css({fontSize: (fontSize*.8)+"px"})
		var paddingTop = ($("#content-title").height() - $("#content-title h1").height()) / 2
		$("#content-title h1").css({paddingTop:paddingTop+"px"})
	}	
	//il faut affecter le font family plus tard pour s'assurer du bon redimentionnement
	$("#content-title h1").css({fontFamily: 'SansationBold'});
	
	//Ajuste le padding des li du menu principal à la largeur nécessaire
	/*console.log($("#nav-main").width())
	console.log($("#nav-main ul").width())*/
	if($("#nav-main ul").width() > $("#nav-main").width()){
		var width_link = 0;
		$("#nav-main > ul > li > a").each(function(index,e){
			width_link += $(this).outerWidth();
		})
		//console.log(width_link)
		padding = ($("#nav-main").width() - width_link) / $("#nav-main > ul > li").size() / 2
		//console.log(padding)
		$("#nav-main > ul > li").css({paddingLeft:padding+"px", paddingRight:padding+"px"})
	}
	
	
	
	
	//#####################################################################################################
	//##  Fonctions de liens  #############################################################################
	$("a[href='#']").click(function(){return false;});
	
	$('a[href*=".pdf"], a[href*=".doc"], a[href*=".docx"], a[href*=".xsl"], a[href*=".xslx"], a[href*=".ppt"], a[href*=".pptx"], a[href*=".txt"]').each(function(){
		var broken_href = $(this).attr('href').split('.');
		var extension = broken_href[broken_href.length-1];
		//.log(extension);
		if($(this).find('img').length < 1){ // Si il ne s'agit pas d'un liens sur imageUntitled Document
			switch(extension){
				
				case 'pdf':
					$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-pdf.png" alt="">').css('line-height','1.2em');
					break
				case 'doc':
				case 'docx':
					$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-word.png" alt="">').css('line-height','1.2em');
					break
				case 'xsl':
				case 'xslx':
					$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-excel.png" alt="">').css('line-height','1.2em');
					break
				case 'ppt':
				case 'pptx':
					$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-powerpoint.png" alt="">').css('line-height','1.2em');
					break
				case 'txt':
					$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-text.png" alt="">').css('line-height','1.2em');
					break
			}
		}
		$(this).attr('target','_blank');
	});
	
	
	$('#section-wrapper a[href*="mailto:"]').each(function(){
		$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-mail.png" alt="">').css('line-height','1.2em').attr('target','_blank');
	});
	
	
	/*$('#section-wrapper a[href*="http://"]').not('a[href*="'+window.location.host+'"]').not('a[href*="youtube.com"]').each(function(){
		$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-externe.png" alt="">').css('line-height','1.2em').attr('target','_blank');
	});*/
	
	$('#section-wrapper a[href*="youtube.com"]').each(function(){
		if($(this).next('img').length < 1){
			$(this).addClass('youtube-text-link').prepend('<img class="inline-icone" src="'+real_path+'img/document-youtube.jpg" alt="">').css('line-height','1.2em');
		}
		//var str = "http://www.youtube.com?v=salut123&test";
		var myregexp = /v=([^&]+)/;
		var cur_href = $(this).attr('href');
		var cur_vid_hash = cur_href.match(myregexp);
		var cur_vid_id = cur_vid_hash[1];
		
		$(this).attr('data-target_div', '#video-div').attr('data-target_subject',cur_vid_id).attr('href','#').addClass('vid-link');
		//$(this).attr('data-target_video', cur_vid_id);
		//.log($(this).dataset());
	});
	
	
	
	//#####################################################################################################
	//##  Affichage des tableau #########################################
	
	$('.editor-wrapper table:not(.no-back)').find("tr:odd td, tr:odd td a .inline-icone-end").css('background','#efefef');
	$('.table-vetement tr').find("td:first, th:first").css('border-left','none');
	
	
	//#####################################################################################################
	//##  Reconstruction de la zone saviez-vous que de l'accueil  #########################################
	if($("#saviez-vous li").size()>0){
		$("#saviez-vous").append('<div id="home-citation-slider" class="clearfix"></div>');
		$("#saviez-vous li").each(function(index){
			var link = "";
			var content = $(this).html();
			if($(this).find("a").size()>0){
				link = $(this).find("a:eq(0)").attr("href");
				content = $(this).find("a:eq(0)").html();
			}
			
			$("#saviez-vous #home-citation-slider").append('<div class="citation-end slide clearfix"><p class="citation">'+content+'</p></div>');
			
			if(link != "") $("#saviez-vous #home-citation-slider .slide:eq("+index+")").append('<a href="'+link+'" class="btn-more-info"><span class="icon"></span>Plus d\'information</a>');
		});
		$("#saviez-vous ul").remove();
		$("#saviez-vous .citation:eq("+Math.floor(Math.random()*$("#saviez-vous .citation").size())+")").parent().show();  
	}
	

	//#####################################################################################################
	//##  Fonction de la recherche  #######################################################################
	$("#search_frm_wrapper #keyword_search").focus(function(){
		$(".search-frm-detail:not(:visible)").fadeIn().dropShadow({left: 0, top: 0, opacity: 0.6, blur: 5, color: "#666666"});
	});	
	$(".search-frm-detail .btn-close").click(function (){
		$(this).parent().fadeOut().removeShadow();
	})
	
	
	//#####################################################################################################
	//##  Roll over du bouton recherche  ##################################################################
	
	$("#search_frm_wrapper a:eq(0)").mouseover(function (){$(this).parent("#search_frm_wrapper").css("background-position","left -33px")});
	$("#search_frm_wrapper a:eq(0)").mouseout(function (){$(this).parent("#search_frm_wrapper").css("background-position","left top")});
	$("#btn_search_submit").click(function(){
		$("#form_search").submit();
	});	
	$("#form_search").submit(function(){
		if(jQuery.trim($("#keyword_search").val()) == $("#keyword_search").attr("title") || jQuery.trim($("#keyword_search").val()) =="" || $("#keyword_search").val().length < 3) return false;
	});	
	
	/*##################################################################*/
	/*##  Gallery de publicites accueil  ###############################*/
	//Fait un random de l'affichage des image du caroussel
	/*
	function shuffle(parent, e) {               // pass the divs to the function
		var replace = $('<div>');
		var size = e.size();
	
		while (size >= 1) {
		   var rand = Math.floor(Math.random() * size);
		   var temp = e.get(rand);      // grab a random div from our set
		   replace.append(temp);        // add the selected div to our new set
		   e = e.not(temp); // remove our selected div from the main set
		   size--;
		}
		parent.html(replace.html() );     // update our container div with the
												 // new, randomized divs
	}
	/*shuffle($('#home-banner-slider'), $('#home-banner-slider .slide') );
	shuffle($('#home-citation-slider'), $('#home-citation-slider .slide') );*/

	
	
	$('#home-banner-slider').wtCarrousel({
	    name: 'c1',
		fadeSpeed: 800,
	    delay: 8500,
	    slidesPrefix: 'slide-a',
		nav: true
	});
	
	
	/*$('#home-citation-slider').wtCarrousel({
	    name: 'c2',
		fadeSpeed: 1500,
	    delay: 10000,
	    slidesPrefix: 'citation-a',
		opaque: false
	});*/
	
	
	//#####################################################################################################
	//##  Ajustement du menu de droite  ####################################################################
	
	
	$("#right-column #right-nav li a:first").addClass("first")
	$("#right-column #right-nav li a:last").addClass("last")
	$("#right-column #right-nav li a").mouseover(function (){$(this).parent("li").addClass("state-over")});
	$("#right-column #right-nav li a").mouseout(function (){$(this).parent("li").removeClass("state-over")});
	
	//#####################################################################################
    //###  Set du lien actif de section  ##################################################

    var current_url = document.location.href;
	if(current_url.indexOf("?") > 0){current_url = current_url.substr(0, current_url.indexOf("?"))}
	if(current_url.indexOf("actualites") > 0){current_url = current_url.substr(0, current_url.indexOf("actualites")+10)}
    var cur_url_split = current_url.split("/");

    function trim(arr2){
        var arr1=new Array();
        for(j=0;j<arr2.length;j++)
        if(arr2[j]!="")
        arr1[arr1.length]=arr2[j];
        arr2.length=arr1.length;
        for(j=0;j<arr1.length;j++)
        arr2[j]=arr1[j];
        return arr2;
    }

    new_array = trim(cur_url_split);

    var last_param = new_array.length - 1;
    var needle = new_array[last_param];

    $("#right-nav a, #nav-main a").each(function(){
        var cur_href = $(this).attr('href');
        var cur_href_split = cur_href.split("/");
        var new_href_array = trim(cur_href_split);
        var last_href_param = new_href_array.length - 1;
        var needle_href = new_href_array[last_href_param];
		
        if(needle_href === needle){
            $(this).parent().addClass('active');
        }
    });
	
	$("#nav-main li.active").parentsUntil("li").parent().addClass("active");

	

	
	
	
	//#####################################################################################################
	//##  Fonction de formulaire  ###############@@@@@#####################################################
	
	$(".textfield").focusin(function(){
		if($(this).val() == $(this).attr('title'))
		{
			$(this).val('');
		}
	});
	
	$(".textfield").focusout(function(){
		if($(this).val() == '')
		{
			$(this).val($(this).attr('title'));
		}
	});
	
	
	//#####################################################################################################
	//##  Affichage de la FAQ  ############################################################################
	
	//$( ".faq > ol" ).accordion({ header: 'li > a', autoHeight: false });
	
	$(".faq > ol > li > strong").wrapInner("<a>").click(function(e){
		var close_only = false;
		if($(this).next(".faq > ol > li > ol, .faq > ol > li > ul").is(":visible")) close_only=true;
		
		$(".faq > ol > li > strong").next(".faq > ol > li > ol, .faq > ol > li > ul").filter(":visible").slideToggle("fast");
		
		if(!close_only)	$(this).next(".faq > ol > li > ol, .faq > ol > li > ul").slideToggle("slow");
		
	});
	
	
	
	//#####################################################################################################
	//##  Ajustement du text-shadow  ####################################################################
	if ( !$.browser.msie ) $("#content-title h1").dropShadow({left: 0, top: 0, opacity: 0.8, blur: 1, color: "#666666"});
	$("#nav-main > ul > li:not(li.active) > a").dropShadow({left: 0, top: 0, opacity: .8, blur: 1,color: "#ffffff"});
	// 

	

	$('.map-canvas').each(function(index, e){
		var value = $(this).html().split("|");
		$(this).html("");
					
	
		var latlng = new google.maps.LatLng(value[1], value[2]);
		var myOptions = {
			zoom: 12,
			center: latlng,
			scrollwheel: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(e, myOptions);
		var marker = new google.maps.Marker({
			position: latlng,
			title: value[3]
		});
		marker.setMap(map);
	});
	
});


