MooTools.lang.setLanguage('fr-FR');

function changeTextSize(size)
{
	var body = $(document.getElementsByTagName('body')[0]);
	body.className = size;
}

var SimpleMenu = new Class({
	element: null,
	delayClose: null,
	className: null,
	openElement: null,
	timeout: null,
	initialize: function(selector, className, delayClose)
	{
		this.element = $(selector);
		this.className = className;
		this.delayClose = delayClose;
		
		var child = this.element.getChildren();
		var l = child.length;
		
		for(x = 0; x < l; x++)
		{
			var c = child[x];
			var s = c.getElement('.submenu');
			
			c.addEvent('mouseenter', this.openMenu.bind(this, c));
			c.addEvent('mouseleave', this.closeMenu.bind(this, c));
			
			if(s)
			{
				s.addEvent('mouseenter', this.openMenu.bind(this, c));
				s.addEvent('mouseleave', this.closeMenu.bind(this, c));
			}
		}
	},
	openMenu: function(el)
	{
		if(this.timeout) $clear(this.timeout);
		if(this.openElement && this.openElement != el) this.directCloseMenu(this.openElement);
		
		el.addClass(this.className);
		this.openElement = el;
	},
	closeMenu: function(el)
	{
		if(this.timeout) $clear(this.timeout);
		this.timeout = this.directCloseMenu.delay(this.delayClose, this, el);
	},
	directCloseMenu: function(el)
	{
		el.removeClass(this.className);
	}
});

function initMenu()
{
	window.addEvent('domready', function() {
		new SimpleMenu('nav', 'show_menu', 2000);
	});
}

function initSlideshow(selector, slide_delay, slide_transition, slide_duration)
{
	window.addEvent('domready', function() {
		var el = $(selector);

		if(el)
		{
			var slideshow = new SlideShow(el,{
				delay: slide_delay,
				transition: slide_transition,
				duration: slide_duration,
				autoplay: true
			});

			el.addEvent('mouseover', function() {
				slideshow.pause();
			});

			el.addEvent('mouseleave', function() {
				slideshow.play();
			});
		}
	});
}

function showAgenda(selector, year, month)
{
	var el = $(selector);
	
	if(el)
	{
		var mTips = new Tips();
		
		if(!year)
		{
			var d = new Date();
			year = d.getFullYear();
			month = d.getMonth() + 1;
		}
		
		spin = new Spinner(el);
		spin.show();
		
		var req = new Request.HTML({
			url: '?module=agenda&year='+year+'&month='+month,
			onSuccess: function(html)
			{
				var cur = el.getElement('table');
				if(cur) cur.dispose();
				
				el.adopt(html);
				mTips.attach('.agenda_tip');
				spin.hide()
				spin.destroy.delay(4000, spin);
			},
			onFailure: function() {
				$('result').set('text', 'The request failed.');
				spin.hide()
				spin.destroy.delay(4000, spin);
			}
		});
		
		req.send();
	}
}

function initAgenda(selector, year, month)
{
	window.addEvent('domready', function() {
		showAgenda(selector, year, month)
	});
}
