Fx.mySlide = Fx.Base.extend({

	options:
	{
		mode: 'vertical',
		useParent: false
	},

	initialize: function(el, options)
	{
		this.setOptions(options);
		
		this.element = $(el);
		
		//
		// wrapper
		//
		
		if (this.options.useParent)
		{
			this.wrapper = $(this.element.parentNode);
		}
		else
		{
			this.wrapper = new Element
			(
			 	'div',
				{
					'styles': $extend(this.element.getStyles('margin'), {'overflow': 'hidden'})
				}
			).injectAfter(this.element).adopt(this.element);
		}
		
		//
		//
		//
		
		//console.info('useParent : %d, parent : %a, height %d', this.options.useParent, this.wrapper, this.wrapper.getStyle('height'));
		
		//this.element.setStyle('margin', 0);
		
		this[this.options.mode]();
		
		//console.info('margin: %s, layout: %s, offset: %d', this.margin, this.layout, this.offset);
		
		//
		// if the wrapper is already slided, we offset the element's margin
		//
		
		if (this.wrapper.getStyle('height').toInt())
		{
			this.open = true;
		}
		else
		{
			this.open = false;
			this.element.setStyle(this.margin, -this.offset);
		}
		
		this.now = [];
		this.parent(this.options);
		
		this.addEvent('onComplete', function()
		{
			this.open = (this.now[0] === 0);
		});
		
		if (window.webkit419) this.addEvent('onComplete', function(){
			if (this.open) this.element.remove().inject(this.wrapper);
		});
	},
	
	// override 'setNow' method

	setNow: function()
	{
		// gofromiel : loop has been unrolled
		
		this.now[0] = this.compute(this.from[0], this.to[0]);
		this.now[1] = this.compute(this.from[1], this.to[1]);
	},

	vertical: function()
	{
		this.margin = 'margin-top';
		this.layout = 'height';
		this.offset = this.element.offsetHeight;
	},

	horizontal: function()
	{
		this.margin = 'margin-left';
		this.layout = 'width';
		this.offset = this.element.offsetWidth;
	},

	/*
	Property: slideIn
		Slides the elements in view horizontally or vertically.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	slideIn: function(mode)
	{
		this[mode || this.options.mode]();
		
		return this.start([this.element.getStyle(this.margin).toInt(), this.wrapper.getStyle(this.layout).toInt()], [0, this.offset]);
	},

	/*
	Property: slideOut
		Sides the elements out of view horizontally or vertically.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	slideOut: function(mode)
	{
		this[mode || this.options.mode]();
		return this.start([this.element.getStyle(this.margin).toInt(), this.wrapper.getStyle(this.layout).toInt()], [-this.offset, 0]);
	},

	/*
	Property: hide
		Hides the element without a transition.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	hide: function(mode)
	{
		this.element.setStyle('display', 'none'); // goformiel
		
		this[mode || this.options.mode]();
		this.open = false;
		return this.set([-this.offset, 0]);
	},

	/*
	Property: show
		Shows the element without a transition.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	show: function(mode)
	{
		this.element.setStyle('display', 'block'); // gofromiel

		this[mode || this.options.mode]();
		this.open = true;
		return this.set([0, this.offset]);
	},

	/*
	Property: toggle
		Slides in or Out the element, depending on its state

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.

	*/

	toggle: function(mode)
	{
		if (this.wrapper.offsetHeight == 0 || this.wrapper.offsetWidth == 0)
		{
			return this.slideIn(mode);
		}
		
		return this.slideOut(mode);
	},
	
	//
	// implement the 'increase' method
	//

	increase: function()
	{
		//
		// using integers for pixel values will help skipping some false changes
		//
		
		if (this.options.unit == 'px')
		{
			now0 = Math.round(this.now[0]);
			now1 = Math.round(this.now[1]);
		}
		else
		{
			now0 = this.now[0];
			now1 = this.now[1];
		}
		
		//console.info('increase >> margin (%s, %d) layout (%s, %d)', this.margin, now0 + this.options.unit, this.layout, now1 + this.options.unit);
		
		this.element.setStyle(this.margin, now0 + this.options.unit);
		this.wrapper.setStyle(this.layout, now1 + this.options.unit);
	}
});

Fx.myLogo = Fx.Style.extend
({
	initialize: function(el)
	{
		this.parent(el, 'opacity', { wait: false, transition: Fx.Transitions.Quint.easeOut });
		
		this.set(0.001);
		
		this.element.onmouseover = function() {	this.start(1); }.bind(this);
		this.element.onmouseout = function() { this.start(0.001); }.bind(this);
	}
});

function tabs_init()
{
	var slide = new Fx.mySlide('more', 
	{
		useParent: true,
		transition: Fx.Transitions.Quint.easeOut
	});
	
	var tabs = new Array
	(
		$('shops-toggle'),
		$('toggle-contact')
	);
	
	var slides = new Array
	(
	 	new Fx.mySlide('more-shops'),
		new Fx.mySlide('more-contact')
	);

	//
	// handler
	//
	
	var _handle = function(e)
	{
		this.toggleClass('active');

		index = this._index;

		/*
		
		!. a tab is clicked
		
			>. tabs are closed
			
				1. we open our tab
				
			>. tabs are opened
			
				>. our tab is opened
				
				>. another tab is opened
		
		*/

		var opened = null;

		for (i = 0 ; i < slides.length ; i++)
		{
			if (!slides[i].open)
			{
				continue;
			}
			
			opened = i;
			
			break;
		}

		if (opened != null)
		{
			//console.info('tabs are opened');

			if (slides[index].open)
			{
				//console.info('our tab is opened');
				//console.info('>> we close our tab');
				
				var delayed_hide = function()
				{
					//console.info('>> tab %d closed', index);
					
					slides[index].hide();
					
					slide.removeEvent('onComplete', delayed_hide);
				};
				
				slide.addEvent('onComplete', delayed_hide);
				
				slide.slideOut();
			}
			else
			{
				//console.info('another tab is opened: %d', opened);
				//console.info('>> close the other tab');
				
				tabs[opened].toggleClass('active');
				
				var delayed_hide = function()
				{
					//console.info('>> hide tab %d', opened);
					
					slides[opened].hide();
					
					slide.removeEvent('onComplete', delayed_hide);

					//console.info('>> show tab %d', index);

					slides[index].show();

					//console.info('>> show slide');

					slide.slideIn();
				}

				slide.addEvent('onComplete', delayed_hide);
				
				slide.slideOut();
			}
		}
		else
		{
			//console.info('tabs are closed');
			//console.info('>> we open our tab');
			
			slides[index].show();
			slide.slideIn();
		}

		if (e)
		{
			e = new Event(e);
	
			e.stop();
		}
	};

	for (i = 0 ; i < tabs.length ; i++)
	{
		tabs[i]._index = i;
		tabs[i].addEvent('click', _handle);
		
		slides[i].hide();
	}
}

function logo_init()
{
	if (document.getElementById('link-home'))
	{
		new Fx.myLogo('link-home');
	}

	if (document.getElementById('link-legend'))
	{
		new Fx.myLogo('link-legend');
	}
}

function contact_init()
{
	var email = document.getElementById('contact-email');
	var message = document.getElementById('contact-message');
	
	var form = email.parentNode;
	
	form.onsubmit = function()
	{
		if (!email.value)
		{
			alert("Merci de saisir votre adresse email");
			
			return false;
		}
		
		if (!message.value)
		{
			alert("Merci de saisir votre message");
			
			return false;
		}

		var req = new XHR
		({
			onSuccess: function()
			{
//				console.info('message success : %a', this.response);
				
				if (this.response.text != 'done')
				{
					alert('Impossible d\'envoyer votre message');
					
					return;
				}
			},
			
			onFailure: function()
			{
				alert('Impossible d\'envoyer la requête');
			}
		});
		
		req.send
		(
		 	'/post.php',
			'email=' + encodeURIComponent(email.value) + 
			'&message=' + encodeURIComponent(message.value)
		);

		$('toggle-contact').fireEvent('click');

		return false;
	};
}

function more_init()
{
	tabs_init();
	logo_init();
	contact_init();
}

window.addEvent('domready', more_init);
