// unobtrusive javascript

document.getElementsByTagName("html")[0].className = "has_js";
var nbExtensionCurrent = 0;
var nbExtensionsMaxi = 0;
var chooseLanguage = false;
var uneArePlayed = false;

$(document).ready(function(){

	// Home : pagination des unes

	$("#a-la-une #pagination-une li:eq(1)").addClass('current');
	$("#a-la-une #pagination-une li a").click(function(event) {
		event.preventDefault();
		var indexUne = $("#a-la-une #pagination-une li a").index($(this));

		if (indexUne > 0) {
			$('#pagination-une li').removeClass("current");
			$(this).parents("li").addClass("current");

			var calc = ((0+660) - (indexUne * 660));
			$('.wrapper-articles').stop().animate({
			    left: calc+'px'
			  }, 2000, function() {
			    // Animation complete.
			  });
		}
	});
	$('#start-stop-slide').click(function(){
		if( uneArePlayed )
		{
			uneArePlayed = false;
			stopSlide();
			$(this).html('►').attr('title', "Démarrer l'animation");
		}
		else
		{
			uneArePlayed = true;
			startSlide();
			$(this).html('■').attr('title', "Arréter l'animation");
		}
	});


	// HOME Cartouche MEDIA

	if ($('.cartouche_home.type_Media').length > 0) {
		$('.cartouche_home.type_Media .onglets li a').click(function(event) {
			event.preventDefault();
			$('.cartouche_home.type_Media .onglets li').removeClass('active');
			var classe = jQuery.trim($(this).parent().attr('class'));
			$(this).parent().addClass('active');
			$('.cartouche_home.type_Media .media > div').removeClass('show');
			$('.cartouche_home.type_Media .media .'+classe).addClass('show');
		});
	}


	// HOME PROFILS
	if ($('#profils li a').length > 0) {
		$('#profils li a').mouseover(function() {
			$(this).parent().children('img:eq(0)').removeClass('hidden');
			$(this).parent().children('img:eq(1)').addClass('hidden');
		});
		$('#profils li a').mouseout(function() {
			$(this).parent().children('img:eq(0)').addClass('hidden');
			$(this).parent().children('img:eq(1)').removeClass('hidden');
		});
	}

	// HOME cartouche multiple
	if ($('.cartouche_home.type_Multiple li a').length > 0) {
		$('.cartouche_home.type_Multiple li a').mouseover(function() {
			$(this).addClass('background_1');
			$(this).parent().children('img:eq(0)').removeClass('hidden');
			$(this).parent().children('img:eq(1)').addClass('hidden');
		});
		$('.cartouche_home.type_Multiple li a').mouseout(function() {
			$(this).removeClass('background_1');
			$(this).parent().children('img:eq(0)').addClass('hidden');
			$(this).parent().children('img:eq(1)').removeClass('hidden');
		});
	}







	// LANGUES
	$('#langues a.langues').click(function() {
		var myHeight = '12px';
		var myBorder = '0';
		var myRadius = '0';
		if (!chooseLanguage) {
			myHeight= ($('#langues .liste_langues').children('a').length * 19) + 'px';
			myBorder = '1px solid #505050';
			myRadius = '10px';
		}

		$('#langues .liste_langues').animate({
		    height: myHeight,
		    border: myBorder,
		    borderRadius: myRadius
		}, 1000, function() {
			if (!chooseLanguage) {
				chooseLanguage = true;
			} else {
				chooseLanguage = false;
			}
		});
	});

	// dates
	if ($('.datepicker').length > 0) {
		$('.datepicker').focus(function () {
	         $(this).select();
	    });
	}

	// Cacher les éléments inutiles si on a javascript
	$('.without-js').hide();

	// Tabs Examples
	$('#tabs').tabs();
	$('ul.onglets li a').click(function() {
		$('ul.onglets li').removeClass('active');
		$(this).parent('li').addClass('active');
	});

	// Colorbox
	loadlColorboxLink();

	//$("a[rel='galerie']").colorbox({transition:"elastic", contentCurrent:"{current} / {total}"});
	$('#display-news a').click(function(){
		$('.clone').remove();
		$('.cloneAffiche').removeClass('cloneAffiche');
		//$('#anciennes-publications:hidden').show();
		restoreAnciennesPublications();
	});

	$('#bt-display1').click(function() {
		return showDisplay1();
	});

	$('#bt-display2').click(function() {
		return showDisplay2();
	});

	$('#bt-display3').click(function() {
		return showDisplay3();
	});

	$('#bt-display4').click(function() {
		return showDisplay4();
	});

	$('#anciennes-publications a').click(function(){
		manageAnciennesPublications( $(this) );
		return false;
	});

	$("a.popup").click(function(){
		var popupHref=this.href;
		if (window.open(popupHref,'popupNewsletter','menubar=no, resizable=yes, scrollbars=yes, status=no, width=800, height=600')) return false;
	});

	$('a.calendar_day_link').live('click',function(){
		var url = $(this).attr('href');
		url = url.replace('salles','calendar/salle');
		url = url.replace('materiels','calendar/materiel');
		$.ajax({
			type: "GET",
			url: url,
			success: function(msg){
				$('#calendar-aff').html(msg);
			}
		});
		return false;
	});

	/* SELECT WEBTV CODE */
	$('div.code a.select').show();
	if (($('div.code .select.perma').length > 0) && ($('#perma').length > 0)) {
		$('div.code .select.perma').click(function() {
			selectElementText('perma');
		});
	}
	if (($('div.code .select.inte').length > 0) && ($('#inte').length > 0)) {
		$('div.code .select.inte').click(function() {
			selectElementText('inte');
		});
	}

	//Date Heure
	afficheDate();

}); // end dom ready

function loadlColorboxLink(){
	if ($('#avertissementgdc').length > 0) {
		var url = $('#avertissementgdc').attr('href');
		$('#avertissementgdc').attr('href', '/layout/set/popup'+url);
		$('#avertissementgdc').colorbox({width:"60%"});
	}
	$('a.colorbox').each(function(){
		$(this).colorbox();
	});
}

function restoreAnciennesPublications() {
	nbExtensionCurrent = 0;
	$('#anciennes-publications').show();
	$('#anciennes-publications a').html( "Voir plus d'actualités [...]" ).click(function(){
		manageAnciennesPublications( $(this) );
		return false;
	});
}

function manageAnciennesPublications( obj )
{
	$('.mode-news .bloc-actu').slideDown();
	checkLink( obj );
	loadlColorboxLink();
	return false;
}

function checkLink( obj )
{
	var more = true;
	var countElems = 0
	$('.cadre-news').each(function(){
		if($('.mode-news').hasClass('mode2') || $('.mode-news').hasClass('mode3'))
			elems=$(this).children('.bloc-actu:hidden:not(".cloneAffiche")');
		else
			elems=$(this).children('.bloc-actu:hidden');
		countElems += $(elems).length;
	});
	if ( countElems == 0 )
		more = false;

	if ( nbExtensionCurrent == nbExtensionsMaxi || !more )
	{
		if( url != '#' )
		{
			$(obj).html( "Voir toute l'actualité [...]" );
			$(obj).unbind( 'click' );
			$(obj).attr( 'href', url );
		}
		else
			$('#anciennes-publications').hide();
	}
}

function showDisplay(displayMax, idMode, renduHome, renduInterne )
{

	$('.mode-news .bloc-actu').hide();
	$('.mode-news .bloc-actu:lt('+displayMax+')').show();

	$('.mode-news').removeClass('mode1').removeClass('mode2').removeClass('mode3').removeClass('mode4').addClass('mode'+idMode);
	if( $('.mode-news .cadre-news').hasClass('grid_8_bis') || $('.mode-news .cadre-news').hasClass('grid_4_bis') )
		$('.mode-news .cadre-news').removeClass('grid_8_bis').removeClass('grid_4_bis').addClass( renduHome );
	else
	{
		if( $('.mode-news .cadre-news').hasClass('full') || $('.mode-news .cadre-news').hasClass('demi') )
			$('.mode-news .cadre-news').removeClass('full').removeClass('demi').addClass( renduInterne );
	}
	checkLink( $('#anciennes-publications a') );
	return false;
}

function showDisplay1()
{
	return showDisplay( 2, 1, 'grid_4_bis', 'demi');
}

function showDisplay2()
{
	return showDisplay( 3, 2, 'grid_8_bis', 'full');
}

function showDisplay3()
{
	return showDisplay( 2, 3, 'grid_8_bis', 'full');
}

function showDisplay4()
{
	return showDisplay( 5, 4, 'grid_4_bis', 'demi');
}

/************		"Caroussel" des une		**********/
var slid_is_play = 0;
var timeout;

function nextActuUne()
{
	var nextIndex = $('#pagination-une li').index($('#pagination-une li.current'))+1;
	if ( nextIndex >= $('#pagination-une li').length) nextIndex = 1;

	if ($('#pagination-une li:eq('+nextIndex+') a') == $('#pagination-une li:first a')) {
		nextIndex = $('#pagination-une li').index($('#pagination-une li.current'))+2;
		if ( nextIndex >= $('#pagination-une li').length) nextIndex = 1;
	}

	$('#pagination-une li:eq('+nextIndex+') a').click();
	timeout = setTimeout( "nextActuUne()", 4000);
}

function startSlide()
{
	if (!slid_is_play)
	{
		slid_is_play = 1;
		nextActuUne();
	}
}

function stopSlide()
{
	clearTimeout(timeout);
	slid_is_play = 0;
}

function selectElementText(objId) {
	fnDeSelect();
	if (document.selection) {
		var range = document.body.createTextRange();
		range.moveToElementText(document.getElementById(objId));
		range.select();
	}
	else if (window.getSelection) {
		var range = document.createRange();
		range.selectNode(document.getElementById(objId));
		window.getSelection().addRange(range);
	}

}
function fnDeSelect() {
	if (document.selection) document.selection.empty();
	else if (window.getSelection) window.getSelection().removeAllRanges();
}

//Date Heure

function afficheDate()// Notre fonction pour afficher la date et l'heure
	{
	jour = new Array ("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
	mois = new Array ("janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre");
	date=new Date();
	datejour=date.getDate();
	heure=date.getHours();
	minute=date.getMinutes();
	seconde=date.getSeconds();
	if (date.getDate()<10) datejour="0"+datejour;
	if (heure<10) heure="0"+heure;
	if (minute<10) minute="0"+minute;
	if (seconde<10) seconde="0"+seconde;
	document.getElementById("DHdate").innerHTML=jour[date.getDay()]+" "+datejour+" "+mois[date.getMonth()];
	document.getElementById("DHheure").innerHTML=+heure+"h"+minute;
	setTimeout("afficheDate()", 60000);
}
