var rsiOverlay, rsiPopup;
window.addEvent('domready', function(){
	// JS
	$$('body').removeClass('no-js').addClass('js');
	
	// SPACES & SECTIONS MENU
	$$('#spaces li a', '#sections li a').addEvents({
		mouseenter: function(event){
			this.get('tween', {property: 'opacity', duration: 'short'}).start(0.0001);
		},
		mouseleave: function(event){
			this.get('tween', {property: 'opacity', duration: 'normal'}).start(1);
		}
	});
	$('spaces').addEvents({
		'mouseenter': function(){
			//this.getElement('ul').fade('in');
			this.getElement('ul').setStyle('display', 'block');
		},
		'mouseleave': function(){
			//this.getElement('ul').fade('out');
			this.getElement('ul').setStyle('display', 'none');
		}
	}).getElement('ul').setStyle('display', 'none');
	$$('#space-0', '#space-1').addEvent('click', function(event){
		event.stop();
		spacesOverlay = new Element('div', {
			id: 'spaces-overlay',
			events: {
				click: function(event){
					event.stop();
					spacesPopup.destroy();
					spacesOverlay.destroy();
				}
			}
		}).fade(0, .25).inject(document.body);
		spacesPopup = new Element('div', {
			id: 'spaces-popup',
			'class': this.get('class')
		}).adopt(
			new Element('a', {
				id: 'spaces-popup-close',
				href: '/',
				text: 'Fermer',
				events: {
					click: function(event){
						event.stop();
						spacesPopup.destroy();
						spacesOverlay.destroy();
					}
				}
			}),
			new Element('div', {
				id: 'spaces-popup-content'
			}).set('load', {evalScripts: false}).load('/tpl/'+this.get('class')+'.tpl.php')
		).fade(0, 1).inject(document.body);
	});
	
	// SEARCH FORM
	$$('#search label input').addEvents({
		blur: function(event){
			if(this.getProperty('value') == '') this.setProperty('value', 'Rechercher sur le site');
		},
		focus: function(event){
			if(this.getProperty('value') == 'Rechercher sur le site') this.setProperty('value', '');
		}
	});
	
	// SUBMIT ON CHANGE
	$$('form.submit-on-change').each(function(form){
		form.getElements('input[type=checkbox]').addEvent('click', function(){
			form.submit();
		});
		form.getElements('select').addEvent('change', function(){
			form.submit();
		});
		form.getElement('p.input.submit').destroy();
	});
	
	// PAGES MENU
	if($('pages')){
		$$('#pages li').each(function(li, i){
			if(ul = li.getElement('ul')){
				if(Browser.Engine.trident4) ul.setStyle('width', li.getSize().x);
				ul.fade('hide');
			}
			li.addEvents({
				'mouseenter': function(event){
					this.addClass('hover');
					if(!this.hasClass('current') && !Browser.Engine.trident4 && (span = this.getElement('span'))) span.get('tween', {property: 'background-position', duration: 'short'}).start('center 30px', 'center 2px');
					if(ul = li.getElement('ul')) ul.fade('in');
				},
				'mouseleave': function(event){
					this.removeClass('hover');
					if(!this.hasClass('current') && !Browser.Engine.trident4 && (span = this.getElement('span'))) span.get('tween', {property: 'background-position', duration: 'normal'}).start('center 2px', 'center 30px');
					if(ul = li.getElement('ul')) ul.fade('out');
				}
			});
		});
	}
	
	// SIDEBAR ACCORDION OFFERS
	if($('sidebar') && $('sidebar').getElement('#offers')){
		new Accordion('#sidebar #offers ul li strong', '#sidebar #offers ul li ul', {
			show: false,
			display: false,
			alwaysHide: true,
			opacity: false,
			onActive: function(toggler, element){
				toggler.addClass('active');
			},
			onBackground: function(toggler, element){
				toggler.removeClass('active');
			},
			transition: 'quint:out',
			duration: 'normal'
		});
		$$('#sidebar #offers ul li strong', '#sidebar #offers ul li ul li a').addEvents({
			mouseenter: function(event){
				this.get('tween', {property: 'opacity', duration: 'short'}).start(0.0001);
			},
			mouseleave: function(event){
				this.get('tween', {property: 'opacity', duration: 'normal'}).start(1);
			}
		});
	}
	
	// HR
	$$('#content #text hr').each(function(hr, i){
		new Element('div', {'class': 'hr'}).wraps(hr);
	});
	
	// ACCORDIONS
	$$('#content #text dl').each(function(dl, i){
		var i = 0;
		dl.getElements('dt').addEvents({
			mouseenter: function(event){
				this.addClass('hover');
			},
			mouseleave: function(event){
				this.removeClass('hover');
			}
		}).some(function(dt){
			if(dt.hasClass('current')) return true;
			i++;
		});
		new Accordion(dl.getElements('dt'), dl.getElements('dd'), {
			alwaysHide: true,
			opacity : true,
			display: i,
			onActive: function(toggler, element){
				if(!Browser.Engine.trident4) element.tween('padding-bottom', 15);
				toggler.addClass('open');
			},
			onBackground: function(toggler, element){
				if(!Browser.Engine.trident4) element.tween('padding-bottom', 0);
				toggler.removeClass('open');
			}
		});
	});
	
	// REMOOZ
	$$('a.remooz', 'a[target=_top]').each(function(element) {
		var reMooz = new ReMooz(element, {
			centered: false,
			origin: element.getFirst(), 
			cutOut: false,
			opacityResize: 0,
			margin: 35,
			parse: 'rel'
		});
	});
	
	// SLIDESHOWS
	$$('div.slideshow').each(function(element){
		var simpleSlideShow = new SimpleSlideShow({
			slides: element.getChildren()
		});
		var next = function(){
			this.forward();
		}
		var periodical = next.periodical(5000, simpleSlideShow);
		element.addEvents({
			mouseenter: function(){
				$clear(periodical);
			},
			mouseleave: function(){
				periodical = next.periodical(5000, simpleSlideShow);
			}
		});
	});
	
	// TARGET-BLANK
	$$('a.target-blank', 'a[target=_blank]').addEvent('click', function(event){
		event.stop();
		window.open(this.get('href'));
	});
	
	// ALIGNS & BORDER
	$$('*[align=left]').removeProperty('align').addClass('align-left');
	$$('*[align=right]').removeProperty('align').addClass('align-right');
	$$('*[align=center]').removeProperty('align').addClass('align-center');
	$$('*[border=0]').removeProperty('border').addClass('no-border');
	$$('*[hspace=0]').removeProperty('hspace').addClass('no-margin');
	$$('*[vspace=0]').removeProperty('vspace').addClass('no-margin');
	
	//Buttons Hovering
	$$('img.has-hover').each(function(element){
		element.addEvent('mouseover', function(e){
			e.stop();
			element.setProperty('src', '/img/'+element.getProperty('id')+'-hover.gif');
		})
		element.addEvent('mouseout', function(e){
			e.stop();
			element.setProperty('src', '/img/'+element.getProperty('id')+'.gif');
		})
	})
	
	if($('banner')) $('spaces').addEvents({
		'mouseenter': function(){
			$('banner').setStyle('display', 'none');
		},
		'mouseleave': function(){
			$('banner').setStyle('display', 'block');
		}
	});
});
