/**
* Fichier javascript global
*/


// js du menu principal
jQuery().ready( function($) {
	
	/* http://www.getfirebug.com/firebug/firebugx.js // http://www.getfirebug.com/lite.html
	* Firebug lite pour éviter des erreurs sous ie et autres navigateurs si l'extension n'est pas activée */	
	if (!window.console || !console.firebug) {
		var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
		"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];	
		window.console = {};
		for (var i = 0; i < names.length; ++i)
			window.console[names[i]] = function() {}
	}
	
	//
	// js de l'input rechercher
	//
	$("#form_searchbox input[@type=text]").focus( function() {
		$(this).val('');
	});
	var inputdefautval = $("#form_searchbox input[@type=text]").val();
	$("#form_searchbox").submit( function() {
		var inputval = $("#form_searchbox input[@type=text]").val();
		if(inputval && inputval != inputdefautval) return true;
		else {
			$("#form_searchbox input[@type=text]").focus();
			return false;
		}
	});

	//
	// js de la nav du haut
	//
	$.fn.menuTop_init = function() {
		$("#menutop a").each( function() {
			var action = $(this).attr('rel');
			if(action) {
				var theid = '#'+action;
				$(this).removeClass('bton_1_actif');
				$(theid).hide();
				if($.browser.msie) 	$("#searchbox").show();
				$(this).trigger('toggle');
			}
		});
	};
	
	$("#menutop a").each( function() {
		var action = $(this).attr('rel');
		if(action) {
			var theid = '#'+action;
			// repositionnement du calque (sous le lien)
			var offset	= $(this).offset();
			$(theid).css('top',offset.top+$(this).height());
			$(theid).css('left',offset.left-5);
			var zindex	= $(theid).css('zIndex');
			$(this).toggle( function() {
				$.fn.menuTop_init();
				$(theid).css('zIndex',10000);
				$(this).addClass('bton_1_actif');
				//$(theid).show();
				$(theid).slideDown();
				if($.browser.msie) 	$("#searchbox").hide();
				else 				$("#searchbox").fadeOut();
			}, function() {
				$(theid).css('Zindex',zindex);
				$(this).removeClass('bton_1_actif');
				$(theid).slideUp();
				if($.browser.msie) 	$("#searchbox").show();
				else 				$("#searchbox").fadeIn();
			});
		}
	});


	//
	// js du hovertip : bulle d'information
	//
	$(".hovertip").each( function() {
		var rel = $(this).attr('rel');
		if($(this).attr('title') && rel == undefined) { // créé le div conteneur
			rel = 'hovertip'+Math.ceil(Math.random()*1000);
			$(this).attr('rel',rel);
			$(this).after('<div id="'+rel+'" class="hovertip_content"><div class="content">'+$(this).attr('title')+'<\/div><\/div>');
			$(this).attr('title','');
		}
	});
	$(".hovertip").hover(
		function() {
			var rel = $(this).attr('rel');
			var mondiv = $("#"+rel+"");
			var offset = $(this).offset();
			$(this).css('cursor','help');
			$(mondiv).css('position','absolute');
			$(mondiv).css('top',offset.top);					
			$(mondiv).css('left',offset.left);
			var height = $(this).innerHeight();
			$(mondiv).css('margin-top',height);
			$(mondiv).css('margin-left','-10px');
			$(mondiv).show();
		}
		,
		function() {
			var rel = $(this).attr('rel');
			var mondiv = $("#"+rel+"");
			$(mondiv).hide();
		}							  
	);
	
	
	$(".hovertip").click(function() {
		var href = $(this).attr('href');
		if(href == "#") return false;
	});


	//
	// js sur des liens spécifiques : popups, messages etc...
	//
	$("a.popup").click( function() {
		var href = $(this).attr('href');
		if(href) {
			var largeur = 550;
			var hauteur	= 400;
			var win3=window.open(href,"","width="+largeur+"px,height="+hauteur+"px,resizable=yes,scrollbars=no");
			win3.moveTo($(window).width()/2-(largeur/2),$(window).height()/2-(hauteur/2));
			win3.focus();	
		}
		return false;
	});

	//
	// js sur des liens spécifiques : popups, messages etc...
	//
	$("a[@target=_blank]").each( function() {
		console.log($(this).attr('href'));
		var href = $(this).attr('href');
		$(this).click( function() {
			window.open(href);		
			return false;						
		});
		$(this).removeAttr('target');
	});


	//
	// décypte les emails
	//
	$(".email").each( function() {
		if(meil = $(this).attr('href')) { // déjà un lien
			var reg=new RegExp("!pt!", "g");
			meil = meil.replace(reg,".");
			var reg=new RegExp("!arb!", "g");
			meil = meil.replace(reg,"@");
			$(this).attr('href',meil);
		}
		if(meil = $(this).text()) {
			var reg=new RegExp("!pt!", "g");
			meil = meil.replace(reg,".");
			var reg=new RegExp("!arb!", "g");
			meil = meil.replace(reg,"@");
			$(this).text(meil);
		}
	});
	
});