//jQuery.noConflict(); 
$(document).ready(function() {
    //HOME
    if($('body#home').size()){
        //CARROUSEL FORMATION
        if ($("#content_formation").size()) {
            $('a.btn_form').click(function(e) {
                e.preventDefault();
                var zone = $(this).attr("rel");
                if(zone == 'formation-2012'){
                	$('#content_formation').removeClass('orange');
                    $('#content_formation').addClass('brown');
                }else{
                	$('#content_formation').addClass('orange');
                    $('#content_formation').removeClass('brown');
                }
                    
                $('.wrap_formation').hide();
                $('#'+zone).show();
            });
            
            $(".wrap_formation li a").mouseover(function()
            {
                var witem = $(this).width()-5,
                	hitem = $(this).next('div').height()+10,
                	css = ($.browser.msie && $.browser.version <= '7.0') 
                		? {"position":"absolute", "margin-top" : -hitem+25}
                		: {"position":"absolute", "margin-top" : -hitem, "margin-left" : witem};
                
                $(this).next("div.ui-tooltip-ifg").css(css);
                $(this).next("div.ui-tooltip-ifg").show();
                
            }).mouseout(function(){
                $("div.ui-tooltip-ifg").hide();
            });
        }
        if ($(".accesexec").size()) {
            var autoscroll = true;
            $('.bt-carousel').mouseover(function(e) {
                autoscroll = false;
                e.preventDefault();
                var zone = $(this).attr("rel");		
                $('.bt-carousel').removeClass('act');
                $(this).addClass('act');
                $('.carousel').hide();
                $('#'+zone).show();
            })
            .mouseout(function(){
                autoscroll = true;
            });
            
            setInterval(function () {
                if(autoscroll){
                    $('.bt-carousel').removeClass('act');
                    var id = "";
                    $("div.accesexec div.carousel").each(function() {
                    	id = $(this).attr('id');
                        if ($(this).is(':hidden')){
                            $('a.bt-carousel[rel='+id+']').addClass('act');
                            $(this).show();
                        } else {
                            $(this).hide();
                        }
                    });
                }
            }, 7000);
        }
            
        /* Transformation des select */
        $('select#domaine').selectmenu();
        $('select#ville').selectmenu();
        $('select#date').selectmenu();
        $('select#diplome').selectmenu();
	
	}
	
	/*Police menu*/
	Cufon.replace("#menu-principal ul.nivo1 a",{fontFamily:'Helvetica Neue LT Std'});
	Cufon.replace(".title-right h2,.button-download a,.bt-carousel span",{fontFamily:'Helvetica Neue LT Std'});
	Cufon.replace("a.btn_form",{fontFamily:'Bodoni Std'});

	/* Menu contact */
	if ($(".lien-contact").size()>0) {
		$(".lien-contact").mouseover(function(){
			$(this).find("ul").css({display:"block"});
		}).mouseout(function(){
			$(this).find("ul").css({display:"none"});
		});
	}
	//Modification IFGCNOF Joan Drozo 18/11/2011 mauvais affichage FF
	if($.browser.msie)
	{
		//alert('au meteorites');
		$("#menu-principal > ul.nivo1 div.button-left-menu > li").mouseover(function()
		{
			var witem = 0;
			
			$(this).parent('div').addClass('menu-selected');
			$("ul.nivo2").hide();
			
			witem = ($.browser.msie && $.browser.version <= '7.0') ? $(this).width()+6 : $(this).width()-10;
			
			$(this).find('ul.nivo2 li').css({"width":witem});
			$(this).find('ul.nivo2').show();
		})
		.mouseout(function()
		{
			$(this).parent('div').removeClass('menu-selected');
			$("ul.nivo2").hide();
		});
	}
	else{
		$("#menu-principal > ul.nivo1 div.button-left-menu > li").mouseover(function()
		{
			var witem = 0;
			
			$(this).parent('div').addClass('menu-selected');
			//$("ul.nivo2").hide();
			
			witem = ($.browser.msie && $.browser.version <= '7.0') ? $(this).width()+6 : $(this).width()-10;
			
			$(this).find('ul.nivo2 li').css({"width":witem});
			$(this).find('ul.nivo2').show();
		})
		.mouseout(function()
		{
			$(this).parent('div').removeClass('menu-selected');
			$("ul.nivo2").hide();
		});
		
		
		$("#menu-principal > ul.nivo1 div.button-left-menu > li ul.nivo2").mouseover(function()
		{
		$(this).show();
		}).mouseout(function()
		{
		$(this).hide();
		});
	}
	// Fin modification IFGCNOF Joan Drozo
	/*Ouverture des popin */
	$("a.popin-open-01").popin({
		width:450,
		height:600,
		className: "mypopin",
		loaderImg : 'fileadmin/templates/images/ajax-loader.gif'
	});    
    $("a.popin-open-rating-formation").popin({
		width:600,
		height:600,
		className: "mypopin",
		loaderImg : 'fileadmin/templates/images/ajax-loader.gif'
	});
	
	//si le div par-region-liste-items existe
	if ($("#par-region-liste-items").size()>0) {
		//si sa hauteur ne dépasse pas la hauteur max
		if ($('#par-region-liste-items').height()<348) {
			//Ne bloque pas sa hauteur
			$('#par-region-liste-items').css('height','auto');
		}
		else {
			//Sinon fait le jscrollpane avec la scrollbar toute belle 
			$('#par-region-liste-items').jScrollPane({showArrows:true,scrollbarWidth:25, dragMinHeight:69, dragMaxHeight:69});
		}
	}
	
	/* Fonction pour effacer la valeur des champs texte au focus */
	if ($(".effacer").size()>0) {
		$(".effacer").each(function(i){
			var defautTxt = $(this).val();
			$(this).click(function(){
				if ($(this).val()==defautTxt) {
					$(this).val("");
				}
			});
			$(this).blur(function(){
				if ($(this).val()=="") {$(this).val(defautTxt);}								   
			});
		});
	}
	/* carrousel vertical colonne de droite */
	$('#personnes-interessees').jcarousel({
        vertical: true,
        scroll: 2
    });
	/* Carrousel utilisé pour les onglets des fiches */
	$('#onglet-fiche').jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback2,
		itemVisibleInCallback: {
            onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation2
        },
		itemVisibleOutCallback: {
            onBeforeAnimation: mycarousel_itemVisibleOutCallbackAfterAnimation2
		},
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	/* Creation du défilement pour les onglets */
	var containerWidth100 = 908;
	var containerWidth = 668;
	var TotalCarControl = 0;
	var OneItemWidth = 0;
	$('.jcarousel-control > a').each(function() {
		TotalCarControl += $(this).width();
		OneItemWidth = $(this).width();
	});
	if (TotalCarControl>parseFloat($('.jcarousel-container').width())) {
		$('.jcarousel-control').wrap('<div class="jcarousel-control-wrap"><div class="jcarousel-control-scroller"></div></div>');
		$('.jcarousel-control-wrap').css({height:$('.jcarousel-control').height()});
		$('.jcarousel-control-wrap').append('<div class="jcarousel-control-fleche1"></div><div class="jcarousel-control-fleche2"></div>');
		$('.jcarousel-control').css({width:TotalCarControl});
		$('.jcarousel-control-scroller').css({width:containerWidth+'px',position:'absolute',top:'0px',left:'15px', height:$('.jcarousel-control').height()});
		$('.bloc100 .jcarousel-control-scroller').css({width:containerWidth100+'px',position:'absolute',top:'0px',left:'15px', height:$('.jcarousel-control').height()});
		$('.jcarousel-control').css({position:'absolute',top:'0px',left:'0px'});
		var endflag=0;
		$('.jcarousel-control-fleche1').click(function() {
			if (parseInt($('.jcarousel-control').css('left'))<0 && endflag!=1) {
				endflag=1;
				if(Math.abs(parseInt($('.jcarousel-control').css('left')))>=OneItemWidth){
					$(".jcarousel-control").animate({"left": "+="+OneItemWidth+"px"}, "slow",function(){
						endflag=0;
					});
				}else{
					$(".jcarousel-control").animate({"left": "0px"}, "slow",function(){
						endflag=0;
					});
				}
			}
		});
		$('.jcarousel-control-fleche2').click(function() {
			if(endflag!=1){
				endflag=1;
				if (parseFloat($('.jcarousel-control').css('width')) + parseFloat($('.jcarousel-control').css('left'))-OneItemWidth>containerWidth) {
					if ($('.jcarousel-control').css('left')!=$('.jcarousel-control').css('width')) {
						$(".jcarousel-control").animate({"left": "-="+OneItemWidth+"px"}, "slow",function(){
							endflag=0;
						});
					}

				}
				else {
					var leftmax=parseInt($('.jcarousel-control').css('width'))-containerWidth;
					$(".jcarousel-control").animate({"left": -leftmax+"px"}, "slow",function(){
						endflag=0;
					});
				}
			}
		});
	}
	/* Carrousel dispositif du parcours */
	$('#carousel-dispositif').jcarousel();
	$('#carousel-diplome').jcarousel();
	/* Onglets type accueil */
	$('.onglets').tabs();
	$('.formation-liste-int').tabs();
	//$('.sinscrire-onglet-fiche').tabs();
	

	/* Liste déroulante sous powermail */	
	$('select.powermail_select').selectmenu();
	$('.tx-kleeselector-pi1 select').selectmenu();
	$('.formulaire-sinscrire select').selectmenu();
	$('.tx-kleetraining select').selectmenu();
	
	

	/*formulaire inscription formation*/
	for(var compte_part=0;compte_part<21;compte_part++)
	{
		var fonction = 'select#participant_fonction_'+compte_part;
		$(fonction).selectmenu();
		var service = 'select#participant_service_'+compte_part;
		$(service).selectmenu();
		var civilite = 'select#participant_civilite_'+compte_part;
		$(civilite).selectmenu();
	}
	
	if(document.getElementById('ligne3'))
		cache_ligne();
	/**********************************/
	

	$('.choix-competence').find('.a-compet').each(function() {
		$(this).parent().find('.choix-liste-formations').css('display','none');
		$(this).click(function() {			
			if ($(this).parent().hasClass('li-compet-act')) {
				$(this).parent().find('.choix-liste-formations').slideToggle("fast");
				$(this).parent().removeClass('li-compet-act');
			}
			
			if ($(this).parent().find('.choix-liste-formations').css('display')=='none') {
				$(this).parent().find('.choix-liste-formations').slideToggle("fast");
				$(this).parent().addClass('li-compet-act');
			}
			return false;
		});
	});
	
	$('.choix-competence-parcours').find('.a-compet').each(function() {
		$(this).parent().find('.choix-liste-formations-parcours').css('display','none');
		$(this).click(function() {			
			if ($(this).parent().hasClass('li-compet-act')) {
				$(this).parent().find('.choix-liste-formations-parcours').slideToggle("fast");
				$(this).parent().removeClass('li-compet-act');
			}
			
			if ($(this).parent().find('.choix-liste-formations-parcours').css('display')=='none') {
				$(this).parent().find('.choix-liste-formations-parcours').slideToggle("fast");
				$(this).parent().addClass('li-compet-act');
			}
			return false;
		});
	});	
	
	
	$('.choix-liste-formations').find('.a-sousdomaine').each(function() {
		$(this).parent().find('.nivo2').css('display','none');
		$(this).click(function() {			
			if ($(this).parent().hasClass('act')) {
				$(this).parent().find('.nivo2').slideToggle("fast");
				$(this).parent().removeClass('act');
			}
			
			if ($(this).parent().find('.nivo2').css('display')=='none') {
				$(this).parent().addClass('act');
				$(this).parent().find('.nivo2').slideToggle("fast");
			}
			return false;
		});
		$(this).parent().find('.nivo2').find('.a-soussousdomaine').each(function() {
			$(this).parent().find('.nivo3').css('display','none');
			$(this).click(function() {				
				if ($(this).parent().hasClass('act')) {
					$(this).parent().find('.nivo3').slideToggle("fast");
					$(this).parent().removeClass('act');
				}
				
				if ($(this).parent().find('.nivo3').css('display')=='none') {
					$(this).parent().addClass('act');
					$(this).parent().find('.nivo3').slideToggle("fast");
				}
				return false;
			});
		});
	});
	
	$('#onglet-fiche').each(function() {
		$('#mycarousel-prev',this).css('top',$(this).find('.jcarousel-clip').height() / 2 + 15);
		$('#mycarousel-next',this).css('top',$(this).find('.jcarousel-clip').height() / 2 + 15);
	});
});

function mycarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).attr('id').replace('navig','')));
        return false;
    });
}

function mycarousel_initCallback2(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).attr('id').replace('navig','')));
        return false;
    });

	$('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });    
}

function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
    $('.num-'+idx).addClass('act');
};

function mycarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {
    $('.num-'+idx).removeClass('act');
};
function mycarousel_itemVisibleInCallbackAfterAnimation2(carousel, item, idx, state) {
    $('#navig'+idx).addClass('act');
	$('#navig'+(idx+1)).addClass('act-sec');
};

function mycarousel_itemVisibleOutCallbackAfterAnimation2(carousel, item, idx, state) {
    $('#navig'+idx).removeClass('act');
	$('#navig'+(idx+1)).removeClass('act-sec');
};

/*Ajout d'une ligne participant pour le formulaire d'inscription à une formation (entreprise) */
var nombre_participant = 2;
var total_participant = 10;
function ajout_participant()
{
	nombre_participant++;
	
	if(nombre_participant<total_participant)
	{
		document.getElementById('ligne'+nombre_participant).style.display='block';
		document.getElementById('cache_'+nombre_participant).value='display_block';
		//document.getElementById('ligne'+nombre_participant).innerHTML = "cela fonctionne";
	}
}

/*Cacher toutes les lignes contenant les participants (initialisation du formulaire) */
function cache_ligne()
{
	var i = 0;
	for(i=3;i<total_participant+1;i++)
	{
		var style = 'none';
		if(document.getElementById('cache_'+(i-1)).value=='display_block')
		{
			style = 'block';
			nombre_participant++;
		}
		
		document.getElementById('ligne'+i).style.display=style;
	}
}


/*afficher le detail d'une region*/
function showVilleDetails(villeID){
	
	//efface les données de la ville eventuellement affichée
	parentVille = document.getElementById('tx_kleeregions_pi1_liste');
	for (var i=0, l=parentVille.childNodes.length; i<l; i++) {

        var child = parentVille.childNodes[i];
		if(child.nodeType==1 && child.style.display=='block'){
			child.style.display='none';
		}

	}
	
	//l'item selectionné dans la liste est deselectionné
	parentVilleItem = document.getElementById('tx_kleeregions_pi1_liste-items');
	for (var i=0, l=parentVilleItem.childNodes.length; i<l; i++) {
		if(parentVilleItem.childNodes[i].className =='active'){
			parentVilleItem.childNodes[i].className ='';
		}
	}
	
	//affichage des données de la ville souhaitée
	document.getElementById(villeID).style.display='block';
	document.getElementById(villeID+'_option').className  ='active';
	return false;
}

/* Fonction qui controle l'activation/désactivation des listes déroulantes dans la recherche avancée de formations*/
function checkDomain(value){
    if(value==0) {
        $('select#sdomaine').val(0); 
        $('select#ssdomaine').val(0); 
    }
    $('select#sdomaine').attr('disabled', 'disabled');
    $('select#ssdomaine').attr('disabled', 'disabled');
    $('select#sdomaine').selectmenu('destroy').selectmenu();
    $('select#ssdomaine').selectmenu('destroy').selectmenu();
}

/* Fonction qui controle l'activation/désactivation des listes déroulantes dans la recherche avancée de formations*/
function checkSDomain(value){
    if(value==0) {
        $('select#ssdomaine').val(0);
    }
    $('select#ssdomaine').attr('disabled', 'disabled');
    $('select#ssdomaine').selectmenu('destroy').selectmenu();
}
