// unobtrusive javascript

document.getElementsByTagName("html")[0].className = "has_js";

var nbExtensionCurrent = 0;
var nbExtensionsMaxi = 0;
var url = '#';
var positionmousehome = '';
var oldimagehome = '';

var numeroSlide = 0;
var oldhanime = 0;
var numeroHanime = 1;

$(document).ready(function(){

	if (($('#hhome li').length > 0) && ($('.hhome li').length > 0)) {
		$('.hcontrol').show();



		var demarre = 1;
		var timer = null;
		$('.hcontrol').click(function() {
			if (demarre == 0) {
				timer=setInterval("doSlide();", 5000);
				demarre = 1;
				$('.hcontrol').text('Arréter');
			} else {
				if (timer != null) {
					clearInterval(timer);
				}
				demarre = 0;
				numeroSlide = 0;
				$('.hcontrol').text('Démarrer');
			}
		});
		$('.hcontrol').click();

		//if ($.browser.msie && $.browser.version <= 7) {$('#hhome li:gt(0)').css('filter', '');}
		var oldImgHome = 0;

		if ($.browser.msie && $.browser.version <= 8) {
			$('#hhome li:eq(0)').show();
			$('#hhome li:gt(0)').hide();
		}

		$('.hhome li a').click(function() {
		    var num = $(this).attr("class");

		    if (oldImgHome != (num-1)) {

			    if ($.browser.msie && $.browser.version <= 8) {
			    	$('#hhome li:eq('+(oldImgHome)+')').hide();
			    	$('#hhome li:eq('+(num-1)+')').show();
			    } else {
			    	$('#hhome li:eq('+(oldImgHome)+')').animate({
			        	opacity: 0
				    }, 1000);
				    $('#hhome li:eq('+(oldImgHome)+')').attr('style', 'z-index:0;');
				    $('#hhome li:eq('+(num-1)+')').animate({
				        opacity: 1
				    }, 1000);
				    $('#hhome li:eq('+(num-1)+')').attr('style', 'z-index:1;');
				}
		    }
		    oldImgHome = (num-1);
		});
	}







	// si on est bien sur l'accueil et qu'il y a des vignettes
	/*if ($('#bandeau ul li').length > 0) {
		$(".imgHome").mouseover(function() {
			$(this).parent().children('.actualite').fadeIn();
			positionmousehome = $(this).parent().children('.actualite').findPos();
			if (oldimagehome != '') {oldimagehome.fadeOut();}
			oldimagehome = $(this).parent().children('.actualite');
		});
		$(".imgHome").mouseout(function(e) {
			if ((
					(e.pageX >= positionmousehome.x) &&
					(e.pageX <= (positionmousehome.x+200))) && ((e.pageY >= positionmousehome.y) && (e.pageY <= (positionmousehome.y+$(this).parent().children('.actualite').height()))
			)) {

			} else {
				$(this).parent().children('.actualite').fadeOut();
			}

		});*/



	if ($('ul.hhome li').length > 0) {
	var lastThumb = -1;
	var currThumb = 0;
		$("ul.hhome li a img").mouseover(function() {
			currThumb = $(this).parent().attr("class");
			if (currThumb !== lastThumb) {
				$(this).parent().parent().children('.actualite').fadeIn();
				positionmousehome = $(this).parent().parent().children('.actualite').findPos();
				if (oldimagehome != '') {oldimagehome.fadeOut();}
				oldimagehome = $(this).parent().parent().children('.actualite');
				lastThumb = currThumb;
			}
		});
		$("ul.hhome li a img").mouseout(function(e) {
			if ((
					(e.pageX >= positionmousehome.x) &&
					(e.pageX <= (positionmousehome.x+200))) && ((e.pageY >= positionmousehome.y) && (e.pageY <= (positionmousehome.y+250))
			)) {

			} else {
				$(this).parent().parent().children('.actualite').fadeOut();
			}
		});

		//$(".imgHome").simpletip({ content: $('.imgHome').parent().children('.actualite').html() });
	}

	// Menu lat�ral, accord�on au survol (pour �viter la perte des liens, et au focus pour la navigation clavier
	// si le menu lat�ral existe et est non vide
	var oldMenuLateralOpened = null;
	if ($('#menu-lateral li a').length > 0) {
		// si ce n'est pas la page de recherche
		var urlpage = ' '+document.location+' ';
		var iWhere = urlpage.indexOf('pticcontent/search');
		var iWhere2 = urlpage.indexOf('Espace-documentaire');
		if ((iWhere < 0) && (iWhere2 < 0))  {
			$('#menu-lateral li ul').css('display', 'none');
			if ($('#menu-lateral li a.current').parent().children('ul').length > 0) {
			    $('#menu-lateral li a.current').parent().children('ul').slideDown();
			}
			$('#menu-lateral li a').mouseover(function() {
				//if (oldMenuLateralOpened != null) oldMenuLateralOpened.slideUp();
				if ($(this).parent().children('ul').length > 0) {
				    $(this).parent().children('ul').slideDown();
				    oldMenuLateralOpened = $(this).parent().children('ul');
				}
			});
			$('#menu-lateral li a').focus(function() {
				//if (oldMenuLateralOpened != null) oldMenuLateralOpened.slideUp();
				if ($(this).parent().children('ul').length > 0) {
				    $(this).parent().children('ul').slideDown();
				    oldMenuLateralOpened = $(this).parent().children('ul');
				}
			});
		}
	}


	// Slider header HOME
	var oldSlideHeader = 1;
	// si le slider existe et est non vide
	if ($('#hslider li').length > 0) {
		// au clic sur un lien
		$("#hslider li a").click(function () {
			//on r�cup�re le num�ro
			var href = $(this).attr('href');
			var numero = href.substr(1,1);
			// on cache l'�l�ment affich� et une fois fini on affiche le nouveau
			$('#uneslider li:eq('+(oldSlideHeader-1)+')').hide('slide', function() {$('#uneslider li:eq('+(numero-1)+')').show('slide'); });
			oldSlideHeader = numero;
		});
	}

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

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

	// Accordion Examples
	//$("#accordion").accordion();

	// Colorbox Examples
	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;
	});

	// supa title
	//if browser(msie) {
	//var h2 = $('.intitule-une').text();
	//$('.intitule-une').empty();
	//$('.bloc-text-une').append(h2).css('color','white');
	//}

	// Home : pagination des unes
	var target = $('#a-la-une .wrapper').get(0);
	$('#a-la-une #pagination-une').localScroll({
		target: target,
		axis: 'xy',
		queue: true,
		duration: 500
	});
	$("#a-la-une #pagination-une li:first").addClass('current');
	$("#a-la-une #pagination-une li a").click(function() {
		$('#pagination-une li').removeClass("current");
		$(this).parents("li").addClass("current");
	});
	$('#start-stop-slide').click(function(){
		if( $(this).hasClass('play') )
		{
			stopSlide();
			$(this).html('Play').removeClass('play');
		}
		else
		{
			startSlide();
			$(this).html('Stop').addClass('play');
		}
	});

	//	Lien de retour
	//$('a.retour').attr('href', 'javascript:history.back(0);');

	$("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;
	});

}); // end dom ready


jQuery.fn.extend({
	   findPos : function() {
	       obj = jQuery(this).get(0);
	       var curleft = obj.offsetLeft || 0;
	       var curtop = obj.offsetTop || 0;
	       while (obj = obj.offsetParent) {
	                curleft += obj.offsetLeft
	                curtop += obj.offsetTop
	       }
	       return {x:curleft,y:curtop};
	   }
	});

function loadlColorboxLink(){
	$('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 )
{
	if ( nbExtensionCurrent < nbExtensionsMaxi )
	{
		nbExtensionCurrent++;
		if($('.mode-news').hasClass('mode2') || $('.mode-news').hasClass('mode3')){
			$('.cadre-news').each(function(){
				elems=$(this).children('.bloc-actu:hidden:not(".cloneAffiche")');
				$(elems[0]).addClass("cloneAffiche").clone().addClass("clone").appendTo('.cadre-news:last').slideDown(function(){
					/*if($(elems).length==1){
						$('#anciennes-publications').hide();
					}*/
				});
			});
		}else{
			$('.cadre-news').each(function(){
				elems=$(this).children('.bloc-actu:hidden');
				$(elems[0]).slideDown(function(){
					/*if($(elems).length==1){
						$('#anciennes-publications').hide();
					}*/
				});
			});
		}
	}
	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( "Toutes les actualités" );
			$(obj).unbind( 'click' );
			$(obj).attr( 'href', url );
		}
		else
			$('#anciennes-publications').hide();
	}
}

function showDisplay(displayMax, idMode, renduHome, renduInterne )
{
	$('.cadre-news').each(function(){
		$(this).children('.bloc-actu').hide();
		$(this).children('.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( 1, 2, 'grid_8_bis', 'full');
}

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

function showDisplay4()
{
	return showDisplay( 2, 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 = 0;
	$('#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 doSlide() {
	var total = $('.hhome li a').length;
	var toslide = (numeroSlide+1);
	if ((numeroSlide+1) >= total) {
		numeroSlide = 0;
		toslide = 0;
	}
	$('.hhome li a:eq('+(toslide)+')').click();
	numeroSlide++;
}

