var alertPeriodicalExecuter;
var boitesPeriodicalExecuter;
var brefPeriodicalExecuter;

function init(){
	targetLinks();
	
	var boites = $$('.v2k7_minibox_container');
	
	if( boites.length>0 ){
		var frequency = (9-boites.length<3) ? 3 : 9-boites.length
		boitesPeriodicalExecuter = new PeriodicalExecuter(scrollEachBoites, frequency);
	}
	
	if( $('v2k7_alerte') ) alertPeriodicalExecuter = new PeriodicalExecuter(scrollAlerte, 5);
	
	if( $('v2k7_bref_container') ){
		new Effect.Morph('v2k7_bref_container',{style:'height:'+$$('#v2k7_bref_container .v2k7_bref_content')[0].offsetHeight+'px;',duration:0.5});
		brefPeriodicalExecuter = new PeriodicalExecuter(scrollBref, 10);
		$('enbref_play').addClassName('actif');
		
		new Event.observe('enbref_next','click',function(e){
			brefPeriodicalExecuter.stop();
			brefPeriodicalExecuter = new PeriodicalExecuter(scrollBref, 10);
			scrollBref();
			$('enbref_next').blur();
			Event.stop(e)
		},false);
		new Event.observe('enbref_play','click',function(e){
			$('enbref_play').addClassName('actif');
			$('enbref_pause').removeClassName('actif');
			brefPeriodicalExecuter = new PeriodicalExecuter(scrollBref, 10);
			$('enbref_play').blur();
			Event.stop(e)
		},false);
		new Event.observe('enbref_pause','click',function(e){
			$('enbref_pause').addClassName('actif');
			$('enbref_play').removeClassName('actif');
			brefPeriodicalExecuter.stop();
			$('enbref_pause').blur();
			Event.stop(e)
		},false);
		new Event.observe('v2k7_bref_container','mouseout',function(e){
			$('enbref_play').addClassName('actif');
			$('enbref_pause').removeClassName('actif');
			brefPeriodicalExecuter = new PeriodicalExecuter(scrollBref, 10);
			$('enbref_play').blur();
			Event.stop(e)
		},false);
		new Event.observe('v2k7_bref_container','mouseover',function(e){
			$('enbref_pause').addClassName('actif');
			$('enbref_play').removeClassName('actif');
			brefPeriodicalExecuter.stop();
			$('enbref_pause').blur();
			Event.stop(e)
		},false);
	}
}
Event.observe(window,'load',init,false);


function scrollBoite( boite ){
	var idBoite = $(boite).getAttribute('id');
	var pile = $$('#'+idBoite+' .v2k7_minibox_content');
	
	if( pile.length<2 ) return;
	
	var first = pile[0];
	var last = pile[ pile.length-1 ];

	pile.each(function(e){
		new Effect.MoveBy(e, -first.offsetHeight, 0);
	});

	new Effect.MoveBy(first, 0, 0, {queue:'end',afterFinish:function(e){
		Element.setOpacity( first, 0 );
		var futur = first.remove();
		pile.each(function(e){
			e.style.top = "";
		});
		
		var parent = last.parentNode;
		if(parent.lastchild == last) {
			parent.appendChild(futur);
		} else {
			parent.insertBefore(futur, last.nextSibling);
		}
		new Effect.Opacity( $(futur) , {duration:0.5, from:0, to:1.0});
	}});
}

function scrollBoites(){
	$$('.v2k7_minibox_container').each(function(e){
		scrollBoite(e);
	});
}

var currentBoite = 0;
function scrollEachBoites(){
	var boites = $$('.v2k7_minibox_container');
	scrollBoite( boites[ currentBoite++ ] );
	if( currentBoite > boites.length-1 ) currentBoite = 0;
}


function scrollAlerte(){
	scrollBoite( $('v2k7_alerte') );
}

function showVideos(){
	$('v2k7_media_box_title_video').style.borderBottom = "none";
	$('v2k7_media_box_title_audio').style.borderBottom = "1px solid #000";
	$('v2k7_media_box_content_video').style.display = "";
	$('v2k7_media_box_content_audio').style.display = "none";
}

function showAudio(){
	$('v2k7_media_box_title_video').style.borderBottom = "1px solid #000";
	$('v2k7_media_box_title_audio').style.borderBottom = "none";
	$('v2k7_media_box_content_video').style.display = "none";
	$('v2k7_media_box_content_audio').style.display = "";
}

function scrollBref(){
	$('enbref_next').addClassName('actif');
	$('enbref_play').removeClassName('actif');

	var pile = $$('#v2k7_bref_container .v2k7_bref_content');
	
	if( pile.length<2 )
	{
		var height = pile[0].offsetHeight;
		new Effect.Morph('v2k7_bref_container',{style:'height:'+height+'px;',duration:0.5});
		brefPeriodicalExecuter.stop();
		return;
	}
	
	var first = pile[0];
	var last = pile[ pile.length-1 ];

	pile.each(function(e){
		new Effect.MoveBy(e, -first.offsetHeight, 0);
	});
	
	var height = pile[1].offsetHeight;
	new Effect.Morph('v2k7_bref_container',{style:'height:'+height+'px;',duration:0.5});

	new Effect.MoveBy(first, 0, 0, {queue:'end',afterFinish:function(e){
		Element.setOpacity( first, 0 );
		var futur = first.remove();
		pile.each(function(e){
			e.style.top = "";
		});
		
		var parent = last.parentNode;
		if(parent.lastchild == last) {
			parent.appendChild(futur);
		} else {
			parent.insertBefore(futur, last.nextSibling);
		}
		
		$('enbref_play').addClassName('actif');
		$('enbref_next').removeClassName('actif');

		new Effect.Opacity( $(futur) , {duration:1, from:0, to:1.0});
	}});
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function targetLinks(){
	var str;
	var where="_blank";
	for(var i=0;i<=(document.links.length-1);i++){
		str=document.links[i].href;
		if((str.search('urmla')==-1) && ((str.search('http://')!=-1) || (str.search('ftp://')!=-1)) || (str.search('.pdf')!=-1)){
		document.links[i].target=where;
		}
	}
}
