/*
$Revision:                 $
$Date:                     $
*/



window.addEvent('domready', function() {
	
	// dnd_agenda_animated
	var animation_delay_agenda = 3500;
	var active_class_agenda = 'dnd_agenda_animated_active';
	
	var active_number_agenda;
	var timer_agenda;

	$$('.dnd_agenda_animated').each(function(main_div) {
		
		var contents_div = main_div.getElements('.dnd_module_agenda_content');
		var nb_contents = contents_div.length;
		active_number_agenda = nb_contents-1;
		
		if( !nb_contents ) return;
		
		
		// Events
		main_div.addEvents({
			mouseover: function() {
				$clear(timer_agenda);
			},
			mouseout: function() {
				$clear(timer_agenda);
				timer_agenda = animate_agenda.periodical(animation_delay_agenda);
			}
		});
		
		main_div.getElements('.agenda_label').addEvents({
			mouseover: function() {
				$clear(timer_agenda);
				
				var number = this.getParent('.dnd_module_agenda_content').get('class').match(/num_([0-9]+)/i);
				number = number[1].toInt() + (nb_contents-1);
				
				animate_agenda(number);
			}
		});
		
		
		// Création du div "info"
		new Element('div', {'class':'animated_info_overlay'})
		.inject(main_div);
		
		var info_title_div = new Element('div', {'class':'animated_info_title'})
		.inject(main_div);
		
		var info_div = new Element('div', {'class':'animated_info'})
		.inject(main_div);
		
		var intro_div = new Element('div', {'class':'animated_intro'})
		.inject(info_div);
		
		var category_div = new Element('div', {'class':'animated_category'})
		.inject(info_div);
		
		var date_div = new Element('div', {'class':'animated_date'})
		.inject(info_div);
		
		var title_div = new Element('div', {'class':'animated_title'})
		.inject(info_title_div);
		
		var subtitle_div = new Element('div', {'class':'animated_subtitle'})
		.inject(info_title_div);
		
		
		// Lien info_div
		info_div.addEvent('click', function(){
			window.location = contents_div[ active_number_agenda ].getElement('a').get('href');
		});
		
		
		
		// Notre function "animate_agenda"
		var animate_agenda = function(number) {
			if(number)
				active_number_agenda = number % nb_contents;
			else
				active_number_agenda = ( active_number_agenda+1 ) % nb_contents;
			
			var current_content = contents_div[ active_number_agenda ];
			
			// Classes CSS
			contents_div.removeClass(active_class_agenda);
			current_content.addClass(active_class_agenda);
			
			
			// Title
			title_div.set('html', current_content.getElement('.agenda_title').get('text'));

			// Intro
			intro_div.set('html', current_content.getElement('.agenda_intro').get('text'));

			// Subtitle
			subtitle_div.set('html', current_content.getElement('.agenda_subtitle').get('text'));
			
			// Category_date
			if( current_content.getElement('.agenda_category') )
			{
				category_div.set('html', current_content.getElement('.agenda_category').get('text'));
			}
			if( current_content.getElement('.agenda_date') )
			{
				date_div.set('html', current_content.getElement('.agenda_date').get('text'));
			}
		};
		
		
		animate_agenda();
		timer_agenda = animate_agenda.periodical(animation_delay_agenda);
		
	});






	/*
	 dnd_agenda_flash
	 */
	$$('.dnd_agenda_flash').each(function(main_div) {
		
		var animation_delay_agenda = 3500;
		var timer_agenda;
		
		
		var contents_div = main_div.getElements('.dnd_module_agenda_content');
		var nb_contents = contents_div.length;
		active_number_agenda = nb_contents-1;
		
		if( !nb_contents || nb_contents==1 ) return;
		
		var clone_div = contents_div[0].clone();
		
		
		// Ajoute le div "flash"
		var flash_div = new Element('div')
		.setStyles({
			width: '100%',
			height: '100%',
			position: 'absolute',
			left: 0,
			top: 0
		})
		.adopt(contents_div)
		.adopt(clone_div)
		.inject(main_div);
		
		
		// Pause au survol
		main_div.addEvents({
			mouseover: function() {
				$clear(timer_agenda);
			},
			mouseout: function() {
				$clear(timer_agenda);
				timer_agenda = animate_agenda.periodical(animation_delay_agenda);
			}
		});
		
		
		// Notre function "animate_agenda"
		var animate_agenda = function(number) {
			
			var posTop = flash_div.getCoordinates(main_div).top - main_div.getHeight();
			var absPosTop = 0 - posTop;
			
			if( absPosTop == ( (1+nb_contents) * main_div.getHeight() ) )
			{
				flash_div.setStyle('top', 0);
				posTop = 0 - main_div.getHeight();
			}
			
			flash_div.tween('top', posTop);
		};
		
		
		//animate_agenda();
		timer_agenda = animate_agenda.periodical(animation_delay_agenda);
		
	});
	
	
	
	/*
	  dnd_agend_mooquee
	 */
	$$('.dnd_agenda_mooquee').each(function(main_div) {
		
		var speed = 40;
		var steps = 1;
		var pauseOnOver = true;
		
		var timer_agenda;
		
		
		var contents_div = main_div.getElements('.dnd_module_agenda_content');
		var nb_contents = contents_div.length;
		
		if( !nb_contents || nb_contents==1 ) return;
		
		
		var clones_div = contents_div.clone();
		var first_clone_div = clones_div[0];
		var first_content_div = contents_div[0];
		
		
		main_div.setStyle('white-space', 'nowrap');
		
		// Ajoute le div "mooquee"
		var mooquee_div = new Element('div')
		.setStyles({
			height: '100%',
			position: 'absolute',
			left: 0
		})
		.adopt(contents_div)
		.adopt(clones_div)
		.inject(main_div);
		
		
		// Pause au survol
		if( pauseOnOver )
		{
			main_div.addEvents({
				mouseover: function() {
					$clear(timer_agenda);
				},
				mouseout: function() {
					$clear(timer_agenda);
					timer_agenda = animate_agenda.periodical(speed);
				}
			});
		}
		
		
		// Notre function "animate_agenda"
		var animate_agenda = function() {
			var posLeft = mooquee_div.getStyle('left').toInt() - steps;
			var absPosLeft = 0 - posLeft;
			
			if( absPosLeft > first_clone_div.getCoordinates(mooquee_div).left )
				posLeft = 0 - first_content_div.getCoordinates(mooquee_div).left - steps;
			
			mooquee_div.setStyle('left', posLeft);
		};
		
		
		//animate_agenda();
		timer_agenda = animate_agenda.periodical(speed);
		
	});
	
	
});



