var scroll = window.getScroll(), size = window.getSize(), middle = window.getScrollTop() + (window.getHeight() / 2), popupWidth = 310, popupHeight = 180;


function envoyer()
{
	if ($('nom').value != '')
	{
		popup.addClass('spinner') ;
		
		var donnees = "nom=" + $('nom').value + "&email=" + $('email').value + "&mode=envoi_ami" ;
	
		new Request.HTML({
			url : "/ajax/ajax_outils.php", 
			method : 'get',
			encoding : 'iso-8859-1',
			update : popup,
			data : donnees,
			onComplete : function ()
				{
					popup.removeClass('spinner') ;								
					$('fermer').addEvent('click', close) ;
				}
		}).send() ;
	}
	else
	{
		alert ("Vous devez indiquer votre nom") ;
	}
}

function close()
{	
	popup.destroy() ;
	fxOverlay.cancel().chain(function(){overlay.destroy()}).start(0);
}


window.addEvent('domready', function ()
{
	// Gestion du bouton "imprimer"	
	$$('.o_fav').each(function(lien)
	{
		lien.addEvent('click', function(e)
		{
			// Gestion "ajout dans mes favoris"
			if (navigator.appName.substring(0,9) == "Microsoft") window.external.AddFavorite(document.URL , document.title) ;
			else window.sidebar.addPanel(document.title, document.URL, "") ;
		}) ;
	}) ;
	
	
	// Gestion du bouton "imprimer"	
	$$('.o_print').each(function(lien)
	{
		lien.addEvent('click', function(e)
		{	
			e = new Event(e).stop() ;
			window.print() ;
		}) ;
	}) ;
	
	
	
	// Gestion du bouton "Envoyer à un ami"
	$$('.o_mess').addEvent('click', function(e)
	{
		e = new Event(e).stop() ;
		
		// Création des éléments
		$(document.body).adopt(
			$$(
			   overlay = new Element("div", {id: "lbOverlay", events: {click: close}}),
			   popup = new Element("div", {id: "o_outils"})
			)				
		) ;
		
		popup.setStyles({top: Math.max(0, middle - (popupHeight / 2)), width: popupWidth, height: popupHeight/*, marginLeft: -popupWidth/2*/, left: (scroll.x + (size.x / 2)) - 180});
		
		var formulaire = '<p><strong>Envoyer à un ami</strong></p> \
		<label for="nom">Votre nom : </label><br /> \
		<input type="text" name="nom" id="nom" class="champ" /><br /> \
		<label for="email">L\'e-mail de votre ami : </label><br /> \
		<input type="text" name="email" id="email" class="champ" /><br />' ;
		popup.set('html', formulaire) ;
		
		$(popup).adopt(btnEnv = new Element("input", {id: "envoyerAmi", value: "envoyer", type: "submit", events: {click: envoyer}})) ;
		
		$(popup).adopt(btnFermer = new Element("a", {id: "fermer", href: "#", events: {click: close}})) ;
		btnFermer.set('html', 'Fermer') ;

		
		// Effets visuels		
		fxOverlay = new Fx.Tween(overlay, {property: "opacity", duration: 400}) ;
		fxOverlay.set(0).start(0.8);
		
		if (Browser.Engine.trident4) // Si IE6
		{
			overlay.style.position = "absolute";
			overlay.setStyles({left: scroll.x, top: scroll.y, width: size.x, height: size.y});
		}
	})
}) ;
