/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|		
|		Copyright (c) 2010 ANR
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|		
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* ______________________[ 02 | Champs focus sur input recherche ]________________________ */

(function($) {
    $.fn.toggleFocus = function() {
        return this.each( function() {
            var input =  $(this);
			if( input.length > 0 ) { 
				var id_input = input.attr("id");
				var form = input.parents("form");
				var label = form.find("label").attr("for",id_input);				
				var old_val = label.text() || "";
				label.css("display","none");
				input.val(old_val);
				
				var news_val = "";
				input.focus(function() {  
					news_val = input.val() ;
					if ( old_val == news_val ) {
						input.val("");
					}
				});
				input.blur(function() {
					if (input.val() != "" ){
						news_val = input.val();
					} else {
						news_val = old_val ;
					}
				   input.val(news_val);
				});
			}
        });
    };              
})(jQuery);

/* ______________________[ 02 | Interactivité du menu principal (menu horizontal) ]________________________ */
/* A special thanks goes to Eric Shepherd for his ALA article about “Hybrid CSS Dropdowns”: http://www.alistapart.com/articles/hybrid/ 
and to Patrick Griffiths and Dan Webb for their htmldog.com article “Sons of Suckerfish”: http://www.htmldog.com/articles/suckerfish/ */

function SmartHover(ele) {
	if(typeof ele != "string") {
		return;
	}	
	var navRoot = $("#"+ele);
	if ( navRoot.length >0 ) {
		var kids = navRoot.find("li:has('ul')");
		var uls = navRoot.find("li ul");
		var kidsa = navRoot.find("li a");		
		kids.each(function(){
			var li = $(this);
			var afocus = li.find("a:first");
			var ul = li.find("ul");
			li.mouseenter(function(){								
				uls.hide(10);				
				kids.removeClass("over");
				ul.stop(true, true).slideDown(300);
				li.addClass("over");
				return false;
			}).mouseleave(function(){
				kids.removeClass("over");
				ul.hide(10);
				return false;
			});					
			afocus.focus(function(){
				li.trigger("mouseenter");
				return false;
			});			
		});
		
		var kidsNoUL = navRoot.find(" > li").not(':has("ul")');
		kidsNoUL.find("a").focus(function(){
			kids.removeClass("over");							  
			uls.hide(10);
			return false;
		});			
		
	}
}



/* ______________________[ 03 | Gestion de la taille du texte d’un article ]________________________ */
/* ********************************* T+ T- */
function SmartSize(args) {
	var cadre = $("#"+args);	
	if( cadre.length > 0 ) {
	/*
		var ftz = cadre.css("fontSize");
		var ftzNum = parseFloat(ftz.substring(0,ftz.length-2));
	*/
		var ftzNum = 12;
		$("#Tplus").click(function(){
			ftzNum = ftzNum+1;			
			cadre.css("fontSize", ftzNum+"px");		
		 });		
		$("#Tmoins").click(function(){		
			ftzNum = ftzNum-1;		
			cadre.css("fontSize", ftzNum+"px");			
		});	
	}	
}

/* ______________________[ 04 | Lancement d’une impression pour les navigateurs compatibles ]________________________ */
function DirectPrint() {
	if (window.print) self.print();
}

/* ______________________[ 05 | Miscellaneous ]________________________ */
function OpenPopup(url,nom,option) {
	window.open(url,nom,option);
}

/* ______________________[ 06 | Ajout de la page courante aux favoris ]________________________ */
function bookmarksite() {
	var title = document.title;
	var url = document.location.href;
	if (window.sidebar) { // firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all) {// ie
		window.external.AddFavorite(url, title);
	}
}

/* ______________________[ 07 | toolBox ]________________________ */
function AddToolButton(targets) {	
	if( typeof targets == "string" ) {
		return;
	}	
	var toolbox = $("#"+targets.idWrap);
	var linkId = targets.linkId || "";
	var imgSrc = targets.imgSrc || "";
	var imgAlt = targets.imgAlt || "";	
	var linkHref = targets.linkHref || "";
	var ele = targets.insertBefore || "";

	if(!linkHref) {
		linkHref = "javascript:;";
	}
	if( toolbox.length > 0 ) {
		var ul = toolbox.find("ul");
		if( ul.length == 0 ) {			
			toolbox.html("<ul></ul>"); 
			ul = toolbox.find("ul");
		}
		var img = '';
		if( imgSrc !="" ) {
			img = '<img src="'+imgSrc+'" alt="'+imgAlt+'"/>';
		}
		var li = '<li><a href="'+linkHref+'" id="'+linkId+'">'+img+'</a></li>';
		
		if( ele !="" && $("#"+ele).length > 0) {	
			var li_frere = $("#"+ele).parents("li:first");
			$(li).insertBefore(li_frere);
		}else {	
			$(li).appendTo(ul);
		}
	}	
}

function setroll(toolbox){
	/*Roll over*/
	if( typeof toolbox != "string" ) {
		return;
	}	
	var ul = $("#"+toolbox).find("ul");
	ul.find("li a").hover(
		function(){			
			roll($(this).find("img"));
		},
		function(){
			roll($(this).find("img"));
		}
	);
}


/** *********************************  [ roll over] */
function roll(o) {
	var src,ftype,newsrc;
	src=o.attr("src");
	ftype=src.substring(src.lastIndexOf('.'), src.length);
	if(/_over/.test(src)) {
		newsrc=src.replace('_over','');
	} else {
		newsrc=src.replace(ftype, '_over'+ftype);
	}
	o.attr("src",newsrc);
}	

/* ______________________[ 03 | equalizeCols ]________________________ */
(function($) {
	/**
	 * equalizes the heights of all elements in a jQuery collection
	 * thanks to John Resig for optimizing this!
	 * usage: $("#col1, #col2, #col3").equalizeCols();
	 */	 
	$.fn.equalizeCols = function(){
		var height = 0;	  
		function normalize(el) {
			return (parseInt(el.css("paddingTop"), 10) || 0)
				+ (parseInt(el.css("paddingBottom"), 10) || 0)
				+ (parseInt(el.css("borderTopWidth"), 10) || 0)
				+ (parseInt(el.css("borderBottomWidth"), 10) || 0);
  		};  
		return this
			.css("height", "auto")
			.each(function() {
				height = Math.max(height, $(this).outerHeight());
			})
			.css("height", function() {
				return height - normalize($(this));
			});
	};	
})(jQuery);


var setEquHeight = function(){

	if( $('#wrap-bloc .contenu').length > 0 ) {
		$("#wrap-bloc .bloc").equalizeCols();
	}
	
	if( $('#telechargement').length > 0 ) {
		$("#telechargement .row").equalizeCols();
	}

};

(function($){	
	$.fn.openclose = function(options) {
		var defaults = {
			labels : new Array("Lire la suite","Masquer la suite")
		};
		var options = $.extend(defaults, options);
		return this.each(function() {
			var global = $(this);
			var labels = options.labels;
			var globalcontent = $(this).find(".inner:first");
			var globaltitle = $(this).find("h2, h3, .eqH3 p:not(.date)");
			global.addClass("close").find(".inner:first").hide();
			globaltitle.wrapInner('<span class="togglebut"><a href="javascript:;"></a></span>');
			globaltitle.find("a").prepend('<span class="label"><span>… '+labels[0]+'</span></span>');
			global.find("h3").click(function() {
				var toggle=$(this);
				globalcontent.slideToggle("fast", function() {
					global.toggleClass("close");
					toggleLabel=toggle.find(".label");
					toggleLabel.html()=='<span class="label">… '+labels[1]+'</span>'?toggleLabel.html('<span class="label">… '+labels[0]+'</span>'):toggleLabel.html('<span class="label">… '+labels[1]+'</span>');
				});
			});
		});
	};	
})(jQuery);


var fnChapoOC = function(){
	if( $('div.chapo a.btn_oc').length > 0 ) {
		$('div.chapo a.btn_oc').click(function(){
			var a = $(this);
			var chapo = a.parents('.chapo');
			var inner = chapo.find('.inner-oc');
			if(inner.length > 0) {
				if( inner.is(":visible") ) {
					inner.slideUp(300,function(){
						a.addClass('close');
					});
				}else {
					inner.slideDown(300,function(){
						a.removeClass('close');
					});
				}
			}
		});
	}
};

var fnVersDetail = function(){
	if( $("div.bloc-visu .vers-detail").length > 0 ) {
		$("div.bloc-visu .vers-detail").each(function(){
			var p = $(this);
			if($("div.bloc-visu p.titre").length > 0 ) {
        var cible = p.parent().find('p.titre a').attr('href') || "javascript:;";
  			p.click(function(){
  				window.location.href = cible;
  				return false;
  			});
      }			
		});
	}
};

/* ______________Lancement script______________________*/

jQuery(document).ready(function($){
    
    /******** Miary **********/
	
	if( $("div.bloc-visu .inner2 div").length > 0 ) {
	    
		$("div.bloc-visu .inner2>div").addClass('vers-detail');
		
	}
	
    /******** Miary **********/
	
	/*  ___ [ togglefocus ] __*/	
	if( $("#mot_cle").length > 0 ) {
		 $("#mot_cle").toggleFocus();
	}
	
	/*  ___ [ toolBox ] __*/	
	if( $("#toolBox").length > 0 ) {
		//AddToolButton({idWrap:"toolBox",linkId:"partager",linkHref:"javascript:;",imgSrc:"",imgAlt:"partager"});				
		//AddToolButton({idWrap:"toolBox",linkId:"ami",imgSrc:"",imgAlt:"envoyer à un ami"});		
		AddToolButton({idWrap:"toolBox",linkId:"print",linkHref:"javascript:DirectPrint();",imgSrc:"",imgAlt:"imprimer la page"});
		AddToolButton({idWrap:"toolBox",linkId:"Tmoins",imgSrc:"",imgAlt:"petit text"});
		AddToolButton({idWrap:"toolBox",linkId:"Tplus",imgSrc:"",imgAlt:"grand text"});		
				
		//setroll("toolBox");
		SmartSize("ColContenu");
	}

	if($("#NavigationPrincipale").length > 0) {
		SmartHover("NavigationPrincipale");
	}
	
	setEquHeight();
  if( $(".openclose").length > 0) {	
		//si autre langage en anglais : passer en parametre une table pour labels			
		if( $("html").attr("lang") == "en" ) {
			var tab = new Array("Open","Close","Display all","Hide all");
			$(".openclose").openclose({labels : tab});			
		}else {
			//par defaut : les labels en francais
			$(".openclose").openclose();
      if($("#ColContenu .openclose").hasClass(".open")){
				$(".openclose.open .togglebut:first a").click();
			}
			else {
				$(".openclose .togglebut:first a").click();
			}
		}			
	}
  //toggleOC();
	
	if( $(".tablesort").length > 0 ) {
    $(".tablesort thead th").each(function(count){$(this).addClass("cell"+count).html("<span>"+$(this).html()+"</span>")});
    $("table.tablesort").tablesorter();
	}
	
	fnChapoOC();
	
	fnVersDetail();
	
});
;