// unobtrusive javascript

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

var nbExtensionCurrent = 0;
var nbExtensionsMaxi = 0;
var url = '#';
var sens;
function defil () {
	if ($('#rubriques-home .next').hasClass('disabled'))
	{
		$sens = 'g';
	}
	if ($('#rubriques-home .prev').hasClass('disabled')) {
		$sens = 'd';
	}
	if ($sens == 'g') {
		$('#rubriques-home .prev').click();
	}
	else {
		$('#rubriques-home .next').click();
	}
}
$(document).ready(function(){
	$('#rubriques-home .prev').addClass('disabled');
	setInterval("defil()", 13000);
	// 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: 548,
			panel_height: 235,
			frame_width: 100,
			frame_height: 40
		});
	}

	//Diapo Actus
	if ($('#actualites').length > 0) {
		$("#actualites .jCarouselLite").jCarouselLite({
		    btnNext: "#actualites .next",
		    btnPrev: "#actualites .prev",
		    circular: false,
		    visible: 2
		});
	}

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

	// SERVICES EN LIGNE
	if (($('#services').length > 0) && ($('#services .center .plus').length > 0)) {
		$('#services .center .plus').css('display', 'block');

		$('#services').css('height', '45px');
		$('#services .center').css('height', '45px');

		var servicesOpened = false;
		$('#services .center .plus a').click(function() {
			if (!servicesOpened) {
				if($.browser.msie){
					$('#services').css('height', '100px');
					$('#services .center').css('height', '100px');
					$('#services .center .plus a').attr('title', 'Moins de services');
					$('#services .center .plus a').html('<img src="/extension/pticbase/design/design_base/images/header/services.png" alt=""><span>fermer</span>');
					servicesOpened = true;
				} else {
					$('#services .center').animate({
						height: '100px'
					});
					$('#services').animate({
						height: '100px'
					}, {
					    duration: 1000,
					    complete: function() {
						$('#services .center .plus a').attr('title', 'Moins de services');
						$('#services .center .plus a').html('<img src="/extension/pticbase/design/design_base/images/header/services.png" alt=""><span>fermer</span>');
						servicesOpened = true;
					    }
					});
				}
			} else {
				if($.browser.msie){

					$('#services').css('height', '45px');
					$('#services .center').css('height', '45px');

					$('#services .center .plus a').attr('title', 'Plus de services');
					$('#services .center .plus a').html('<img src="/extension/pticbase/design/design_base/images/header/services.png" alt="">de <span>services</span>');
					servicesOpened = false;
				} else {
					$('#services .center').animate({
						height: "45px"
					});
					$('#services').animate({
						height: "45px"
					}, {
					    duration: 1000,
					    complete: function() {
						$('#services .center .plus a').attr('title', 'Plus de services');
						$('#services .center .plus a').html('<img src="/extension/pticbase/design/design_base/images/header/services.png" alt="">de <span>services</span>');
						servicesOpened = 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();
}
