// unobtrusive javascript

document.getElementsByTagName("html")[0].className = "has_js";

var nbExtensionCurrent = 0;
var nbExtensionsMaxi = 0;
var url = '#';

$(document).ready(function(){

	$(".youtubebox").colorbox({iframe:true, innerWidth:640, innerHeight:390});
	
	// dates
	if ($('.datepicker').length > 0) {
		$('.datepicker').focus(function () {
	         $(this).select();
	    });
	}

	// HOME slider images bottom right
	if ($('#photos').length > 0) {
		$(".diaporama1").jDiaporama({
			animationSpeed: "slow",
			delay:5
		});
	}
	
	
	// HOME services
	if ($('#rubriques-home .abso a').length > 0) $('#rubriques-home .abso a').easyTooltip();
	if ($('#rubriques-home .box.vie_pratique a').length > 0) $('#rubriques-home .box.vie_pratique a').easyTooltip();
	
	// OUTILS
	if ($('#aides-content ul li a').length > 0) $('#aides-content ul li a').easyTooltip();

	
	// ANIMATION AIDES NAVIGATION
	// HOME PROFILS
	if ($('#aides-content li a').length > 0) {
		$('#aides-content li a').mouseover(function() {
			$(this).parent().children('img:eq(0)').removeClass('hidden');
			$(this).parent().children('img:eq(1)').addClass('hidden');
		});
		$('#aides-content li a').mouseout(function() {
			$(this).parent().children('img:eq(0)').addClass('hidden');
			$(this).parent().children('img:eq(1)').removeClass('hidden');
		});
	}

	// Cacher les éléments inutiles si on a javascript
	$('.without-js').hide();
	$('.has_js').show();

	// 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:eq(1)").addClass('current');
	$("#a-la-une #pagination-une li a").click(function() {

		var indexUne = $("#a-la-une #pagination-une li a").index($(this));
		
		if (indexUne > 0) {
			$('#pagination-une li').removeClass("current");
			$(this).parents("li").addClass("current");
		} else {
			$(this).parents("li").removeClass("current");
		}
	});
	$('#start-stop-slide').click(function(){
		if( $(this).hasClass('play') )
		{
			stopSlide();
			$(this).html('►').attr('title', "Démarrer l'animation").removeClass('play').removeClass('current');
		}
		else
		{
			startSlide();
			$(this).html('■').attr('title', "Arréter l'animation").addClass('play').removeClass('current');
		}
	});

	/*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 = 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()", 13000);
}

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();
}
