if (!Wd.Panel)
{
	Wd.Panel = {};
}

function WSetupContactPane(panel)
{
	//
	// form elements
	//
	
	var sbmt = $('contact-submit');

	var sbmt_fx = new Fx.Style(sbmt, 'opacity', { wait: false, duration: 250 });

	sbmt_fx.set(0.01);

	sbmt.onmouseover = function()
	{
		sbmt_fx.start(1);
	};

	sbmt.onmouseout = function()
	{
		sbmt_fx.start(0.01);
	};
	
	//
	// submit anchor
	//
	
	sbmt.form.onsubmit = function()
	{
		var email = $('contact-email').getValue();
		var message = $('contact-message').getValue();

		if (!email)
		{
			alert("Merci de saisir votre adresse email");
			
			return false;
		}
		
		if (!message)
		{
			alert("Merci de saisir votre message");
			
			return false;
		}
		
		var req = new XHR
		({
		 	method: 'post',
			
			onSuccess: function(response)
			{
				if (response != 'done')
				{
					alert('Erreur lors de l\'envoie du message');
					
					return;
				}
				
				panel.hide();
			},
			
			onFailure: function()
			{
				alert('Impossible d\'envoyer la requête');
			}
		});

		req.send
		(
		 	'post.php',
			'email=' + encodeURIComponent(email) + 
			'&message=' + encodeURIComponent(message)
		);
				
		return false;
	};
}

Wd.Panel.Icon = Options.extend
({
	options:
	{
		positon: 0,
		onClick: Class.empty
	},
	
	initialize: function(el, options)
	{
		this.setOptions(options);
		this.element = $(el);
	
		this.fx = new Fx.Style(this.element, 'opacity', { duration: 250, wait: false } );
		this.fx.set(0.01);
	
		this.element.onmouseover = function()
		{
			this.fx.start(1);
		}.bind(this);
	
		this.element.onmouseout = function()
		{
			this.fx.start(0.01);
		}.bind(this);
	
		this.element.onclick = function()
		{
			this.options.onClick(this.options.position);
			
			return false;
		}.bind(this);
	}
});

Wd.Panel.ViewPort = new Class
({
	position: 0,
	
	initialize: function(el, navigation)
	{
		this.element = $(el);
		this.navigation = navigation;
		
		this.element.getChildren().each
		(
			function(child)
			{
				child.setStyle('float', 'left');
			},
			
			this
		);
		
		//
		// transition
		//
		
		this.transition = new Fx.Style
		(
			this.element, 'left',
			{
				duration: 250,
				transition: Fx.Transitions.Quint.easeOut
			}
		);
			
		//
		// bind navigation
		//
		
		this.navigation.each
		(
			function(hot)
			{
				hot.onclick = function(hot)
				{
					this.setPosition(this.navigation.indexOf(hot));
					
					return false;
				}.bind(this, hot);
			},
			
			this
		);
	},
	
	setPosition: function(position)
	{
		var active = this.navigation[position];
		
		this.navigation.each
		(
			function(hot)
			{
				if (active == hot)
				{
					hot.addClass('active');
				}
				else
				{
					hot.removeClass('active');
				}
			},
			
			this
		);

		//
		// Safari bug : I need to keep track of the 'index' since transition without
		// a start value produce a runtime error
		//

//		transition.start(-index * 500);

		this.transition.start(-this.position * 500, -position * 500);

		this.position = position;
	}
});

Wd.Panel.Hub = new Class
({
	initialize: function(el)
	{
		this.element = $(el);
	
		//
		// create panel viewport
		//

		this.vp = new Wd.Panel.ViewPort('panel-view', $$('#panel ul a'));
			
		//
		// setup panes
		//
		
		WSetupContactPane(this);
	
		//
		// the element is automatically appenned and removed by the Blacktouch.
		// Thus, we need to remove it now.
		//
		
		this.element.remove();
		
		this.bt = new Wd.Blacktouch
		(
			this.element,
	
			{
				appear:
				{
					duration: 250,
					transition: Fx.Transitions.Quint.easeOut
				},
				
				disappear:
				{
					duration: 250,
					transition: Fx.Transitions.Quint.easeIn
				}
			}
		);
	
		//
		// icons
		//
		
		new Wd.Panel.Icon
		(
			'icon-about', { position: 0, onClick: this.show.bind(this) }
		);

		new Wd.Panel.Icon
		(
			'icon-contact', { position: 1, onClick: this.show.bind(this) }
		);
	},
	
	show: function(position)
	{
		this.vp.setPosition(position);
		this.bt.appear();
	},

	hide: function()
	{
		this.bt.disappear();
	}
});

window.addEvent
(
	'domready', function()
	{
		var panel = document.getElementById('panel');
	
		if (!panel)
		{
			return;
		}
		
		new Wd.Panel.Hub(panel);
	}
);
