/* Png Rollovers (except IE6, look into correctpng.js) */
function initRollOvers() {
  imgsToRoll=$("img.imgSwap");
  if ($(imgsToRoll).size()<1){return;}
  $(imgsToRoll).each(function(){$(this).mouseover(function(){imgSrc=$(this).attr("src");$(this).attr("src",imgSrc.replace("_off","_on"));}); $(this).mouseout(function(){imgSrc=$(this).attr("src");$(this).attr("src",imgSrc.replace("_on","_off"));});});
}

/* fonction pour 1. mettre le label dans le input, 2. vider le champs de recherche au focus 
et 3. le remplir au blur si laissé vide */
jQuery.fn.labelOver = function(overClass) {
	return this.each(function(){
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			
			this.hide = function() {
			  label.css({ textIndent: -10000 })
			}
			
			this.show = function() {
			  if (input.val() == '') label.css({ textIndent: 0 })
			}

			// handlers
			input.focus(this.hide);
			input.blur(this.show);
		   label.addClass(overClass).click(function(){ input.focus() });
			
			if (input.val() != '') this.hide(); 
		}
	})
}

// remplacement des input type img par un png transparent
function initSubmitLinks() {
  submiter = $(".inputImg");
  for (var j = 0; j <$(submiter).size(); j++) {
  	// récup src et remplacement nouvelle image (png)
    var submitImg = $(submiter[j]).attr("src");
	 var newImg = submitImg.toString().replace("gif","png");
	 // récup de l'id du formulaire
    var formId = $(submiter[j]).parents("form").attr("id");
	 var newIdImg = formId+"_"+(Math.round(Math.random()*10000));
	 htmlCode="<span class=\"imgBt\"><img src=\"" + newImg + "\" alt=\"" + $(submiter[j]).attr("alt") + "\" id=\"" + newIdImg + "\" title=\"Cliquer pour soummettre le formulaire\" /></span>";
    $(submiter[j]).parent().append(htmlCode);
    if (!$.browser.safari) { $(submiter[j]).parent().find("img").attr({"width":$("#"+newIdImg).width(),"height":$("#"+newIdImg).height()}); }
	 $(submiter[j]).parent().find("img").attr("class",$(submiter[j]).attr("class"));
	 $(submiter[j]).parent().find("span.imgBt").click(function(){
		var imgId = $(this).children().attr("id");
		var formIdArray = imgId.toString().split("_");
		formId = formIdArray[0].split("#");
	 	submitForm("#"+formId[formId.length-1]);
		return false;
	 });
     $(submiter[j]).remove();
  }
}

function submitForm(imgId) {
	//alert(imgId);
	$(imgId).get(0).submit();
};

// remplacement des input type img par un png transparent, avec validation du form (à finir de mettre en place)
function initSubmitLinksCheckForm() {
  submiter = $(".inputImg");
  for (var j = 0; j <$(submiter).size(); j++) {
  	// récup src et remplacement nouvelle image (png)
    var submitImg = $(submiter[j]).attr("src");
	 var newImg = submitImg.toString().replace("gif","png");
	 // récup de l'id du formulaire
    var formId = $(submiter[j]).parents("form").attr("id");
	 var newIdImg = "btImg"+(Math.round(Math.random()*10000));
	 htmlCode="<a href=\"#" + formId + "\"><img src=\"" + newImg + "\" alt=\"" + $(submiter[j]).attr("alt") + "\" id=\"" + newIdImg + "\" title=\"Cliquer pour soummettre le formulaire\" /></a>";
    $(submiter[j]).parent().append(htmlCode);
    if (!$.browser.safari) { $(submiter[j]).parent().find("img").attr({"width":$("#"+newIdImg).width(),"height":$("#"+newIdImg).height()}); }
	 $(submiter[j]).parent().find("img").attr("class",$(submiter[j]).attr("class"));
	 $(submiter[j]).parent().find("a").click(function(){
	 	submitForm($(this).attr("href"));
		return false;
	 });
   if (!$.browser.safari) { $(submiter[j]).remove(); }
  }
}

	// fonctions au load
	$(document).ready(function(){
		
		// appel de la fonction jQuery.fn.labelOver
		$('#search label').labelOver('over-apply');
		$('#searchFooter label').labelOver('over-apply');
		
		// vidage/remplissage des input avec déjà une valeur au load
		$("input.preRempli").toggleVal();
		
		// dropdown menus
		$("#menu").superfish({
			hoverClass	: "on",
			animation : { opacity:"show" }
		});
		
		// ajustement des blocs du footer tous à la même hauteur
		var heightBlockMax=0;
		$(".firstLine").each(function(){ if( $(this).height() > heightBlockMax ) heightBlockMax = $(this).height(); }); // get max height
		$(".firstLine").each(function(){ $(this).css("height",heightBlockMax); }); // assign max height
		heightBlockMax=0;
		
		// ajustement des blocs du footer tous à la même hauteur
		var heightBlockMax=0;
		$(".secondLine").each(function(){ if( $(this).height() > heightBlockMax ) heightBlockMax = $(this).height(); }); // get max height
		$(".secondLine").each(function(){ $(this).css("height",heightBlockMax); }); // assign max height
		heightBlockMax=0;
		
		// boites listing avec bords ombrés
		$("#header").wrap('<div id="headerContainerExt"><div id="headerContainer"></div></div>');
		$("#page").wrap('<div id="pageContainerExt"><div id="pageContainer"></div></div>');
		$("#footer").wrap('<div id="footerContainerExt"><div id="footerContainer"></div></div>');
		$("#agendas").wrap('<div id="agendasContainerExt"><div id="agendasContainer"></div></div>');
		
		// remplacement des boutons de form en gif par boutons en png
		initSubmitLinks();
		initRollOvers();
		
		// boites "chercher personnalites" et "annuaire" : fermeture au load, puis ouverture au click
		$("#peopleSearch span").toggleClass("opened");
		$("#directorySearch span").toggleClass("opened");
		$("#formPeopleSearch").hide();
		$("#formDirectorySearch").hide();
		$("#peopleSearch").click(function(){
			$("#formPeopleSearch").toggle();
			$("#peopleSearch span").toggleClass("opened");
		});
		$("#directorySearch").click(function(){
			$("#formDirectorySearch").toggle();
			$("#directorySearch span").toggleClass("opened");
		});
		
	});
