var ltie7 = ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent));

$(function(){
	
	$('img[@src$=.png]').pngfix();
	
	handleMenu.init();
	handleListMembres.init();
	
	if (ltie7) {
		$('img[@usemap]').css('border','none');
		$('#menu ul li:first-child').css('background', 'none');
		$('#encart h2:first-child, #encart h3:first-child, #encart h4:first-child, #encart h5:first-child, #encart h6:first-child, #encart p:first-child, #encart pre:first-child, #encart blockquote:first-child, #encart ul:first-child, #encart ol:first-child, #encart dl:first-child, #encart address:first-child, #encart table:first-child').css('margin-top','0');
		$('#temoignage .fonction + p').css('margin-top','0.8em');
		$('.infosGroupe + p').css('margin-top','0');
		$('.listeElmtsThematique .elmtsThematique:first-child').css('background','none');
	}
});




var handleMenu = function(){
	var menu, li ,ul;
	function init(){
		menu=$('#menu');
		li=$('#menu>li');
		sousMenu=$('#menu>li>.sousMenu');
		a=$('#menu>li>.sousMenu a');
		
		hide(sousMenu);
		li.hover(function(){
			var jQelmt=$(this)
			show(jQelmt.find('>.sousMenu'));
			hide(jQelmt.siblings().find('>.sousMenu'));
			jQelmt.find('span').css({backgroundPosition:'-116px 0', padding: '11px 10px 7px'});
		},function(){
			hide(sousMenu);
			$(this).find('span').css({backgroundPosition:'0 0', padding: '18px 10px 0'});
		});
		menu.bind("blur", handleEvents).bind("focus", handleEvents);
		if ($.browser.msie) a.bind("blur", handleEvents).bind("focus", handleEvents);
	}
	function show(jQelmt){
		if (jQelmt) jQelmt.css({top: '34px'});
	}
	function hide(jQelmt){
		if (jQelmt) jQelmt.css({top: '-5000px'});
	}
	function handleEvents(e){
		var target=$(e.target);
		var type=e.type;
		if (e.type=='blur') hide(sousMenu);
		if (e.type=='focus') {
			if(target.parents('ul:first').is('#menu')) show($(target).siblings('.sousMenu'));
			else show($(target).parents('.sousMenu'));
		}
	}
	return {init:init};
}();

var handleListMembres = function(){
	function init(){
		$('#listeMembres .membres').each(function(index){
			var jQe=$(this);
			if (index % 3 == 0) jQe.css({marginLeft : '0', marginRight : '23px', clear: 'left'});
			else if (index % 3 == 1) jQe.css({marginLeft : '22px', marginRight: '22px'});
			else if (index % 3 == 2) jQe.css({marginLeft : '23px', marginRight: '0'});
		}).find('h2').adjustSize().end().adjustSize();
	}
	return {init:init};
}();

$.fn.adjustSize=function(options) {
	var settings = {
		periode: 3,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	if (options) {
		jQuery.extend(settings, options);
	}
	var totalSize = $(this).size();
	var nbPeriodes = Math.ceil(totalSize/settings.periode);
	var index = 0;
	if (settings.mode == 'height' || settings.mode == 'both') {
		var maxTotalHeightArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalHeightArray[blockId]) maxTotalHeightArray[blockId]=0;
			var blockTotalHeight = $(this).height();
			if (blockTotalHeight > maxTotalHeightArray[blockId]) {
				if (settings.minHeight && settings.minHeight > blockTotalHeight) blockTotalHeight = settings.minHeight;
				maxTotalHeightArray[blockId] = blockTotalHeight;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedHeight = maxTotalHeightArray[blockId];
			if (ltie7) $(this).height(blockComputedHeight+'px');
			else $(this).css('minHeight',blockComputedHeight+'px');
			index++;
		});
	}
	if (settings.mode == 'width' || settings.mode == 'both') {
		var maxTotalWidthArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalWidthArray[blockId]) maxTotalWidthArray[blockId]=0;
			var blockTotalWidth = $(this).width() ? $(this).width() : $(this).attr('offsetWidth') ;
			if (blockTotalWidth > maxTotalWidthArray[blockId]) {
				if (settings.minWidth && settings.minWidth > blockTotalHeight) blockTotalWidth = settings.minWidth;
				maxTotalWidthArray[blockId] = blockTotalWidth;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedWidth = maxTotalWidthArray[blockId];
			if (ltie7) $(this).width(blockComputedWidth+'px');
			else $(this).css('minWidth',blockComputedWidth+'px');
			index++;
		});
	}
	
	return this;
}