
function resize_amiando(id, width){
	var iframes = $$('#'+id+' iframe');
	if(iframes.length > 0){
		for(var i = 0; i < iframes.length; i++){
			iframes[i].set('width', width + 'px');
		}
	}
}

window.addEvent( 'domready', function() {
     
    if($$('a[rel=external]').length>0){
        initExternalLinks();
    }
    if($$(".carrousel").length>0){
        carrousel();
    }
    if($$(".chapitre").length>0){
        chapitre();
    }
	if($('sommaire_ancre')){
		sommaire_auto_ancre();
	}
    $$('body').addClass("js_active");
	
	  if ($$(".ss_menu").length>0){
		column_height ($$(".ss_menu"),".col");
	}
	
    if ($$(".bando").length>0){
		column_height ($$(".bando"),".bloc");
	}
	
	if ($("wrap_contact")){
		loadFormulaire();
	}
	
});


/* 
Sommaire automatique dans les articles.
	-> Permet de construire un sommaire automatique avec ancre
	-> a partir des h3 existant dans le bloc XML
*/
function sommaire_auto_ancre() {

	var sommaire_ul = $('sommaire_ancre');
	var all_header = $('repereArticle').getElements('h3');
	
	var tab_li = '';
	var tabValue = [];

	
	
	all_header.each(function(item,i) {
		
		// On construit le sommaire auto
		tab_li = tab_li + '<li id="titre'+i+'"><a href="#section'+[i]+'">'+item.get('text')+'</a></li>';
		// On ajoute les id aux h3
		item.set('id', 'section'+[i]);
		
		// On ajoute lancre top aux H3 
		item.addClass('section_top');
		var p = new Element('p', {"class": "top"});
		var myAnchor = new Element("a", {
			"href": "#sommaire_ancre",
			"html": "Top",
			"title": "Go on top"
	
		});
		
		myAnchor.inject(p);
		p.inject(item , 'after' );
	
	});

	sommaire_ul.set('html', tab_li);

}

/* 
Chapitres : accordeon :  forme des sections pour H3 
*/
function chapitre() {
	
	// On ajoute les classe section au H3
	var all_element = $('repereArticle').getChildren();
	
	var tabElt = [];
	var tabSection = [];
	var cpt = 0;
	var new_div_section = false;

	all_element.each(function(item,i) {
		if (item.tagName != 'H3') {
			if( $chk( new_div_section ) ) 	{
				item.inject( new_div_section );
			}
		} else {
				new_div_section = new Element('div', {
						'class': 'section'
                });
				new_div_section.inject( item, 'after' );
				cpt++;
			}
	});
		
	// On forme les accordeons
    var myAccordion = new Accordion($$('.chapitre #repereArticle h3'), $$('.chapitre .section'), {
        display:-1,
        alwaysHide:true,
        onActive: function(toggler) {toggler.addClass('moins');},
        onBackground: function(toggler) {toggler.removeClass('moins');} 
    });
}
/* 
Carrousel
    @param bloc = bloc contenant le carrousel
*/
function carrousel() {

	// On ajoute les ID ou il faut 
	var all_caroussel = $$('.carrousel');
	all_caroussel.each(function(item, index){
			item.setProperty('id', 'carrousel_'+index);	
	});
	
	var all_caroussel_mask = $$('.carrousel_mask');
	all_caroussel_mask.each(function(item, index){
			item.setProperty('id', 'carrousel_mask_'+index);	
	});
	var all_caroussel_content = $$('.carrousel_content');
	all_caroussel_content.each(function(item, index){
			item.setProperty('id', 'carrousel_content_'+index);	
	});

	// On creer le constructeur pour chaque carroussel
	all_caroussel.each(function(item, index){
			var class_presente = item.get('class').split(' ');
			
			if (class_presente.contains('bloc_libre_large')) {
				var width = 182;
				var item_visible = 5;
			}
			if (class_presente.contains('bloc_libre_medium')) {
				var width = 190;
				var item_visible = 3;
			}
			if (class_presente.contains('bloc_libre_small')) {
				var width = 230;
				var item_visible = 1;
			}
			if (class_presente.contains('bloc_libre_home')) {
				var width = 175;
				var item_visible = 1;
			}
			
			new SlideItMoo({
				overallContainer: 'carrousel_'+index,
				elementScrolled: 'carrousel_mask_'+index,
				thumbsContainer: 'carrousel_content_'+index,
				itemsVisible:item_visible,
				elemsSlide:1,
				duration:300,
				itemsSelector: '.vue',
				itemWidth: width
			}); 
	});


}



/**
* Emulateur de target=_blank,
* Pour concilier en aparence des exigences contradictoires
* (validation XHTML stricte & ouverture des liens externes dans une nouvelle fenêtre).
*/
function initExternalLinks()
{
	$$( 'a[rel=external]' ).each( function( item ) {
        item.target = '_blank';
    });
}


/* 
Egaliseur de blocs
    @param zone = zone de référence dans laquelle les blocs se resizent
    @param bloc = bloc à resizer
*/
function column_height (zone, str_bloc){
    zone.each( function(el) {
        var max_height = 0;
        var bloc = el.getChildren(str_bloc);
        bloc.each( function(item) {
            max_height = Math.max(max_height, (item.getSize().y-12 ));
        });
        bloc.setStyle('min-height', max_height);
    });
}


/*
* Formulaire de contact AJAX
*/
function loadFormulaire() {
	
	$$('input[type=button]').addEvent('click', function(e) {
			
		/** On modifie la valeur du input type hidden pour savoir quel bouton on a clique, et savoir du coup a quel etape on se trouve  **/
		$('etape').set('value', e.target.get('name'));

		/**
		 * Prevent the submit event
		 */
		e.stop();
		
		/**
		 * This empties the log and shows the spinning indicator
		 */
		var log = $('wrap_contact').addClass('ajax-loading');
		$('monFormulaire').set('send', {
								url: '/layout/set/ajax/contact/formulaire', 
								method: 'post',
								evalScripts: true, 
								onComplete: function(response) {
										log.removeClass('ajax-loading');
										log.set('html', response);
										loadFormulaire();
								}
		});
		$('monFormulaire').send();
					
	});
	
}

// Reconstitue les liens des images sur les fiches EPI (activité)
function checkEpiActivityLink(prefixe){
	var links = $$('.l_replace');

	links.each(function(item,i) {
		
		var tmp = item.getProperty('src');
		item.setProperty('src', prefixe + tmp);
	});
	
	
}




