function PageInit()
{
	HeadlineManager.init();
	UtmController.init();
	
	FormValidate.title = "Veuillez corriger les erreurs suivantes :";
	
	initHeoesManager();
	initCarousels();
	initMenu();
	initDefaultTooltips();
	initHighlighting();
	initFormValidation();
	initTabs();
	initCurvyCorners();
	initCalendarTooltips();
	initEasterEgg();
	/*showDidierLetter();*/
}

function initCarousels()
{
	if($('video-list')) {
		new VCarousel('video-list', {
			numVisible: 1, 
			scrollInc: 3,
			numVisible: 3,
			buttonStateHandler: buttonStateHandler
		});
	}
}

function initHeoesManager()
{
	if($('heroesMenu')) {
		new SimpleHeroes('heroesMenu', 'heroesPictures', 'heroesLoading');
	}
}

function initMenu()
{
	if($('root')) {
		this.menu1 = new Menu('root', 'menu1');
	}
}

function initTabs()
{
	$A($$('.tabs a')).each(function(a) {
	
		Event.observe(a, 'click', function(e, a) {
		
			$$('.tab_content').invoke('hide');
			$$('.tabs a').invoke('removeClassName', 'selected');
			
			var id = $A(a.href.split('=')).last();
        	$('tab_'+id).show();
        	
			a.addClassName('selected');
        	Event.stop(e);
        	
		}.bindAsEventListener(this, a));
		
	});
}

function initFormValidation()
{
	// All forms having the 'formvalidate' class will have validators built and submit event handled.
	$A($$('form.formvalidate')).each(function(f) {
	
		FormValidate.buildValidators(f.id);
		
		f.observe('submit', function(e, f) {
					
			if(!FormValidate.validate(f.id, true, showFormError.bind(FormValidate)))
			{
				Event.stop(e);
				return false;
			}
			
			return true;
		}.bindAsEventListener(this, f));
		
	});
}

function initHighlighting()
{
	// All inputs with a parent having the 'ctrlHolder' class will handle highlighting.
	$A($$('.ctrlHolder .textInput')).each(function(i) {
	
		i.observe('blur', function(e, i) {
			$(i).up('.ctrlHolder').removeClassName('selected');
		}.bindAsEventListener(this, i));
		
		i.observe('focus', function(e, i) {
			$(i).up('.ctrlHolder').addClassName('selected');
		}.bindAsEventListener(this, i));
		
	});
}

function initDefaultTooltips()
{
	// All link having the 'tooltipLink' class will handle tooltips.
	$A($$('a.tooltipLink')).each(function(a) {
	
		//var my_tooltip = new Tooltip(a, a.title);
		
		new Effect.Tooltip(a, a.title, {
			className: 'tooltip',
			offset: {
				'x' : 15, 
				'y' : -7
			}
		});
		
		// Cancel default tooltip.
		a.title = "";
	});
}

function initCurvyCorners()
{
	// Curvy corners
	settings = {
		tl: { radius: 10 },
		tr: { radius: 10 },
		bl: { radius: 10 },
		br: { radius: 10 },
		antiAlias: true,
		autoPad: true
	}

	var myBoxObject = new curvyCorners(settings, "rounded");
	myBoxObject.applyCornersToAll();
}

function initCalendarTooltips()
{
	// Tooltip calendar.
	if($('calendar')) {
		$A($$('#calendar td')).each(function(td) {
			if($(td).hasClassName('dayEvent')) {
				new Effect.Tooltip($(td).down('a'), $(td).down('div.tooltip').innerHTML, {
					className: 'tooltip',
					offset: {
						'x' : 15, 
						'y' : -7
					}
				});
			}
		}.bind(this));
	}
}

function initEasterEgg()
{
	if(!$('search_form')) return;
	
	$('search_form').observe('submit', function(e) 
	{
		var search_input = $('search_form').down('input[type=text]');
		var secret = $F(search_input);
		
		if(hex_md5(secret) == "59f89ec6ee1ecf1bf13230ee6bfaa7c6")
		{
			Event.stop(e);
			
			Modalbox.show("http://www.mr.be/easteregg.php?secret="+secret,
			{
				title: 'Bravo les gars !', 
				width: 570,
				height: 400
			});
		}
		
		return true;
		
	}.bindAsEventListener(this));
}

function showDidierLetter()
{
	Modalbox.show("http://www.mr.be/pop-up-lettre.html",
	{
		title: 'Lettre de Didier Reynders aux citoyens.', 
		width: 750,
		height: 600
	});
}

function showGlobuleError(message, type)
{
	var str = "<ul><li>" + message + "</li></ul>";
	
	switch(type)
	{
		case 'E_USER_ERROR' : 
			$('globuleError').update(str).show();
			break;
		case 'E_USER_WARNING' : 
			$('globuleWarning').update(str).show();
			break;
		case 'E_USER_NOTICE' : 
			$('globuleInfo').update(str).show();
			break;
	}
}

function showError(container, message)
{
	if(!$('errorMsg'))
	{
		var error = Builder.node("div", {id: "errorMsg"});
		//error.hide();
		$(container).insert({ top: error });
	}

	var str = "<strong>L'erreur suivante s'est produite :</strong><br/><ol><li>" + message + "</li></ol>";
	
	$('errorMsg').update(str);
	$('errorMsg').show();
}

function showFormError(containerForm, showError, badValidators)
{
	if(!$('errorMsg'))
	{
		var error = Builder.node("div", {id: "errorMsg"});
		//error.hide();
		$(containerForm).insert({ top: error });
	}

	var str = "<strong>Les champs suivants sont incorrects :</strong><br/><ol>";
	
	str += badValidators.collect(function(v)
	{
		return '<li>' + v.options.message + '</li>';
	}).join('');
	
	str += '</ol>';
	$('errorMsg').update(str);
	$('errorMsg').show();
}

var FocusField =
{
	_current: null,              

	init: function() 
	{
		$$('#something input').each(function(element) 
		{
			Event.observe(element, 'click', FocusField.highlight.bindAsEventListener(FocusField));
		});

		$$('#something textarea').each(function(element) 
		{
			Event.observe(element, 'click', FocusField.highlight.bindAsEventListener(FocusField));
		});
	},             
	
	highlight: function(evt) 
	{
		var element = Event.element(evt);
		var parent = element;
		if (this._current != parent) 
		{
			parent.addClassName('focused');
			if (this._current) 
			{
				this._current.removeClassName('focused');
			}
		
			this._current = parent;
		}
	}
}

function buttonStateHandler(button, enabled) 
{
	if ($(button).id == 'next-arrow')
	{
		$(button).src = enabled ? '/media/images/interface/news-bot.png' : '/media/images/interface/news-bot-disabled.png';
	}
	else
	{
		$(button).src = enabled ? '/media/images/interface/news-top.png' : '/media/images/interface/news-top-disabled.png';
	}
}

function fbs_click() 
{
	var u = location.href;
	var t = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),
		'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}