// Fonction de création d'un slide
function createSlide(texte) {
	
	// delai d'animation
	var delay = 500;
	
	var masque = $('<div>', {
		css: {
			'position': 'absolute',
			'z-index': '999',
			'top': '0',
			'left': '0',
			'width': $('body').width()+'px',
			'height': $('body').height()+'px',
			'background-color': 'gray',
			'opacity': '0.5',
			'filter': 'alpha(opacity=50)',
			'cursor': 'pointer',
			'display': 'none'
		},
		click: function() {
			slide.fadeOut(delay, function() { $(this).remove() });
			masque.fadeOut(delay, function() { $(this).remove() });
		}
	});
	$('body').append(masque);
	masque.fadeIn(delay);
	var slide = $('<div>', {
		css: {
			'position': 'absolute',
			'z-index': '1000',
			'padding': '10px',
			'background-color': '#FFFFFF',
			'text-align': 'center',
			'font-weight': 'bold',
			'display': 'none',
			'border-color': '#91cde9',
			'border-width': '2px',
			'border-style': 'solid'
		}
	});
	slide.append($('<div>', { html: texte }));
	slide.append($('<img>', {
		'class': 'png_bg',
		id: 'slide_close',
		src: 'images/fermer.png',
		alt: 'Fermer',
		title: 'Fermer',
		css: {
			'cursor': 'pointer'
		},
		click: function() {
			slide.fadeOut(delay, function() { $(this).remove() });
			masque.fadeOut(delay, function() { $(this).remove() });
		}
	}));
	$('#slide_close').live('mouseover', function() {
		$(this).attr('src','images/fermer_hover.png');
	});
	$('#slide_close').live('mouseout', function() {
		$(this).attr('src','images/fermer.png');
	});
	var ctn_close = $('<div>', {
		css: {
			'position': 'absolute',
			'top': '-15px',
			'right': '-15px'
		}
	});
	var link_close = $('<a>', {
		id: 'link_close',
		alt: 'Fermer',
		title: 'Fermer',
		href: 'javascript:void(0);',
		click: function() {
			slide.fadeOut(delay, function() { $(this).remove() });
			masque.fadeOut(delay, function() { $(this).remove() });
		}
	});
	var img_close = $('<img>', {
		'class': 'png_bg',
		src: 'images/btn_close_slide.png',
		alt: 'Fermer',
		title: 'Fermer',
		width: '30',
		height: '30'
	});
	link_close.append(img_close);
	ctn_close.append(link_close);
	slide.append(ctn_close);
	
	$('body').append(slide);
	slide.fadeIn(delay, function() {
		slide.css({
			'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
			'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px'
		});
	});
	slide.corner();
	$(window).resize(function() {
		masque.css({
			'width': $('body').width()+'px',
			'height': $('body').height()+'px'
		});
		slide.css({
			'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
			'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px'
		});
	});
	
	$(document).keyup(function(e) {
		if(e.keyCode == 27) {
			$('#link_close').trigger('click');
		}
	});
}

function trace(msg) {
	if (window.console) console.log(msg);
	else alert(msg);
}

$(document).ready(function() {
	$('#infos_pratiques').click(function() {
		$('#liens_infos ul').fadeToggle('slow');
	});
	
	$('#menu_centre td').hover(
		function() {
			$(this).css('background-image', 'url(\'images/bouton_hover.png\')');
			$(this).children('a').css('text-decoration', 'underline');
		},
		function() {
			$(this).css('background-image', 'url(\'images/'+$(this).attr('id')+'.png\')');
			$(this).children('a').css('text-decoration', 'none');
		}
	);
	$('#menu_centre td').click(function() {
		location.href = $(this).children('a').attr('href');
	});

	
	// Activation du lien externe SNPI
	$('#snpi').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	// Gestion du slide d'informations
	if ($('#message_ariane').size() > 0) {
		var message = $('#message_ariane').html();
		$('#message_ariane').remove();
		createSlide('<p>'+message+'</p>');
	}
	
	// Activation du lien externe DSoft
	$('#dsoft a').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
});
