/*
	JS pour SRQ - Principal
	Simon - egzakt.com
	2010-01-18
*/

var thickbox_width = 750;

function bloc_deroulant(objet) {
	if (objet.hasClass("selected")) {
		objet.parent().next().slideUp("normal",function() { objet.removeClass("selected"); });
	} else {
		objet.parent().next().slideDown("normal");
		objet.addClass("selected");
	}
	
	if (!$(".bloc_deroulant_selected a.selected").length) {
		objet.parent().toggleClass("bloc_deroulant_selected");
	}
	return false;
}

$(function() {
	$(".zone_deroulante:not(.ouvert)").hide();
	
	// Nouveau : fermer le bloc deja ouvert
	$("a.lien_deroulant").click(
		function(){
			var new_a = $(this);
			var old_a = $('a.lien_deroulant.selected');
			var same_same = old_a.attr('id') == new_a.attr('id');

			// fermer le bloc deja ouvert
			if (old_a.length > 0 && !same_same) {
				old_a.parent().next().slideUp(
					"normal",
					function() { bloc_deroulant(old_a); return bloc_deroulant(new_a); }
				);
			} else { return bloc_deroulant(new_a); }
			
			return false;
		}
	);


	// Colorbox
	$(".pagebox").colorbox({width:thickbox_width, height:600, iframe:true});
	$(".videobox").colorbox({width:600,height:600, iframe:true,
		onComplete: function() {
			// trip : enlever la marge au bas, il n'y a pas de title.
			if($("#cboxLoadedContent").length > 0) {
				var margin = parseInt($("#cboxLoadedContent").css('margin-bottom'));
				var height = parseInt($("#cboxLoadedContent").css('height'));
				$("#cboxLoadedContent").css('height',(height+margin)+'px');
				$("#cboxLoadedContent").css('margin-bottom',0);
			}
		}
		
	});

	$(".imagebox, .thickbox").click(function() {
		if ($(this).is('img')) {
			$.fn.colorbox({ href: $(this).attr('src'), title: $(this).attr('alt') })
		} else if ($(this).is('a')) {
			$(this).colorbox({ })
		} else {
			return false;
		}
	});

	// Tableaux
	$(".fcktexte table").attr("cellSpacing","4").attr("border","0");
	var tr = 1;
	$(".fcktexte table tbody").each(function() {
		tr = 1;
		$(this).children("tr").not(".header").each(function() {
			if ((tr % 2) == 0)
				$(this).addClass("paire");
			tr++;
		});
	});

	$(".fcktexte table tbody tr:first-child td, .fcktexte table tbody tr:first-child th").addClass("premier_ligne");
	$(".fcktexte table tbody tr td:first-child, .fcktexte table tbody tr th:first-child").addClass("premier_colonne");


	// Emails
	$("a").each(function() {
		if ($(this).attr('href').match(/mailto/)) {
			var mt = decode_emailfilter($(this).attr('href'));
			$(this).attr('href',mt);
			if ($(this).html() == "Activer javascript")
				$(this).html(mt.replace(/mailto:/g,''));
		}
	});


	$('.video .lecteur a').hover(
		function() {
			if ($(this).next('.handle').length > 0) $(this).next('.handle').children('a').addClass('selected');
		},
		function() {
			if ($(this).next('.handle').length > 0) $(this).next('.handle').children('a').removeClass('selected')
		}
	);

	// ce qui ne fonctionne pas sur IE, le simuler
	if ($.browser.msie && parseInt($.browser.version) < 7) {
		$('.video .handle a').addClass('ie6');
	}

	// Nouvelles relies
	$('table#nouvelles_liste td').hover(function() { $(this).addClass('over'); }, function() { $(this).removeClass('over'); }).click(function() {
		window.location.href = $(this).children('li').children('a').attr('href');
	});

	// Suite des nouvelles
	$('.nouvelle .intro p:last').addClass('dernier');

	// Champs obligatoires
	$('input, textarea, select').each(function() {
		if ($(this).hasClass('requis')) {
			var label_text = $(this).parents('td').prev().children('label').html();
			if ($(this).parents('td').prev().children('label').children('span').length < 1)
				$(this).parents('td').prev().children('label').html(label_text+ ' <span class="requis">*</span>');
		}
	});
	
	if (typeof accueil_ready == "function")
		accueil_ready();
	if (typeof realisations_ready == "function")
		realisations_ready();
	if (typeof contact_ready == "function")
		contact_ready();
	if (typeof nouvelles_ready == "function")
		nouvelles_ready();
});


function decode_emailfilter(mt) {
	return mt.replace(/\?/g,'a').replace(/\$/g,'e').replace(/\@/g,'i').replace(/\&/g,'o').replace(/\%/g,'u').replace(/\//g,'@');
}

// HELPERS

function en_attente(conteneur,start) {
	if (start) {
		$(conteneur+" .en_attente").show();
	} else {
		$(conteneur+" .en_attente").hide();
	}
}