jQuery(document).ready(function() {
    $("body").addClass("js").removeClass("no-js");

    /* Gestion du rollOver en Fade (pas IE) */
    if ($.browser.msie) {} else {
        $('.imgRollover').mouseover(function() {
            $(this).fadeTo(200, 0.6);
        }).mouseout(function(){
            $(this).fadeTo(200, 1);
        });
    }

    $("ul.tabNavigation li a").hover(function() {
        var elmt = $(this);
		
        if(!elmt.hasClass('selected')) {
            $('> .bulle2', this).show();
        }
		
    }, function() {
        var elmt = $(this);
		
        if(!elmt.hasClass('selected')) {
            $('> .bulle2', this).hide();
        }
    });


    //Cufon.replace( '#background .legende span.titre');
	
	
    $('#background ul').cycle({
        fx:     'fade',
        speed:  '1000',
        timeout: 0,
        next:   '#next',
        prev:   '#prev'
    });
	
	
	
    if ($.browser.msie) {
        effetFade = "none";
    } else {
        effetFade = "fade";
    }
    var carrousel_newsMsg = $('#newsMsg > ul:first');
    carrousel_newsMsg.cycle({
        fx:     effetFade,
        speed:  '500',
        timeout: 3000,
        cleartypeNoBg:   true,
        pager : '#navControl ul',
        pagerAnchorBuilder: function(idx, slide) {
            var num = idx + 1;
            var title = $('a',slide).html();
            return '<li><a href="#"><span title="' + num + ' : '+title+'">' + num + '</span></a></li>';
        },
        before: function(idx, slide) {
            $('#newsMsg ul li a').removeAttr('title');
        },
        after: function(idx, slide) {
            $('a',slide).attr('title','Actualité en cours de consultation');
        }
	
    });
	
    $('#nextactu').bind('click',function(){
        if (jQuery('.playpause').hasClass('pause')) {
            jQuery('.playpause').removeClass('pause').addClass('play');
            if($('.playpause').hasClass('contrast'))
            {
                $('.playpause img').attr('src','/tpl/img/p-play-contrast.png');
            }
            else
            {
                $('.playpause img').attr('src','/tpl/img/p-play.png');
            }
            var altnext =$('.playpause img').attr('alt');
            $('.playpause img').attr('alt',$('.playpause img').attr('altnext'));
            $('.playpause img').attr('altnext',altnext);
            carrousel_newsMsg.cycle('pause');
        }
        carrousel_newsMsg.cycle('next');
        return false;
    });
    $('#prevactu').bind('click',function(){
        if (jQuery('.playpause').hasClass('pause')) {
            jQuery('.playpause').removeClass('pause').addClass('play');
            if($('.playpause').hasClass('contrast'))
            {
                $('.playpause img').attr('src','/tpl/img/p-play-contrast.png');
            }
            else
            {
                $('.playpause img').attr('src','/tpl/img/p-play.png');
            }
            var altnext =$('.playpause img').attr('alt');
            $('.playpause img').attr('alt',$('.playpause img').attr('altnext'));
            $('.playpause img').attr('altnext',altnext);
            carrousel_newsMsg.cycle('pause');
        }
        carrousel_newsMsg.cycle('prev');
        return false;
    });
	
	
    $('#videoGal ul').cycle({
        fx:     effetFade,
        speed:  '0',
        timeout: 0,
        next:   '#next2',
        prev:   '#prev2'
    });
	
    $('#portfolio ul').cycle({
        fx:     effetFade,
        speed:  '1500',
        timeout: 1500
    });
	
	
    jQuery('.playpause').click(function() {
        if (jQuery(this).hasClass('pause')) {
            // basculer en jouer
            jQuery(this).removeClass('pause').addClass('play');
            if($(this).hasClass('contrast'))
            {
                $('.playpause img').attr('src','/tpl/img/p-play-contrast.png');
            }
            else
            {
                $('.playpause img').attr('src','/tpl/img/p-play.png');
            }
            var altnext =$('.playpause img').attr('alt');
            $('.playpause img').attr('alt',$('.playpause img').attr('altnext'));
            $('.playpause img').attr('altnext',altnext);
            carrousel_newsMsg.cycle('pause');
        } else {
            // basculer en pause
            jQuery(this).removeClass('play').addClass('pause');
            if($(this).hasClass('contrast'))
            {
                $('.playpause img').attr('src','/tpl/img/pause-hover-contrast.png');
            }
            else
            {
                $('.playpause img').attr('src','/tpl/img/pause-hover.png');
            }
			
            var altnext =$('.playpause img').attr('alt');
            $('.playpause img').attr('alt',$('.playpause img').attr('altnext'));
            $('.playpause img').attr('altnext',altnext);
            carrousel_newsMsg.cycle('resume');
        }
        return false;
    });
    $('#newsMsg ul.listeActualites li  a').bind('focus',function(){
        if (jQuery('.playpause').hasClass('pause')) {
            jQuery('.playpause').removeClass('pause').addClass('play');
            if($('.playpause').hasClass('contrast'))
            {
                $('.playpause img').attr('src','/tpl/img/p-play-contrast.png');
            }
            else
            {
                $('.playpause img').attr('src','/tpl/img/p-play.png');
            }
            var altnext =$('.playpause img').attr('alt');
            $('.playpause img').attr('alt',$('.playpause img').attr('altnext'));
            $('.playpause img').attr('altnext',altnext);
            carrousel_newsMsg.cycle('pause');
        }
        carrousel_newsMsg.cycle('pause');
    });
    $('#newsMsg ul.listeActualites li  a').bind('focusout',function(){
         jQuery('.playpause').removeClass('play').addClass('pause');
            if($('.playpause').hasClass('contrast'))
            {
                $('.playpause img').attr('src','/tpl/img/pause-hover-contrast.png');
            }
            else
            {
                $('.playpause img').attr('src','/tpl/img/pause-hover.png');
            }

            var altnext =$('.playpause img').attr('alt');
            $('.playpause img').attr('alt',$('.playpause img').attr('altnext'));
            $('.playpause img').attr('altnext',altnext);
        carrousel_newsMsg.cycle('resume');
    });
    //Correctif Carte intéractive FDU 20111121
    $('#international #infos').hide();
    /*
  $("a.fancybox").fancybox({
		'titleShow'	: true,
		'titlePosition' : 'over',
		'titleFormat' : function() {
        var title = $(this).attr("title");
        var titleFancy = "";
		    var hTitle = title.split('-');
		    var i = 0;
        for (i=0 ; i < hTitle.length - 1; i++) {
				titleFancy += hTitle[i];
			}
			return '<span id="fancybox-title-over">' + titleFancy + '</span>'; //TODO: supprimer le ouverture fenetre modale
		},
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'overlayOpacity' : 0.75,
		'margin' : 0,
		'padding' : 0
	});*/
	
	
    $('a.fancybox').click(function(){
        anchorFocused=this;
    }).fancybox({
        'titleShow'	: true,
        'titlePosition' : 'over',
        'titleFormat' : function() {
            var title = '';
            if($(this).attr("title") != null)
            {
                title = $(this).attr("title");
            }
            var titleFancy = "";
            var hTitle = '';
            if(title != '')
            {
                hTitle = title.split('-');
            }
            var i = 0;
            for (i=0 ; i < hTitle.length - 1; i++) {
                titleFancy += hTitle[i];
            }
            if(titleFancy != '')
            {
                return '<span id="fancybox-title-over">' + titleFancy + '</span>'; //TODO: supprimer le ouverture fenetre modale
            }
            else{
                return '';
            }
        },
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'overlayOpacity' : 0.75,
        'margin' : 0,
        'padding' : 0,
        onComplete : function(){
            $('a').attr('tabIndex', '-1');
            $('input').attr('tabIndex', '-1');
            $('area').attr('tabIndex', '-1');
            $('select').attr('tabIndex', '-1');
            $('textarea').attr('tabIndex', '-1');
            $('button').attr('tabIndex', '-1');
            $('#fancybox-left a').attr('tabIndex', '2');
            $('#fancybox-right a').attr('tabIndex', '2');
            $('#fancybox-close a').attr('tabIndex', '1');
        },
        onClosed : function(){
            $('#fancybox-left a').removeAttr('tabIndex', '-1');
            $('#fancybox-right a').removeAttr('tabIndex', '-1');
            $('#fancybox-close a').removeAttr('tabIndex', '-1');
            $('a').attr('tabIndex', '0');
			$('input').attr('tabIndex', '0');
            $('area').attr('tabIndex', '0');
            $('select').attr('tabIndex', '0');
            $('textarea').attr('tabIndex', '0');
            $('button').attr('tabIndex', '0');

            anchorFocused.focus();
        }
    });
	
	
    $("a.inline").fancybox({
        'hideOnContentClick': false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'margin' : 0,
        'padding' : 0,
        swf : {
            wmode: 'transparent'
        }
    });
    $("a.group").fancybox();


    DD_roundies.addRule('#header ul#nav, #header ul#nav li .sousNav', '10px', true);
    DD_roundies.addRule('#header ul#metanav, a.button', '5px 5px 0 0', true);
    DD_roundies.addRule('#infoShare', '0px 10px 0 0', true);
    DD_roundies.addRule('.item #submitForm, p.button a', '5px', true);
    if ($.browser.mozilla) {
        DD_roundies.addRule('.encart', '7px', true);
    }
    if ($.browser.opera) {
        DD_roundies.addRule('.encart', '7px', true);
    }

    //DD_roundies.addRule('.encart', '7px', true);
    /*
var selected = $('#wrapNav > ul > li > .active').parent(); // li rub selectionne
var activeContent = false;
if($('[id^=menu]', selected).css('display') == 'block') {
        activeContent = true;
}

$('#wrapNav > ul > li').hover(function () {

        // debug IE 6 z-index for tooltip
$('#mainContent').removeAttr('style');

        // masque l'onglet active
        var parent = $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').parent();
        if(parent.hasClass('mainMenuItemWrapper')) {
                $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').parent().css({'display': 'none'});
                $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').css({'display': 'block'});
        } else {
                $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').css({'display': 'none'});
        }

        $('#wrapNav > ul > li > .active').removeClass('active');

        // active l'onglet survole
        $('> a', this).addClass('active');
        $('[id^=menu]', this).parent().css({'display': 'block'});
        $('[id^=menu]', this).css({'display': 'block'});

}, function () {

        // masque l'onglet active
        var parent = $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').parent();
        if(parent.hasClass('mainMenuItemWrapper')) {
                $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').parent().css({'display': 'none'});
                $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').css({'display': 'block'});
        } else {
                $('#wrapNav > ul > li > .active').parent().find('[id^=menu]').css({'display': 'none'});
        }
        $('#wrapNav > ul > li > .active').removeClass('active');

        // active l'onglet correspondant à la rubrique active
        $('> a', selected).addClass('active');
        if(activeContent) {
                $('[id^=menu]', selected).css({'display': 'block'});
        }

});*/
	
    // YLO : menu principale, fait passer le contenu sur 2 colonnes
    /*$('.sousNav').each(function () {
		var elmt = $(this);
		elmt.show(); // affichage obligatoire pour faire le calcul de hauteur en jQuery - masqué à la fin de la fonction
		
		elmt.append('<ul></ul>');
		var mainUl = $('> ul:first', elmt);
		var secondUl = $('> ul:not(:first)', elmt);
		var total = mainUl.height();
		var target = total / 2;
		
		var stop = false;
		var i = $('li', mainUl).length - 1;
		while(!stop) {
			temp = $('> li:eq('+i+')', mainUl);
			$('> li:eq('+i+')', mainUl).remove();
			secondUl.prepend(temp);
			
			if(mainUl.height() < secondUl.height()) {
				stop = true;
				
				var restore = $('> li:first', secondUl);
				$('> li:first', secondUl).remove();
				mainUl.append(restore);
				
			} else {
				i--;
			}
		}
		elmt.hide();
	});*/


    /* Mega Drop MENU */
	
    //Set custom configurations
    var config = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 500, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };
	
    $("ul#nav li .sousNav").css({
        'opacity':'0',
        'display':'none'
    }); //Fade sub nav to 0 opacity on default
    $("ul#nav li").hoverIntent(config); //Trigger Hover intent with custom configurations
    $("ul#nav > li").focusin(function(e) {
        megaHoverOver($(this), e);
    }).focusout(function (e) {
        megaHoverOut($(this), e);
    }); //Trigger Focus intent with custom configurations
	
    $("ul#nav > li > .sousNav > ul > li > a").focusin(function() {
        $(this).addClass("actif");
    }).focusout(function () {
        $(this).removeClass("actif");
    }); //Trigger Focus intent with custom configurations



    //On Hover Over
    function megaHoverOver(elt, e){
		
        /* 	correctif efr : fixer le focus au clavier
			si pas d'evenement défini => evenement souris
			sinon evenement clavier
		*/
        if (e === undefined) {
            elt = $(this);
        }
		
		
        elt.find(".sousNav").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
        elt.find("> a").addClass("actif");
        (function($) {
            //Function to calculate total width of all ul's
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() { //for each ul...
                    rowWidth += $(this).width(); //Add each ul's width together
                });
            };
        })(jQuery);
	
        if ( elt.find(".row").length > 0 ) { //If row exists...
	
            var biggestRow = 0;
	
            elt.find(".row").each(function() {	//for each row...
                $(this).calcSubWidth(); //Call function to calculate width of all ul's
                //Find biggest row
                if(rowWidth > biggestRow) {
                    biggestRow = rowWidth + 100;
                }
            });
	
            elt.find(".sousNav").css({
                'width' :555
            }); //Set width
            elt.find(".row:last").css({
                'margin':'0'
            });  //Kill last row's margin
	
        } else { //If row does not exist...
	
            elt.calcSubWidth();  //Call function to calculate width of all ul's
            elt.find(".sousNav").css({
                'width' : 555
            }); //Set Width
	
        }
    }

    //On Hover Out
    function megaHoverOut(elt, e){
	
        /* 	correctif efr : fixer le focus au clavier
			si pas d'evenement défini => evenement souris
			sinon evenement clavier
		*/
        if (e === undefined) {
            elt = $(this);
        }
	
        elt.find("> a").removeClass("actif");
        elt.find(".sousNav").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it
        });
    }
        
    $('#newsMsg #control').css('position', 'absolute');

});

