// unobtrusive javascript

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

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

$(document).ready(function(){

	// MENU PRINCIPAL
	// menu déroulant
	/*$('.Menu-without-js').removeClass('Menu-without-js').addClass('Menu');
	$('ul#menu').addClass('has_js');
	$('#globmenu').addClass('has_js');
	$('#menu .Menu').fadeOut('fast');
	$('#menu > li').hover(function() {
		if (obj) {
			obj.find('ul').css('display', 'none');
			obj = null;
		} //if
		$(this).find('ul').css('display', 'block');
	}, function() {
		obj = $(this);
		setTimeout(
			"checkHover()",
			0);
	});
	var oldMenu = null;
	$('#menu li a').focus(function() {
		if ($(this).attr('href') == 'javascript:void(0);') {
		  if (oldMenu != null) {
			  oldMenu.hide();
		  }
		  if ($(this).parent().children('.Menu').length > 0) {
		      $(this).parent().children('.Menu').show();
		      oldMenu = $(this).parent().children('.Menu');
		  }
		}
	});
	$("#menu li a:last").blur(function() {
		$('.Menu').hide();
	});*/

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

	// HOME galerie
	if ($('#photos').length > 0) {
		$('.backdark').removeClass('backdark');
		$('#photos .filmstrip').css('display', 'block');
		$('#photos').galleryView({
			panel_width: 710,
			panel_height: 275,
			frame_width: 100,
			frame_height: 40
		});


	}

	// HOME slider images bottom right
	if ($('#photos').length > 0) {
		$(".diaporama1").jDiaporama({
			animationSpeed: "slow",
			controls: true,
			currentimage: false,
			delay: 5,
			infos: false
		});
	}

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

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

	if (document.referrer != "") {
		$('a.retour').attr('href', document.referrer);
	}

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

}); // end dom ready

function loadlColorboxLink(){
	$('a.colorbox').each(function(){
		$(this).colorbox();
	});
}

function restoreAnciennesPublications() {
	nbExtensionCurrent = 0;
	$('#anciennes-publications').show();
	$('#anciennes-publications a').html( "<span class='fl'>>></span> 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( "<span class='fl'>>></span> 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 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();
}

function checkHover() {
	if (obj) {
		obj.find('ul').css('display', 'none');
	} //if
} //checkHover
