$(document).ready(function() {
	
	$('body').addClass('js');
	
	// gestion du menu déroulant
	$('#mainMenu>ul>li>ul').each(function(){
		$(this).css('margin-bottom', '-1000em');
	});
	
	$('#mainMenu>ul>li').mouseover(function(){
		$(this).addClass('MenuOpened');
		handleMenuMask($('>ul',this));
	}).mouseout(function(){
		$(this).removeClass('MenuOpened');
		handleMenuMask();
	});
	$('a').focus(function(){
		$('#mainMenu>ul>li').removeClass('MenuOpened');
		handleMenuMask();
		if($(this).closest('#mainMenu').length == 1){
			$(this).closest('li[id]').addClass('MenuOpened');
			handleMenuMask($('>ul',this));
		};
	});
	
	
	// gestion du mask pour IE6
	var menuMask = $('#mainMenuMask');
	var handleMenuMask = function(ul){
		if (ul == undefined || ul.length == 0){
			$(menuMask).css('display', 'none');
		} else {
			var p = $(ul).offset();
			$(menuMask).css('left', p.left+'px')
					   .css('top', p.top+'px')
					   .css('display', 'block')
					   .css('width', $(ul).width()+'px')
					   .css('height', $(ul).height()+'px')
			
		}
	}
	
	// listes zébrées
	var cptZebra;
	$('#listesMembres ul,#listeDocuments').find('li').each(function(i, item){
		if (i==0){
			cptZebra = 0;
		}
		if (cptZebra%2 == 1){
			$(item).addClass('odd');
		}
		cptZebra++;
	});
	
	
	$("#formInscriptionReunion").hide();
	$("#formCommandePublication").hide();
	
	$('.showForm').click(function(){
		var href = $(this).attr('href');
		var iSharp = href.lastIndexOf('#');
		var id = href.substring(iSharp+1, href.length - iSharp);
		
		$('#'+id).slideDown('slow');
		return true;
	});
	
	// bouton d'impression
	$('#printPage').each(function(){
		$(this).attr('title', libelle['printPage']);
		$(this).click(function(){
			window.print();
		});
	});
	
	// bouton d'agrandissement du texte
	$('#growText').each(function(){
		$(this).attr('title', libelle['growText']);
		$(this).click(window.growText)
	});

	// bouton de réduction du texte
	$('#shrinkText').each(function(){
		$(this).attr('title', libelle['shrinkText']);
		$(this).click(window.shrinkText)
	});
	
	// taille du texte à l'affichage de la page
	window.manageTextSize();
	
	// gestion des formulaires de tri
	$('.orderingForm form').each(function(){
		var form = this;
		$('select', this).change(function(){form.submit()});
		$('input[type=submit]', this).remove();
	});
	
});


var growText = function(){manageTextSize('+')}
var shrinkText = function(){manageTextSize('-')}

var manageTextSize = function(size){
	var sizes = [];
	sizes['-2'] = '.5';
	sizes['-1'] = '.75';
	sizes['0'] = '1';
	sizes['1'] = '1.25';
	sizes['2'] = '1.5';
	var actualSize = $.cookie('textSize')?parseInt($.cookie('textSize')):0;
	var newSize = actualSize;
	if (size){
		switch(size){
			case '+': newSize++;
				break;
			case '-': newSize--;
				break;
		}
		if (sizes[newSize] != undefined){
			$.cookie('textSize', newSize, {path: '/'});
		} else {
			newSize = actualSize;
		}
	}
	$('#page').css('font-size', sizes[newSize]+'em');
}
