var ltie7 = ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent));
$(function(){
	// Fix png
	$('img[src$=.png]').ifixpng();
	$('input[type=image][src$=.png]').ifixpng();
	$('table').each(function(){
		$(this).find('tr:odd').addClass('pair');
	})
	
	if (ltie7) {
		$('img[@usemap]').css('border','none');
		
	}
	// Gestion du formulaire inscription : on cache l'identifiant et on le rempli avec l'email
	
	menu.init();
	glossaire.init();
	myprofil.init();
	faq.init();
	searchAAP.init();
	gmap.init();
	userAccount.init();
	$(".element_sitemap > li").adjustSize({periode: 3});
	
	$(".download").click(	function () 
		{
			//alert($(this).attr('href'));
			
			var type 	=	$(this).attr('href');
			var test	=	type.indexOf("http://");
			if (test != -1)
				type	=	"S";
			else
				type	=	"T";
			
			//alert(type);
			return xt_click(this,'C','',$(this).attr('href'),type);
		});
});

var userAccount = function() {
	function init() {
		var CheckedForm=$("#form_inscri, #userEditForm");
		if (CheckedForm.size() != 0) {
			$("#user_identifiant input:first, #user_identifiant label:first", CheckedForm).parent().hide();
			CheckedForm.submit(function() {
				var UserNameValue        = $("#user_identifiant input:first",    	CheckedForm).val();
				var UserMailValue        = $("#user_email input:first",         	CheckedForm).val();	
				if (UserNameValue=='') $("#user_identifiant input:first", CheckedForm).val(UserMailValue);
			});
			return true;
		}
		return false;
	}
	return {init:init}
}();

var login = function() {
	function init() {
		$(".blocHome #courriel_id").each(function() {
			$(this).val('Courriel');
		}).click(function() {
			if($(this).val() == 'Courriel')$(this).val('');
		}).blur(function() {
			if($(this).val() == '')$(this).val('Courriel');
		});
		$(".blocHome #passwd_id").each(function() {
			$(this).val('Mot de passe');
		}).click(function() {
			if($(this).val() == 'Mot de passe')$(this).val('');
		}).blur(function() {
			if($(this).val() == '')$(this).val('Mot de passe');
		});	
		return false;
	}
	return {init:init}
}();

var searchAAP = function() {
	function init() {
		$("#searchAAP_formID").submit(function() {
			var eZRek=$(this).attr('action');
			eZRek+=($("#searchAAP_formID select[@name='etats']").val()!="")		?("/(etat)/" + $("#searchAAP_formID select[@name='etats']").val()):"";
			eZRek+=($("#searchAAP_formID select[@name='themes']").val()!="")	?("/(theme)/"+ $("#searchAAP_formID select[@name='themes']").val()):"";
			eZRek+=($("#searchAAP_formID select[@name='annees']").val()!="")	?("/(annee)/"+ $("#searchAAP_formID select[@name='annees']").val()):"";
			location.href=eZRek;
			return false;
		});
	}
	return {init:init}
}();

var glossaire = function() {
	function init() {
		$('a.glossary').each(function(){
			$(this).after('<div class="tooltip">'+$(this).attr('title')+'</div>');
			$(this).attr('title','');
			var tooltip=$(this).next('.tooltip');
			tooltip.parent().css('position','relative');
			var offsetA=$(this).offset();
			var offsetP=tooltip.parent().offset();
			$(this).mouseover(function(){
				tooltip.css({top:parseInt(offsetA.top-offsetP.top+20)+"px",left:parseInt(offsetA.left-offsetP.left+20)+"px"}).addClass('tooltipVisible');
			}).mouseout(function(){
				$(this).next('.tooltip').removeClass('tooltipVisible').css({top:'',left:''});
			});
		});
	}
	return {init:init}
}();

var menu = function(){
	var menuList, menuItem, menuSsList, links
	function init() {
		
		menuList=$('#menuList');
		menuItem=$('.menuItem', menuList);
		menuSsList=$('.menuSsList', menuItem);
		links=$('a', menuList);
		
		menuItem.hover(function(){showSsMenu($(this))},function(){hideSsMenu($(this))});
		links.focus(function(){showSsMenu($(this))}).blur(function(){hideSsMenu($(this))});
	}
	function showSsMenu(jqElmt){
		jqElmt=getMenuItem(jqElmt)
		jqElmt.addClass('menuSsListDisplayed');
	};
	function hideSsMenu(jqElmt){
		jqElmt=getMenuItem(jqElmt)
		jqElmt.removeClass('menuSsListDisplayed');
	};
	function getMenuItem(jqElmt) {
		return jqElmt.is('.menuItem') ? jqElmt : jqElmt.parents('.menuItem:first');
	}
	return {init:init}
}();

var faq=function(){
	function _init() {
		$('#faqListe h3 a').click(function(){
			var text=$(this).parents('.faqItem').find('.faqItemText');
			if (text.is(':visible')) text.hide();
			else text.show();
			return false;
		});
	}
	return {init:_init}
}();


var myprofil=function() {
	function _init() {
		if ($('#encartLogin').size()>0)
			$('#encartLogin').load('/layout/set/encart_myprofil/content/view/full/'+$('#encartLoginForm').attr('rel'),function(){
				$('#preLoadEncartLoginForm').remove();
				login.init();
			});
	}
	return {init:_init}
}();


var gmap=function(){
	function _init() {
		$('body .gmap').css('display', 'none');
		$('.showMap a').click(function(){
			var map=$(this).parent().find('.gmap');
			if (map.is(':visible')) map.hide();
			else map.show();
			return false;
		});
	}
	return {init:_init}
}();

function adjustSelects() {
	if($('.adjustSelect').size() > 0 && $.browser.msie) {
		$('.adjustSelect').each(function(){
			$(this).css('width', '');
			$(this).css('position','absolute');
			$(this).css('left','0');
			$(this).css('z-index','100');
			var passe=0;
		}).mouseover(function(){
				$(this).css('width','auto');
				$(this).css('z-index','1000');
			passe=0;
		}).click(function(){
			if (passe == 1) {
				$(this).css('width', 'auto');
				$(this).css('z-index','1000');
				passe = 0;
			}
			passe=1;
		}).blur(function(){
			$(this).css('width', '');
			$(this).css('z-index','100');
			passe=0;

		}).mouseout(function(){
			if (passe==0){
				$(this).css('width', '');
				$(this).css('z-index','100');
			}
		});
	}
}

$.fn.adjustSize=function(options) {
	var settings = {
		periode: 2,
		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;
}
