window.addEvent('domready', function(){

	//POPUP FORM REQUESTS
	$('newsletter_form').addEvent('submit', function(e) {
		new Event(e).stop();
		var log = $('newsletter_results').addClass('ajax-loading');
		new Request({
			method: this.method,
			url: this.action,
			onSuccess: function(responseHTML) {
				log.removeClass('ajax-loading');
				$('newsletter_form').set('html',responseHTML);
			},
			onFailure: function() {
				alert('Action Fail: Impossible de contacter le serveur');
			}
		}).send(this.toQueryString());
	});


	if($chk($('commande'))){
		$('commande_form').getElements('input.check2display').each(function(item){	
			var id = item.getProperty('id');
			if(!item.getProperty('checked')){$('selecteur_'+id).hide();}
			if(item.getProperty('checked')){$('selecteur_'+id).show();}
			item.addEvent('click', function(e){
				if(item.getProperty('checked')){$('selecteur_'+id).show();}
				else{$('selecteur_'+id).hide();}
			});
		});
		/*
		function calcul_commande(){
			$('abo_calcul_total').set('html',0)
			$('numeros_calcul_total').set('html',0);
			$('commande_calcul_total').set('html',0).addClass('ajax-loading');
			if($('abonnement').getProperty('checked')){$('abo_calcul_total').set('html','35');}
			if($('numeros').getProperty('checked')){
				$('liste_numeros').getElements('option').each(function(item){
					if(item.getProperty('selected')){$('numeros_calcul_total').set('html',$('numeros_calcul_total').get('html').toInt()+5);}
					item.addEvent('click', function(e){
						calcul_commande();
					});
				});
			}
			$('commande_calcul_total').set('html',
				$('numeros_calcul_total').get('html').toInt()+$('abo_calcul_total').get('html').toInt()
			);
			$('commande_calcul_total').removeClass('ajax-loading');
			$('abonnement').addEvent('click', function(e){calcul_commande();});
			$('numeros').addEvent('click', function(e){calcul_commande();});
		}
		calcul_commande();
		*/
		$('commande_form').addEvent('submit', function(e) {
			new Event(e).stop();
			$('commande_results').addClass('ajax-loading');
			new Request({
				method: this.method,
				evalScripts: true,
				evalResponse: true,
				url: this.action,
				onSuccess: function(responseHTML) {
					$('commande_results').removeClass('ajax-loading');
					$('commande_form').set('html',responseHTML);
				
					$('commande_form').getElements('input.check2display').each(function(item){	
						var id = item.getProperty('id');
						if(!item.getProperty('checked')){$('selecteur_'+id).hide();}
						item.addEvent('click', function(e){
							if(item.getProperty('checked')){$('selecteur_'+id).show();}
							else{$('selecteur_'+id).hide();}
						});
					});
				},
				onFailure: function() {
					alert('Action Fail: Impossible de contacter le serveur');
				}
			}).send(this.toQueryString());
		});
	}


	if($chk($('outils'))){


		$('outils').getElements('a.lien_outil').each(function(item){
			item.addEvent('click', function(e){
				e.stop();
				$('sous_'+item.getProperty('id')).toggle();
			});
		});



	}

	//MASK PROCESSES FOR EMAIL AND NEWSLETTER POPUPS
	var myMask = new Mask('mask');
	var mainScrolling = new Fx.Scroll($('whole_site'));

        $('mask').setStyle('z-index', 1001);
        $('popup_contents').hide().setPosition({relativeTo: $('all'),position: 'upperLeft'}).setStyle('z-index',1002);

	$$('#popup_contents div.popup_box').each(function(item){
			item.hide();
	});
	$('whole_site').getElements('a.contact_link').each(function(item){
		item.addEvent('click', function(e){
			e.stop();
			myMask.show();
			mainScrolling.toTop();
			$('results').empty().hide();
			$('popup_contents').setStyle('width', 400);
			$('popup_contents').position({
				relativeTo: 'navbar',
				position: 'center'
			});
			$('popup_contents').set('tween');
			$('popup_contents').show();
			$('popup_contents').tween('margin-top', [-1000,0]);
			$('contact_link_content').tween('opacity', [0,1]).show();
		});
	});
	$('whole_site').getElements('a.newsletter_link').each(function(item){
		item.addEvent('click', function(e){
			e.stop();
			myMask.show();
			mainScrolling.toTop();
			$('results').empty().hide();
			$('popup_contents').setStyle('width', 400);
			$('popup_contents').position({
				relativeTo: 'navbar',
				position: 'center'
			});
			$('popup_contents').set('tween');
			$('popup_contents').show();
			$('popup_contents').tween('margin-top', [-1000,0]);
			$('newsletter_link_content').tween('opacity', [0,1]).show();
		});
	});

	myMask.addEvent('click', function(e){
		myMask.hide();
		$('popup_contents').hide();

		$$('#popup_contents div.popup_box').each(function(item){
				item.hide();
		});
	});

	//MAPS PROCESSES
	var locations = $$('#Map div.location');
	locations.each(function(item, index){
		item.getElements('.popup').set('tween', {duration: 200});
		item.getElements('.point, .popup').addEvent('mouseenter', function(){
		    item.getElements('.popup').fade('in').show();
		});
		item.addEvent('mouseleave', function(){
		    item.getElements('.popup').fade('out');
		});
	});

	if($chk($('Archives'))){	
		$('Scrolling').set('tween');
		var myFx = new Fx.Scroll($('Archives_scroller'));
				myFx.toTop();

		/*
		$('Archives_scroller').addEvent('mousemove', function(e) {
				var x = $('Archives_scroller').getPosition().x;
				var x2 = e.client.x;
				var width = $('Archives_scroller').getSize().x;
				var amount_of_scrolling = (x2-x-width/2)/(width/2)*100;
				$('ScrollerRight').set('html',amount_of_scrolling*100);
				myFx.set(amount_of_scrolling);

		}).bind($('Archives_scroller'));
		*/

		$('Archives').getElements('a.link_annee').each(function(item){
			item.addEvent('click', function(e){
				$$('#Archives a.link_annee').each(function(item){item.removeClass('expose_on')});
				item.addClass('expose_on');
				var id = item.getProperty('id');
				e.stop();
				//$$('div.annee').each(function(item){item.hide();});
				//$('content_'+id).show();
				$('Scrolling').tween('margin-left', 0);
				myFx.toElement('content_'+id);
			});
		});
		$('Archives').getElements('div.numero').each(function(item){
			item.setStyle('background-color', '#ffffff');
			item.addEvent('mouseover', function(e){
				item.tween('background-color', '#dddddd');
			});
			item.addEvent('mouseout', function(e){
				item.tween('background-color', '#ffffff');
			});
		});
		$('ScrollerRight').addEvent('click', function(e){ 
				e.stop();
				var current_margin = $('Scrolling').getStyle('margin-left').toInt();
				//alert(current_margin);
				$('Scrolling').tween('margin-left', [current_margin,current_margin-400]);	
		});
		$('ScrollerLeft').addEvent('click', function(e){ 
				e.stop();
				var current_margin = $('Scrolling').getStyle('margin-left').toInt();
				$('Scrolling').tween('margin-left', [current_margin,current_margin+400]);	
		});
	}
	//MAP PROCESSES EXTERNAL LINKS PROCESSED WITH MASK
	$('all').getElements('a.spip_out').each(function(item){
		item.addEvent('click', function(e){
			e.stop();
			mainScrolling.toTop();
			$('framed_link_content').empty();
			myMask.show();
			var href = item.getProperty('href');
			$('popup_contents').show();
			var aFrame = new Element('iframe', {
				'src': href,
				'style':'width:100%;height:600px;border:0px;margin:0;padding:0;overflow:vertical;',
				'class': 'frame_out'
			}).inject('framed_link_content');
			$('framed_link_content').show();
					$('popup_contents').position({
						relativeTo: 'navbar',
						position: 'upperLeft'
					});
			$('popup_contents').set('tween', {duration: 'long'});
			$('popup_contents').tween('width', [0,1000]);


			//var myFx = new Fx.Morph('popup_contents').set({'width': '800px','height': '600px;'});
		});
	});
	$('all').getElements('div.selecter a').each(function(item){
		item.addEvent('click', function(e){
			e.stop();
			var id = item.getProperty('id');
			item.getParent().getElements('a').each(function(item){item.removeClass('expose_on')});
			item.addClass('expose_on');
			item.getParent().getParent().getElements('div.bloc_liste').each(function(bloc){bloc.hide();});
			$('contenu_'+id).show();
		});
	});
});

